$(document).ready(function(){	
	$('.zedx_map').maphilight({
		fade: true,
		fillColor:"ffffff",
		fillOpacity:0.4,
		strokeColor:"ffffff",
		strokeOpacity:0.1,
		strokeWidth:5
		});
		 // Use the each() method to gain access to each elements attributes
  
     if($('#searchCate2').length){
		$('#searchCate2').autobox(); 
		}
	if($('#header_recherche').length){
		$('#header_recherche').autobox(); 
		}	
	if($('.autobox').length){
		$('.autobox').autobox(); 
		}	
	if($('#searchCate').length){
		$('#searchCate').autobox(); 
	}
$("#menupro").hover(function(){
  $(this).find("ul").fadeIn();
},function(){
  $(this).find("ul").fadeOut();
}); 

			$("#slider").easySlider({
				auto: true, 
				continuous: true,
				numeric: true,				
				vertical: false,
				speed: 300,
				pause: 5000
			});
			$(".fancybox").fancybox({scrolling:"no",padding:0,margin:0,'autoScale': true, showCloseButton:true,onStart:function(){
$("#formlogin").slideDown ();
$("#formloginpwd").css("display","none");
}});
			
			//fonction recherche
			//$('#header_recherche').click(function(){if($(this).attr('value')=='Recherchez'){$(this).attr('value','');}});
			$('#btn_search_header').click(function(){recherche_header()});
			$('#btnSearch').click(function(){recherche_doc()});	  
			$("#search").submit(function(){
			if($('#select_cat2 option:selected').attr('value')==''){
				switch($("#searchCate2").attr('value')){
					/*case '':
					case 'Recherche':
						alert('Merci de choisir une catégorie');
						return false;
						break;*/
					default:
						return true;
						break;					
				}
			}
			else{
					var indice = $('#select_cat2 option:selected').text();
					$('.indice_opt').attr('value',indice);
					return true;
				}	
	})
		});
	
	
		function recherche_header() {
			var idx_header_texte = $('#header_recherche').attr('value');				
			if(idx_header_texte !='' && idx_header_texte != 'Recherchez'){idx_header_texte = 'OK';} else {alert('Veuillez saisir un element de recherche.');}
			if(idx_header_texte == 'OK'){$('#search_header').submit();/*alert('Votre recherche est en cours');*/}
		}
		
		function recherche_doc() {
			var idx_select = $('#selectSearch option:selected').attr('value');
			var idx_texte = $('#champSearch').attr('value');
			//alert(idx_select);alert(idx_texte);
			if(idx_select !='' && idx_select != 'catégorie'){idx_select = 'OK';} else {alert('Veuillez choisir une catégorie.');}		
			if(idx_texte !='' && idx_texte != 'Recherche'){idx_texte = 'OK';} else {alert('Veuillez saisir un element de recherche.');}
			if(idx_texte == 'OK' && idx_select == 'OK'){$('#search').submit();/*alert('Votre recherche est en cours');*/}
		}
		
function validatesearchcat(obj){
if(obj.select_cat.value!=""){
  obj.action=obj.select_cat.value;
}else{
  alert("Veuillez choisir une catégorie");
}
}
function validateInscription(obj,racineweb){
  if(obj.nom.value==""){
    alert("Veuillez indiquer votre nom");
    obj.nom.focus();
    return false;
  }
  if(obj.prenom.value==""){
    alert("Veuillez indiquer votre prénom");
    obj.prenom.focus();
    return false;
  }
  if(obj.email.value==""){
    alert("Veuillez indiquer votre email");
    obj.email.focus();
    return false;
  }
  if(obj.email.value==""){
    alert("Veuillez indiquer votre email");
    obj.email.focus();
    return false;
  }
  if(obj.cp.value==""){
    alert("Veuillez indiquer votre code postal");
    obj.cp.focus();
    return false;
  }
  if(obj.ville.value==""){
    alert("Veuillez indiquer votre ville");
    obj.ville.focus();
    return false;
  }
  if(obj.societe.value==""){
    alert("Veuillez indiquer votre societe");
    obj.societe.focus();
    return false;
  }
  if(obj.username.value==""){
    alert("Veuillez indiquer votre identifiant");
    obj.identifiant.focus();
    return false;
  }
  if(obj.pass.value==""){
    alert("Veuillez indiquer votre mot de passe");
    obj.pass.focus();
    return false;
  }
  if(obj.pass.value!=obj.pass2.value){
    alert("Vous avez mal retapé votre mots de passe");
    obj.pass.focus();
    return false;
  }
  
  /*
  var elemtxt=new Array();
  for(i=0;i<obj.elements.length;i++){
    elemtxt.push("\""+obj.elements[i].name+"\":\""+obj.elements[i].value+"\"");
  }
  param="{"+elemtxt.join(",")+"}";
  */
  param={nom:obj.nom.value,prenom:obj.prenom.value,email:obj.email.value,tel:obj.tel.value,adresse:obj.adresse.value,
  cp:obj.cp.value,ville:obj.ville.value,country_id:obj.country_id.value,societe:obj.societe.value,
  activity_id:obj.activity_id.value,username:obj.username.value,pass:obj.pass.value}
  //alert(param)
  $.post(racineweb+"/tpl/saveusr.php",param,
  function(data) {    
      if(data=="ok"){
        alert("Votre inscription a été prise en compte");
      }else{
        alert("Il existe déjà un compte affecté a cet email, si vous avez oublié votre mot de passe cliquez sur connexion");
      }
      $.fancybox.close();
  })
  return false;
}
function validateLogin(obj,racineweb){
  if(obj.username.value==""){
    alert("Veuillez indiquer votre identifiant");
    obj.username.focus();
    return false;
  }
  if(obj.pass.value==""){
    alert("Veuillez indiquer votre mot de passe");
    obj.pass.focus();
    return false;
  }
  param={username:obj.username.value,pass:obj.pass.value}
  $.post(racineweb+"/tpl/testlogin.php",param,
  function(data) {    
      //alert(data)
      if(data=="erreur"){
        alert("Login/mot de passe non valide");
      }else{
        window.location.href=window.location.href;
      }  
  })
  return false;
}
function showpwd(){
  $("#formlogin").slideUp ();
  $("#formloginpwd").slideDown ();
}
function validatePWD(obj,racineweb){
  if(obj.email.value==""){
    alert("Veuillez indiquer votre email");
    obj.email.focus();
    return false;
  }
  param={email:obj.email.value}
  $.post(racineweb+"/tpl/forgotpwd.php",param,
  function(data) {    
      //alert(data)
      if(data=="erreur"){
        alert("Email non trouvé en base");
      }else{
        alert("Votre mot de passe a été envoyé par mail");
        $.fancybox.close();
      }  
  })
  return false;
}
function AddPanier(racine,produitref,url){
  if(islog){
    $.post(racine+"/tpl/gestionpanier.php", { 'action':'add','nb': 1, 'id': produitref },function(data) {
      //alert(data);
     window.location.href=url
   });
 }else{
  alert("Veuillez vous identifier")
 }
}
function deletePanier(racine,id){
  if(islog){
    $.post(racine+"/tpl/gestionpanier.php", { 'action':'delete','id': id },function(data) {
     window.location.reload();
   });
 }else{
  alert("Veuillez vous identifier")
 }
}
function chgPanier(racine,id,nb){
  if(islog){
    $.post(racine+"/tpl/gestionpanier.php", { 'action':'chg','id': id,'nb': nb },function(data) {
     window.location.reload();
   });
 }else{
  alert("Veuillez vous identifier")
 }
}
$(document).ready(function(){
	
		var $txt	 	= $('#searchCate2'),
			$cat		= $('#select_cat'),
			$btn		= $('#btnCate'),
			$form		= $('#search'),
			valid      	= $form.submit(function(){
			         
								if($('#select_cat2 option:selected').attr("value")!=''){
									return true;
								}
								else{
									alert('Merci de choisir une catégorie');
									return false;
								}
							  });
							  
		$txt.focus(
			function(){
				//if($txt.attr("value")=='Recherche'){$txt.attr("value",'');}
			}
		);
		
		$txt.keydown(
			function(e){
				if(e.which==13){valid;}
			}
		);
		
		$btn.click(
			function(){
				valid;
			}
		);
		
		$cat.change(
			function(){
				var opt	= $('#select_cat2 option:selected').attr('name'),
					url = '<?=urlp(__nodeiddocument__)?>/'+opt;
				$('#search').attr('action',url);	
			}
		);
});
function share(val){
  myurl=window.location.href.split("#");
  if(val==1){
    //alert("http://www.facebook.com/share.php?u="+myurl+"&t=Pur%20Inside");
    window.open("http://www.facebook.com/share.php?u="+myurl[0]+"&t="+document.title,"facebook","width=200,height=200");        
  }else{
    window.open("http://twitter.com/share?text="+myurl[0]);
  } 
}
