/**
 * @author alex
 */

var MENU_ID = '#mainmenu';
var CONTENT_ID = '#maincontent';
var	MENU_CLOSED_HEIGHT = 25;
var COOKIE_OPTIONS = { path: '/', expires: 10 };
var MENU_STATE = 'menu_state';
var FIRST_TIME = true;

var MainMenu = {
	init: function(MenuState){
		MENU_HEIGHT = jQuery(MENU_ID).height();
		jQuery(CONTENT_ID).css({ "margin-bottom": MENU_CLOSED_HEIGHT });
		if(MenuState == 'closed') {
			jQuery(MENU_ID).css({ 'height': MENU_CLOSED_HEIGHT + 'px'});
		} else {
			jQuery(MENU_ID).css({ 'height': MENU_HEIGHT - ((jQuery.browser.msie && jQuery.browser.version <= 6) ? 1 : 0) + 'px'});
		}
		jQuery.cookie(MENU_STATE, MenuState, COOKIE_OPTIONS);
	},
	animateMainMenu: function(waitTime, finalHeight, MenuState){
		waitTime = waitTime || 0;
		finalHeight = finalHeight || MENU_CLOSED_HEIGHT;
		MenuState = MenuState || 'closed';
		//jQuery('#headline').html('waitTime: ' + waitTime + '<br />finalHeight: ' + finalHeight + '<br />MenuState: ' + jQuery.cookie(MENU_STATE));
		jQuery(MENU_ID).wait(waitTime).animate({ 
			height: finalHeight + 'px'}, 
			700, 
			'swing', 
			function(){
				jQuery.cookie(MENU_STATE, MenuState, COOKIE_OPTIONS);
				FIRST_TIME = false;
				//jQuery('#headline').html('waitTime: ' + waitTime + '<br />finalHeight: ' + finalHeight + '<br />MenuState: ' + jQuery.cookie(MENU_STATE));
			}
		);
		if(waitTime == 0) {
			jQuery(MENU_ID).dequeue();
		}
		if(!FIRST_TIME && jQuery.cookie(MENU_STATE) == 'open') {
			jQuery(MENU_ID).dequeue();
		}
	}
}

jQuery.fn.wait = function(time, type) {
    time = time || 0;
    type = type || 'fx';
    return this.queue(type, function() {
        var self = this;
        setTimeout(function() {
            jQuery(self).dequeue();
        }, time);
    });
};

var Menu = {
	init: function() {
		if(jQuery.cookie(MENU_STATE) == undefined || jQuery.cookie(MENU_STATE) == false) {
			MainMenu.init('open');
			MainMenu.animateMainMenu(5000, MENU_CLOSED_HEIGHT, 'closed');
		} else {
			FIRST_TIME = false;
			MainMenu.init(jQuery.cookie(MENU_STATE));
			if(jQuery.cookie(MENU_STATE) == 'open') {
				MainMenu.animateMainMenu(0, MENU_CLOSED_HEIGHT, 'closed');
			}
		}
		var enter = function(){
			FIRST_TIME = false;
			MainMenu.animateMainMenu(0, MENU_HEIGHT, 'open');
		};
		var leave = function(){
			MainMenu.animateMainMenu(0, MENU_CLOSED_HEIGHT, 'closed');
		};
		jQuery(MENU_ID).mouseenter(enter);
		jQuery(MENU_ID).mouseleave(leave);
	}
}

jQuery(document).ready(function(){
	Menu.init();
	jQuery('.instructor').columnFilters({alternateRowClassNames:['','listrow-odd']});
});

