﻿
function closeBubble() {
    $("#bubble").hide();
}

//function closeEnglishBubble() {
//    $("#english_bubble").hide();
//}

//Pour indexOf dans ie.
if (!Array.indexOf) {
    Array.prototype.indexOf = function (obj) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == obj) {
                return i;
            }
        }
        return -1;
    }
}

function goToCalendar(id) {
    max = $("#calendrierscroller").data("total")
    current = $("#calendrierscroller").data("current");

    if (id >= 1 && id <= max && id != current) {
        nextId = id;

        if (nextId == 1) {
            indexLeft = 1;
        } else {
            indexLeft = nextId - 1;
        }

        nextPos = $('#mois' + indexLeft).position();
        nextPos = nextPos.left;

        $("#mois" + current).removeClass("current");
        $("#mois" + nextId).addClass("current");

        if (id < max && id > 1) {
            $("#calendrierscroller").stop().animate({ 'left': -nextPos }, 1000, null, function () { $("#mois-events" + nextId).fadeIn("100"); });
            $("#mois-events" + current).fadeOut("100");
        } else {
            $("#mois-events" + current).fadeOut("100", function () { $("#mois-events" + nextId).fadeIn("100"); });
        }
        $("#displayevents").hide();
        $("#calendrierscroller").data("current", nextId);
    }
}

function initAdhesion() {
    $('#catalogue_optiontypeadh input[name=grTypeAdhesion]:checked').each(function () {
        var id = $(this).attr('id');
        if (id == 'rbNouveau') {
            $('#catalogue_dureeadhesion_nouv').show();
            $('#catalogue_infosrenouv').hide();
        }
        else {
            $('#catalogue_infosrenouv').show();
            $('#catalogue_dureeadhesion_nouv').hide();
        }
    });

}

$(function () {


    $("#slider").has("img").nivoSlider({
        effect: 'fade',
        animSpeed: 500,
        pauseTime: 7000,
        directionNav: false,
        controlNavThumbs: true,
        controlNavThumbsSearch: '.jpg',
        controlNavThumbsReplace: '_thumb.jpg',
        pauseOnHover: true,
        manualAdvance: false,
        captionOpacity: 0.9
    });

    $(".pubslider").has("img").nivoSlider({
        effect: 'fade',
        animSpeed: 500,
        pauseTime: 7000,
        directionNav: false,
        controlNav: false,
        pauseOnHover: true,
        manualAdvance: false
    });

    // slider video barre laterale
    $(".videothumb").has("img").nivoSlider({
        effect: 'fade',
        animSpeed: 500,
        pauseTime: 7000,
        controlNav: false,
        directionNav: false,
        controlNavThumbs: false,
        pauseOnHover: true,
        manualAdvance: false,
        beforeChange: function () {
            data = $('.videothumb').data("nivo:vars");
            nextSlide = data.currentSlide + 1;
            if (nextSlide == data.totalSlides) { nextSlide = 0; }
            if ($("#videocaption" + nextSlide).length > 0) {
                $("#videocaption" + data.currentSlide).fadeOut(500, function () {
                    $("#videocaption" + nextSlide).fadeIn(500);
                });
            }
        }
    });

    // round corners
    $('.box').append("<span class='rb'></span><span class='lb'></span>");
    $('.box').append("<span class='rt'></span><span class='lt'></span>");

    // page rabais 
    selectedArray = $.cookie("rabais_selected");
    if (selectedArray != null) {
        selectedArray = selectedArray.split(',');
    } else {
        selectedArray = new Array();
    }

    $("#rabais input[type=checkbox]").each(function () {
        $this = $(this);
        chkId = $this.attr("id").replace("chk_rabais", '');
        if (selectedArray.indexOf(chkId) >= 0) {
            $this.attr('checked', 'checked');
        }
    });

    $("#rabais li:nth-child(even)").addClass("even");
    $("#rabais input[type=checkbox]").bind('change', function () {
        $this = $(this);
        chkId = $this.attr("id").replace("chk_rabais", '');
        if ($this.attr('checked') == true) {
            if (selectedArray.indexOf(chkId) == -1) {
                selectedArray.push(chkId);
            }
        } else {
            index = selectedArray.indexOf(chkId);
            selectedArray.splice(index, 1);
            $("div.tous input[type=checkbox]").attr('checked', false);
        }
        $.cookie("rabais_selected", selectedArray);
    });

    // max par page menu deroulant
    $("#maxpage a.current").bind('mouseenter', function (e) {
        $("#maxpage ul").show();
    });
    $("#maxpage ul").bind("mouseleave", function (e) {
        $(this).hide();
    });

    // nav
    $("#subnav li:nth-child(odd)").addClass("odd");
    $("#subnav li:has(ul)").addClass("hassub");

    $(".news li:nth-child(odd)").addClass("odd");

    // page recherche
    $(".searchresults li:nth-child(even)").addClass("even");

    // calendrier ------------------------------------------------------------
    $("#calendrierscroller").data("current", 2);
    $("#calendrierscroller").data("total", $("#calendrierscroller .singlecalendrier").length);


    $(".singlecalendrier table td a").bind("mouseenter", function () {

        if ($("#diplayevents").length == 0) {
            $("body").append("<div id='displayevents'></div>")
        }

        $cell = $(this).parent();
        cellHeight = $cell.outerHeight();
        cellWidth = $cell.outerHeight();
        list = $cell.find("ul").html();
        pos = $(this).parent().offset();
        posTop = pos.top + cellHeight;


        if (pos.left + 250 > 850) {
            posLeft = pos.left - 220;
        } else {
            posLeft = pos.left;
        }


        $("#displayevents").html("<ul>" + list + "</ul>");
        $("#displayevents").show();
        $("#displayevents").css({ 'left': posLeft, 'top': posTop })
    });
    $(".singlecalendrier table td a").bind("mouseleave, mouseout", function () {
        $("#displayevents").hide();
    });

    $(".singlecalendrier tr td:last-child").addClass("last")
    $(".singlecalendrier tr:nth-child(even)").addClass('odd');




    $(".singlecalendrier a").click(function (e) {
        $clicked = $(this).parentsUntil(".singlecalendrier").parent();
        moisIndex = parseInt($clicked.attr("id").replace("mois", ""));
        goToCalendar(moisIndex);
        e.preventDefault();
    });

    $("#next").click(function (e) {
        current = $("#calendrierscroller").data("current");
        goToCalendar(current + 1);
        e.preventDefault();
    });

    $("#previous").click(function (e) {
        current = $("#calendrierscroller").data("current");
        goToCalendar(current - 1);
        e.preventDefault();
    });

    // forms ------------------------------------------------------------------

    $("input[type='radio']").each(function () {
        $this = $(this);
        if ($this.attr('checked')) {
            $this.next(".radioreplace").addClass("checked");
        }
    });
    $("input[type='checkbox']").each(function () {
        $this = $(this);
        if ($this.attr('checked')) {
            $this.next(".checkboxreplace").addClass("checked");
        }
    });

    $("input[type='radio']").change(function () {
        $this = $(this);
        $this.parents(".radio").find(".checked").removeClass('checked');
        if ($this.attr('checked')) {
            $this.next(".radioreplace").addClass("checked");
        }
    });
    $(".radioreplace").click(function () {
        $this = $(this);
        $this.prev("input").attr("checked", 'checked');
        $this.parents(".radio").find(".checked").removeClass('checked');
        //alert($this.parents(".radio").attr("class"));
        $this.addClass("checked");
    });
    $("input[type='checkbox']").change(function () {
        $this = $(this);
        if ($this.attr('checked')) {
            $this.next(".checkboxreplace").addClass("checked");
        } else {
            $this.next(".checkboxreplace").removeClass("checked");
        }
    });
    $(".checkboxreplace").click(function () {
        $this = $(this);
        if ($this.hasClass("checked")) {
            $this.prev("input").attr("checked", '');
            $this.removeClass("checked");
        } else {
            $this.prev("input").attr("checked", 'checked');
            $this.addClass("checked");
        }
    });





    // map ------------------------------------------------------------------

    $("area").mousemove(function (e) {
        $this = $(this);
        $rel = $("#imgmap");
        x = e.pageX - $rel.offset().left;
        y = e.pageY - $rel.offset().top;

        x = Math.min(x, 545);

        $("#phylactere").css({ left: x - 136, top: y - 175 });
    });
    $('area').mouseover(function (e) {
        $("#phylactere").show();
        $this = $(this);
        $("#phylactere li").hide();

        $("#phylactere #region" + $this.attr("alt")).show();
        e.preventDefault();
    });
    $('area').mouseleave(function (e) {
        $("#phylactere").hide();
    });

    // font size ------------------------------------------------------------

    // lire cookie
    fontDisplay = $.cookie('fontDisplay');
    bigSize = 16;
    normalSize = 12;
    if (fontDisplay != 'normal' && fontDisplay != 'big') {
        fontDisplay = 'normal';
        $.cookie('fontDisplay', fontDisplay);
    } else {
        $('#' + fontDisplay + 'size').addClass('active');
        if (fontDisplay == 'big') {
            $('body').css('font-size', bigSize + 'px');
        }
    }

    $("#normalsize").click(function (e) {
        $(this).addClass('active');
        $("#bigsize").removeClass('active');
        fontDisplay = 'normal';
        $.cookie('fontDisplay', fontDisplay);
        $('body').css('font-size', normalSize + 'px');
        e.preventDefault();
    });
    $("#bigsize").click(function (e) {
        $(this).addClass('active');
        $("#normalsize").removeClass('active');
        fontDisplay = 'big';
        $.cookie('fontDisplay', fontDisplay);
        $('body').css('font-size', bigSize + 'px');
        e.preventDefault();
    });


    // search ------------------------------------------------------------
    var bubbleTimer = null;
    var timerShort = 5000;
    var timerLong = 15000;

    $("#btn_search").click(function (e) {
        //closeEnglishBubble();
        $("#bubble").toggle();
        e.preventDefault();
    });
    $("#bubble").bind("mouseleave", function () {
        clearTimeout(bubbleTimer);
        bubbleTimer = setTimeout("closeBubble()", timerShort);
    });
    $("#bubble").bind("mouseenter", function () {
        clearTimeout(bubbleTimer);
    });

    $(".searchbox").bind("keyup", function () {
        clearTimeout(bubbleTimer);
        bubbleTimer = setTimeout("closeBubble()", timerLong);
    });

    $(".searchbox").bind("focus", function () {
        if (this.value == this.title) {
            this.value = '';
            $(this).removeClass('empty');
        }
        clearTimeout(bubbleTimer);
        bubbleTimer = setTimeout("closeBubble()", timerLong);
    });
    $(".searchbox").bind("blur", function () {
        if (this.value == '') {
            this.value = this.title;
            $(this).addClass('empty');
        }
        clearTimeout(bubbleTimer);
        bubbleTimer = setTimeout("closeBubble()", timerLong);
    });

    // ------------------------------------------------------------------

    // English ------------------------------------------------------------
    //    var englishBubbleTimer = null;

    //    $("#btn_english").click(function (e) {
    //        closeBubble();
    //        $("#english_bubble").toggle();
    //        clearTimeout(englishBubbleTimer);
    //        englishBubbleTimer = setTimeout("closeEnglishBubble()", timerShort);
    //        e.preventDefault();
    //    });

    // ------------------------------------------------------------------

    // catalogue ------------------------------------------------------------
    initAdhesion();
    $('#catalogue_optiontypeadh input[name=grTypeAdhesion]').change(function (e) {
        initAdhesion();
    });
    $('#catalogue_optiontypeadh .radioreplace').click(function (e) {
        initAdhesion();
    });
    $('#catalogue_infosrenouv .noMembre input:text').autotab_filter('numeric');
    $('#catalogue_infosrenouv .noClub input:text').autotab_magic();
    $('.form .tel input:text').autotab_magic().autotab_filter('numeric');
    $('.form .ddn input:text').autotab_magic().autotab_filter('numeric');

    // catalogue dans frame
    $('iframe.autoHeight').iframeAutoHeight({ heightOffset: 30 });
    // ------------------------------------------------------------------
});
