$(document).ready(function () { 
	schulstad.megadropdown.init();
}); 

var schulstad = schulstad || {};
schulstad.megadropdown = (function() {
    //	settings
    var contSelector = '#menu ul.m1st';

    // private vars
    var $cont,
		t;

    var init = function() {
        $cont = $(contSelector);

        $cont.find(".m2nd-wrapper").each(function() {
            var h = $(this).height();
            $(this).data('height', h);
        });
        $cont.find(".m2nd-wrapper").css({
            display: 'none',
            height: '0px'
        });


        $(contSelector + " > li > a")
			.mouseover(function() {
			    if ($(this).hasClass('active')) return;
			    closeActive();
			    var ul = $(this)
					.addClass('active')
					.siblings(".m2nd-wrapper");
			    if (ul.length > 0) $(this).addClass('dropdown'); else return

			    if (/msie|MSIE 6/.test(navigator.userAgent)) {
			        ul
					    .css({
					        display: 'block',
					        height: ul.data('height')
					    });
			    }
			    else if (/msie|MSIE 7/.test(navigator.userAgent) || /msie|MSIE 8/.test(navigator.userAgent)) {
			        ul
					    .css({
					        display: 'block'
					    })
					    .stop()
					    .animate({
					        height: ul.data('height')
					    }, {
					        duration: 600
					    });
			    }
			    else {
			        ul
					    .css({
					        opacity: 0,
					        display: 'block'
					    })
					    .stop()
					    .animate({
					        height: ul.data('height'),
					        opacity: 1
					    }, {
					        duration: 600
					    });
			    }




			});

        $cont
			.mouseover(function() {
			    clearTimeout(t);
			})
			.mouseout(function() {
			    t = setTimeout(closeActive, 250);
			});
    };


    var closeActive = function() {
        var $el = $(contSelector + " > li > a.active");
        var ul = $el.removeClass('active').siblings(".m2nd-wrapper");
        if (/msie|MSIE 6/.test(navigator.userAgent)) {
            ul
			.css({
			    display: 'none',
			    height: 0
			})
        }
        else if (/msie|MSIE 7/.test(navigator.userAgent) || /msie|MSIE 8/.test(navigator.userAgent)) {
            ul.css({
                display: 'block'
            }).stop().animate({
                height: 0
            }, {
                duration: 600,
                complete: function() {
                    $(this).hide();
                }
            });
        }
        else {
            ul.css({
                display: 'block'
            }).stop().animate({
                height: 0,
                opacity: 0
            }, {
                duration: 600,
                complete: function() {
                    $(this).hide();
                }
            });
        }
    };


    return {
        init: init
    }
})();
