var KGK = window.KGK || {};

KGK.StartMenu = (function () {

    function prepare() {

        var s = $('#business-navigation ul ul').show().each(function () {
            var $title = $('<h3></h3>').text($(this).closest('li').find('a:eq(0)').text());
            $(this).wrap($('<div class="outer-wrapper"><div class="wrapper"></div></div>'));
            $title.insertBefore($(this));
            $(this).closest('li').addClass('has-children');
        });
    }

    function eventSetup() {

        // Hides all menus on document click
        $(document).click(function () {
            $('.outer-wrapper').fadeOut('fast', function () {
                $(this).closest('li').removeClass('open').find('a:first').removeClass('open');
            });
        });
		var timer = null;
        $('#business-navigation .has-children').each(function () {
            $(this).hover(function () {
        		var $this = $(this).find('a:eq(0)');
                $this.closest('li').addClass('open');
                $this.next('.outer-wrapper')
			    .css({
			        top: -($this.next('.outer-wrapper').outerHeight() / 2) + 20 + 'px' // positions the menu roughly in the middle. take the header into account
			    });
			   	timer = setTimeout(function() {
			    	$this.addClass('open').next('.outer-wrapper').fadeIn('fast', function () {
			        	$('.outer-wrapper').not($(this)).hide().prev('a').removeClass('open').closest('li').removeClass('open');
			    	});
		    	}, 200);
            }, function () {
            	clearTimeout(timer);
                var $this = $(this).find('a:eq(0)');
                $this.removeClass('open').next('.outer-wrapper').fadeOut('fast', function () {
                    $this.closest('li').removeClass('open');
                });
            }).find('> a').click(function (e) {
                e.preventDefault();
            });
        });
    }

    function init() {
        if ($('#business-navigation').length) {
            prepare();
            eventSetup();
        }
    }

    return {
        init: init
    };

})();


$(document).ready(function () {
    KGK.StartMenu.init();

    // Global navigation	
    var t = null;
    var q = null;
    $('#global-navigation > ul > li').hover(function () {
        clearTimeout(t);
        var $this = $(this);

        $this.addClass('active');

        $(this).find('.mega-menu-wrapper').show().css({
            zIndex: 1000
        });

        $(this).closest('#global-navigation').find('.mega-menu-wrapper').not($this.find('.mega-menu-wrapper')).hide().closest('li').removeClass('active');
    }, function () {
        var $this = $(this);
        t = setTimeout(function () {
            $this.removeClass('active').find('.mega-menu-wrapper').hide();
        }, 400);
    }).find('a:eq(0)').click(function (e) { e.preventDefault(); });

    $('#global-navigation').find('.column > ul').each(function () {
        if ($(this).children().length == 1) {
            $(this).addClass('single');
        }
        else {
            $(this).find('li:eq(0)').addClass('col-header').find('a').attr('href', '#');
        }
    });

    $('.products > ul').easyListSplitter({
        colNumber: 3,
        direction: 'vertical'
    });

    // Sticky contact puff
    var q = $('#contact-puff');
    var p = $('#positioner');
    var topOffset = 25;
    var leftOffset = 25;

    if (p.length) {
        $(window).scroll(function () {

            if ($(window).scrollTop() > p.offset().top - topOffset) {
                q.css({
                    position: 'fixed',
                    top: topOffset,
                    left: p.offset().left + leftOffset
                });
            }
            else {
                q.css({
                    position: 'absolute',
                    top: topOffset,
                    left: leftOffset
                });
            }

        });
    }

});

