﻿$(document).ready(function () {

	// When user clicks anywhere, remove all open advanced menus
	// This script closes all toggle boxes on the whole page if you click anywhere on the page
	// But only if the element has the class "toggle" applied
	$("body").click(function () {
		hideAllToggleable();
	});
});

function hideAllToggleable(dontHideThisId) {
	if (typeof dontHideThisId != "undefined") {
		if (dontHideThisId.length > 0)
			$(".toggleable:not(#" + dontHideThisId + ")").slideUp(25);
	} else {
		$(".toggleable").slideUp(25);
	}
}



// Breadcrumb Hover
var breadcrumbSlideSpeed = 150;
var breadcrumbMinHoverTime = 150;

$(function () {
    var hoverConfig = {
        interval: breadcrumbMinHoverTime,
        sensitivity: 7,
        over: function () {
            $(this).addClass("hoverlong");
            $(this).find(".quicklinks").slideDown(breadcrumbSlideSpeed);
        },
        out: function () {
            $(this).find(".quicklinks").slideUp(breadcrumbSlideSpeed);
            var breadcrumb = $(this);
            setTimeout(function () { breadcrumb.removeClass("hoverlong"); },
                breadcrumbSlideSpeed - 60);
        }
    };
    $(".liBreadCrumb").hoverIntent(hoverConfig);
});




// Country chooser Hover
var countrychooserSlideSpeed = 150;
var countrychooserOverlayMinHoverTime = 150;

$(function () {
    var countryChooserHoverConfig = {
        interval: countrychooserOverlayMinHoverTime,
        sensitivity: 7,
        over: function () { $("#countrychooser div.countries").slideDown(countrychooserSlideSpeed); },
        out: function () { $("#countrychooser div.countries").slideUp(countrychooserSlideSpeed); }
    };
    $("#country-slider").hoverIntent(countryChooserHoverConfig);
});




// ProductPage Listing expand / close
function listingLinkToggle(idHref, textUp, textDown) {
    var clcollapse = "collapselink";
    var clexpand = "expandlink";

    if (textUp.length > 0 && textDown.length > 0) {
        clcollapse = "collapselinknoimage";
	    clexpand = "expandlinknoimage";
	}

	$detailedText = $('#' + idHref).parent().find('div.detailedText');
	$detailedText.slideToggle(400,
                        function () {
                        	$('#' + idHref).text($(this).is(':visible') ? textUp : textDown);
                        	$('#' + idHref).removeClass($(this).is(':visible') ? "expandlink" : "collapselink");
                        	$('#' + idHref).removeClass($(this).is(':visible') ? "clexpand" : "collapselinknoimage");
                        	$('#' + idHref).addClass($(this).is(':visible') ? clcollapse : clexpand);
                        }
                    );

	return false;
}



// Main menu
var menuOverlayMinHoverTime = 150;

$(function () {
    var overlayHoverConfig = {
        interval: menuOverlayMinHoverTime,
        sensitivity: 7,
        over: function () { $(this).addClass("mainhoverlong"); },
        out: function () { $(this).removeClass("mainhoverlong"); }
    };
    $("#mainmenu li").hoverIntent(overlayHoverConfig);
    $("#mainmenu li").hover(
        function () { $(this).addClass("mainhover"); },
        function () { $(this).removeClass("mainhover"); }
    );
});



// Main menu overlay
var hiddenProductsSlideSpeed = 300;
var showFeaturedFadeSpeed = 150;

function showFeatured() {
    var featuredId = $(this).attr("data-featured");
    $(this).closest(".menuoverlay").find(".featuredproduct:visible").hide();
    $("#" + featuredId).show();
}

function showDefaultFeatured() {
    $(this).closest(".menuoverlay").find(".featuredproduct:visible").hide();
    $(this).closest(".menuoverlay").find(".featuredproducts .default").show();
}

function showFeaturedFade() {
    var featuredId = $(this).attr("data-featured");
    if (!featuredId)
        showDefaultFeaturedFade.apply($(this));
    else {
        if ($(this).closest(".menuoverlay").find("#" + featuredId).is(":visible"))
            return;        
        if ($(this).closest(".menuoverlay").find(".featuredproduct").is(":visible")) {
            $(this).closest(".menuoverlay").find(".featuredproduct:visible").fadeOut(showFeaturedFadeSpeed, function () {
                $(this).closest(".menuoverlay").find(".featuredproduct").hide();
                $("#" + featuredId).fadeIn(showFeaturedFadeSpeed * 2);
            });
        } else {
            $(this).closest(".menuoverlay").find(".featuredproduct").hide();
            $("#" + featuredId).fadeIn(showFeaturedFadeSpeed * 2);
        }
    }
}

function showDefaultFeaturedFade() {
    if (!$(this).closest(".menuoverlay").find(".featuredproducts .default").is(":visible")) {
        $(this).closest(".menuoverlay").find(".featuredproduct:visible").fadeOut(showFeaturedFadeSpeed, function () {
            $(this).closest(".menuoverlay").find(".featuredproduct").hide();
            $(this).closest(".menuoverlay").find(".featuredproducts .default").fadeIn(showFeaturedFadeSpeed * 2);
        });        
    }        
}

$(function () {
    $(".menuoverlay .expand").click(function (e) {
        e.preventDefault();
        $(this).closest("ul").find("li:hidden").slideDown(hiddenProductsSlideSpeed);
        $(this).addClass("hidden");
    });

    $(".menuoverlay .categories a[data-featured]").hoverIntent(showFeaturedFade, function () { });
    $(".menuoverlay .section").hoverIntent(showDefaultFeaturedFade, function () { });
});
