/*
	DOMhelp 1.0
	written by Chris Heilmann
	http://www.wait-till-i.com
	To be featured in "Beginning JavaScript for Practical Web Development, Including  AJAX" 
*/
DOMhelp={
	debugWindowId:'DOMhelpdebug',
	init:function(){
		if(!document.getElementById || !document.createTextNode){return;}
	},
	lastSibling:function(node){
		var tempObj=node.parentNode.lastChild;
		while(tempObj.nodeType!=1 && tempObj.previousSibling!=null){
			tempObj=tempObj.previousSibling;
		}
		return (tempObj.nodeType==1)?tempObj:false;
	},
	firstSibling:function(node){
		var tempObj=node.parentNode.firstChild;
		while(tempObj.nodeType!=1 && tempObj.nextSibling!=null){
			tempObj=tempObj.nextSibling;
		}
		return (tempObj.nodeType==1)?tempObj:false;
	},
	getText:function(node){
		if(!node.hasChildNodes()){return false;}
		var reg=/^\s+$/;
		var tempObj=node.firstChild;
		while(tempObj.nodeType!=3 && tempObj.nextSibling!=null || reg.test(tempObj.nodeValue)){
			tempObj=tempObj.nextSibling;
		}
		return tempObj.nodeType==3?tempObj.nodeValue:false;
	},
	setText:function(node,txt){
		if(!node.hasChildNodes()){return false;}
		var reg=/^\s+$/;
		var tempObj=node.firstChild;
		while(tempObj.nodeType!=3 && tempObj.nextSibling!=null || reg.test(tempObj.nodeValue)){
			tempObj=tempObj.nextSibling;
		}
		if(tempObj.nodeType==3){tempObj.nodeValue=txt}else{return false;}
	},
	createLink:function(to,txt){
		var tempObj=document.createElement('a');
		tempObj.appendChild(document.createTextNode(txt));
		tempObj.setAttribute('href',to);
		return tempObj;
	},
	createTextElm:function(elm,txt){
		var tempObj=document.createElement(elm);
		tempObj.appendChild(document.createTextNode(txt));
		return tempObj;
	},
	closestSibling:function(node,direction){
		var tempObj;
		if(direction==-1 && node.previousSibling!=null){
			tempObj=node.previousSibling;
			while(tempObj.nodeType!=1 && tempObj.previousSibling!=null){
				 tempObj=tempObj.previousSibling;
			}
		}else if(direction==1 && node.nextSibling!=null){
			tempObj=node.nextSibling;
			while(tempObj.nodeType!=1 && tempObj.nextSibling!=null){
				 tempObj=tempObj.nextSibling;
			}
		}
		return tempObj.nodeType==1?tempObj:false;
	},
	initDebug:function(){
		if(DOMhelp.debug){DOMhelp.stopDebug();}
		DOMhelp.debug=document.createElement('div');
		DOMhelp.debug.setAttribute('id',DOMhelp.debugWindowId);
		document.body.insertBefore(DOMhelp.debug,document.body.firstChild);
	},
	setDebug:function(bug){
		if(!DOMhelp.debug){DOMhelp.initDebug();}
		DOMhelp.debug.innerHTML+=bug+'\n';
	},
	stopDebug:function(){
		if(DOMhelp.debug){
			DOMhelp.debug.parentNode.removeChild(DOMhelp.debug);
			DOMhelp.debug=null;
		}
	},
	getKey:function(e){
		if(window.event){
	      var key = window.event.keyCode;
	    } else if(e){
	      var key=e.keyCode;
	    }
		return key;
	},
/* helper methods */
	getTarget:function(e){
		var target = window.event ? window.event.srcElement : e ? e.target : null;
		if (!target){return false;}
		while(target.nodeType!=1 && target.nodeName.toLowerCase()!='body'){
			target=target.parentNode;
		}
		return target;
	},
	stopBubble:function(e){
		if(window.event && window.event.cancelBubble){
			window.event.cancelBubble = true;
		} 
		if (e && e.stopPropagation){
			e.stopPropagation();
		}
	},
	stopDefault:function(e){
		if(window.event && window.event.returnValue){
			window.event.returnValue = false;
		} 
		if (e && e.preventDefault){
			e.preventDefault();
		}
	},
	cancelClick:function(e){
		if (window.event){
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if (e && e.stopPropagation && e.preventDefault){
			e.stopPropagation();
			e.preventDefault();
		}
	},
	addEvent: function(elm, evType, fn, useCapture){
		if (elm.addEventListener){
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
	},
	cssjs:function(a,o,c1,c2){
		switch (a){
			case 'swap':
				o.className=!DOMhelp.cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
			break;
			case 'add':
				if(!DOMhelp.cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
			break;
			case 'remove':
				var rep=o.className.match(' '+c1)?' '+c1:c1;
				o.className=o.className.replace(rep,'');
			break;
			case 'check':
				var found=false;
				var temparray=o.className.split(' ');
				for(var i=0;i<temparray.length;i++){
					if(temparray[i]==c1){found=true;}
				}
				return found;
			break;
		}
	},
    safariClickFix:function(){
      return false;
    }
}
DOMhelp.addEvent(window, 'load', DOMhelp.init, false);





pn={
  // CSS classes
  paginationClass:'paginated',
  dynamicClass:'dynamic',
  showClass:'show',
  paginationNavClass:'paginatedNav',

  // pagination counter properties
  // number of elements shown on one page
  increase:1,
  // counter: _x_ will become the current start position
  //          _y_ the current end position and 
  //          _z_ the number of all data rpws
  counter:'&#160;_x_&#160;de&#160;_y_&#160;of&#160;_z_&#160;',
  // previous and next links, only text is allowed
  nextLabel:'Suivante',
  previousLabel:'Precedente',

  init:function(){
    var tablebody;
    if(!document.getElementById || !document.createTextNode){return;}
    var ts=document.getElementsByTagName('table');
    for(var i=0;i<ts.length;i++){
      if(!DOMhelp.cssjs('check',ts[i],pn.paginationClass)){continue;}
      if(ts[i].getElementsByTagName('tr').length<pn.increase+1){continue;}
	  tablebody=ts[i].getElementsByTagName('tbody')[0];
      ts[i].datarows=tablebody.getElementsByTagName('tr');
      ts[i].datarowsize=ts[i].datarows.length;
      ts[i].current=null;
      DOMhelp.cssjs('add',ts[i],pn.dynamicClass);
      pn.createPaginationNav(ts[i]);
      pn.showSection(ts[i],0);
    }
  },
  createPaginationNav:function(table){
    var navBefore,navAfter;
    navBefore=document.createElement('p');
	DOMhelp.cssjs('add',navBefore,pn.paginationNavClass);
    navBefore.appendChild(DOMhelp.createLink('#',pn.previousLabel));
    navBefore.appendChild(document.createElement('span'));
    counter=pn.counter.replace('_x_',1);
    counter=counter.replace('_y_',pn.increase);
    counter=counter.replace('_z_',table.datarowsize-1);
    navBefore.getElementsByTagName('span')[0].innerHTML=counter;
    navBefore.appendChild(DOMhelp.createLink('#',pn.nextLabel));
    table.parentNode.insertBefore(navBefore,table);
    navAfter=navBefore.cloneNode(true);
    table.parentNode.insertBefore(navAfter,table.nextSibling);
    table.topPrev=navBefore.getElementsByTagName('a')[0];
    table.topNext=navBefore.getElementsByTagName('a')[1];
    table.bottomPrev=navAfter.getElementsByTagName('a')[0];
    table.bottomNext=navAfter.getElementsByTagName('a')[1];
    DOMhelp.addEvent(table.topPrev,'click',pn.navigate,false);  
    DOMhelp.addEvent(table.bottomPrev,'click',pn.navigate,false);  
    DOMhelp.addEvent(table.topNext,'click',pn.navigate,false);  
    DOMhelp.addEvent(table.bottomNext,'click',pn.navigate,false);  
    table.bottomNext.onclick=DOMhelp.safariClickFix;
    table.topPrev.onclick=DOMhelp.safariClickFix;
    table.bottomPrev.onclick=DOMhelp.safariClickFix;
    table.topNext.onclick=DOMhelp.safariClickFix;
	table.topCounter=navBefore.getElementsByTagName('span')[0];
	table.bottomCounter=navAfter.getElementsByTagName('span')[0];
  },
  navigate:function(e){
    var start,table;
    var t=DOMhelp.getTarget(e);
    while(t.nodeName.toLowerCase()!='a'){
      t=t.parentNode;
    }
    if(t.getAttribute('href')==null || t.getAttribute('href')==''){return;}
    if(t.parentNode.previousSibling && 
       t.parentNode.previousSibling.nodeName.toLowerCase()=='table'){
      table=t.parentNode.previousSibling;
    } else {
      table=t.parentNode.nextSibling;
    }
    if(t==table.topNext || t==table.bottomNext){
      start=table.current+pn.increase;
    } else if(t==table.topPrev|| t==table.bottomPrev){
      start=table.current-pn.increase;
    }
    pn.showSection(table,start);
  },
  showSection:function(table,start){
    var i;
    pn.changePaginationNav(table,start);
    if(table.current != null){
      for(i=table.current;i<table.current+pn.increase;i++){
        if(table.datarows[i]){
          DOMhelp.cssjs('remove',table.datarows[i],pn.showClass);
        }
      }
    }
    for(i=start;i<start+pn.increase;i++){
      if(table.datarows[i]){
	      DOMhelp.cssjs('add',table.datarows[i],pn.showClass);
      }
    }
    table.current=start;
  },
  changePaginationNav:function(table,start){
    if(start-pn.increase<0){
      table.bottomPrev.removeAttribute('href');
      table.topPrev.removeAttribute('href');
    } else{
      table.bottomPrev.setAttribute('href','#');
      table.topPrev.setAttribute('href','#');
    }
    if(start+pn.increase>table.datarowsize-2){
      table.bottomNext.removeAttribute('href');
      table.topNext.removeAttribute('href');
    }else{
      table.bottomNext.setAttribute('href','#');
      table.topNext.setAttribute('href','#');
    }
    var counter=pn.counter.replace('_x_',start+1)
    var last=start+pn.increase;
    if(last>table.datarowsize){last=table.datarowsize;}
    counter=counter.replace('_y_',last)
    counter=counter.replace('_z_',table.datarowsize)
    table.topCounter.innerHTML=counter;
    table.bottomCounter.innerHTML=counter;
  }
}
DOMhelp.addEvent(window,'load',pn.init,false);
