﻿(function($)
{
	jQuery.fn.dropDown = function(options)
	{
		var subNavigation = $(this),
            dropDowns = subNavigation.find('div.dropdown'),
            topLevelOptions = $(this).children('li.subNav'),
            isIE7OrLess = $.browser.msie && parseInt($.browser.version) < 8,
            isIE8OrLess = $.browser.msie && parseInt($.browser.version) < 9;
		if (!isIE8OrLess)
		{
			setPosition(subNavigation, dropDowns);
		}

		$('li.level1').hoverIntent({
			interval: 10,
			over: function()
			{
				$('.dropdown', this).css('width', '200px');
				$('.ddBox', this).css('display', 'none');
				$('.highlightMi', this).removeClass('highlightMi');
			}
		});

		$('li.ddMenuItem:not(.hasChildren)', subNavigation).hoverIntent({
			interval: 10,
			over: function()
			{
				var parent = $(this).parent().parent();
				parent.css('width', '200px');
				parent.find('.ddBox').css('display', 'none');
			}
		});

		$('li.ddMenuItem.hasChildren', subNavigation).hoverIntent({
			interval: 10,
			over: showThirdLevelCategories,
			out: function()
			{
				var parentDropDown = $(this).parent().parent();
				if (isIE7OrLess)
				{
					parentDropDown.css('min-width', '');
				}
				parentDropDown.css('width', '400px');
			}
		});

		$('li.navCol li.jsNav.hasChildren', subNavigation).hoverIntent({
			interval: 10,
			over: showFourthLevelCategories,
			out: function()
			{
				if (isIE7OrLess)
				{
					$(this).parent().parent().css('min-width', '');
				}
			}
		});

		subNavigation.delegate('.dropdown', 'mouseleave', resetMargin);
		return subNavigation;

		function setPosition(subNavigation, dropDowns)
		{
			var fourthLevelLists = $('li.rightDD ol.jsNav li.navCol2 ol', subNavigation),
                thirdLevelLists = $('li.rightDD ol.jsNav', subNavigation),
                secondLevelLists = $('li.rightDD', subNavigation),
                elems = [];
			secondLevelLists.each(function()
			{
				var subNavWidth = $(this).outerWidth(),
                    ddRightWidth = dropDowns.outerWidth(),
                    diff = subNavWidth - ddRightWidth + 1;
				dropdown = $('div.dropdown', this);
				dropdown.data('marginLeft', diff);
				elems.push(dropdown);
			});
			for (var i = 0, len = elems.length; i < len; i++)
			{
				var elem = elems[i];
				elem.css('margin-left', elem.data('marginLeft') + 'px');
			}
		}

		function showThirdLevelCategories()
		{
			$('.ddMenuItemDiv').removeClass('highlightMi');
			$('.ddMenuItemDiv', this).addClass('highlightMi');
			var 
				parentDropDown = $(this).closest('.dropdown'),
				categoryIndex = $(this).index(),
                container = $(this.parentNode).siblings(),
                thirdLevelCategoryList = $([1]);
			thirdLevelCategoryList.context = thirdLevelCategoryList[0] = $(container).children('ol.jsNav')[categoryIndex];
			$('.ddBox, .ddBox>ol, .navCol2').hide();
			var dropDownDiv = thirdLevelCategoryList[0].parentNode.parentNode;
			var numCols = thirdLevelCategoryList.children().length;
			if (dropDownDiv.parentNode.className.indexOf('rightDD') !== -1)
			{
				positionRHSDropDowns(dropDownDiv, numCols);
			}
			if (numCols > 0)
			{
				thirdLevelCategoryList.show();
				$(this).parent().siblings().show();
			}
			var dropDown = jQuery(this).parent().parent();
			/*dropDown.css('min-height', dropDown.innerHeight());*/
			parentDropDown.css('width', '400px');

		}

		function showFourthLevelCategories()
		{
			var 
                parentDropDown = $(this).closest('.dropdown'),
                categoryIndex = $(this).index(),
                container = $(this.parentNode.parentNode).siblings(),
                fourthLevelCategoryList = $([1]);

			fourthLevelCategoryList.context = fourthLevelCategoryList[0] = $(container).children('.navCol2 ol')[categoryIndex];
			$('.navCol2, .navCol2>ol').hide();

			var dropDownDiv = fourthLevelCategoryList[0].parentNode.parentNode.parentNode.parentNode;
			var numCols = fourthLevelCategoryList.children().length;
			if (dropDownDiv.parentNode.className.indexOf('rightDD') !== -1)
			{
				positionRHSDropDowns(dropDownDiv, numCols);
			}
			if (numCols > 0)
			{
				fourthLevelCategoryList.show();
				fourthLevelCategoryList.parent().show();
			}
			var dropDown = jQuery(this).parent().parent().parent().parent();
			/*dropDown.css('min-height', dropDown.innerHeight());*/
			parentDropDown.css('width', '600px');
		}

		function positionRHSDropDowns(dropDownDiv, numCols)
		{
			var leftMargin = numCols === 0 ? 0 : numCols === 1 ? isIE7OrLess ? -216 : -206 : numCols === 2 ? isIE7OrLess ? -391 : -381 : isIE7OrLess ? -566 : -556;
			var $dropDownDiv = $(dropDownDiv);
			leftMargin += $dropDownDiv.data('marginLeft') || 0;
			$dropDownDiv.css('margin-left', leftMargin + 'px');
			if (isIE7OrLess) $dropDownDiv.css('min-width', Math.abs(leftMargin) + $dropDownDiv.width() + 'px');
		}

		function resetMargin()
		{
			$(this).css('margin-left', ($(this).data('marginLeft') || 0) + 'px');
			if (isIE7OrLess)
			{
				$(this).css('min-width', '');
			}
			$('.ddBox, .ddBox>ol').hide();
			$('.ddMenuItemDiv').removeClass('highlightMi');
		}
	};
} (jQuery));



(function ($) {
    $.fn.hoverIntent = function (f, g) {
        var cfg = {
            sensitivity: 7,
            interval: 100,
            timeout: 0
        };
        cfg = $.extend(cfg, g ? {
            over: f,
            out: g
        } : f);
        var cX, cY, pX, pY;
        var track = function (ev) {
            cX = ev.pageX;
            cY = ev.pageY;
        };
        var compare = function (ev, ob) {
            ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
            if ((Math.abs(pX - cX) + Math.abs(pY - cY)) < cfg.sensitivity) {
                $(ob).unbind("mousemove", track);
                ob.hoverIntent_s = 1;
                return cfg.over.apply(ob, [ev]);
            } else {
                pX = cX;
                pY = cY;
                ob.hoverIntent_t = setTimeout(function () {
                    compare(ev, ob);
                }, cfg.interval);
            }
        };
        var delay = function (ev, ob) {
            ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
            ob.hoverIntent_s = 0;
            // removed, was causing js error
            //return cfg.out.apply(ob, [ev]);
        };
        var handleHover = function (e) {
            var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
            while (p && p != this) {
                try {
                    p = p.parentNode;
                } catch (e) {
                    p = this;
                }
            }
            if (p == this) {
                return false;
            }
            var ev = jQuery.extend({}, e);
            var ob = this;
            if (ob.hoverIntent_t) {
                ob.hoverIntent_t = clearTimeout(ob.hoverIntent_t);
            }
            if (e.type == "mouseover") {
                pX = ev.pageX;
                pY = ev.pageY;
                $(ob).bind("mousemove", track);
                if (ob.hoverIntent_s != 1) {
                    ob.hoverIntent_t = setTimeout(function () {
                        compare(ev, ob);
                    }, cfg.interval);
                }
            } else {
                $(ob).unbind("mousemove", track);
                if (ob.hoverIntent_s == 1) {
                    ob.hoverIntent_t = setTimeout(function () {
                        delay(ev, ob);
                    }, cfg.timeout);
                }
            }
        };
        return this.mouseover(handleHover).mouseout(handleHover);
    };
})(jQuery);


jQuery(document).ready(function(){
    jQuery('ul#subNavigation').dropDown();
});




