/*----------------------------------------------------------------------------//
// Javascript pour le site client  -------------------------------------------//
//----------------------------------------------------------------------------//
// Client : -
// Note   : -
// Date   : -
//----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
// 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/date.js");
importJavascript("/includes/js/plugins/date_fr.js");
importJavascript("/includes/js/plugins/jquery.datePicker.js");
importJavascript("/includes/js/plugins/jquery.bgiframe.js");
importJavascript("/includes/js/plugins/jquery.thickbox.js");
importCss("/includes/js/plugins/jquery.datePicker.css", "screen");
importCss("/includes/js/plugins/thickbox.css", "screen");

/*----------------------------------------------------------------------------*/
// Declarations jQuery
/*----------------------------------------------------------------------------*/
function SetSelected(a) {
    jQuery("#Outils li a").removeClass("On");
    a.addClass("On");
}
function equalHeight(group) {
    tallest = 0;
    group.each(function() {
        thisHeight = jQuery(this).height();
        if (thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}

jQuery(document).ready(function() {

    if(jQuery("#BlocSondage p.ReponseSondage:last")!=null)
        jQuery("#BlocSondage p.ReponseSondage:last").addClass("DernierItem");
    
    jQuery("#Copyright p:first").css("padding-top", "33px");
    jQuery("#NavFixe li:first-child").css("padding-left", "0");
    jQuery("#NavFixe li:last-child").css("border", "0");
    jQuery("#FilAriane li:first-child").css("padding-left", "0");
    jQuery("#FilAriane li:last-child").css("background", "none");
    jQuery("h3 + h2").css("margin-top", "0");
    jQuery("table h2").css("background", "none");
    jQuery("hr").wrap("<div class='hr'></div>");
    if (jQuery("#VerrouRepertoire").length > 0) {
        jQuery("#VerrouRepertoire").bgiframe();
        jQuery("#VerrouRepertoire").css("opacity", 0.5)
    }
    if (jQuery("#VerrouInscription").length > 0) {
        jQuery("#VerrouInscription").bgiframe();
        jQuery("#VerrouInscription").css("opacity", 0.5)
    }
    jQuery(".FormulaireGris table.TableauFormPersonnes tr:last td").css("background", "none");

    // Fonction pour "customiser" le calendrier
    var CalendarCallBack = function($td, thisDate, month, year) {
        // Verifier si cette date correspond à une activite
        if (arrActivity != null) {
            var d = thisDate.getDate();
            var currentMonth = thisDate.getMonth();
            var currentYear = thisDate.getFullYear();
            if (month == currentMonth && currentYear == year) {
                if (typeof (arrActivity[year + "-" + (Number(month) + 1) + "-" + d]) != "undefined") {
                    // Changer le style de la date
                    $td.addClass('ActivityDay');
                    $td.bind('click',
                function() {
                    if (isCalendarPage) {
                        cal.showDay(d, Number(month) + 1, year);
                    }
                    else {
                        var location;
                        document.location.href = arrActivity[year + "-" + (Number(month) + 1) + "-" + d][0];
                    }
                });
                    $td.attr('title', arrActivity[year + "-" + (Number(month) + 1) + "-" + d][1]);
                }
            }
        }
    }

    //Calendrier
    if (jQuery("#Calendrier").length > 0) {
        jQuery("#Calendrier").datePicker({
            inline: true,
            showYearNavigation: false,
            hoverClass: false,
            startDate: '01/08/2008',
            renderCallback: CalendarCallBack,
            month: currentMonth - 1,
            year: currentYear
        });
    }

    // Styleswitcher
    jQuery("a.PlusPetit").click(function() { jQuery("body").css("font-size", "56.4%"); SetSelected(jQuery(this)); putcookie("font-size", "56.4%", 30); return false; });
    jQuery("a.Normal").click(function() { jQuery("body").css("font-size", "62.6%"); SetSelected(jQuery(this)); putcookie("font-size", "62.6%", 30); return false; });
    jQuery("a.PlusGrand").click(function() { jQuery("body").css("font-size", "68.9%"); SetSelected(jQuery(this)); putcookie("font-size", "68.9%", 30); return false; });


    // Plan du site
    if (jQuery("ul#Plan")) {
        jQuery("ul#Plan li:first").css("background", "none");
    }

    //Set last size
    var cookieSize = getcookie("font-size");
    //alert(cookieSize);
    if (cookieSize != null) {
        jQuery("body").css("font-size", cookieSize);
        switch (cookieSize) {
            case "56.4%":
                SetSelected(jQuery("a.PlusPetit"));
                break;
            case "62.6%":
                SetSelected(jQuery("a.Normal"));
                break;
            case "68.9%":
                SetSelected(jQuery("a.PlusGrand"));
                break;
        }
    }

});

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;
}

function redirectToSearchPage() {
    var query = jQuery('#searchQuery').val();
    if (query != null && query != '') {
        window.location = '/resultats-recherche.html?lang=FR-CA&q=' + query;
    }
}

jQuery(document).ready(function() {
    jQuery('#btnRecherche').click(function() {
        redirectToSearchPage();
    });
});

jQuery(document).ready(function() {
    jQuery('textarea[maxlength]').keypress(function(e) {
        var max = parseInt(jQuery(this).attr('maxlength'));
        if (jQuery(this).val().length > max)
            return false;
    });
    jQuery('textarea[maxlength]').keyup(function() {
        var max = parseInt(jQuery(this).attr('maxlength'));
        if (jQuery(this).val().length > max) {
            jQuery(this).val(jQuery(this).val().substr(0, max));
        }
    });
});  
