var previous_id ="-1";
var now_id ="-1";

$("document").ready(function () { 
	
	/* POPUP NEWSLETTER */
 	 $(".popupnewsletter").fancybox();

	/* DEPARTEMENT */
	$("#id_departement").change(function(){
		$("#myForm").submit();
	});
	
	/* NEWSLETTER */
	$("#doNewsletter").click(function(){
	  var newsletter = $("INPUT[name=newsletter]").attr("value");
	  if (trim(newsletter)=="") return;
	  $.get("ajax.newsletter.php?newsletter="+newsletter, function(data) {
		$("#newsletterZone").fadeOut("fast",function(){
			$("#newsletter_ok").fadeIn("fast");
		});
	  });
	});

	/* RECHERCHE */
	$("#doRecherche").click(function(){
		$("#myForm").attr("action","recherche.php");
		$("#myForm").submit();
	});
	
	/* SONDAGE */
	$("#valideSondage").click(function(){
	  var id_sondageexpress = $("INPUT[name=id_sondageexpress]").attr("value");
	  var id_choix = $("INPUT[name=id_choix]:checked").attr("value");
	  if (id_choix) $("#bloc_sondage").load("ajax.sondage.php?id_sondageexpress="+id_sondageexpress+"&id_choix="+id_choix);		
	});

	/* TOOLTIPS AGENDA */
	$(".hasTooltip[title]").tooltip();
	
	/* BANDEAU INFOS DEFILANT */
	$("#closeInfo").click(function(){
		$("#info").slideUp("fast");	
	});
	
	/* DERNIERS BLOCS/LIENS/MENUS */
	$("#liens A:last").css("border","0");
	$("#autreActuHome .blocBis:last").css("border","0");
	$("#autreActuHome .blocBis:last").css("margin-bottom","0");
	$(".subMenu LI:last-child").css("border","0");
	
	
	/* MENU DEROULANT */
	$("UL.subMenu").mouseenter(function () { 	
		$(this).parent().find("A.menulink").addClass("hover");
	});
	$("UL.subMenu").mouseleave(function () { 	
		$(this).parent().find("A.menulink").removeClass("hover");
		$(this).slideUp("fast");
	});
	$("#sites").mouseover(function () { 
		$("#myMenu LI A.menulink").removeClass("hover");
		$("UL.subMenu").slideUp("fast");
	});	
	$("#myHeader").mouseover(function () { 
		$("#myMenu LI A.menulink").removeClass("hover");
		//$("UL.subMenu").slideUp("fast");
	});	
	$("#myMenu LI A.menulink").mouseenter(function () { 																						 
		$(this).addClass("hover");
		$(this).parent().find("UL").slideDown("fast");  
		now_id = $(this).attr("id");
		if ((previous_id != now_id) && previous_id !="-1") {
			$("#"+previous_id).parent().find("UL").slideUp("fast");
			$("#"+previous_id).removeClass("hover");
		}
		previous_id = now_id;
	});

});

/* VALIDATION DES FORMULAIRES */
function checkForm(champ,genre) {
	valeur = $("INPUT#" + champ).attr("value");
	if (genre=="text") {
		if (valeur=="" || valeur==undefined) {
			alertForm(champ,true);
			return 1;
		} else {
			unAlertForm(champ);
			return 0;
		}
	} else if (genre=="mail") {
		if (checkMail(valeur)==false) {
			alertForm(champ,true);
			return 1;
		} else {
			unAlertForm(champ);
			return 0;
		}
	} else if (genre=="cp") {
		if (checkCP(valeur)==false) {
			alertForm(champ,true);
			return 1;
		} else {
			unAlertForm(champ);
			return 0;
		}
	} else if (genre=="radio") {
		if (checkRadio(champ)==false) {
			alertForm(champ,false);
			return 1;
		} else {
			unAlertForm(champ);
			return 0;
		}
	} else if (genre=="textarea") {
		if (checkArea(champ)==false) {
			alertForm(champ,true);
			return 1;
		} else {
			unAlertForm(champ);
			return 0;
		}
	}
}

function alertForm(champ,isFocus) {
	$("DIV#messageErreur").css("display","block");
	$("#"+champ).addClass("errorBG")
	$("#"+champ).parent("DIV.formField").find("LABEL").removeClass("normal");
	$("#"+champ).parent("DIV.formField").find("LABEL").addClass("errorLabel");
}

function unAlertForm(champ) {
	balise = "INPUT[name="+champ+"]";
	$("#"+champ).removeClass("errorBG")
	$("#"+champ).parent("DIV.formField").find("LABEL").removeClass("errorLabel");
	$("#"+champ).parent("DIV.formField").find("LABEL").addClass("normal");
}

function checkMail(monMail) {
	var reg = new RegExp('^[-_\.0-9a-zA-Z]{1,}@[-_\.0-9a-zA-Z]{1,}[\.][0-9a-zA-Z]{2,}$', 'i');
	if(reg.test(monMail)) {
		return(true);
	} else {
		return(false);
	}
}

function checkCP(monCP) {
	var reg = new RegExp("^[0-9]{5}");
	if(reg.test(monCP)) {
		return(true);
	} else {
		return(false);
	}
}

function checkRadio(champ) {
	nb = parseInt($("INPUT#" + champ + "[type=radio]:checked").length);
	if(nb!=0) {
		return(true);
	} else {
		return(false);
	}
}

function checkArea(champ) {
	texte = $("TEXTAREA#" + champ ).val();
	if(texte!="") {
		return(true);
	} else {
		return(false);
	}
}


/*!
 * webTicker 1.0
 * Examples and documentation at: 
 * http://www.jonmifsud.com
 * 2011 Jonathan Mifsud
 * Version: 1.0.0 (9-APRIL-2011)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * Requires:
 * jQuery v1.2.x or later
 * 
 */


jQuery.fn.webTicker = function(settings) {
		settings = jQuery.extend({
		travelocity: 0.07
		}, settings);		
		return this.each(function(){
				var $strip = jQuery(this);
				$strip.addClass("newsticker")
				var stripWidth = 0;
				var $mask = $strip.wrap("<div class='mask'></div>");
				var $tickercontainer = $strip.parent().wrap("<div class='tickercontainer'></div>");	
				$strip.find("li").each(function(i){
					stripWidth += jQuery(this, i).outerWidth(true); // thanks to Michael Haszprunar
				});
				$strip.width(stripWidth+20);//20 used for ie9 fix					
				function scrollnews(spazio, tempo){
					$strip.animate({left: '-='+ spazio}, tempo, "linear", function(){
						$strip.children().last().after($strip.children().first());
						var first = $strip.children().first();
						var width = first.outerWidth(true);
						var defTiming = width/settings.travelocity;
						var tmpleft = $strip.css("left");
						var outWidth = $strip.children().last().outerWidth(true);
						var left = parseInt(tmpleft.replace('px','')) + outWidth;
						$strip.css("left", left);
						scrollnews(width, defTiming);
					});
				}
				
				var first = $strip.children().first();
				var travel = first.outerWidth(true);
				var timing = travel/settings.travelocity;
				scrollnews(travel, timing);				
				$strip.hover(function(){
					jQuery(this).stop();
				},
				function(){
					var offset = jQuery(this).offset();
					var first = $strip.children().first();
					var width = first.outerWidth(true);
					var residualSpace = offset.left + width;
					var residualTime = residualSpace/settings.travelocity;
					scrollnews(residualSpace, residualTime);
				});			
		});	
};
