﻿/// <reference path="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.js"/>

// jQuery extend
(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() + 20; //Add each ul's width together
        });
        rowWidth += 5;
    };
})(jQuery);

// 
(function ($) {

    var selectedSideMenu;

    //On Hover Over
    function megaHoverOver() {
        $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in

        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, function () { //Fade to 0 opactiy
            $(this).hide();  //after fading, hide it
        });
    }

    function sideMenuExpand(e) {

        if (selectedSideMenu) selectedSideMenu.hide("fast");
        var parent = $(this).parent();
        selectedSideMenu = $(".sub", parent);
        selectedSideMenu.show("");
    }

    function clearFocused() {
        $(this).val("");
    }

    $(document).ready(function () {
        //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: 500, // number = milliseconds delay before onMouseOut
            out: megaHoverOut // function = onMouseOut callback (REQUIRED)
        };

        $("#menu li .sub").css({ 'opacity': '0' }); //Fade sub nav to 0 opacity on default
        $("#menu li").hoverIntent(config); //Trigger Hover intent with custom configurations

        $("#sidemenu>li>a").click(sideMenuExpand);

        $("#q").focus(clearFocused);
        $("#signup").focus(clearFocused);
        $("#subscribe-email").focus(clearFocused);

        $('#slider').nivoSlider({
            effect: "fade",
            slices: 5,
            animSpeed: 1000,
            pauseTime: 4000,
            startSlide: 0,
            prevText: '&laquo;',
            nextText: '&raquo;',
            directionNavHide: false
        });

        $("#sidemenu li.all-products").click(function (e) {
            $("#menu li.all div.sub").css("opacity", "1").css("display", "block");
        });

        $("#float-box").click(function (e) {
            $("#float-box > div.sub").css("display", "block");
        });
        $("#subscribe-close").click(function (e) {
            e.stopImmediatePropagation();
            $("#float-box > div.sub").css("display", "none");
        });
    });
}(jQuery));
