// JavaScript Document


//TAB : ces deux raiables doivent etre definies dans le fichier qui utilise ce script
//var enablepersist="off" //Enable saving state of content structure using session cookies? (on/off)
//var collapseprevious="no" //Collapse previously open content when opening present? (yes/no)

//TAB :  Cache tous les elements de la page de la classe switchcontent.


var maxsteps=30; // number of steps to take to change from start color to endcolor
var stepdelay=40; // time in miliseconds of a single step
//**Note: maxsteps*stepdelay will be total time in miliseconds of fading effect
var startcolor= new Array(255,255,255); // start color (red, green, blue)
var endcolor=new Array(0,0,0); // end color (red, green, blue)

var ie4=document.all

var curindex=0
var totalcontent=0


var fwidth='354px'; //set scroller width
var fheight='500px'; //set scroller height

var fadelinks=0; 



var ie4=document.all&&!document.getElementById;
var DOM2=document.getElementById;
var faderdelay=0;
var index=0;
var dealy=30000;


/*Rafael Raposo edited function*/
//function to change content
function changecontent(which){
	curindex=which;
	document.getElementById("content"+curindex).style.color="rgb("+startcolor[0]+", "+startcolor[1]+", "+startcolor[2]+")"
	//document.getElementById("content"+which).innerHTML=begintag+fcontent[index]+closetag
	
	if (fadelinks)
		linkcolorchange(1);
	colorfade(1, 15);
}

// colorfade() partially by Marcio Galli for Netscape Communications.  ////////////
// Modified by Dynamicdrive.com

function linkcolorchange(step){
	var obj=document.getElementById("content"+curindex).getElementsByTagName("A");
	if (obj.length>0){
		for (i=0;i<obj.length;i++)
			obj[i].style.color=getstepcolor(step);
	}
}

var fadecounter;

function colorfade(step) {
	if(step<=maxsteps) {	
		document.getElementById("content"+curindex).style.color=getstepcolor(step);
		if (fadelinks)
			linkcolorchange(step);
		step++;
		fadecounter=setTimeout("colorfade("+step+")",stepdelay);
		//alert(fadecounter)
	} else {
		clearTimeout(fadecounter);
		document.getElementById("content"+curindex).style.color="rgb("+endcolor[0]+", "+endcolor[1]+", "+endcolor[2]+")";
		//setTimeout("changecontent("+curindex+")", delay);	
	}   
}

function getstepcolor(step) {
	var diff
	var newcolor=new Array(3);
	for(var i=0;i<3;i++) {
		diff = (startcolor[i]-endcolor[i]);
		if(diff > 0) {
			newcolor[i] = startcolor[i]-(Math.round((diff/maxsteps))*step);
		} else {
			newcolor[i] = startcolor[i]+(Math.round((Math.abs(diff)/maxsteps))*step);
		}
	}
	return ("rgb(" + newcolor[0] + ", " + newcolor[1] + ", " + newcolor[2] + ")");
}

function get_total(){
	if (ie4){
		while (eval("document.all.content"+totalcontent))
			totalcontent++
	} else {
		while (document.getElementById("content"+totalcontent))
			totalcontent++
	}
}

function contract_all(){
	for (y=0;y<totalcontent;y++){
		if (ie4)
			eval("document.all.content"+y).style.display="none"
		else
			document.getElementById("content"+y).style.display="none"
	}
}

function expand_one(which){
	contract_all()
	if (ie4)
		eval("document.all.content"+which).style.display="block"
	else
		document.getElementById("content"+which).style.display="block"
	changecontent(which)
}

function updateLinks(){
	
	var links="";var i=0;
	
	_min = (curindex+1)*eltPerPage - (eltPerPage-1);
	_max = ((curindex+1)*eltPerPage > docs_nb )? docs_nb : (curindex+1)*eltPerPage;
	
	p_B = (curindex==0)? totalcontent : curindex; 
	
	p_F = (curindex<totalcontent-1)? curindex+2: 1;
	
	links +="<table>";
	links +="	<tr>";
	
	links +="		<td witdh=\"30px\">";
	if(curindex > 0 ) links +="<img style='cursor:pointer;' onclick='rotate_B()' onmouseout='MM_swapImgRestore()' onmouseover=\"MM_swapImage('previousPub','','../media/images/nav_pubs/previous_on.gif',1)\" src=\"../media/images/nav_pubs/previous.gif\" name=\"previousPub\"  border=\"0\" id=\"previousPub\" title=\"Go to page " +p_B+"\" />";
	else links +="<img src=\"../media/images/nav_pubs/previous_off.gif\" name=\"previousPub\"  border=\"0\" id=\"previousPub\" title=\"No previous page\" />";
	links +="</td>";
	links +="		<td width='90px'><div style=\"text-align:center\">";
	for (i=0;i<totalcontent;i++) {
		if(i==curindex) 	links +="&nbsp;<span class='here' style='color:white;'>"+ (i+1) +"</span>&nbsp;" 	
		else 				links +="&nbsp;<a href='javascript:goToPage("+ i +")' title='Go to page "+ (i+1) +"'>"+ (i+1) +"</a>&nbsp;";
	}
	links +="		</div></td>";
	links +="		<td witdh=\"30px\" >";
	if(curindex < totalcontent-1 ) links +="		<img style='cursor:pointer;' onclick=\"rotate_F()\" onmouseout=\"MM_swapImgRestore()\" onmouseover=\"MM_swapImage('nextPub','','../media/images/nav_pubs/next_on.gif',1)\" src=\"../media/images/nav_pubs/next.gif\" name=\"nextPub\"  border=\"0\" id=\"nextPub\" title=\"Go to page " +p_F+"\" />";
	else links +="<img src=\"../media/images/nav_pubs/next_off.gif\" name=\"nextPub\"  border=\"0\" id=\"nextPub\" title=\"No next page\" />";
	
	links +="</td>";
	links +="	</tr>";
	links +="</table>";
	
	
	document.getElementById('pageLinks').innerHTML = links;
	document.getElementById('comment').innerHTML = docType + " <strong>"+ (_min) +"</strong> - <strong>"+ (_max) +"</strong> out of <strong>"+ (docs_nb)+"</strong>";
	
	visibleIndex= "doc_"+visiblepub[curindex];
	//alert(curindex);
	//alert(visibleIndex);
	//expandcontent(visibleIndex);
	//document.getElementById(visibleIndex).style.display="block";
}

function rotate_content(){
	get_total();
	curindex=(curindex<totalcontent-1)? curindex+1: 0
	openPage(curindex);
	setTimeout("rotate_content()",delay)
}
function rotate_B(){
	get_total();
	curindex=(curindex==0)? totalcontent-1: curindex-1
	openPage(curindex);
}
function rotate_F(){
	get_total();
	curindex=(curindex<totalcontent-1)? curindex+1: 0
	openPage(curindex);	
}

function goToPage(c){
	get_total();
	curindex=c;
	//alert("c = "+c);
	openPage(curindex);
}

function openPage(c){
	expand_one(c);
	//alert(c +" = "+ src[c]);
	document.getElementById("_docs_").src = src[c];
	updateLinks()
}




function do_onload(){
	uniqueidn=window.location.pathname+"firsttimeload"
	getElementbyClass("switchcontent")
	if (enablepersist=="on" && typeof ccollect!="undefined"){
		document.cookie=(get_cookie(uniqueidn)=="")? uniqueidn+"=1" : uniqueidn+"=0" 
		firsttimeload=(get_cookie(uniqueidn)==1)? 1 : 0 //check if this is 1st page load
		if (!firsttimeload)
			revivecontent()
	}
	//expandcontent('pub1');
}


// TAB : Crée un tableau avec tous les elements de la class "classname" 
function getElementbyClass(classname){
	ccollect=new Array()  //Tableau (variable globale) contenant la liste de toutes les classes
	var inc=0
	var alltags=document.all? document.all : document.getElementsByTagName("*")
	for (i=0; i<alltags.length; i++){
	if (alltags[i].className==classname)
		ccollect[inc++]=alltags[i]
	}
}

//TAB : Cache tous les contenus sauf celui avec omit comme classe
function contractcontent(omit){
	var inc=0
	while (ccollect[inc]){
		if (ccollect[inc].id!=omit)
			ccollect[inc].style.display="none"
		inc++
	}
}

function expandcontent_n(){
	var x ="doc_"+ visiblepub[curindex];
	//alert(x);
	expandcontent(x);
	
}

function expandcontent(cid){
	//alert(cid);
	if (currentpub!=cid){
		if (typeof ccollect!="undefined"){
			if (collapseprevious=="yes")
				contractcontent(cid)
			//document.write(cid)	
			//alert(document.getElementById(cid).style.display);
			document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="block")? "block" : "none"
			//alert(document.getElementById(cid).style.display);
			document.getElementById(cid).style.color="#ff0099";
		}
		currentpub=cid
	}
}

function revivecontent(){
	contractcontent("pub1")  // cache tout
	selectedItem=getselectedItem() 	// recupere l'element qui a été selectionné a la derniere visite de la page
	
	//ajoute style.display="block" à tous les composants de l'element selectionné.
	selectedComponents=selectedItem.split("|") 
	for (i=0; i<selectedComponents.length-1; i++)
		document.getElementById(selectedComponents[i]).style.display="block"
}

function get_cookie(Name) { 
	var search = Name + "="
	var returnvalue = "";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
		if (offset != -1) { 
			offset += search.length
			end = document.cookie.indexOf(";", offset);
		if (end == -1) end = document.cookie.length;
			returnvalue=unescape(document.cookie.substring(offset, end))
		}
	}
	return returnvalue;
}

function getselectedItem(){
	if (get_cookie(window.location.pathname) != ""){
		selectedItem=get_cookie(window.location.pathname)
		return selectedItem
	}
	else
		return ""
}

function saveswitchstate(){
	var inc=0, selectedItem=""
	while (ccollect[inc]){
		if (ccollect[inc].style.display=="block")
		selectedItem+=ccollect[inc].id+"|"
		inc++
	}
	document.cookie=window.location.pathname+"="+selectedItem
}

if (window.addEventListener)
	window.addEventListener("load", do_onload, false)
else if (window.attachEvent)
	window.attachEvent("onload", do_onload)
else if (document.getElementById)
	window.onload=do_onload

if (enablepersist=="on" && document.getElementById)
	window.onunload=saveswitchstate