var ajaxAvailable = false;
var FromOnglet = false;
detail = false;
treeviewsecond = "";


$(document).ready(function(){
	
	// fourth example
	$(".treeview").treeview({
		collapsed: true,
		unique: true,
		animated: "fast"
	});
	
	$(".ClassDetailPrint").live("click", function(){
		PrintDetail($(this).attr('rel'));		
	});
	
	$(".ClassDetailSend").live("click", function(){
		SendDetail($(this).attr('rel'));		
	});
	
	$(".hitarea").live("click", function(){
		var tmp = $("#debutLoc").attr('value')+"/"+ $(this).parent().find("span:first").attr("id");
		//navClick(tmp);
		$("#location").attr('value',tmp  );
		DoajaxRequest("treeview");		
	});
	
	$(".SecondeListe-hitarea").live("click", function(){
		//alert(treeviewsecond);
		if( $(this).parent().attr("class") == "SecondeListe collapsable lastCollapsable" ){
			if(treeviewsecond != "")fermeCurrentTreeView();
			treeviewsecond = $(this).parent().attr("id");
		}else treeviewsecond = "";		
	});
	
	$(".SpanTreeview").live("click", function(){
		//alert(treeviewsecond);
		if( $(this).parent().attr("class") == "SecondeListe collapsable lastCollapsable" ){
			if(treeviewsecond != "")fermeCurrentTreeView();
			treeviewsecond = $(this).parent().attr("id");
		}else if( $(this).parent().attr("class") != "SecondeListe last" && $(this).parent().attr("class") != "last" ) treeviewsecond = "";		
	});
	
	
	$(".hautPage img").hover(function(){
		   $(this).attr("src","images/plugins/sarisearch/haut_on.gif");
	 },function(){
		 	$(this).attr("src","images/plugins/sarisearch/haut_off.gif");
	 });

	$(".treeview span").hover(function(){
		   $(this).addClass("hover");
	 },function(){
		 	$(this).removeClass("hover");
	 });
	
	
  $("#ajax-loader").ajaxStart(function(){
	 if(detail == false){
		 $("#Ajaxresult").hide();
		 $(this).show();
	 }
  });

  $("#query").click(function () {
	  if($(this).attr('value') == 'Taper un mot-cl\351') $(this).attr('value',"");
  });
  

  $("#backgroundPopup").click(function () {
	  return popupClose();
  });
  
  
  $("#query").blur(function () {
	  if($(this).attr('value') == '') $(this).attr('value',"Taper un mot-cl\351");
  });
  
  $(".SpanTreeview").click(function(){
	  var tmp = $("#debutLoc").attr('value')+"/"+$(this).attr('id');
	  //navClick(tmp);
	  $("#location").attr('value',tmp  );
	  DoajaxRequest("treeview");
  });
  
  $(".SpanTreeview").hover(function(){
	  $(this).css("cursor","pointer");
  });
  
  
  
  $("#ajax-loader").ajaxStop(function(){
	  if(detail == false){
		  $(this).hide();
		  $("#Ajaxresult").show();
	  }
  });

  $("#formajaxsearch").submit(function(){
	  $("#location").attr('value',"");
	  return DoajaxRequest("facetquery");
    });
  
  $(".submit").click(function(){
	  $("#location").attr('value',"");
	  return DoajaxRequest("facetquery");
    });
  
  $(".urlDetailSari").live("click", function(){
	  var url = $(this).attr('href');
	  return showDetail(url);
    });
  
  $("#popupClose").live("click", function(){
	  return popupClose();
    });
  
  $("#onglet a").click(function(){
	  FromOnglet = true;
	  $("#location").attr('value',"");
	  
	  if($(this).attr('id')=="ACTIVITE" ){
	  		navOngletActivite();
	  }else if($(this).attr('id')=="ASSOCIATIONS" ){
		   navOngletAssossiation(); 
	  }else if($(this).attr('id')=="EQUIPEMENT" ){
		  navOngletEquipement();
	  }
	  RazForm();
  });
});

function fermeCurrentTreeView(){
	$("#"+treeviewsecond).attr("class","SecondeListe expandable lastExpandable");
	$("#"+treeviewsecond).find("ul").attr("style","display: none;");
}

function DoajaxRequest(tmpSend){
	$("#DivProp").hide();
	if($("#query").attr('value') == 'Taper un mot-cl\351') $("#query").attr('value',"");
	if(tmpSend == "treeview"){
		 $("#VfacetArrondissement").attr('value',"");	
		 $("#VfacetPublics").attr('value',"");		
		 $("#VfacetTypeAct").attr('value',"");	
	}
	var strform = $("#formajaxsearch").serialize();
	//alert("slqidycgaqz     " + strform);
	var facetArrondissement = "facetArrondissement";
	var facetPublics = "facetPublics";
	var facetType = "facetType";
	var facetquery = "facetquery";
	var facettypeAct = "facettypeAct";
    $.ajax({
	type: "POST",
	url: "jsp/site/ajax.jsp",
	data: strform,
	dataType:"json",
	success: function(msg){
	  var tmp = eval(msg);
	  if($("#debutLoc").attr('value')=="ACTIVITE" ){
		  if(tmpSend!=facetPublics) $("#facetPublics").replaceWith(tmp.VfacetPublics);
		  if(tmpSend!=facettypeAct) $("#facettypeAct").replaceWith(tmp.VfacetTypeAct);
		  if(tmpSend!=facetArrondissement) $("#facetArrondissement").replaceWith(tmp.VfacetArrondissement);		  
	  }else if($("#debutLoc").attr('value')=="ASSOCIATIONS" ){
		  if(tmpSend!=facetArrondissement) $("#facetArrondissement").replaceWith(tmp.VfacetArrondissement);		  
	  }else if($("#debutLoc").attr('value')=="EQUIPEMENT" ){
		  if(tmpSend!=facetArrondissement) $("#facetArrondissement").replaceWith(tmp.VfacetArrondissement);
	  }
	 
	  var reg = new RegExp("[/]+", "g");
	  var Stmp = $("#location").attr("value").split(reg);
	  if(Stmp.length > 4)$("#navEtprop").replaceWith(tmp.location);
	  else $("#navEtprop").replaceWith("<div id=\"navEtprop\"></div>");
	  $("#Ajaxresult").replaceWith(tmp.result);
	  
	  var tmp2 = tmp.treeview;
	  //if (tmpSend == facetPublics || tmpSend == facetType || tmpSend == facetArrondissement || tmpSend == facetquery )
	  if (tmpSend != "treeview") RefreshTree(tmp2,$("#debutLoc").attr('value'));
	  
	  if($("#debutLoc").attr('value')=="ACTIVITE" ){
		  $("#titreOnglet").replaceWith("<h1 id='titreOnglet' style='margin-bottom: 0'>Activit\351s / stages</h1>");
		  $("#touslesthemes").replaceWith("<h2 id='touslesthemes' onclick='navClick(this.id)'>Activit\351s / Stages</h2>	");	
		  Doariane();
		  $("#Ariane").attr("style","display: block;");
	  }else if($("#debutLoc").attr('value')=="ASSOCIATIONS" ){
		  $("#titreOnglet").replaceWith("<h1 id='titreOnglet'  style='margin-bottom: 0'>Associations / Organismes</h1>");
		  $("#Ariane").attr("style","display: none;");
	  }else if($("#debutLoc").attr('value')=="EQUIPEMENT" ){
		  $("#titreOnglet").replaceWith("<h1 id='titreOnglet'  style='margin-bottom: 0'>Lieux de Pratique</h1>");
		  $("#touslesthemes").replaceWith("<h2 id='touslesthemes' onclick='navClick(this.id)'>Lieux de Pratique</h2>	");
		  Doariane();
		  $("#Ariane").attr("style","display: block;");		  
	  }
	  
	  if(Stmp.length > 4){
		  tmpMotClefProp = "<h3 class='motclefProp'>"+Stmp[3]+"</h3>"		  
		  $("#navEtprop").clone().appendTo("#DivProp");
		  $("#DivProp").prepend(tmpMotClefProp);
		  $("#navEtprop").empty();		 
		  if( $("#proposition").html() != "" ) $("#DivProp").show();
	  }
	  
	  
    }
     });
    if($("#query").attr('value') == '') $("#query").attr('value','Taper un mot-cl\351');
    return false;

}

function clientWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
    return myWidth;
}

function Doariane(){
	var reg = new RegExp("[/]+", "g");
	var tmpLotactionTab = $("#location").attr("value").split(reg);
	var tmpStringAriane = "";
	for ( var int = 1; int < tmpLotactionTab.length-1; int++) {
		if (int == 1){
			tmpStringAriane = tmpStringAriane + tmpLotactionTab[int];
		}else tmpStringAriane = tmpStringAriane + " > " + tmpLotactionTab[int];
	}
	//alert($("#location").attr("value"));
	$("#Ariane").replaceWith("<span id='Ariane'>"+tmpStringAriane+"</span>");
}

function showDetail(url){
//	if(ajaxAvailable){
//		document.getElementById('backgroundPopup').style.opacity='0.7';
//		document.getElementById('backgroundPopup').style.filter='alpha(opacity=70)';
//		document.getElementById('backgroundPopup').style.display='block';
//		document.getElementById('popup').style.position='absolute';
//		document.getElementById('popup').style.display='block';
//		var cw = parseInt((clientWidth()-parseInt(document.getElementById('popup').offsetWidth))/2);
//		document.getElementById('popup').style.top='30px';
//		document.getElementById('popup').style.left=cw+'px';
//	}else{
		detail = true;
		
		$.ajax({
			type: "POST",
			url: url,
			dataType:"json",
			success: function(msg){
				var tmp = eval(msg);
				$("#popup").empty();
				$("#popup").append(tmp.html);
				$("#DetailPrint").attr('rel',url);
				$("#DetailSend").attr('rel',url);
				document.getElementById('backgroundPopup').style.opacity='0.7';
				document.getElementById('backgroundPopup').style.filter='alpha(opacity=70)';
				document.getElementById('backgroundPopup').style.display='block';
				document.getElementById('popup').style.position='absolute';
				document.getElementById('popup').style.display='block';
				var cw = parseInt((parseInt(document.getElementById('plugin').offsetWidth)-parseInt(document.getElementById('popup').offsetWidth))/2);
				document.getElementById('popup').style.top='30px';
				document.getElementById('popup').style.left=cw+'px';
				var t2 = document.getElementById("content_pop").offsetHeight;
  				document.getElementById("popup").style.height = t2+'px';	
  				document.getElementById("popup").scrollTop = 0;
			},
			error:function(msg){
				alert('Une erreur est survenue lors\n de la recherche d\'informations...\nVeulliez nous excuser pour le d\351sagr\351ment caus\351. ');
			}
		});
//	}
		detail = false;
		return false;
}

function popupClose(){
	document.getElementById('backgroundPopup').style.display='none';
	document.getElementById('popup').style.display='none';
	return false;
}


function AjoutFC(id,Vclass){
	//alert('AjoutFC \n 1:'+Vclass+'\n 2:'+id);
	Valtmp = $("#V"+Vclass).attr('value')+","+id;
	$("#V"+Vclass).attr('value',Valtmp);
	//alert('input hidden V'+Vclass+' : '+$("#V"+Vclass).attr('value'));
}

function SupprFC(id,Vclass){
	//alert('SupprFC \n 1:'+Vclass+'\n 2:'+id);
	var tmp = new Array();
	var reg = new RegExp("[,]+", "g");
	var tmpActuel = $("#V"+Vclass).attr('value').split(reg);
	for (var i=0; i<tmpActuel.length; i++) {
	 	if (tmpActuel[i] != id) tmp.push(tmpActuel[i]);
	}
	$("#V"+Vclass).attr('value',tmp.join(","));
	//alert('input hidden V'+Vclass+' : '+$("#V"+Vclass).attr('value'));
}

function inputCheckBox(tmp){
	tmp.checked?AjoutFC(tmp.id ,tmp.className):SupprFC(tmp.id,tmp.className);
	DoajaxRequest(tmp.className);
}

function navClick(tmp){
	if(tmp == 'touslesthemes'){
		  $("#location").attr('value',"");
		  DoajaxRequest("treeview");
	}else{
		  $("#location").attr('value',tmp  );
		  DoajaxRequest("treeview");   
	}	
}

function RefreshTree(tmp2,valDebloc){
	Divarbre = "<div id='Arbre'>"+tmp2+"</div>";
	if(valDebloc != "ASSOCIATIONS") $("#Arbre").replaceWith(Divarbre);
	
	// fourth example
	$("#browser").treeview({
	collapsed: true,
	unique: true,
	animated: "fast",
	persist: "cookie"

	});
	
	$(".SpanTreeview").click(function(){
		  var tmp = $("#debutLoc").attr('value')+"/"+$(this).attr('id');
		  //navClick(tmp);
		  $("#location").attr('value',tmp  );
		  DoajaxRequest("treeview");
	  });
	
	$(".SpanTreeview").hover(function(){
		  $(this).css("cursor","pointer");
	  });
	  
	  
}

function pagination(tmp){
	var url=tmp.substring(0,tmp.indexOf("?"));
	var param = tmp.substring(tmp.indexOf("?")+1,tmp.length);
	
	$("#DivProp").hide();
	$.ajax({
	type: "POST",
	url: url,
	data: param,
	dataType:"json",
	success: function(msg){
	  var tmp = eval(msg);
	  $("#Ajaxresult").replaceWith(tmp.result);
	  $(".urlDetailSari").click(function(){
		  
		  showDetail($(this).attr('href'));
	    });
	  
	  if($("#debutLoc").attr('value')=="ACTIVITE" ){
		  $("#titreOnglet").replaceWith("<h1 id='titreOnglet'>Activit\351s / stages</h1>");	  
	  }else if($("#debutLoc").attr('value')=="ASSOCIATIONS" ){
		  $("#titreOnglet").replaceWith("<h1 id='titreOnglet'>Associations / Organismes</h1>");	  
	  }else if($("#debutLoc").attr('value')=="EQUIPEMENT" ){
		  $("#titreOnglet").replaceWith("<h1 id='titreOnglet'>Lieux de Pratique</h1>");
	  }
	  
	  var reg = new RegExp("[/]+", "g");
	  var Stmp = $("#location").attr("value").split(reg);
	  if(Stmp.length > 4)$("#navEtprop").replaceWith(tmp.location);
	  else $("#navEtprop").replaceWith("<div id=\"navEtprop\"></div>");
	  
	  if(Stmp.length > 4){
		  tmpMotClefProp = "<h3 class='motclefProp'>"+Stmp[3]+"</h3>"		  
		  $("#navEtprop").clone().appendTo("#DivProp");
		  $("#DivProp").prepend(tmpMotClefProp);
		  $("#navEtprop").empty();		 
		  if( $("#proposition").html() != "" ) $("#DivProp").show();
	  }
	  
  	}
     });

    return false;
}

function RazForm(){
	$("#VfacetArrondissement").attr('value',"");
	$("#VfacetPublics").attr('value',"");
	$("#pagination_sarisearch").attr('value',"");
	DoajaxRequest("facetType");
}

function PrintDetail(tmpURL){
	param = tmpURL.substring(tmpURL.indexOf("?")+1,tmpURL.length);
//	alert("jsp/site/detailPrint.jsp?"+param+"&print=ok");
	var w = window.open("jsp/site/detailPrint.jsp?"+param+"&print=ok","Print","menubar=no, location=no, status=no, scrollbars=yes , resizable=no , directories=no, width=505, height=510 ");
}

function SendDetail(tmpURL){
	param = tmpURL.substring(tmpURL.indexOf("?")+1,tmpURL.length);
//	alert("jsp/site/detailPrint.jsp?"+param+"&print=ok");
	var w = window.open("jsp/site/detailSend.jsp?"+param,"Send","menubar=no, location=no, status=no, scrollbars=yes , resizable=no , directories=no, width=590, height=746 ");
}


function navOngletActivite(){
	$("#VfacetType").attr('value',",Activites");
		  $("#debutLoc").attr('value',"ACTIVITE");
		  $("#ACTIVITE").attr('class',"onglet-actif");
		  $("#ASSOCIATIONS").attr('class',"onglet");
		  $("#EQUIPEMENT").attr('class',"onglet");
		  $("#facetArrondissement").css("display","block");
		  $("#facetTypeAct").css("display","block");
		  $("#facetPublics").css("display","block");
		  $("#Arbre").css("display","block");
		  $("#resultats").css("margin-left","200px");
}

function navOngletAssossiation(){
	$("#VfacetType").attr('value',",Organisme");
		  $("#debutLoc").attr('value',"ASSOCIATIONS");
		  $("#ASSOCIATIONS").attr('class',"onglet-actif");
		  $("#ACTIVITE").attr('class',"onglet");
		  $("#EQUIPEMENT").attr('class',"onglet");
		  $("#facetArrondissement").css("display","block");
		  $("#facetTypeAct").css("display","none");
		  $("#facetPublics").css("display","none");
		  $("#VfacetTypeAct").attr('value',"");
		  $("#Arbre").css("display","none");
		  $("#resultats").css("margin-left","0");
}

function navOngletEquipement(){
	 $("#VfacetType").attr('value',",Equipement");
		  $("#debutLoc").attr('value',"EQUIPEMENT");
		  $("#EQUIPEMENT").attr('class',"onglet-actif");
		  $("#ACTIVITE").attr('class',"onglet");
		  $("#ASSOCIATIONS").attr('class',"onglet");
		  $("#facetArrondissement").css("display","block");
		  $("#facetTypeAct").css("display","none");
		  $("#facetPublics").css("display","none");
		  $("#Arbre").css("display","block");
		  $("#resultats").css("margin-left","200px");
		  $("#VfacetTypeAct").attr('value',"");
}

function linkNoResultat(){
	var txt="<ul>";
			if($("#debutLoc").attr('value')!='ACTIVITE' )
				txt+='<li><a href="javascript:navOngletActivite();RazForm();">Activit\351s et stages</a></li> ';
				
			if($("#debutLoc").attr('value')!='ASSOCIATIONS' )
				txt+='<li><a href="javascript:navOngletAssossiation();RazForm();">Associations et organisme</a></li> ';
				
			if($("#debutLoc").attr('value')!='EQUIPEMENT' )
				txt+='<li><a href="javascript:navOngletEquipement();RazForm();">Lieux de pratique</a></li>';
			txt+="</ul>";
			$("#otherLink").replaceWith(txt);
}
