﻿(function ($) {
    $.fn.createSubNavigation = function () {
        var isDrop = false;
        var isDropSearch = false;
        createJavascriptLinks();
        $('#dd_overlay').click(function () {
            //hideItems();
            hideDropDown();
            hideSearch();
            removeOverlay();
        });
        $('#dd_overlay').css({ display: 'none' });
        $('#dd_border').css({ display: 'none',
            position: 'absolute',
            'z-index': '1000',
            top: '48px',
            'margin-left': '0',
            left: '286px'
        });
        function createJavascriptLinks() {
            $('#mainnav a').each(function () {
                if ($(this).attr('href') == '#dropdown') {
                    $(this).click(function () {
                        toggleDropDown();
                        //showDropDown();
                        return false;
                    });
                }
                else if ($(this).attr('class') == 'bg_mn_search navlink') {
                    $(this).attr('href', '');
                    $(this).click(function () {
                        toggleSearch();
                        //showSearch($(this));
                        return false;
                    });
                }
                $('#mainnav')[0].hidePopups = function () { hideDropDown(); hideSearch(); removeOverlay(); };
            });
        }
        function toggleDropDown() {
            if (isDrop) {
                removeOverlay();
                hideDropDown();
            }
            else {
                $('.hdrContainer').stop();
                $('#dd_overlay').stop();
                $('select').stop();
                $('#ql_dropdown').stop();
                $('#pg_dropdown').stop();
                $('#quicklinks').stop();
                $('#pagination').stop();
                $('#dd_overlay').stop();
                $("body").stop();

                setOverlay('0.65');
                showDropDown();
            }
        }
        function toggleSearch() {
            if (isDropSearch) {
                hideSearch();
                removeOverlay();
            }
            else {
                $('.hdrContainer').stop();
                $('#dd_overlay').stop();
                $('select').stop();
                $('#ql_dropdown').stop();
                $('#pg_dropdown').stop();
                $('#quicklinks').stop();
                $('#pagination').stop();
                $('#dd_overlay').stop();
                $("body").stop();

                setOverlay('0');
                showSearch();
            }
            //(isDropSearch) ? hideSearch() : showSearch();
        }
        function showSearch() {
            isDropSearch = true;
            $('#mn_search').css({ 'background-position': '0 -33px' });
            $('#dd_search').removeClass('hide').addClass('show');
            hideDropDown();
        }
        function hideSearch() {
            isDropSearch = false;
            $('#mn_search').css({ 'background-position': '0 0' });
            $('#dd_search').removeClass('show').addClass('hide');
            //removeOverlay();
        }
        function showDropDown() {
            isDrop = true;
            $('#dd_border').css({ display: 'block' });
            hideSearch();
        }
        function hideDropDown() {
            isDrop = false;
            $('#dd_border').css({ display: 'none' });
            //removeOverlay();
        }
        function setOverlay(transparency) {
            //alert('set');
            var arrPageSizes = ___getPageSize();
            // TRYING TO MAKE THIS FADE IN
            $('#dd_overlay').css({ opacity: transparency, 'background-color': '#000000', width: Math.max(arrPageSizes[0], $("body").outerWidth(), arrPageSizes[2]),
                height: Math.max(arrPageSizes[1], $("body").outerHeight(), arrPageSizes[3])
            }).fadeIn('100'); //animate({opacity: '.65'}, 100);
            $('select').css({ 'visibility': 'hidden' });
            //$('#dd_overlay #overlay_bg').hide().css({display: 'block', width: arrPageSizes[0], height: arrPageSizes[1]}).show();
            //alert(arrPageSizes[0] + ';' + arrPageSizes[1] + ';' + arrPageSizes[2] + ';' + arrPageSizes[3] + ';');
            $('#ql_dropdown').css({ display: 'none' });
            $('#pg_dropdown').css({ display: 'none' });
            $('#quicklinks').css({ 'z-index': '0 !important', 'z-index': '-1' });
            $('#pagination').css({ 'z-index': '0 !important', 'z-index': '-1' });
            // If window was resized, calculate the new overlay dimensions
            $(window).resize(function () {
                var arrPageSizes = ___getPageSize();
                $('#dd_overlay').css({ width: Math.max(arrPageSizes[0], $("body").outerWidth(), arrPageSizes[2]),
                    height: Math.max(arrPageSizes[1], $("body").outerHeight(), arrPageSizes[3])
                });
            });

            //Closes all dropdowns that are open
            for (var i = 0; i < this.AllDropDowns.length; i++) {
                var dd = AllDropDowns[i];
                dd.CloseDropDown();
            }
        }
        function removeOverlay() {
            //alert('remove');
            $('.hdrContainer').css({ 'z-index': '1001' });
            //$('#dd_overlay #overlay_bg').css({display: 'none'});
            //$('#dd_overlay').animate({opacity: '0'}, 100).css({display: 'none'});
            $('#dd_overlay').fadeOut(); //(function() { $('#dd_overlay').css({display: 'none'}); });
            $('select').css({ 'visibility': 'visible' });
            //$('#dd_overlay').css({display: 'none'});
            if ($('#quicklinks').length > 0) {
                $('#quicklinks').css({ 'z-index': '500' });
            }
            if ($('#pagination').length > 0) {
                $('#pagination').css({ 'z-index': '500' });
            }
        }
        /**		 / THIRD FUNCTION		 * getPageSize() by quirksmode.com		 *		 * @return Array Return an array with page width, height and window width, height		 */
        function ___getPageSize() {
            var xScroll, yScroll;
            if (window.innerHeight && window.scrollMaxY) {
                xScroll = window.innerWidth + window.scrollMaxX;
                yScroll = window.innerHeight + window.scrollMaxY;
            } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
                xScroll = document.body.scrollWidth;
                yScroll = document.body.scrollHeight;
            } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
                xScroll = document.body.offsetWidth;
                yScroll = document.body.offsetHeight;
            }
            var windowWidth, windowHeight;
            if (self.innerHeight) {	// all except Explorer
                if (document.documentElement.clientWidth) {
                    windowWidth = document.documentElement.clientWidth;
                } else {
                    windowWidth = self.innerWidth;
                }
                windowHeight = self.innerHeight;
            } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
                windowWidth = document.documentElement.clientWidth;
                windowHeight = document.documentElement.clientHeight;
            } else if (document.body) { // other Explorers
                windowWidth = document.body.clientWidth;
                windowHeight = document.body.clientHeight;
            }
            // for small pages with total height less then height of the viewport
            if (yScroll < windowHeight) {
                pageHeight = windowHeight;
            } else {
                pageHeight = yScroll;
            }
            // for small pages with total width less then width of the viewport
            if (xScroll < windowWidth) {
                pageWidth = xScroll;
            } else {
                pageWidth = windowWidth;
            }
            arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
            return arrayPageSize;
        };
    };
})(jQuery);
