/*----------------------------------------------------------------------------//
// Javascript pour le site client  -------------------------------------------//
//----------------------------------------------------------------------------//
// Client : Ville de boisbriand
// Date   : 17 aout 2009
//----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
// Inclusion dynamique des fichiers externes javascripts
/*----------------------------------------------------------------------------*/
function importJavascript(src)
{
	document.write("<script type=\"text/javascript\" src=\"" + src + "\"></scr" + "ipt>");
}
function importCss(href, media)
{
	document.write('<link rel="stylesheet" type="text/css" media="' + media + '" href="' + href + '" />');
}

/*----------------------------------------------------------------------------*/
// Importations de fichiers
/*----------------------------------------------------------------------------*/
// importJavascript("../includes/js/plugins/jquery.plugin.js");
// importCss("../includes/js/plugins/jquery.plugin.css", "screen");

importJavascript("/includes/js/plugins/abso.caroussel.js");
importJavascript("/includes/js/plugins/jquery.date.js");
importJavascript("/includes/js/plugins/jquery.date.fr.js");
importJavascript("/includes/js/plugins/abso.datepicker.js");
importJavascript("/includes/js/plugins/jquery.pngfix.js");

importCss("/includes/js/plugins/abso.datepicker.css", "screen");


/*----------------------------------------------------------------------------*/
// Declarations jQuery
/*----------------------------------------------------------------------------*/
/*Global Vars*/
var onepixel = 6.25;//1px in %
var petitpx = 11;
var normalpx = 12;
var grandpx = 13;


$(document).ready(function() {

	// PNG - Page chapitre
	$("div.CadreListing img").pngfix();

	// Publicité animé à l'accueil
	AbsoCaroussel.init({
		id: 'PubliciteAnim',
		wrap:true, 
		autostart: {
			interval:5000
		}
	});

	///Champ texte - focus
	$("input:text").focus(function() {
		if ($(this).val() == $(this).attr("title")) { $(this).val(""); };
	}).blur(function() {
		if ($(this).val() == "") { $(this).val($(this).attr("title")); };
	});

	/* -- Changement de la grosseur de texte
	-------------------------------------------*/
	$("#FontSize a").each(function() {
		$(this).attr("href", "javascript:void(0);");
		$(this).click(function() {
			//Get size type
			var sizeType = $(this).attr("rel");
			switch (sizeType) {
				case "grand":
					newSize = grandpx;
					break;
				case "petit":
					newSize = petitpx;
					break;
				case "normal":
				default:
					newSize = normalpx;
			}
			var formattedSize = (newSize * onepixel) + "%";
			$("body").css("font-size", formattedSize);
			putcookie("font-size", formattedSize, 30);
			putcookie("font-size-type", sizeType, 30);
			//Ajout de la classe active
			$("#FontSize a").removeClass("On");
			$(this).addClass("On");
		});
	});

	$("a#BtActionCalendrier").click(function() {
		if ($("a#BtActionCalendrier span").text() == "Ouvrir le calendrier") {
			$("a#BtActionCalendrier span").text("Fermer le calendrier");
		}
		else {
			$("a#BtActionCalendrier span").text("Ouvrir le calendrier");
		};
		$("#BlocCalendrier").slideToggle("normal");
	});

	// Plan du site
	if ($("ul#PlanSite")) {

		$("ul#PlanSite li:first").css("border-top", "0px");
		$("ul#PlanSite ul li.PlanSiteSousItem:first-child").each(function() {
			var sm_height = $(this).height();

			if ($(this).next().height() > sm_height)
				sm_height = $(this).next().height();
			else
				$(this).next().height(sm_height);

			if ($(this).next().next().height() > sm_height)
				sm_height = $(this).next().next().height();
			else
				$(this).next().next().height(sm_height);
		});

		$("ul#SiteMap li.SiteMapSousItem:nth-child(3n+1)").css("clear", "both");

	}
	// Corrections aux styles
	
	$("table.Tableau tr td:first-child").css("border-left", "0");
	$("table.Tableau tr:last td").css("border-bottom", "0");
	$("#SearchBox a").wrapInner("<span></span>");
});

function initFontSize(){
	//Set last size
	var cookieSize = getcookie("font-size");
	var cookieSizeType = getcookie("font-size-type");
	//alert(cookieSize);
	//alert(cookieSizeType);
	if(cookieSize) {
		$("body").css("font-size", cookieSize);
		//Ajout de la classe active
		$("#FontSize a").removeClass("On");
		$("#FontSize li."+cookieSizeType+" a").addClass("On");
	}
}
/* -- Fonctions pour la gestion des cookies
   -------------------------------------------*/
function putcookie(name, value, days)
{
  var expire = "";
  if(days != null) {
    expire = new Date((new Date()) + days);
    expire = "; expires=" + expire.toGMTString()+";";
  }
  document.cookie = name + "=" + escape(value) + expire + "path=/";
}
function getcookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1) { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

/* - Slider inline
------------------------------------*/
function initSlideContent(obj) {

    if ($("ul.SliderInline", obj).length > 0) {

		var slideTotal = ($("ul.SliderInline li", obj).length);
        var slideCurrent = 1;
        if (slideTotal > slideCurrent) {
            $("ul.SliderInline li", obj).hide();
            $("ul.SliderInline li:first", obj).show();
            $("a.BackSliderInline", obj).hide();

            $("a.NextSliderInline", obj).click(function() {
                $("a.BackSliderInline", obj).show();
                $("ul.SliderInline li:eq(" + (slideCurrent - 1) + ")", obj).hide();
                slideCurrent++;
                $("ul.SliderInline li:eq(" + (slideCurrent - 1) + ")", obj).show();

                if ((slideCurrent + 1) > slideTotal) {
                    $("a.NextSliderInline", obj).hide();
                }
            });

            $("a.BackSliderInline", obj).click(function() {
                $("a.NextSliderInline", obj).show();
                $("ul.SliderInline li:eq(" + (slideCurrent - 1) + ")", obj).hide();
                slideCurrent--;
                $("ul.SliderInline li:eq(" + (slideCurrent - 1) + ")", obj).show();

                if ((slideCurrent - 1) <= 0) {
                    $("a.BackSliderInline", obj).hide();
                }
            });
        }
		else {
		    $("a.BackSliderInline", obj).hide();
		    $("a.NextSliderInline", obj).hide();
        }
    }
}

/* - Fonction : Validation des champs de formulaire - */

function ValidateForm(groupName, OnlyOneErrorByField, classError) {
	var msg = new Array();
	var ctl = new Array();
	if (classError != '') jQuery('.' + classError).removeClass(classError);
	//Ne pas oublier d'appliquer "Page_ClientValidate()" avant d'appeler cette procédure
	for (var i = 0; i < Page_Validators.length; i++) {
		if (Page_Validators[i].validationGroup == groupName && !Page_Validators[i].isvalid) {
			if (OnlyOneErrorByField) {
				var exist = false;
				for (var y = 0; y < ctl.length; y++) {
					if (ctl[y] == Page_Validators[i].controltovalidate)
						exist = true;
				}

				if (!exist) {
					msg[msg.length] = Page_Validators[i].errormessage;
					if (classError != '') jQuery('#' + Page_Validators[i].controltovalidate).addClass(classError);
				}

			}
			else {
				msg[msg.length] = Page_Validators[i].errormessage;
				if (classError != '') jQuery('#' + Page_Validators[i].controltovalidate).addClass(classError);
			}

			ctl[i] = Page_Validators[i].controltovalidate;
		}
	}
	return msg;

}

function CheckForm(validationGroup) {
    if (typeof (Page_ClientValidate) == 'function') {
        if (!Page_ClientValidate(validationGroup)) {
            var msg = ValidateForm(validationGroup, true, "Erreur");
            return false;
        }
    }
    return true;
}
