// JavaScript Document

$(document).ready(function() {

    // SelecBox
    $('select#Currency').selectmenu({
        style: 'dropdown',
        menuWidth: 150
    });

    $('select#Brands').selectmenu({
        style: 'dropdown',
        menuWidth: 120
    });

    $('select#Budget').selectmenu({
        style: 'dropdown',
        menuWidth: 120
    });

    $('select.Qty').selectmenu({
        style: 'dropdown',
        menuWidth: 120
    });

    $('select.Produit').selectmenu({
        style: 'dropdown',
        menuWidth: 160
    });

    $('#MonCompteSelect').selectmenu({
        style: 'dropdown',
        menuWidth: 120
    });

    //Set custom configurations
    var config = {
        sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
        interval: 100, // number = milliseconds for onMouseOver polling interval
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
        timeout: 50, // number = milliseconds delay before onMouseOut
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)
    };

    /* MEGA Menu BOX */
    //On Hover Over
    function megaHoverOver() {
        $(this).find(".sub").stop().fadeTo('fast', 0.9).show(); //Find sub and fade it in
        (function($) {
            //Function to calculate total width of all ul's
            jQuery.fn.calcSubWidth = function() {
                rowWidth = 0;
                //Calculate row
                $(this).find("ul").each(function() { //for each ul...
                    rowWidth += $(this).width(); //Add each ul's width together
                });
            };
        })(jQuery);

        /* --------- Modification Rose Haylland --------- */
        if ($(this).find(".sub").attr('id') == "vitrinesousmenu") {
            $(this).find(".sub").css({ "width": "810px" });
            /*$(this).find(".sub").css({ "margin-left": "-410px" });*/
        }
        else if ($(this).find(".sub").attr('id') == "large_accessories") {

            var WidthMenu1 = parseInt($("#brands_designers").css('width'));
            var WidthMenu2 = parseInt($("#inside_out").css('width'));
            var WitdhTotal = WidthMenu1 + WidthMenu2 + 'px';

            $(this).find(".sub").css({ "width": "808px" });
            $(this).find(".sub").css({ "margin-left": "-" + WitdhTotal });

        }
        else {

            if ($(this).find(".row").length > 0) { //If row exists...

                var biggestRow = 0;

                $(this).find(".row").each(function() {	//for each row...
                    $(this).calcSubWidth(); //Call function to calculate width of all ul's
                    //Find biggest row
                    if (rowWidth > biggestRow) {
                        biggestRow = rowWidth;
                    }
                });

                $(this).find(".sub").css({ 'width': biggestRow }); //Set width
                $(this).find(".row:last").css({ 'margin': '0' });  //Kill last row's margin

            } else { //If row does not exist...

                $(this).calcSubWidth();  //Call function to calculate width of all ul's
                $(this).find(".sub").css({ 'width': rowWidth }); //Set Width

            }
        }

    }
    //On Hover Out
    function megaHoverOut() {
        $(this).find(".sub").stop().fadeTo('fast', 0.9, function() { //Fade to 0 opactiy
            $(this).hide();  //after fading, hide it
        });
    }

    $("ul#topnav li .sub").css({ 'opacity': '50' }); //Fade sub nav to 0 opacity on default
    $("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations	







    $("#FR_PanierLink").hover(function() {
        $(this).next("em").stop(true, true).animate({ opacity: "show", top: "29" }, "slow");
    }, function() {
        $(this).next("em").animate({ opacity: "hide", top: "220" }, "fast");
    });

    $("#US_PanierLink").hover(function() {
        $(this).next("em").stop(true, true).animate({ opacity: "show", top: "29" }, "slow");
    }, function() {
        $(this).next("em").animate({ opacity: "hide", top: "220" }, "fast");
    });


    /* SLIDER */
    $("#slider").easySlider({
        auto: true,
        continuous: true
    });

    /* SLIDER */
    $("#slider2").easySlider({
        auto: true,
        continuous: true,
        nextId: "slider1next2",
        prevId: "slider1prev2"
    });

    /* HightSlide */
    hs.graphicsDir = '/shopping/content/images/graphics/';
    hs.outlineType = 'rounded-white';
    hs.showCredits = false;
    hs.wrapperClassName = 'draggable-header';
    hs.dimmingDuration = 500;
    hs.dimmingOpacity = 0.75;

    // ToolTip
    $('#ToolTipPics *').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " - ",
        fade: 250
    });
    
//    $('#news').innerfade({ 
// 	  animationtype: Type of animation 'fade' or 'slide' (Default: 'fade'), 
// 	  speed: Fading-/Sliding-Speed in milliseconds or keywords (slow, normal or fast) (Default: 'normal'), 
// 	  timeout: Time between the fades in milliseconds (Default: '2000'), 
// 	  type: Type of slideshow: 'sequence', 'random' or 'random_start' (Default: 'sequence'), 
//  		containerheight: Height of the containing element in any css-height-value (Default: 'auto'),
// 	  runningclass: CSS-Class which the container get’s applied (Default: 'innerfade'),
// 	  children: optional children selector (Default: null)
//   });


    $('#backgroundRotator').innerfade({ speed: 2000, timeout: 4000, type: 'sequence', containerheight: '220px' });

});

function UpdateQuantite(id, qte) {

    $.ajax({
        type: "POST",
        url: "/shopping/panier/UpdateQuantite",
        data: "id_reference=" + id + "&quantite=" + qte,
        success: function(msg) {

            $("#GlobalContainer")[0].innerHTML = msg;

            //UpdatePanierToolTip();

            $('select.Qty').selectmenu({
                style: 'dropdown',
                menuWidth: 120
            });

            $('#backgroundRotator').innerfade({ speed: 2000, timeout: 4000, type: 'sequence', containerheight: '220px' });
        }
    });


}

function UpdatePanier() {
    $('select.Qty').selectmenu({
        style: 'dropdown',
        menuWidth: 120
    });

    $('#backgroundRotator').innerfade({ speed: 2000, timeout: 4000, type: 'sequence', containerheight: '220px' });
}

function CreateCompteSuccess() {
    $('#backgroundRotator').innerfade({ speed: 2000, timeout: 4000, type: 'sequence', containerheight: '220px' });
}

function MonCompteMenuChange(drop)
{
    if ($("#MonCompteSelect")[0].selectedIndex == 1) {
        document.location.href = '/shopping/moncompte/mescommandes'
    }
    else if ($("#MonCompteSelect")[0].selectedIndex == 2) {
    document.location.href = '/shopping/moncompte/mescoordonnees'
    }
    else if ($("#MonCompteSelect")[0].selectedIndex == 3) {
    document.location.href = '/shopping/moncompte/monmotdepasse'
    }
    else if ($("#MonCompteSelect")[0].selectedIndex == 4) {
    }
}

function VerifyStock(id_produit) {

    if ($("#ChoixTaille").length > 0)
        taille = $("#ChoixTaille")[0].value;
    else
        taille = "";

    if ($("#ChoixCouleur").length > 0)
        couleur = $("#ChoixCouleur")[0].value;
    else
        couleur = "";
    
        $.ajax({
            type: "POST",
            url: "/shopping/produit/VerifyStock",
            data: "id_produit=" + id_produit + "&taille=" + taille + "&couleur=" + couleur,
            success: function(msg) {

                if (msg != "OK") {
                    $("#ConfirmationAddToCart")[0].innerHTML = "<div style='margin-top: 60px; margin-left: 7px; margin-right: 7px; text-align: center;'>" + msg + "<br/><input type='button' value='Continuer' style='margin-top: 50px; float: none;' onclick='javascript: modal1.close();' class='Button_Form_Large' /></div>";
                    ShowPopup();
                    flag = true;
                }
            }
        });
    //}
}

var flag = false;
var modal1;

function ShowPopup() {

    flag = false;

    modal1 = $("#ConfirmationAddToCart").modal({
        onOpen: function(dialog) {
            dialog.data.show();
            dialog.overlay.fadeIn('fast', function() {
                dialog.container.fadeIn('fast');
            });
        },
        onClose: function(dialog) {
            if (flag == true) {
                $("#ChoixCouleur").val('');
                $("#ChoixTaille").val('');
            }

            
            this.close();
        }
    });
}

function ShowPopupWithMessage(msg) {

    $("#ConfirmationAddToCart")[0].innerHTML = "<div style='margin-top: 100px; margin-left: 7px; margin-right: 7px; text-align: center;'>" + msg + "<br/></div>";
    ShowPopup();
}
