// JavaScript Document

if (!window.mainsite) var mainsite='/';
else mainsite=window.mainsite;

function openDeroul(id) {
	var obj = document.getElementById(id);
	if(obj.style.display == 'none') { obj.style.display = 'block' }
	else { obj.style.display = 'none' }
}

function selectVal(val,deroulId,myform,champ) {
  if(myform!='none') {
    var doc = eval("document.forms."+myform+"."+champ)
    doc.value = val;
	if(val!='all'){
		document.getElementById(deroulId+"_val").innerHTML = val + ' jours';
	}
	else{
		document.getElementById(deroulId+"_val").innerHTML = 'toujours';
	}
  }
  else { document.getElementById(deroulId+"_val").innerHTML = val; }
  openDeroul(deroulId);
}

function selectVal2(val1,val2,deroulId,myform,champ) {
  var doc = eval("document.forms."+myform+"."+champ)
  doc.value = val1;
  document.getElementById(deroulId+"_val").innerHTML = val2;
  openDeroul(deroulId);
}

var t="";
var id_courant="";
var mActif="";
var mActif2="";
function showsubmenu(id_arbo,topWidth,obj) {
  if(document.getElementById("submenu"+id_arbo)) {
    if(id_courant==id_arbo) clearTimeout(t);
    else if(id_courant!="" && document.getElementById("submenu"+id_arbo)) {
      if (document.getElementById("submenu"+id_courant)) document.getElementById("submenu"+id_courant).style.display="none";
      if(obj != mActif) {
        if (mActif.className.substr((mActif.className.length-6), 6) == '_actif') mActif.className = mActif.className.substr(0, (mActif.className.length-6));
      }
    }
    id_courant=id_arbo;
      if(obj) {
        if (obj.className.substr((obj.className.length-6), 6) != '_actif') obj.className=obj.className+"_actif";
        mActif=obj;
      }
      if(topWidth) document.getElementById("submenu"+id_arbo).style.marginTop=topWidth+"px";
      document.getElementById("submenu"+id_arbo).style.display="block";
      if (!z) var z=100;
      document.getElementById("submenu"+id_arbo).style.zIndex=z+1;
  }
	else{
		if(obj) {
			hidesubmenu(id_courant);
			if (obj.className.substr((obj.className.length-6), 6) != '_actif') obj.className=obj.className+"_actif";
			mActif2=obj;
     }
	}
}
function onmouseoutmenu(){
	if (mActif2.className.substr((mActif2.className.length-6), 6) == '_actif') mActif2.className = mActif2.className.substr(0, (mActif2.className.length-6));
}
function hidesubmenu(id_arbo) {
  if(document.getElementById("submenu"+id_arbo)) {
    document.getElementById("submenu"+id_arbo).style.display="none";
    if (mActif.className.substr((mActif.className.length-6), 6) == '_actif') mActif.className = mActif.className.substr(0, (mActif.className.length-6));
  }
}
function hidesubmenu2(id_arbo) {
  clearTimeout(t);
  window.t=setTimeout('hidesubmenu('+id_arbo+')',500);
}

function replaceAll(oldStr,findStr,repStr) {
  var srchNdx = 0;
  var newStr = "";
  while (oldStr.indexOf(findStr,srchNdx) != -1)
  {
    newStr += oldStr.substring(srchNdx,oldStr.indexOf(findStr,srchNdx));
    newStr += repStr;
    srchNdx = (oldStr.indexOf(findStr,srchNdx) + findStr.length);
  }
  newStr += oldStr.substring(srchNdx,oldStr.length);
  return newStr;
}

function getCookieVal(offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	var noms = decodeURIComponent(document.cookie.substring(offset, endstr));
	noms=replaceAll(noms, '+', ' ');
	return noms;
}

function GetCookie(name)
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}


function repondreSondage(id_sondage,options,mode){
	var trouve=false;
	var reponse='';
	for(var i=0;(!trouve)&&(i<options.length);i++){
		if(trouve=options[i].checked)
		reponse=options[i].value;
	}
	if (trouve==false){
		alert('il faut choisir une réponse');
		return false;
	}
	loadXMLDocGeneric('/dynamic/web.php?q=answer_poll&id_sondage='+encodeURIComponent(id_sondage)+'&reponse='+encodeURIComponent(reponse)+'&mode='+encodeURIComponent(mode), 'innerHTML', 'text/html', 'sondage_'+id_sondage,'');
}

function repondreSondage2(id_sondage,options,url){
	var trouve=false;
	var reponse='';
	for(var i=0;(!trouve)&&(i<options.length);i++){
		if(trouve=options[i].checked)
		reponse=options[i].value;
	}
	if (trouve==false){
		alert('il faut choisir une réponse');
		return false;
	}
	loadXMLDocGeneric('/dynamic/web.php?q=answer_poll&id_sondage='+encodeURIComponent(id_sondage)+'&reponse='+encodeURIComponent(reponse), 'Javascript', 'text/html', 'location.href="'+url+'"','');
}

function loadSondage(id_sondage){
	if(GetCookie('my_poll')!=null){
		var tableau = GetCookie('my_poll').split(';');
		var trouve=false;
		for(var i=0;(!trouve)&&(i<tableau.length);i++){
			if(tableau[i]==id_sondage)	trouve=true;
		}
		if (trouve){
			document.getElementById('sondage_'+id_sondage).style.display='none';
			document.getElementById('resultat_'+id_sondage).style.display='block';
		}else{
			document.getElementById('sondage_'+id_sondage).style.display='block';
			document.getElementById('resultat_'+id_sondage).style.display='none';
		}
	}
}

function openArticle2(idArt) {
  idBloc = idArt.substr(0, (idArt.length-1));
  if (!eval(document.getElementById(idArt+"b"))) return;
  for(i=1; document.getElementById(idBloc+i+"b") || document.getElementById(idBloc+(i+1)+"b"); i++)
  {
    itemA = eval(document.getElementById(idBloc+i+"a"));
    itemB = eval(document.getElementById(idBloc+i+"b"));
    if(idBloc+i!=idArt) {
      if(itemA.className.substr((itemA.className.length-3), 3)=="_on") itemA.className = itemA.className.substr(0, (itemA.className.length-3));
      if(itemB) itemB.style.display = 'none';
    }
    else {
      if(itemA.className.substr((itemA.className.length-3), 3)!="_on") itemA.className = itemA.className+"_on";
      if(itemB) itemB.style.display = 'block';
    }
  }
}

/* AJAX GENERIC */
var req = false;
function loadXMLDocGeneric(url, typeaction, mimetype, cible1, cible2)
{
	if (window.req!=false)
	{
		if (window.req.abort) window.req.abort();
		window.req=null;
		window.req=false;
	}

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		window.req = new XMLHttpRequest();
		if (window.req.overrideMimeType) {
			window.req.overrideMimeType(mimetype);
		}
	} else if (window.ActiveXObject) { // IE
		try {
			window.req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				window.req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!window.req) {
		alert('Abandon :( Impossible de créer une instance XMLHTTP');
		return false;
	}
	
	window.req.onreadystatechange = function() { processReqChangeGeneric(typeaction,cible1,cible2); };
	window.req.open('GET', url, true);
	window.req.send(null);
}

function processReqChangeGeneric(typeaction, cible1, cible2) {
	if (window.req.readyState == 4)
	{
		if (window.req.status == 200)
		{
			if (typeaction=='innerHTML' && document.getElementById(cible1))
			{
				document.getElementById(cible1).innerHTML = req.responseText;
			}
			else if (typeaction=='CommentsIns')
			{
				var tableau = new Array();
				if (document.getElementById(cible1))
				{
					tableau = req.responseText.split(';');
					if (tableau[0]==1){
						document.getElementById(cible2).style.display='none';
					}
					document.getElementById(cible1).innerHTML = tableau[1];
					document.getElementById('commentaire').value = '';
				}
			}
			else if (typeaction=='SendFriend')
			{
				var tableau = new Array();
				if (document.getElementById(cible1))
				{
					tableau = req.responseText.split(';');
					if (tableau[0]==1){
						document.getElementById(cible2).style.display='none';
					}
					document.getElementById(cible1).innerHTML = tableau[1];
				}
			}
			else if (typeaction=='Rating')
			{
				var tableau = new Array();
				if (document.getElementById(cible1) && document.getElementById(cible2))
				{
					tableau = req.responseText.split('|');
					if (tableau[0]=='1'){
						document.getElementById(cible1).innerHTML = tableau[1];
						document.getElementById(cible2).innerHTML = tableau[2];
					}
					else document.getElementById(cible1).innerHTML = tableau[1];
				}
			}
			else if (typeaction=='Alert')
			{
				alert(window.req.responseText);
			}
			else if (typeaction=='Javascript')
			{
				eval(cible1);
			}
			else if(typeaction=='login'){
				var tableau = new Array();
				if (document.getElementById(cible1))
				{
					tableau = req.responseText.split(';');
					if (tableau[0]==0)	document.getElementById(cible1).innerHTML = tableau[1];
					else if (tableau[0]==1)	{
						window.document.location.reload();
					}
				}
			}

		}
		else
		{

		}
		window.req.abort();
		window.req=null;
		window.req=false;
	}
}
/* */

function AddBook(url, label) {
  if (url=='' || !url) url=location.href;
  if (label=='' || !label) label=document.title;

  if ( navigator.appName != 'Microsoft Internet Explorer' ){
   window.sidebar.addPanel(label,url,"");
  }
  else{
    window.external.AddFavorite(url,label);
  }
}

function rating(id_content, val) {
	if(GetCookie('my_vote')!=null){
		var tableau = GetCookie('my_vote').split(';');
		var trouve=false;
		for(var i=0;(!trouve)&&(i<tableau.length);i++){
			if(tableau[i]==id_content)	trouve=true;
		}
		if(trouve)	document.getElementById('content_rating').innerHTML='d&eacute;j&agrave; not&eacute;';
		else loadXMLDocGeneric('/dynamic/web.php?q=rating&id_contenu='+encodeURIComponent(id_content)+'&type=1&rank='+encodeURIComponent(val), 'Rating', 'text/html', 'content_rating', 'content_avg_rating');
	}
	else loadXMLDocGeneric('/dynamic/web.php?q=rating&id_contenu='+encodeURIComponent(id_content)+'&type=1&rank='+encodeURIComponent(val), 'Rating', 'text/html', 'content_rating', 'content_avg_rating');
}
  
var image_curr='';

function hasloaded(image,wintop,largeur,hauteur){
  if(hauteur-image.height>0){
    var posh=wintop+(hauteur-image.height)/2;
  }else{
    var posh=wintop+10;
  }
  if(largeur-image.width>0){
    var posl=(largeur-image.width)/2;
  }
  else{
    var posl=10;
  }

  if((image_curr!='') && (image_curr!=image.src) && (document.getElementById("fullimage").style.display=="block"))
  {
    document.getElementById("fullimage").innerHTML = "";
    document.getElementById("fullimage").style.display = "none";
    document.getElementById("fullimage").style.top = posh+"px";
    document.getElementById("fullimage").style.left = posl+"px";
    document.getElementById("fullimage").innerHTML = "<a href=\"javascript:FullImage('"+image.src+"')\"><img title=\"Fermer\" alt=\"Fermer\" src=\""+image.src+"\" width="+image.width+" height="+image.height+" border=\"0\" /></a>";
    document.getElementById("fullimage").style.display = "block";
    document.getElementById("fullimage").style.zIndex = 101000;

  }else if(document.getElementById("fullimage").style.display=="none"){
    document.getElementById("fullimage").style.top = posh+"px";
    document.getElementById("fullimage").style.left = posl+"px";
    document.getElementById("fullimage").innerHTML = "<a href=\"javascript:FullImage('"+image.src+"')\"><img title=\"Fermer\" alt=\"Fermer\" src=\""+image.src+"\" width="+image.width+" height="+image.height+" border=\"0\" /></a>";
    document.getElementById("fullimage").style.display = "block";
    document.getElementById("fullimage").style.zIndex = 500000;
  }else{
    document.getElementById("fullimage").innerHTML = "";
    document.getElementById("fullimage").style.display = "none";
  }
  image_curr=image.src;
}

function FullImage(arg){
	var wintop=10;
	if (navigator.appName=='Opera') { //Opera
		var wintop=parseInt(document.body.scrollTop);
		var hauteur=document.body.clientHeight;
		var largeur=document.body.clientWidth;
	}
	else if ((document.getElementById) && (!document.all)) { //NS6
		var wintop=parseInt(window.pageYOffset);
		var hauteur=window.innerHeight;
		var largeur=window.innerWidth;
	}
	else if ((document.getElementById) && (document.all)) { //IE6
		var wintop=parseInt(document.documentElement.scrollTop);
		var hauteur=document.documentElement.clientHeight;
		var largeur=document.documentElement.clientWidth;
	}
	else if (document.getElementById) { //IE5
		var wintop=parseInt(document.body.scrollTop);
		var hauteur=document.body.clientHeight;
		var largeur=document.body.clientWidth;
	}
	var image=new Image;
	image.src=arg;
	if(!image.complete)		image.onload = function(){hasloaded(this,wintop,largeur,hauteur)};
	else hasloaded(image,wintop,largeur,hauteur);
}

var image_curr2='';
var image_texte_curr2='';
var image_titre_curr2='';
function LoadingElements(image) {
	if ((image_curr2!='') && (image_curr2!=image.src) && (document.getElementById("fullimage2").style.display=="block")) {
		document.getElementById("fullimage2").style.display="none";
		document.getElementById("fullimage2").InnerHTML="";
		document.getElementById("fullimage2").innerHTML = "<span class=\"sp_fullimage_24h_haut\">&nbsp;</span><span class=\"sp_fullimage_24h_milieu\"><span class=\"sp_fullimage_24h_image\"><a href=\"javascript:FullImage2('"+image.src+"','')\"><img title=\"Fermer\" alt=\"Fermer\" src=\""+image.src+"\" width="+image.width+" height="+image.height+" border=\"0\" /></a></span><a href=\"javascript:FullImage2('"+image.src+"','')\"><span class=\"sp_fullimage_24h_titre\">"+image_titre_curr2+"</span><span class=\"sp_fullimage_24h_texte\">"+image_texte_curr2+"</span></a></span><span class=\"sp_fullimage_24h_bas\">&nbsp;</span>";
		document.getElementById("fullimage2").style.display="block";
		}
	else if(document.getElementById("fullimage2").style.display=="none") {
		document.getElementById("fullimage2").innerHTML = "<span class=\"sp_fullimage_24h_haut\">&nbsp;</span><span class=\"sp_fullimage_24h_milieu\"><span class=\"sp_fullimage_24h_image\"><a href=\"javascript:FullImage2('"+image.src+"','')\"><img title=\"Fermer\" alt=\"Fermer\" src=\""+image.src+"\" width="+image.width+" height="+image.height+" border=\"0\" /></a></span><a href=\"javascript:FullImage2('"+image.src+"','')\"><span class=\"sp_fullimage_24h_titre\">"+image_titre_curr2+"</span><span class=\"sp_fullimage_24h_texte\">"+image_texte_curr2+"</span></a></span><span class=\"sp_fullimage_24h_bas\">&nbsp;</span>";
		document.getElementById("fullimage2").style.display="block";
		}
	else {
		document.getElementById("fullimage2").InnerHTML="";
		document.getElementById("fullimage2").style.display="none";
	}
	image_curr2=image.src;
}

function FullImage2(arg, id) {		
	var image2=new Image;
	image2.src=arg;
	if(id!=''){
		image_texte_curr2=news[id][0];
		image_titre_curr2=news[id][1];
	}
	if(!image2.complete) {
		image2.onload = function() {LoadingElements(this);}
	}			
	else {
		LoadingElements(image2);
	}
}

function diapoMove(num){
	milieu=num+1+"/"+photos_diapo.length;
	fin = "";
	fin2 = "";
	if(num!=0)	fin += '<a href="javascript:diapoMove('+(num-1)+')"><img src="/images/pic_prec.gif" border="0" alt="précédente" /></a>';
	if(num!=(photos_diapo.length-1))	fin2 = '<a href="javascript:diapoMove('+(num+1)+')"><img src="/images/pic_suiv.gif" border="0" alt="suivante" /></a>';
	if (photos_diapo[num][2]==''){
		document.getElementById("diaporama").innerHTML = '<img src="/commun/n509x359/'+photos_diapo[num][0]+'" border="0" alt="" /><br /><div style="float:right">'+fin2+'</div><div style="float:left">'+fin+'</div>'+milieu+'<br/></div>'+photos_diapo[num][1]+'<br />';
	}else{
		document.getElementById("diaporama").innerHTML = '<a href="'+photos_diapo[num][2]+'" ><img src="/commun/n509x359/'+photos_diapo[num][0]+'" border="0" alt="" title="cliquer pour agrandir la photo"  /></a><br /><div style="float:right">'+fin2+'</div><div style="float:left">'+fin+"</div>"+milieu+'<br/></div>'+photos_diapo[num][1]+"<br />";
	}
}

function comments() {
	var identifiant=GetCookie('my_identifiant');
	if( (identifiant=='') || (identifiant==null) ){
		location.href='/dynamic/profil.php?q=identif&destination='+encode64(location.href+'#commentform');
	}else{
		var div = document.getElementById("commenter");
		if (!div)
		{
			alert('Impossible de commenter cet article.');
			return;
		}
		if(div.style.display == 'none') div.style.display = "block";
		else div.style.display = "none";

		document.getElementById("envoi").style.display = "none";
		//document.getElementById("bloguer").style.display = "none";
		location.href='#commentform';
	}
}

function allcomments(myid)
{
	loadXMLDocGeneric('/dynamic/web.php?q=all_comments&id_article='+encodeURIComponent(myid), 'innerHTML', 'text/html', 'commentslist', '');
	if (document.getElementById('btallcomments')) document.getElementById('btallcomments').innerHTML='';
}

function commenter(id_content){
	var commentaire=document.getElementById('commentaire').value;
	loadXMLDocGeneric('/dynamic/web.php?q=commentaire_ins_ajax&article='+encodeURIComponent(id_content)+'&commentaire='+encodeURIComponent(commentaire), 'CommentsIns', 'text/html', 'comment_results', 'comment_form');
}

function openCommentForm()
{
	if(location.href.search(/#commentform/)!=-1){
		var div = document.getElementById("commenter");
		if(div && div.style.display == 'none') div.style.display = "block";
	}
}

function sendbymail() {
	
	var div = document.getElementById("envoi");
	if (div.style.display == 'none') {
		div.style.display = "block";
		document.getElementById('TB_overlay').style.display = "block";
	}
	else {
		div.style.display = "none";
		document.getElementById('TB_overlay').style.display = "none";
	}
	//document.getElementById("commenter").style.display = "none";
	//location.href='#sendfriendform';
}

function sendFriend(){
	var id_article = document.getElementById('sendfriend_id_article').value;
	var url = document.getElementById('sendfriend_url').value;
	var nom = document.getElementById('sendfriend_nom').value;
	var from = document.getElementById('sendfriend_from').value;
	var dest = document.getElementById('sendfriend_dest').value;
	var email = document.getElementById('sendfriend_email').value;
	var message = document.getElementById('sendfriend_message').value;
	if ((nom=='')||(nom==null)||(nom==undefined) || (from=='')||(from==null)||(from==undefined)|| (dest=='')||(dest==null)||(dest==undefined)|| (email=='')||(email==null)||(email==undefined)|| (message=='')||(message==null)||(message==undefined))
	{
		alert('Veuillez remplir tous les champs');
		return false;
	}
	else{
		loadXMLDocGeneric('/dynamic/web.php?q=sendfriend&id_article='+encodeURIComponent(id_article)+'&url='+encodeURIComponent(url)+'&nom='+encodeURIComponent(nom)+'&from='+encodeURIComponent(from)+'&dest='+encodeURIComponent(dest)+'&email='+encodeURIComponent(email)+'&message='+encodeURIComponent(message), 'SendFriend', 'text/html', 'sendfriends_results', 'envoi');
		document.getElementById('TB_overlay').style.display = "none";
		return true;
	}
}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);

   return output;
}

  var startSize = "";
  var startSize2 = "";
  function textSize(valeur) {
    if (startSize=="") startSize = parseInt(document.getElementById('articleMain').style.fontSize);
	var size = parseInt(document.getElementById('articleMain').style.fontSize.substr(0, 2));
    if (valeur<0 && size > startSize)
      document.getElementById('articleMain').style.fontSize = parseInt(document.getElementById('articleMain').style.fontSize.substr(0, 2)) + valeur + "px";
    else if (valeur>0 && size <= 24)
      document.getElementById('articleMain').style.fontSize = parseInt(document.getElementById('articleMain').style.fontSize.substr(0, 2)) + valeur + "px";
	
	if(document.getElementById('articleChapeau')){
		if (startSize2=="") startSize2 = parseInt(document.getElementById('articleChapeau').style.fontSize);
	    var size2 = parseInt(document.getElementById('articleChapeau').style.fontSize.substr(0, 2));
		if (valeur<0 && size2 > startSize2)
	      document.getElementById('articleChapeau').style.fontSize = parseInt(document.getElementById('articleChapeau').style.fontSize.substr(0, 2)) + valeur + "px";
	    else if (valeur>0 && size2 <= 24)
	      document.getElementById('articleChapeau').style.fontSize = parseInt(document.getElementById('articleChapeau').style.fontSize.substr(0, 2)) + valeur + "px";
	}
  }
  
function freesearch()
{
	var tag=document.getElementById('freesearch').value;
	tag=replaceAll(tag, ' ', '-');
	var since=document.getElementById('since').value;
	if (tag!='') location.href=mainsite+'recherche/'+encodeURIComponent(tag)+'/'+encodeURIComponent(since);
	return;
}

function printpage(url) {
	url=url.split('#');
	var w=window.open(url[0].replace('.html', 'p.html'), '_blank');
}

var lastUne=0;
var currentUne=0;
function loopUnes(id_une) {
  setTimeout('showNextUne("'+id_une+'")', 8000)
}

function showNextUne(id_une) {
  var arUnes = new Array();
  for(var i=1;i<=4;i++) {
    if(document.getElementById(id_une+i)) arUnes[arUnes.length] = id_une+i;
  }
  if(arUnes.length>1) {
    if(currentUne==(arUnes.length-1)) {
      currentUne=0;
      lastUne=arUnes.length-1;
    }else {
      lastUne=currentUne;
      currentUne=currentUne+1;
    }
    document.getElementById(arUnes[currentUne]).style.display = "block";
    document.getElementById(arUnes[lastUne]).style.display = "none";
  
    setTimeout('showNextUne("'+id_une+'")', 8000)
  }
}

