

jQuery(document).ready(
	function(){
		jQuery('div#navigation').each(function(i) {
			new MainMenu(jQuery(this));
		});
	}
);


function MainMenuPoint(li, ul, mainmenu, level1, visible) {
	var instance = this;
	this.li = li;
	this.ul = ul;
	this.level1 = level1;
	this.visible = visible;
	
	this.mainmenu = mainmenu;
	
	this.debug = this.mainmenu.debug;
	
	this.animation;
	
	this.log = function(str) {
		if (this.debug && typeof console != 'undefined') {
			console.log('MainMenuPoint: ' + str);
		}
	}
	this.mover = function(el) {
		if (this.visible) return;
		this.log('> ' + jQuery("a:first img", instance.li).attr('alt'));
		if (this.level1) {
			this.mainmenu.hideCurrent(this);
		}
		this.ul.css({
			'z-index': this.mainmenu.getNextZ()
			});
		this.ul.stop(true, true).fadeIn(200);
	}
	this.mout = function(el) {
		if (this.visible) return;
		this.log('< ' + jQuery("a:first img", instance.li).attr('alt'));
		if (this.level1) {
			this.mainmenu.showCurrent(this);
		}
		this.ul.fadeOut(300);
	}
	
	this.li.hover(
		function() {
			instance.mover(jQuery(this));
		}
		, 
		function() {
			instance.mout(jQuery(this));
		}
	);
}

function MainMenu(container) {
	var instance = this;
	this.debug = true;
	this.container = container;
	this.z = 40;
	
	this.currentMain;
	
	this.getNextZ = function() {
		return this.z++;
	}
	
	this.log = function(str) {
		if (this.debug && typeof console != 'undefined') {
			console.log('MainMenu: ' + str);
		}
	}
	
	this.haveHidden = false;
	this.hideCurrent = function(dontHide) {
		this.log('hideCurrent ?');
		if (!this.currentMain || this.currentMain == dontHide) return;
		this.log('hideCurrent !');
		this.haveHidden = true;
		this.currentMain.ul.fadeOut(200);
		
	}
	this.showCurrent = function() {
		this.log('showCurrent ?');
		if (!this.haveHidden) return;
		this.log('showCurrent !');
		this.currentMain.ul.fadeIn(200);
	}
	
	
	var mainLis = jQuery('> ul > li', this.container);
	
	this.log('mainLis ' + mainLis.length);
	
	var lis = jQuery('li', mainLis);

	
	this.log('lis ' + lis.length);

	mainLis.each(
			function(i) {
				var ul = jQuery('ul:first', this);
				if (i == 0) {
					ul.css({
						'width': '589px'
					});
				}
				var visible = !ul.is(':hidden');
				var m = new MainMenuPoint(jQuery(this), ul, instance, true, visible);
				if (visible) {
					instance.currentMain = m;
				}
			}
		);

	/*lis.each(
		function(i) {
			var ul = jQuery('ul:first', this);
			var visible = !ul.is(':hidden');
			new MainMenuPoint(jQuery(this), ul, instance, false, visible);
		}
	);*/
	
	this.z++;
	
	
}

