var affinez = true;

$(document).ready(function() {	
	$("a.filtres").toggle(function(){
		$('#img_loader2').css("display","block");
		if(affinez){
			$.ajax({
				type: "POST",
				url: "/ajax.php",
				data: "action=affinez&q="+q,
				success: function(data){
					$('#affinez').html(data);
					affinez = false;
				}
			});
		}		
		$(this).html('Cacher les filtres');
		$('#affinez').show();
	},function(){
		$(this).html('Afficher les filtres');
		$('#affinez').hide();
	});
/*
	$('a.jspagin').click(function () {
        $(window.location).attr('href', $(this).attr('title'));
		return false;
    });*/

	$('div.offre a.oe').click(function() {return src($(this).attr("id").substring(3));});

	$("a.morejob").click(function(){
		$("img.morejob").show();
		var id = $(this).attr("id").substring(3);
		$(this).remove();
		$.ajax({
			type: "POST",
			url: "./ajax.php",
			data: "action=morejob&id="+id,
			success: function(data){
				 $("img.morejob").remove();
				 $('#morejob').append(data);
			}
		});
		return false; 
	});
	
	$("a.morecluster").click(function(){
		var parent = $(this).parent().parent();
		var id = $(this).attr("id").substring(3);
		$(this).remove();
		$("#sp_"+id).css("display","block");
		$.ajax({
			type: "POST",
			url: "./ajax.php",
			data: "action=moreCluster&id="+id,
			success: function(data){
				parent.html(data);
			}
		});
		return false; 
	});

	$('div.ads-resultat2 h2, ads-resultat1 h2').hide();

	$("ul.share li").hover(function(){
		$(this).css('background-position','0px -20px');
	}, function(){
		$(this).css('background-position','0px 0px');
	});	
	$("ul.share2 li").hover(function(){
		$(this).css('background-position','0px -40px');
	}, function(){
		$(this).css('background-position','0px -20px');
	});
	$("ul.share2 li.postuler").hover(function(){
		$(this).css('background-position','0px -80px');
	}, function(){
		$(this).css('background-position','0px -60px');
	});

});
/**************************************************************************************/
function ca_filtrage(ths,v){
	$(ths).html('<img alt="" border="0" src="http://www.offres-emploi.ma/images/loader.gif">');
	$.ajax({
		type: "POST",
		url: "./ajax.php",
		data: "action=ca_filtrage&v="+v,
		success: function(data){
			location.reload(true);
		}
	});
	return false;
}
/**************************************************************************************/
function shareBox(id,inhref,original_referer,text,page){
	var html = '';
	if(page=='resultat'){html = '<ul class="share">';}
	html = html + '<li><a class="email" rel="nofollow" onclick="return sendToFriends(\''+id+'\',\'send'+id+'\');" href="#">Email</a></li>';
	html = html + '<li><a class="twitter" rel="nofollow" onclick="return sharing(\'twitter\',\'http://twitter.com/share?_=1292019690879&count=horizontal&original_referer='+original_referer+'&text='+text+'&url='+inhref+'&via=offres__emploi\');" href="#">Tweet</a></li>';
	html = html + '<li><a class="facebook" rel="nofollow" onclick="return sharing(\'facebook\',\''+inhref+'\');" href="#">Share</a></li>';
	html = html + '</ul>';
	html = html + '<div id="send'+id+'" class="send"></div>';
	if(page=='resultat'){
		$('a#oe_'+id).parent().append(html);
		$('a#oe_'+id).parent().css('padding-bottom','0');
	}
}
/**************************************************************************************/	
function sendToFriends(id,div){
	$('div.send').html('');
	$('#'+div).append($('img.loader').clone());
	$('#'+div).css("display","block");
	$('#'+div+' img').css("display","inline");
	$.ajax({
		type: "POST",
		url: "./ajax.php",
		data: "action=sendToFriends_form&id="+id,
		success: function(data){
			$('#'+div).html(data);
		}
	});
	return false;
}
/**************************************************************************************/
function alerteDelete(a,c,ths){
	$.ajax({
		type: "POST",
		url: "./ajax.php",
		data: "action=alerte_delete&c="+c+"&a="+a,
		success: function(data){
			 $(ths).parent().html(data);
		}
	});
	return false; 
}
/**************************************************************************************/
function alerteInsert(m,q){
	var email = m.val();
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9_\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		alert('Votre email n\'est pas valide.');
		m.focus();
		m.css('background-color','#FFDDDD');
		m.parent().css('background-color','#FFDDDD');
		return false;
	}else{
		var p = m.parent();
		p.html("<div class=\"en-cours\">Enregistrement de l'alerte en cours...</div>");
		$.ajax({
			type: "POST",
			url: "./ajax.php",
			data: "action=alerte_insert&m="+email+"&q="+q,
			success: function(data){
				p.html(data);
			}
		});
	}
	return false; 
}
/**************************************************************************************/
function sharing(r,url){
	if(r=="facebook"){
		window.open('http://www.facebook.com/sharer.php?u='+url,'Facebook','resizable=no, location=no, width=640, height=440, menubar=no, status=no, scrollbars=no, menubar=no');
	}else if(r=="twitter"){
		window.open(url,'Twitter','resizable=no, location=no, width=740, height=540, menubar=no, status=no, scrollbars=no, menubar=no');
	}
	$.ajax({
		type: "POST",
		url: "./ajax.php",
		data: "action=rs_action&rs="+r
	});
	return false;
}
/**************************************************************************************/	
function dispplus_ville(){
	$(".villeDispnone").show();
	$(".villeDispplus").hide();
	return false;
}
/**************************************************************************************/
function dispplus_src(){
	$(".srcDispnone").show();
	$(".srcDispplus").hide();
	return false;
}
/**************************************************************************************/
function dispplus_en(){
	$(".enDispnone").show();
	$(".enDispplus").hide();
	return false;
}
/**************************************************************************************/
function search_statistics(q){
	$.ajax({
		type: "POST",
		url: "/ajax.php",
		data: "action=search_statistics&q="+q,
		success: function(data){}
	});
	return true;
}
/**************************************************************************************/
function src(id){
	//if(urls && urls[id]){
		var url = "http://www.offres-emploi.ma/postuler.php?id="+id;
		//urls[id];
		var oWin = window.open(url, '_blank');
		if (oWin) {
			if (oWin.focus) oWin.focus();
			/*$.ajax({
				type: "POST",
				url: "/ajax.php",
				data: "action=oe_visits&oe="+id,
				success: function(data){}
			});*/
			return false;
		}
		oWin = null;
		return false;
	/*}else{
		return true;
	}*/
}
/**************************************************************************************/
function ajouter_favoris() {
    var title = window.document.title;
    var url = window.location.href;

	if (window.sidebar) {
		window.sidebar.addPanel(title, url,"");
	}
	else if( window.external ) {
		window.external.AddFavorite( url, title);
	}
	else if(window.opera && window.print) { 
	    var a = document.createElement("a");
        a.rel = "sidebar";
        a.target = "_search";
        a.title = title;
        a.href = url;
        a.click();
	}
}
/**************************************************************************************/
function valider(button,div){
	document.getElementById("nom").style.backgroundColor="#FFFFFF";
	document.getElementById("email").style.backgroundColor="#FFFFFF";
	document.getElementById("ami").style.backgroundColor="#FFFFFF";
	if(ValidateForm('email','','RisEmail','nom','','R','ami','','RisEmail')){
		param = "action=sendToFriends_send&name="+document.getElementById("nom").value+"&email="+document.getElementById("email").value+"&ami="+document.getElementById("ami").value+"&id="+document.getElementById("id").value;
		param = param +"&alerte="+(document.getElementById("alerte").checked == true ? "1":"0")+"&alerte_ami="+(document.getElementById("alerte_ami").checked == true ? "1":"0");
		$('#'+div).prepend($('img.loader').clone());
		$('div#'+div+' img').css("display","inline");
		button.disabled=true;
		$.ajax({
			type: "POST",
			url: "./ajax.php",
			data: param,
			success: function(data){
				button.disabled=false;
				$('#'+div).html(data);
			}
		});
	}else{
		return false;
	}
} 
/**************************************************************************************/
function findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
/**************************************************************************************/
function alert_errors(msg){
	document.getElementById('errors').innerHTML=msg;
}
function nom_champ(champ){
	document.getElementById(champ).style.backgroundColor="#FFF0F0";
	switch (champ){
		case 'nom' : return 'Votre nom'  ; break;
		case 'ami'  : return 'Adresse e-mail de votre ami(e)'; break;
		case 'email': return 'Votre adresse email' ; break;	
	}
}
function ValidateForm() { //v4.0
   var i,p,q,nm,test,num,min,max,errors='',args=ValidateForm.arguments;
   for (i=0; i<(args.length-2); i+=3) { 
  		test=args[i+2]; 
		val=findObj(args[i]);
  	    if (val) {
			nm=val.name; 		
			if ((val=val.value)!="") {			
				if (test.indexOf('isEmail')!=-1) { 
					p=val.indexOf('@');
        			if(p<1 || p==(val.length-1)) errors+='- '+nom_champ(nm)+' n\'est pas valide.<br>';
			    } else if (test!='R') { 
					num = parseFloat(val);
       				if (isNaN(val)) errors+='- '+nom_champ(nm)+' doit contenir un nombre.<br>';
        			if (test.indexOf('inRange') != -1) { 
						p=test.indexOf(':');
          				min=test.substring(8,p); 
						max=test.substring(p+1);
          				if(num<min || max<num) errors+='- '+nom_champ(nm)+' doit contenir un nombre entre '+min+' et '+max+'.<br>';
    				}
				} 
			} else if (test.charAt(0) == 'R') errors += '- '+nom_champ(nm)+' est obligatoire.<br>'; 
		}
    }
  if (errors) {
    alert_errors('Veuillez renseigner ou corriger les champs en rouge ci-dessous:<br>'+errors); return false;
  } else {
    return true;
  }
}
/**************************************************************************************/
function CheckDate(d){
      var j=d.substring(0,d.indexOf("/"));
      var m=d.substring(d.indexOf("/")+1,d.lastIndexOf("/"));
      var a=d.substring(d.lastIndexOf("/")+1);
      var separateur="/";
      if(d.indexOf("/")==-1&&d!=""){
         alert("Les séparateurs de la date doivent être des "+separateur);
		 return false;}
      if(d==""||(isNaN(j))||(j<1)||(j>31)||(isNaN(m))||(m<1)||(m>12)||isNaN(a)||(a<0)||(a>3000)){
         alert("La date n'est pas valide,(format JJ/MM/AAAA).");
		 return false;
	  }
      return true;
}
