//Initialisation des variables js
var current_overlay =0;
var cb = {}; //variable pour les fonctions callback ajax
var content_id_box='';
//calendar
if(typeof(Cal_min_day)=='undefined') var Cal_min_day = Array();
if(typeof(Cal_max_day)=='undefined') var Cal_max_day = Array();
if(typeof(Cal_id_jour)=='undefined') var Cal_id_jour = Array();
if(typeof(Cal_current_day)=='undefined') var Cal_current_day = Array();
   
 
//tabs
if(typeof(pagination_num_onglet)=='undefined') {var pagination_num_onglet=6;}




function afp_open_link(local_type,local_target,local_parameters,local_contexte){
 	/*ouverture de lien suivant le contexte
 	contexte d'appel : local, distant (iframe NON IMPLEMENTE) 
 	type de lien : content, overlay, url ou (module NON IMPLEMENTE)
 	target du lien : url cible
 	parameters : nom_parameter=value;nom_parameter=value;
 	*/
 	
 	//Initialisation
	var xhr = getXhr();
	var stamp = new Date();	
	var parameters_global = "product="+encodeURI(product_site)+"&lang="+encodeURI(lang)+"&id_client="+encodeURI(id_client);
	if(local_type=='') local_type='overlay';
	
	//gestion des parametres
	mot='';type='';sous_type="";id="";completion=1;ferme=1;tracker="";option="";
	width=600;height=500;
	local_parameters = local_parameters.replace("'","\\'");
	liste_parametres = local_parameters.split('&');
	for(i=0;i<liste_parametres.length;i++){
		if(liste_parametres[i]!=""){
			liste_parametres[i] = liste_parametres[i].split('=');
			//alert(""+liste_parametres[i][0]+" = '"+liste_parametres[i][1]+"';");
			eval(""+liste_parametres[i][0]+" = '"+liste_parametres[i][1]+"';");
		}
	}
	
 	//alert(local_contexte+'/'+local_type+'/'+local_parameters+'/'+local_target);
 	if(local_contexte=='local' && local_target=='' && local_type=='content'){
 			if(document.getElementById('tabs')){ //Module tabs existant
 				if(document.getElementById('content_id_installed')){ // Module content existe
 					//APPEL DE LA FONCTION GO SEARCH
 					
 					
 					//alert(mot+type+sous_type+id+completion+ferme+tracker+option);
 					go_search(mot,type,sous_type,id,completion,ferme,tracker,option);
 				}
 				else local_type='overlay'; //pas de content
 			}else{ //pas de module tabs
 				if(document.getElementById('content_id_installed')){ // Module content existe
 					//APPEL DU POPULATE DU CONTENT
 					
 					//on affiche le loader
					document.getElementById('content_loader').style.display='block'
					
					//appel du contenu
					xhr.open("GET",url_site+"modules/content/populate.php?"+parameters_global+"&"+encodeURI(local_parameters),false);
					xhr.send(null);
					//réponse
					maDiv = document.createElement("div");
					maDiv.id = 'content'+stamp.getTime();
					maDiv.innerHTML=xhr.responseText;
					document.getElementById('content').appendChild(maDiv);
					document.getElementById('content'+stamp.getTime()).style.zIndex=10;
					document.getElementById('content'+stamp.getTime()).className='content_page theme_'+type;
					document.getElementById('afpportal_'+document.getElementById('content_id_installed').value).style.display = 'block';
					document.getElementById('content').style.display = 'block';
					document.getElementById('content'+stamp.getTime()).style.display='block';
					//execution js si besoin //document.body
					execJS(document.getElementById('content'));
					
					//on kill le loader
					document.location.href = "#top_content";
					document.getElementById('content_loader').style.display='none';
 				}
 				else local_type='overlay'; //pas de content
 			}
	}
 	else {
 		
	 	if(local_type!='overlay'){
	 		 //alert(local_target);
			 //Rechargement de page
	 		 if(local_contexte=='local') local_parameters = 'id_client='+id_client+'&lang='+lang+'&';
	 		 if(local_target.length>0) local_lien = local_target;
	 		 else local_lien = "?"+local_parameters;
	 		 	
	 		 if(typeof(page)!='undefined' && page!='' && page!=1 && local_target=='') local_lien += "page="+page+"&";
	 		 if(typeof(type)!='undefined' && type!='') local_lien += "afp_content_type="+type+"&";
	 		 if(typeof(sous_type)!='undefined' && sous_type!='') local_lien += "afp_content_sous_type="+sous_type+"&";
	 		 if(typeof(afp_content_id)!='undefined' && afp_content_id!='') local_lien += "afp_content_id="+afp_content_id+"&";
			 if(typeof(afp_content_type)!='undefined' && afp_content_type!='') local_lien += "afp_content_type="+afp_content_type+"&";
			 if(typeof(afp_bio_id)!='undefined' && afp_bio_id!='') local_lien += "afp_bio_id="+afp_bio_id+"&";
			 if(typeof(afp_team_id)!='undefined' && afp_team_id!='') local_lien += "afp_team_id="+afp_team_id+"&";
			 if(typeof(id)!='undefined' && id!='') local_lien += "afp_content_id="+id+"&";
			 if(typeof(disc)!='undefined' && disc!='') local_lien += "afp_global_disc="+disc+"&";
			 if(typeof(result_id)!='undefined' && result_id!='') local_lien += "afp_result_id="+result_id+"&";
			 if(typeof(event_id)!='undefined' && event_id!='') local_lien += "afp_event_id="+event_id+"&";
			 if(typeof(afp_event_id)!='undefined' && afp_event_id!='') local_lien += "afp_event_id="+afp_event_id+"&";
			 if(typeof(afp_global_event)!='undefined' && afp_global_event!='') local_lien += "afp_global_event="+afp_global_event+"&";
			 if(typeof(phase_id)!='undefined' && phase_id!='') local_lien += "afp_phase_id="+phase_id+"&";
	 		 if(typeof(preview)!='undefined' && preview!='') local_lien += "preview="+preview+"&";
	 		 if(typeof(default_letter)!='undefined' && default_letter!='') local_lien += "default_letter="+default_letter+"&";
	 		 //alert(local_lien);
			 //alert('test'); 
	 		 //supprimer le dernier &
	 		 if(local_lien.substring(local_lien.length-1)=='&') local_lien = local_lien.substring(0,local_lien.length-1)
	 		 if (typeof(module)=='undefined') {module='';}
			 if ((id_client!=282 && id_client!=241 && id_client!=243 && id_client!=279 && id_client!=289) || (module==2761 || module==6770)) {
	 		 window.parent.location = local_lien;
			 } else {
			 window.location = local_lien;	 
			 }
	 	}
	}
 	
 	if(local_type=='overlay'){ // overlay par defaut
		  //alert(type);
		  if (type!='infographie') {
		  overlay_width = width;
		  overlay_height = height;
		  } else {
		  overlay_width = 780;
		  overlay_height = 580;
		  }
		  //alert(overlay_width);
		   result=returnSize();
			var myWidth=result[0];
			var myHeight=result[1];
			document.getElementById('afp_voile_alert').style.width = myWidth+"px";
			document.getElementById('afp_voile_alert').style.height =document.documentElement.scrollTop+myHeight+"px";
			document.getElementById('afp_voile_alert').style.display = "block";
			//centrage de la box d'alert
			document.getElementById('afp_alert').style.width = overlay_width+"px";
			document.getElementById('afp_alert').style.height = overlay_height+"px";	
			document.getElementById('afp_alert').style.display = "block";
			windowCenter('afp_alert');
			document.getElementById('afp_alert').style.display = "none";
			document.getElementById('afp_alert').style.zIndex =	100000;
			document.getElementById('afp_alert').innerHTML	= '<table height="100%" width="100%" bgcolor="#ffffff"><tr><td height="100%" align="center"><img src="'+url_site+'img/loader_content.gif" width="16" height="16" /></td></tr></table>';
			
			//agrandissement de la box
			//new Effect.Appear('afp_alert',0,{duration:2, fps:25, from:0.0, to:1.0 });
			new Effect.Grow('afp_alert',0,{duration:2, fps:25, from:0.0, to:1.0 });
		
			
			/*if(local_contexte=='local'){
				//mode synchrone
				xhr.open("GET",url_site+"modules/content/populate.php?"+parameters_global+"&"+encodeURI(local_parameters)+"&overlay=yes",false);
				xhr.send(null);
			}*/
			
			if(typeof(result_id)!='undefined'  && result_id!=""){ 
				local_module_source = 'result'; 
			}
			else local_module_source = 'content';
			local_lien = url_site+"modules/"+local_module_source+"/populate.php?"+parameters_global+"&"+encodeURI(local_parameters)+"&overlay=yes&iframe=1";
			
			//ajout du css defaut
	    var script = document.createElement('link');
	    script.href = url_site+'clients/default/'+local_module_source+'/'+local_module_source+'.css';
	    script.type = 'text/css';
	 		script.rel = 'stylesheet';
	 		script.media = 'screen';
	 		script.charset = 'utf-8';
	 		
	 		//Appel iframe
	 		document.getElementById('afp_alert').innerHTML = '<div class="content"><div id="content_overlay" style="width:'+overlay_width+'px;overflow:hidden"><div style="position:absolute;float:left" id="current_overlay_'+current_overlay+'"><iframe id="afp_iframe" src="'+local_lien+'" style="width:'+overlay_width+'px;height:'+overlay_height+'px;overflow:hidden" scrolling=no frameborder=0></iframe></div><div id="alert_tools" style="width:170px;cursor:pointer;position:absolute;left:'+(parseInt(overlay_width)+20)+'px;top:0px"><div style="float:left" onclick="clean_alert ()"><img src="'+url_site+'img/closed_alert.png" \></div><div id="alert_next_btn"  onclick="Appear_overlay_frame(\'plus\');" style="float:left;display:none"><img src="'+url_site+'img/next_frame.png" \></div><div id="alert_preview_btn" onclick="Appear_overlay_frame(\'moins\');" style="display:none;float:left"><img src="'+url_site+'img/preview_frame.png" \></div></div></div></div>';
				
	    // Et injection dans le DOM :
	   /* if(local_contexte=='local'){
				var headTag = document.getElementsByTagName('head')[0]; 
	    	headTag.appendChild(script);
			}*/
			
			//affichage
			/*if(local_contexte=='local'){
				document.getElementById('afp_alert').innerHTML = '<div class="content"><div id="content_overlay" style="width:'+width+'px;overflow:hidden"><div style="position:absolute;float:left" id="current_overlay_'+current_overlay+'">'+xhr.responseText+'</div><div id="alert_tools" style="width:170px;cursor:pointer;position:absolute;left:'+(parseInt(width)+20)+'px;top:0px"><div style="float:left" onclick="clean_alert ()"><img src="'+url_site+'img/closed_alert.png" \></div><div id="alert_next_btn"  onclick="Appear_overlay_frame(\'plus\');" style="float:left;display:none"><img src="'+url_site+'img/next_frame.png" \></div><div id="alert_preview_btn" onclick="Appear_overlay_frame(\'moins\');" style="display:none;float:left"><img src="'+url_site+'img/preview_frame.png" \></div><div style="float:left"><a href="http://www.addthis.com/bookmark.php?&amp;url='+local_lien+'&amp;title='+mot+'" onmouseover="return addthis_open(this, \'\', \''+local_lien+'\', \'[TITLE]\');" onmouseout="addthis_close();" onclick="return addthis_sendto();"><img src="'+url_site+'img/share_it.png" border="0" \></a></div></div></div></div>';
			}
			else{
				document.getElementById('afp_alert').innerHTML = '<div class="content"><div id="content_overlay" style="width:'+width+'px;overflow:hidden"><div style="position:absolute;float:left" id="current_overlay_'+current_overlay+'"><iframe id="afp_iframe" src="'+local_lien+'" style="width:'+width+'px;height:'+height+'px;overflow:hidden" scrolling=auto frameborder=0></iframe></div><div id="alert_tools" style="width:170px;cursor:pointer;position:absolute;left:'+(parseInt(width)+20)+'px;top:0px"><div style="float:left" onclick="clean_alert ()"><img src="'+url_site+'img/closed_alert.png" \></div><div id="alert_next_btn"  onclick="Appear_overlay_frame(\'plus\');" style="float:left;display:none"><img src="'+url_site+'img/next_frame.png" \></div><div id="alert_preview_btn" onclick="Appear_overlay_frame(\'moins\');" style="display:none;float:left"><img src="'+url_site+'img/preview_frame.png" \></div><div style="float:left"><a href="http://www.addthis.com/bookmark.php?&amp;url='+local_lien+'&amp;title='+mot+'" onmouseover="return addthis_open(this, \'\', \''+local_lien+'\', \'[TITLE]\');" onmouseout="addthis_close();" onclick="return addthis_sendto();"><img src="'+url_site+'img/share_it.png" border="0" \></a></div></div></div></div>';
				//var headTag = document.getElementById('afp_iframe').getElementsByTagName('head')[0]; 
	    	//headTag.appendChild(script);
			
			}*/
			
	}
 	
 	
 			
}

//*************** FONCTIONS GENERALES ******************************
function execJS(contenu){
 	//parse et execute le js dans contenu
 	//pour les documents ajax
 	
  var le_js="";
  
  contenu=contenu.getElementsByTagName('script');
  for(i=0;i<contenu.length;i++){
  	if(contenu[i].innerHTML!=''){
  		//alert(contenu[i].innerHTML);
  		eval(contenu[i].innerHTML);
  	}
  }
}

function getXhr(){
  var xhr = null; 
	if(window.XMLHttpRequest) // Firefox et autres
	   xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject){ // Internet Explorer 
	   try {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
	}
	else { // XMLHttpRequest non supporté par le navigateur 
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
	   xhr = false; 
	} 
  return xhr
}

function Appear_overlay_frame(sens) {
	current = 'current_overlay_'+current_view_overlay;
	if (sens=='plus') {
	next = 'current_overlay_'+parseInt(current_view_overlay+1);
	new Effect.Appear(current, {duration:0.5, fps:25, from:1.0, to:0.0, afterFinish: function() { $$$(current).style.display='none'} });
	new Effect.Appear(next, {duration:0.5, fps:25, from:0.0, to:1.0 });
	current_view_overlay = parseInt(current_view_overlay+1);
	} else if (sens=='news') {
	next = 'current_overlay_'+current_overlay;
	new Effect.Appear(current, {duration:0.5, fps:25, from:1.0, to:0.0, afterFinish: function() { $$$(current).style.display='none' } });
	new Effect.Appear(next, {duration:0.5, fps:25, from:0.0, to:1.0 });
	current_view_overlay = current_overlay;	
	} else {
	next = 'current_overlay_'+parseInt(current_view_overlay-1);
	new Effect.Appear(current, {duration:0.5, fps:25, from:1.0, to:0.0, afterFinish: function() { $$$(current).style.display='none' } });
	new Effect.Appear(next, {duration:0.5, fps:25, from:0.0, to:1.0 });
	current_view_overlay = parseInt(current_view_overlay-1);
	}
	if (current_view_overlay==0) {document.getElementById('alert_preview_btn').style.display='none';} else {document.getElementById('alert_preview_btn').style.display='block';}
	if (current_view_overlay<current_overlay) {document.getElementById('alert_next_btn').style.display='block';} else {document.getElementById('alert_next_btn').style.display='none';}
}

function clean_alert () {
	document.getElementById('afp_voile_alert').style.display='none';
	document.getElementById('afp_alert').style.display='none';
	document.getElementById('afp_alert').innerHTML='';
	current_overlay =0;
    current_view_overlay =0;
	overlay_width =0;
	overlay_height =0;
	overlay_allready = '|';
	
}

function windowCenter(myId) {
	result=returnSize();
	var myWidth=result[0];
	var myHeight=result[1];
	var elementWidth=document.getElementById(myId).offsetWidth;
	var elementHeight=document.getElementById(myId).offsetHeight;
    var myScrollLeft=document.documentElement.scrollLeft;
	var myScrollTop=document.documentElement.scrollTop;
	//alert(myScrollLeft+'/'+myWidth+'/'+(myWidth/2)+'/'+(elementWidth/2));
	var posX=myScrollLeft+myWidth-myWidth/2-elementWidth/2
	var posY=myScrollTop+myHeight-myHeight/2-elementHeight/2;
	if(posY<myScrollTop) posY = myScrollTop+20;
	document.getElementById(myId).style.left=posX+"px";
	document.getElementById(myId).style.top=posY+"px";
}
function inc_js(testFct,fileName) {
			  //alert(testFct);
			  if (window[testFct]){
				
			  } else {
				//alert(fileName);
				var head = document.getElementsByTagName("body")[0];
				var fileref=document.createElement('script')
  				fileref.setAttribute("type","text/javascript")
 				fileref.setAttribute("src", fileName)
				head.appendChild(fileref);
				if (testFct=='Lightbox') {
				setTimeout(function() {lightbox=new Lightbox();},3000);	
				}
			  }
}
function getVarURL(nomVariable)
{
	//extraction d'une variable GET d une url
	var infos = location.href.substring(location.href.indexOf("?")+1, location.href.length)+"&"
	if (infos.indexOf("#")!=-1)
		infos = infos.substring(0,infos.indexOf("#"))+"&"
	var variable=0
	{
		nomVariable = nomVariable + "="
		var taille = nomVariable.length
		if (infos.indexOf(nomVariable)!=-1)
			variable = infos.substring(infos.indexOf(nomVariable)+taille,infos.length).substring(0,infos.substring(infos.indexOf(nomVariable)+taille,infos.length).indexOf("&"))
	}
	return variable
}

function returnSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  var result = new Array(myWidth,myHeight);
  return result;
 }

