var nbr_slide=0; // Nombre de diapo/slide
var active_slide=0; // Diapo/slide active
var start_slide = Math.floor(Math.random()*3) + 1; 

var indexUrl = '/navig.php?t=11';
var lexique = false;

window.onload=init;

function init() {
	startList("header");
	startList("main");
	startList("footer");
	if (nbr_slide>0) {
		gere_nav_diapo();
	}
	
	if (lexique) {
		for (i=0;i<lexique.length;i++) {
			$('#contindex').highlight(lexique[i]);
		}
		$("#contindex .highlight").each(
			function(index) {
				this.rel="texte de test";
				this.title="Tous les articles à propos de \"" + $(this).text() +"\"";
				/*$(this).cluetip({
					width: 180, fx: {open: 'fadeIn', openSpeed: '500'}
				});*/
			}
		);
	}
	addLinkerEvents();
}
var t;
function top() {
	if (document.body.scrollTop!=0 || document.documentElement.scrollTop!=0) {
		window.scrollBy(0,-50);
		t=setTimeout('top()',10);
	} else {
		clearTimeout(t);
	}
}
function gere_diapo(o) {
	nbr_slide++;
	if (nbr_slide==start_slide) { o.style.display='block'; active_slide=start_slide; }
	
}
function gere_nav_diapo() {
	if (_('diapo_prec') && _('diapo_suiv')) {
		_('diapo_prec').onclick=function() {
			active_slide--;
			if (active_slide<1) active_slide=nbr_slide;
			myslides=getElementsByClassName('diapo');
			classRepl(myslides,'none');
			_('diapo'+active_slide).style.display='block';
			return false;
		}
		_('diapo_prec').href='javascript:;';
		_('diapo_suiv').onclick=function() {
			active_slide++;
			if (active_slide>nbr_slide) active_slide=1;
			myslides=getElementsByClassName('diapo');
			classRepl(myslides,'none');
			_('diapo'+active_slide).style.display='block';			
			return false;
		}
		_('diapo_suiv').href='javascript:;';
	} else {
		if (_('nav_diapo')) _('nav_diapo').style.display='none';
	}
}

function startList(n) {
	if (document.getElementById) {
		navRoot = document.getElementById(n);
		node = navRoot;//getnextnode(navRoot);
		while (node) {
			node = getnextnode(navRoot,node);
			if (node) {
				// applique iepngfix sur les png
				if (node.nodeName=="IMG" && document.all) {
					mysplit=node.src.split('.');
					myext=mysplit[mysplit.length-1];
					if (myext.toLowerCase()=='png') node.style.behavior="url(/build/iepngfix.htc);";
				}			
				if (node.nodeName=="LI" && n=='header') {
					node.onmouseover=function() {
						this.className+="_menuover";
					}
					node.onmouseout=function() {
						this.className=this.className.replace("_menuover", "");
					}
				}
				if (node.id=="RECHERCHE") {
					node.onfocus=function() {
						if (this.value=='Rechercher') {
							this.value='';
						}
					}
					node.onblur=function() {
						if (this.value=='') {
							this.value='Rechercher';
						}
					}
				}
				if (node.className=='diapo' && n=='main') {
					gere_diapo(node);
				}
				if (node.className=='lien_haut' && n=='footer') {
					node.onclick=function() {
						top();
						return false;
					}
					node.href="#"
				}	
				if (node.className=='img_swap' && n=='footer') {
					node.onmouseover=function() {
						nid=this.id;
						nsrc='/imgs/'+nid+'_over.gif';
						this.src=nsrc;
					}
					node.onmouseout=function() {
						nid=this.id;
						nsrc='/imgs/'+nid+'.gif';
						this.src=nsrc;						
					}
				}				
			}
		}
	}
}
/*
	FONCTIONS CONTEXTUELLES
*/



/* 
	FONCTIONS NOTATION
*/


/* NOTATION */
function notation(i,n) {
	if (!i) return false;
	o=_("avis"+i);
	if (!o) return false;
	var poststr = "id=" + encodeURI( i );
	if (n) if (! isNaN(n)) if (parseInt(n)>0 && parseInt(n)<6) poststr += "&n=" + encodeURI( n );
	
	makePOSTRequest('/note.php', poststr);
}


var http_request = false;
function makePOSTRequest(url, parameters) {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Impossible d\'�tablir une instance XMLHTTP');
		return false;
	}

	http_request.onreadystatechange = affnotation;
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}

var note="<img src=\"/images/etoile<ON>.gif\" title=\"cliquer pour donner la note de <NOTE>\" alt=\"cliquer pour donner la note de <NOTE>\" onclick=\"notation(<ID>,<NOTE>);\" />";
function affnotation() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			//alert(http_request.responseText);
			result = http_request.responseText;
			res=result.split("=");
			if (res[0]=="ERR") {
				alert("Erreur : "+res[1]);
				return false;
			}else{
				o=_("avis"+res[1]);
				if (!o) return false;
				oo=_("stat"+res[1]);
				if (!oo) return false;
				//oo.innerHTML="[moyenne : "+res[2]+", votes "+res[4]+"]";
				doc="";
				for (i=1;i<6;i++) {
					d=note;
					if (i<=parseInt(res[3])) {
						d=d.replace("<ON>",1);
					}else{ 
						d=d.replace("<ON>",0);
					}
					d=d.replace("<NOTE>",i);
					d=d.replace("<NOTE>",i);
					d=d.replace("<NOTE>",i);
					d=d.replace("<ID>",res[1]);
					
					doc+=d;
				}
				o.innerHTML=doc;
				oo=_("rep"+res[1]);
				if (res[5]) if (oo) oo.innerHTML=res[5];
			}
		} else {
			alert('Il y a problème avec la requête.');
		}
	}
}


function noaccent(chaine) {temp = chaine.replace(/[àâä]/gi,"a");temp = temp.replace(/[éèêë]/gi,"e"); temp = temp.replace(/[ç]/gi,"c");temp = temp.replace(/[îï]/gi,"i");temp = temp.replace(/[ôö]/gi,"o");temp = temp.replace(/[ùûü]/gi,"u"); return temp;}

/*
	FONCTIONS STANDARS PNF/WAAPS
*/
function _(o) {
	if (!o) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById(o)) return false;
	return document.getElementById(o);
}
function getnextnode(source,o) {
	if (!o) return false;
	oo=false;
	if (o.firstChild) return o.firstChild;
	if (o.nextSibling) return o.nextSibling;
	o=o.parentNode;
	while (!oo && o && (o!=source)) {
		oo=o.nextSibling;
		o=o.parentNode;
	}
	return oo;
}
function getElementsByClassName(needle) {
	var my_array = document.getElementsByTagName("*");
	var retvalue = new Array();
	var i;
	var j;
	for (i=0,j=0;i<my_array.length;i++) {
		var c = " " + my_array[i].className + " ";
		if (c.indexOf(" " + needle + " ") != -1) retvalue[j++] = my_array[i];
	}
	return retvalue;
}
function classRepl(whatReplace,style) {
	for(var i=0;i<whatReplace.length;i++) {	
   		whatReplace[i].style.display=style;
     }
}


/* FONCTIONS.JS */

var win="";
function opennamewin(u, n, w, h) {
	if (win!="") {if (! win.closed) { win.close();}}
	stats="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no," ;
	stats += "resizable=no,width="+w+",height="+h+",left=0,top=0";
	win= window.open (u,n,stats);
	win.focus();
}

function popup(obj)
{
	if (obj != null)	opennamewin(obj.href, obj.target, 532, 640);
}

var debug=false;


function position(o,s) {
	l=0;
	t=0;
	while (o) {
		l+=o.offsetLeft;
		t+=o.offsetTop;
		o=o.offsetParent;
		if (o.id=="page") o=false;
	}
	r=(s==0) ? l:t;
	return r;
}


picto="";
function zoom(picture,legende){
	picto = picture;
	wi=100;
	hi=100;
	titre="Zoom";
	legendetex=legende;
	stats="toolbar=no,location=no,directories=no,status=no,menubar=no," ;
	stats += "resizable=yes,width=" + wi + ",height="+hi ;
	imgwin = window.open ("",titre,stats);
	window.setTimeout("makepage(picto,legendetex)",100) ;
	return false;
}

function makepage(pic,legendetexx)
{
	legendetexxx=legendetexx;
	if (legendetexxx=="") espace=0;
	else espace=85;

	imgwin.document.clear();
	imgwin.document.write("<head><title>Croustipate</title>");
	imgwin.document.write("<s"+"cript language='Java"+"Script'>function resizeme(){picheight=document.images['posterpic'].height;picwidth=document.images['posterpic'].width;this.window.resizeTo(picwidth+40,picheight+60+"+espace+");}<"+"/s"+"cript>");
	imgwin.document.write("</head><body style='background-color:#c12634' onload='window.setTimeout(\"resizeme();\",100);' ondblclick='window.close();'><center><table><tr><td>");
	imgwin.document.write("<img name='posterpic' align='absmiddle' src='"+pic+"' border='0' alt=''>");
	imgwin.document.write("</td></tr>");

	if (espace>0)
	{
		imgwin.document.write("<tr><td><center><font face='Verdana,sans-serif' size=2>"+legendetexxx+"<font size=1><br>Double clic sur la fen�tre pour fermer</font></font></center></td></tr></table></center></body>");
	}

	imgwin.document.close();
	imgwin.focus();
}

function hide_col_droite(id_section)
{
	bloc = "bloc_actu";
	if (id_section == '38') bloc = "bloc_en_cours";
	if (id_section == '39') bloc = "bloc_presse";
	
	
	if (bloc == "bloc_en_cours" || bloc == "bloc_actu" || bloc == "bloc_presse")
	{
		if (document.getElementById)
		{
			obj = document.getElementById(bloc);
			if (obj)
			{
				obj.style.display="none";
			}
			
		}
	}
}

var sommaire_retour="<div class=\"haut\" ><a href=\"#haut\"><img src=\"/images/haut.gif\" alt=\"haut de la page\" title=\"haut de la page\" /></a></div>";

function sommaire(n,typ) {
	if (! n) return false;
	if (!typ) typ="H1";
	if (document.getElementById) {
		navRoot = document.getElementById(n);
		if (!navRoot) return false;
		navRootSom = document.getElementById(n+"_sommaire");
		if (!navRootSom) return false;
		node = navRoot;//getnextnode(navRoot);
		sommaire="";
		nsommaire=0;
		while (node) {
			node = getnextnode(navRoot,node);
			if (node) {
				if (node.className==typ || node.nodeName==typ) {
					nsommaire++;
					le_lien=n+"_sommaire_"+nsommaire;
					
					sommaire+="<li><a href=\"#"+le_lien+"\">"+node.innerHTML+"</a></li>\n";
					if (nsommaire==1) node.innerHTML="<a name=\""+le_lien+"\"></a>"+node.innerHTML;
					else node.innerHTML=sommaire_retour+"<a name=\""+le_lien+"\"></a>"+node.innerHTML;
				}
			}
		}
		if (sommaire!="") {
			sommaire="<ul>\n"+sommaire+"</ul>";
			navRootSom.innerHTML+=sommaire;
			navRootSom.style.display="block";
			//navRoot.innerHTML+=sommaire_retour;
		}
	}

}


function addExtDocEvents() {
	var as = document.getElementsByTagName("a");
	var extDoc = [".doc",".xls",".exe",".zip",".pdf"];
	var extTrack = ["advanced-web-metrics.com"];
	// add further document types as required
	for(var i=0; i<as.length; i++) {
		var tmp = as[i].getAttribute("onclick");
	// Tracking electronic documents - doc, xls, pdf, exe, zip
	if (tmp != null && tmp.indexOf('urchinTracker') > -1) continue;
	for (var j=0; j<extDoc.length; j++) {
		if (as[i].href.indexOf(extTrack[0]) != -1 &&
			as[i].href.indexOf(extDoc[j]) != -1) {
				var splitResult = as[i].href.split(extTrack[0]);
				as[i].setAttribute("onclick",((tmp != null) ? tmp : "") +
					"urchinTracker('/downloads" +splitResult[1]+ "');");
				break;
			}
		}
	}
}


function addLinkerEvents() {
	var as = document.getElementsByTagName("a");
	var extTrack = ["http://www.ctip.asso.fr", "http://ctip.asso.fr"];
	// List of local sites that should not be treated as an outbound link. Include at least your own domain here
	
	var extDoc = [".doc",".xls",".exe",".zip",".pdf",".js"];
	//List of file extensions on your site. Add/edit as you require
	
	/*If you edit no further below this line, Top Content will report as follows:
		/ext/url-of-external-site
		/downloads/filename
		/mailto/email-address-clicked
	*/
	
	for(var i=0; i<as.length; i++) {
		
		var flag = 0;
		var tmp = as[i].getAttribute("onclick");

		// IE6-IE7 fix (null values error) with thanks to Julien Bissonnette for this
		if (tmp != null) {
		  tmp = String(tmp);
		  if (tmp.indexOf('urchinTracker') > -1 || tmp.indexOf('_trackPageview') > -1) continue;
    	}
		// Tracking electronic documents - doc, xls, pdf, exe, zip
		for (var j=0; j<extDoc.length; j++) {
		
			if ((as[i].href.indexOf(extTrack[0]) != -1 || as[i].href.indexOf(extTrack[1]) != -1) && as[i].href.indexOf(extDoc[j]) != -1) {
				as[i].onclick = function(){ 
					if(this.href.indexOf(extTrack[0]) != -1) {
						var splitResult = this.href.split(extTrack[0]);
					} else if (this.href.indexOf(extTrack[1]) != -1) {
						var splitResult = this.href.split(extTrack[1]);
					}
					pageTracker._trackPageview('/downloads' +splitResult[1])+ ";" +((tmp != null) ? tmp+";" : "");
					//alert("downloads" +splitResult[1])
				}
				break;
			}
		}


	}
}

function openPrint()
{
	if (confirm("Pensez à la planète : Souhaitez-vous confirmer l'impression ?"))
	{
		openPrintProcess();
	}
}

function openPrintProcess() {
	
	printPopup = window.open(document.URL, "printPopup");
	setTimeout("printPopup.changeMediaCSS()", 2000);
	//setTimeout("printPopup.print()", 700);	
}

function changeMediaCSS ()
{
	/*if (document.getElementById) 
	{
		if (document.getElementById("cssprint")) 
		{
			document.getElementById("cssprint").media = (document.getElementById("cssprint").media=="screen") ? "print":"screen";
		}
	}*/
	$("#cssprint").attr("media", "screen, print");
	
	setTimeout(function(){print();}, 500);
}



