﻿// initialise plugins
jQuery(document).ready(function () {

    $('ul.sf-menu').superfish({ autoArrows: false });

    $("#accordion").accordion({
        autoHeight: false,
        collapsible: true,
        active: false,
        icons: { 'header': 'ui-icon-squaresmall-plus', 'headerSelected': 'ui-icon-squaresmall-minus' },
        navigation: true
    });

    // Jquery Roll Over
    $(".simplehover").children().mouseover(function () {
        $(this).attr("src", $(this).attr("src").replace("-off.", "-on."));
    });

    $(".simplehover").children().mouseout(function () {
        $(this).attr("src", $(this).attr("src").replace("-on.", "-off."));
    });

    $("#ProductImage a.show").lightBox();

    setTimeout("patate(1)", 8000);

    $('.addtoCartForm').dialog({
        autoOpen: false,
        draggable: true,
        modal: true,
        hide: 'slide',
        resizable: false,
        minHeight: 250,
        open: function (type, data) {
            $(this).parent().appendTo("form");
        },
        buttons: {
            "Cancel": function () { $(this).dialog("close"); },
            "Annuler": function () { $(this).dialog("close"); },
            "Add to your cart": function () {
                $('.ProductContentPanel').hide();
                $('.LoadingImage').show();
                $(".btnAddToCart").click();
            },
            "Ajouter au panier": function () {
                $('.ProductContentPanel').hide();
                $('.LoadingImage').show();
                $(".btnAddToCart").click();
            }
        }
    });


    $('.addtoWishForm').dialog({
        autoOpen: false,
        draggable: true,
        modal: true,
        hide: 'slide',
        resizable: false,
        minHeight: 250,
        open: function (type, data) {
            $(this).parent().appendTo("form");
        },
        buttons: {
            "Cancel": function () { $(this).dialog("close"); },
            "Add to your wish list": function () {
                $('.ProductContentPanel').hide();
                $('.LoadingImage').show();
                $(".btnAddToWishList").click();
            },
            "Annuler": function () { $(this).dialog("close"); },
            "Ajouter à votre liste": function () {
                $('.ProductContentPanel').hide();
                $('.LoadingImage').show();
                $(".btnAddToWishList").click();
            }
        }
    });

    if ($('.WelcomeMessage').length) {
        //WelcomeMessage Expose
        $('.WelcomeMessage').expose({
            onBeforeLoad: function () { $('.WelcomeMessage').fadeIn("slow"); },
            onBeforeClose: function () { $('.WelcomeMessage').fadeOut("slow"); }
        });

        $('.CloseButton').click(function () {
            $.mask.close();
        });
    }
});

var displayIndex = new Array();

function patate(indexToChange) {    
    var ctrlName = "ctl00_ucRotatingCategory_rptCategories_ctl0" + indexToChange + "_";

    var index = 0;
    do {
        index = Math.floor(Math.random() * myCategoryPicture.length);
    } while (contains(displayIndex, index) == 1);

    displayIndex[indexToChange] = index;

    // No fade Effect
    //$('#' + ctrlName + 'lnkImage').children().attr('src', myCategoryPicture[index]);
    //$('#' + ctrlName + 'lnkText').html(myCategoryName[index]);

    //Fade effect
    $('#' + ctrlName + 'lnkImage').children().fadeTo("slow", 0, function() {
        $('#' + ctrlName + 'lnkImage').children().attr('src', myCategoryPicture[index]);
        $('#' + ctrlName + 'lnkImage').children().fadeTo("fast", 1);
        $('#' + ctrlName + 'lnkText').html(myCategoryName[index]);
        $('#' + ctrlName + 'lnkImage').children().attr('title', myCategoryName[index]);
        $('#' + ctrlName + 'lnkImage').attr('href', myCategoryURL[index]);
        $('#' + ctrlName + 'lnkText').attr('href', myCategoryURL[index]);
    });

    //Change to next sequencial number until 8
    indexToChange = (indexToChange + 1) % 9;
    
    if (indexToChange == 1)
        setTimeout("patate(" + indexToChange + ")", 8000);
    else
        setTimeout("patate("+indexToChange+")", 100);
}

function contains(array, value) {
    for (var i = 0; i < array.length; i++) {
        if (array[i] == value)
            return 1;
    }  
    return 0;
}

function showCartDialog(locale) {
    $('.addtoCartForm').dialog("open");
    
    var dlgLastButton = $('.ui-dialog-buttonpane').find('button:last');
    dlgLastButton.addClass('ui-state-disabled');
    dlgLastButton.attr('disabled', true);

    $('.ProductContentPanel').hide();
    $('.LoadingImage').show();
    
    if (locale == 'fr-CA') {
        $(".addtoCartForm ~ .ui-dialog-buttonpane")
            .children("button:contains('Cancel')")
            .hide();

        $(".addtoCartForm ~ .ui-dialog-buttonpane")
            .children("button:contains('Add to your cart')")
            .hide();

    } else {
        $(".addtoCartForm ~ .ui-dialog-buttonpane")
            .children("button:contains('Annuler')")
            .hide();
        $(".addtoCartForm ~ .ui-dialog-buttonpane")
            .children("button:contains('Ajouter au panier')")
            .hide();
    }    
}

function enableCartDialog() {
    var dlgLastButton = $('.ui-dialog-buttonpane').find('button:last');
    dlgLastButton.removeClass('ui-state-disabled');
    dlgLastButton.attr('disabled', false);

    $('.ProductContentPanel').show();
    $('.LoadingImage').hide();
}

function closeCartDialog() {
    $('.addtoCartForm').dialog("close");
}

function showWishDialog(locale) {
    $('.addtoWishForm').dialog("open");

    var dlgLastButton = $('.ui-dialog-buttonpane').find('button:last');
    dlgLastButton.addClass('ui-state-disabled');
    dlgLastButton.attr('disabled', true);

    $('.ProductContentPanel').hide();
    $('.LoadingImage').show();

    //alert(locale);

    if (locale == 'fr-CA') {
        $(".addtoWishForm ~ .ui-dialog-buttonpane")
            .children("button:contains('Cancel')")
            .hide();

        $(".addtoWishForm ~ .ui-dialog-buttonpane")
            .children("button:contains('Add to your wish list')")
            .hide();

    } else {
        $(".addtoWishForm ~ .ui-dialog-buttonpane")
            .children("button:contains('Annuler')")
            .hide();
        $(".addtoWishForm ~ .ui-dialog-buttonpane")
            .children("button:contains('Ajouter à votre liste')")
            .hide();
    }
}

function enableWishDialog() {
    var dlgLastButton = $('.ui-dialog-buttonpane').find('button:last');
    dlgLastButton.removeClass('ui-state-disabled');
    dlgLastButton.attr('disabled', false);

    $('.ProductContentPanel').show();
    $('.LoadingImage').hide();    
}

function closeWishDialog() {
    $('.addtoWishForm').dialog("close");
}




jQuery(document).ready(function () {


    //Set where we are from
    $(".productlink, .shoppingcartproductname, .kezsearchlink, .AddCartImage, .AddWishImage").click(function () {

        if ($.cookie('returnto') == null) {
            var url = location.href.substring(location.href.indexOf(location.host) + location.host.length + 1);

            if (url.indexOf("#") > 0) { url = url.substring(0, url.indexOf("#")); }

            //set item
            url = url + "#" + $(this).attr("id");

            $.cookie('returnto', encodeURI(url));
        }

    });


});
