// JavaScript Document
var current_onglet=1;
var nb_onglet=1;
var max_num_onglet=1;
var onglet_static = 0;

//var tabs_dynamique = 1;

function go_search(mot,type,sous_type,id,completion,ferme,tracker,option) {
	
	var btn_ferme = '';
	if (completion!=9 && nb_onglet==1) {document.getElementById('search_onglets').innerHTML='';} 
	if(document.getElementById('content'+current_onglet)!=null){
		document.getElementById('content'+current_onglet).style.display='none';
	}
		
	//vérification si l'onglet existe
	if(!search_onglet_exist(mot)){
		
			//autorise l'ajout d'onglet en fonction de préférence utilisateur
			max_num_onglet ++;
			if (completion!=9) {
				nb_onglet++;
			} 
			else {onglet_static++;}
			//Gestion des fleche historique dans le content
			if (nb_onglet>1) {
				for (z=parseInt(onglet_static+3);z<=parseInt(nb_onglet+onglet_static);z++) {
					if (document.getElementById('alert_preview_btn_'+z)) {document.getElementById('alert_preview_btn_'+z).style.display='block';}	
				}
			}
			if (nb_onglet>2) {
				for (z=parseInt(onglet_static+2);z<=parseInt(nb_onglet+onglet_static);z++) {
				if (document.getElementById('alert_next_btn_'+z)) {document.getElementById('alert_next_btn_'+z).style.display='block';}	
				}
			}
			current_onglet=max_num_onglet;
			if (tabs_dynamique==1 || (tabs_dynamique==0 && ferme==0)) {
				//ajout de l'onglet
				maDiv = document.createElement("div");
				maDiv.id = 'search_onglet'+current_onglet;
				maDiv.className = 'search_onglet';
				maDiv.style.display='block';
				if (ferme==1) {btn_ferme='<td valign=top width=12><div class=search_onglet_close OnClick=\'search_delete_onglet("'+current_onglet+'");\'></div></td>';}
				if(mot.length>10 && completion!=9) {madivContent='<table border=0 cellspacing=0 cellpadding=1 width=100%><tr><td style="cursor:pointer;" ><marquee behavior="scroll" direction="left" scrollamount="2" scrolldelay="0" onclick="';
				if(tracker!="" && tracker!=undefined) {madivContent+=tracker;}
				madivContent+='search_view_onglet('+current_onglet+')">'+mot+'</marquee></td>'+btn_ferme+'</tr></table>';
				maDiv.innerHTML=madivContent} else { madivContent='<table border=0 cellspacing=0 cellpadding=1 width=100%><tr><td style="cursor:pointer;" onclick="';
				if(tracker!="" && tracker!=undefined) {madivContent+=tracker;}
				madivContent+='search_view_onglet('+current_onglet+')">'+mot+'</td>'+btn_ferme+'</tr></table>';
				maDiv.innerHTML=madivContent;
				}
				//on conserve l'id de l'onglet bio pour rappel nav depuisle content detail bio 
				if (completion==9 && type=='bios') { maDiv.innerHTML+='<input type="hidden" name="id_onglet_tabs_bio" id="id_onglet_tabs_bio" value="'+current_onglet+'" />';}
				maDiv.style.zIndex=10;
				if (completion!=9) {
				document.getElementById('search_onglets').appendChild(maDiv);
				} else {
				document.getElementById('search_onglets_static').appendChild(maDiv);	
				}
				//document.getElementById('search_field').value=mot;	
				//document.getElementById('search_onglet1').style.display='block';
			}
			//document.getElementById('search_onglet'+current_onglet).style.display='block';
			//document.getElementById('search_onglet'+current_onglet).innerHTML='<marquee scrollamount="2" scrolldelay="0" style="cursor:pointer" onclick="search_view_onglet('+current_onglet+')">'+mot+'</marquee>'
			//document.getElementById('content').style.zIndex=10;
			//document.getElementById('content').style.backgroundColor='#FFFFFF';
			
		
			//ajout du contenu
			var xhr = getXhr();
			//on affiche le loader
			document.getElementById('content_loader').style.display='block'
			//mode synchrone
			//alert(url_site+"modules/content/populate.php?product="+encodeURI(product_site)+"&type="+encodeURI(type)+"&sous_type="+encodeURI(sous_type)+"&id="+id+"&id_client="+id_client+"&lang="+lang+"&target_id="+document.getElementById('content_id_installed').value+"&current_onglet="+current_onglet+"&titre="+mot+"&nb_onglet="+nb_onglet+"&onglet_static="+onglet_static+"&option="+option);
			xhr.open("GET",url_site+"modules/content/populate.php?product="+encodeURI(product_site)+"&type="+encodeURI(type)+"&sous_type="+encodeURI(sous_type)+"&id="+id+"&id_client="+id_client+"&lang="+lang+"&target_id="+document.getElementById('content_id_installed').value+"&current_onglet="+current_onglet+"&titre="+mot+"&nb_onglet="+nb_onglet+"&onglet_static="+onglet_static+"&option="+option,false);
			xhr.send(null);
			
			if(completion!=9 && document.getElementById('content_id_installed')){ 
				document.getElementById('afpportal_'+document.getElementById('content_id_installed').value).style.display='block';
				document.getElementById('content').style.display='block';
			}
	
			//alert(current_onglet);
			//réponse
			maDiv = document.createElement("div");
			maDiv.id = 'content'+current_onglet;
			maDiv.innerHTML=xhr.responseText;
			document.getElementById('content').appendChild(maDiv);
			document.getElementById('content'+current_onglet).style.zIndex=10;
			document.getElementById('content'+current_onglet).className='content_page theme_'+type;
			
			//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';
			search_view_onglet(current_onglet);
				
			search_update_navigation("last");
			
			
			
		}

		
		//Ajout des mot clés recherchés
		if(completion==1){
			var xhr = getXhr()
			// On défini ce qu'on va faire quand on aura la réponse
			xhr.onreadystatechange = function(){
				// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
				if(xhr.readyState == 4 && xhr.status == 200){
					//alert(xhr.responseText);
				
				} 
			}
			
			xhr.open("GET",url_site+"modules/content/log_keywords.php?keywords="+encodeURI(mot)+"&type="+encodeURI(type)+"&sous_type="+encodeURI(sous_type)+"&id="+id+"&id_site="+id_client+"&lang="+lang,true);
			xhr.send(null);
	}
}


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 search_view_onglet(cible) {
	//Affichage du content de l'onglet cliqué
	//alert(cible+current_onglet);
	
	if(current_onglet!=1){
		document.getElementById('content_home').style.display='none';
		document.getElementById('content'+current_onglet).style.display='none';
	}
	if(document.getElementById('afpportal_'+content_id_box)!=null) {document.getElementById('afpportal_'+content_id_box).style.display='block';}
	document.getElementById('content').style.display='block';
	document.getElementById('content').scrollTop=0;
	document.getElementById('content'+cible).style.display='block';
	
	//document.getElementById('afpportal_result_box').style.zIndex=10 ??
	//mise au premier plan
	
	document.getElementById('content').style.zIndex=10;
	document.getElementById('content'+cible).style.zIndex=10;	
	current_onglet = cible;
}

function search_delete_onglet(cible) {
	//suppression de l'onglet et affichage du "home" si on est sur l'onglet supprimé
	document.getElementById('search_onglets').removeChild(document.getElementById('search_onglet'+cible));
	document.getElementById('content').removeChild(document.getElementById('content'+cible));
	//document.getElementById('content'+cible).style.display='none';
	//document.getElementById('search_onglet'+cible).style.display='none';
	//maDiv.style.zIndex=10;
	
	nb_onglet--;
	
	if(current_onglet==cible){
		document.getElementById('afpportal_'+content_id_box).style.display='none';
		document.getElementById('content').style.display='none';
		current_onglet = 1;
	}
	
	search_update_navigation("del");
}

function search_onglet_exist(libelle) {
	//Renvoi True si onglet existe, False sinon
	thisChild = document.getElementById('search_onglets').firstChild;
	trouve = false;
	while (!trouve && thisChild != null ){
		if ( thisChild.nodeType == 1 ){
			//alert(thisChild.innerHTML);
			if(thisChild.innerHTML.indexOf(">"+libelle+"<")!=-1){ 
				trouve = true;
				//alert(thisChild.id.substr(thisChild.id.length-1));
				search_view_onglet(thisChild.id.substr(thisChild.id.length-1));//affichage box existante
			}
		}
		thisChild = thisChild.nextSibling;
	}
	thisChild = document.getElementById('search_onglets_static').firstChild;
	while (!trouve && thisChild != null ){
		if ( thisChild.nodeType == 1 ){
			//alert(thisChild.innerHTML);
			if(thisChild.innerHTML.indexOf(">"+libelle+"<")!=-1){ 
				trouve = true;
				//alert(thisChild.id.substr(thisChild.id.length-1));
				search_view_onglet(thisChild.id.substr(thisChild.id.length-1));//affichage box existante
			}
		}
		thisChild = thisChild.nextSibling;
	}	
	return trouve;
}

function search_update_navigation(action) {
	//Mise à jour des onglets visibles et des fleches de navigation
	
		thisChild = document.getElementById('search_onglets').firstChild;
		old_child = "";
		cpt_display = 0;
		arrow_left = false;
		arrow_right = false;
		cpt=0;
		//parcours des onglets
		while ( thisChild != null ){
			if ( thisChild.nodeType == 1 ){
					//alert(thisChild.id+"/"+thisChild.style.display); 
					//alert(cpt+"/"+pagination_num_onglet+"/"+nb_onglet);
					//alert(thisChild.style.display);
					if(nb_onglet>pagination_num_onglet+1){
						if(action=="last" && cpt<nb_onglet-pagination_num_onglet-1){ //on affiche les pagination_num_onglet suivants 
							thisChild.style.display = "none";
						}
						else if(action=="last") thisChild.style.display = "block";
							
						if(action=="next" && cpt_display>0 && cpt_display<=pagination_num_onglet){ //on affiche les pagination_num_onglet suivants 
							thisChild.style.display = "block";
							cpt_display++;
						}
						if(action=="next" && thisChild.style.display!="none" && cpt_display==0){ // action next > on cache le premier
							thisChild.style.display = "none";
							cpt_display++;
						}
						//alert(thisChild.id+"/"+pagination_num_onglet+"/"+cpt_display);
						//alert(thisChild.id+"/"+thisChild.style.display); 
						
						if(action=="prev" && thisChild.style.display!="none" && cpt_display<pagination_num_onglet){
							if(old_child!=""){
								old_child.style.display = "block";
								cpt_display++;
							}
							//alert("display :"+old_child.id+"/"+cpt_display);
							
						}
							
						if(action=="prev" && thisChild.style.display!="none" && cpt_display>=pagination_num_onglet){ //on cache le dernier
							//alert("cache:"+thisChild.id);
							thisChild.style.display="none";
							cpt_display++;
						}
					}
					
					//alert(thisChild.id+"/"+thisChild.style.display);
					if(action=="del" && thisChild.style.display!="none" && cpt_display==0){
						if(old_child!=""){
							old_child.style.display = "block";
							cpt_display++;
						}
						//alert("display :"+old_child.id+"/"+cpt_display);
						
					}
					//alert("display :"+thisChild.id+"/"+cpt_display);
					if(action=="del" && cpt_display>0 && cpt_display<pagination_num_onglet){ //on affiche le dernier
						//alert("cache:"+thisChild.id);
						thisChild.style.display="block";
						cpt_display++;
					}	
					
					
				if(cpt==1 && old_child.style.display=="none") arrow_left = true;
				old_child = thisChild;
				cpt++;
				
			}
			thisChild = thisChild.nextSibling;
			if(thisChild == null && old_child.style.display=="none") arrow_right = true;
		

		//affichage fleche gauche
		//alert(cpt_display);
		if (tabs_dynamique==1) {//on n'affiche les fleches qu'en mode dynamique
			if(arrow_left){
				document.getElementById('search_onglet_command_left_on').style.display='block';
				document.getElementById('search_onglet_command_left_off').style.display='none';
			}
			else{
				document.getElementById('search_onglet_command_left_on').style.display='none';
				document.getElementById('search_onglet_command_left_off').style.display='block';
			}
				if(arrow_right){
				document.getElementById('search_onglet_command_right_on').style.display='block';
				document.getElementById('search_onglet_command_right_off').style.display='none';
			}
			else{
				document.getElementById('search_onglet_command_right_on').style.display='none';
				document.getElementById('search_onglet_command_right_off').style.display='block';
			}
		}
		
	}
	
	
}

