
createClass(
	'mainMenu.Menu',
	function(options){
//		console.log('constructor mainMenu.Menu',this);
		var me = this;
		
		var defOptions = {
			timeout	: 2000
		};
		
		this.options = $.extend(defOptions, options);
		
//		console.log('mainMenu.Menu options', this.options);
		
		$(this.options.tabElements).each(function(idx, el){
			me.tabs.push(new mainMenu.MenuTab(me, el));
		})
		$('body').click(function(ev){return me.onClickOutside(this, ev);});
	},
	{
		timer			: null,
		defaultTab		: null,
		options			: null,//tabsContainer, tabElements, timeout, subtabsContainer, subtabElements, defaultTab
		tabs			: [],
		switchToDefault					: function(activeMenuTab){
			var me = this;
			if(this.timer){
				clearTimeout(this.timer);
			}
			this.timer = setTimeout(function(){return me.switchToDefault_timerCallback(activeMenuTab)}, this.options.timeout);			
//			console.log('switching to default in '+this.options.timeout+' ms...');
		},
		switchToDefault_timerCallback	: function(activeMenuTab){
//			console.log('...switched to default');
			this.switchTabTo(this.defaultTab);
		},
		stopSwithToDefault				: function(menuTab, menuElement){
			if(this.timer){
				clearTimeout(this.timer);
//				console.log('...swichitng aborted');
			}			
		},
		switchTabTo						: function(tab){
//			console.log("switching to %o tab", tab);
			if(tab){
				
				if( this.options.subtabsDynamic ){
				
					this.rollUpAll();
					$(this.options.subtabElements).css({'display':'none'});
					
					if(tab.subTab){
						$(tab.subTab.subtabElement).css({'display':''});
					}
				}
	
				///
				for(i in this.tabs){
					var t =this.tabs[i];
					if(t == tab){
						if(t.isDefault() ){
							t.tabElement.addClass( 'hilite active' );
						} else {
							t.tabElement.addClass( 'hilite' )
						}
					}else{
						if(t.isDefault() ){
							//t.tabElement.removeClass('active');
						}else{
							t.tabElement.removeClass('active hilite');
						}
					}
				}
			}else{
				
				if( this.options.subtabsDynamic ){
					$(this.options.subtabElements).css({'display':'none'});
					this.rollUpAll();
					$(this.options.tabElements).removeClass( 'hilite active' );
				}	
			}
			
		}
		,
		onClickOutside					: function(sender, ev){
//			console.log(sender, ev);
			this.rollUpAll();
		},
		rollUpAll						: function(){
			for(i in this.tabs){
				var t = this.tabs[i];
				t.rollUpAll();
			}
		}
	}
);

createClass(
	'mainMenu.MenuTab',
	function(menu, el){
		var me = this;
		this.menu = menu;
		this.tabElement = $(el);
		this.tabElement
			.mouseover(function(ev){return me.tabMouseOver(this, ev);})
			.mouseout(function(ev){return me.tabMouseOut(this, ev);});
//		console.log('constructor mainMenu.MenuTab',this);
		if(cl = this.tabElement.attr('class').match(/jqmm\d+/)){
			cl = '.'+cl[0];
			this.setSubtab($(cl, this.menu.options.subtabsContainer ));
		}
		if(this.menu.options.defaultTab[0] == this.tabElement[0]){
//			console.log('found default tab', this);
			this.menu.defaultTab = this;
		}

	},
	{
		menu			: null,
//		tabsContainer	: null,
		tabElement		: null,
		subTab			: null,
		tabMouseOver		: function(sender, ev){
//			console.log('tabMouseOver',sender, this);
			this.menu.stopSwithToDefault(this, sender)
			this.menu.switchTabTo(this);
			if( this.menu.options.subtabsDynamic ){
				this.tabElement.addClass('active hilite');
			} else {
				this.tabElement.addClass('hilite');
			}
		},
		tabMouseOut			: function(sender, ev){
//			console.log('tabMouseOut', this, sender);
			this.menu.switchToDefault(this, sender);
		},
		swithToDefault		: function(sender){
			this.menu.switchToDefault(this, sender);
		},
		stopSwithToDefault	: function(sender){
			this.menu.stopSwithToDefault(this, sender)			
		},
		
		setSubtab			: function(el){
//			console.log('setSubMenu', el);
//			if(el.length == 1){
			this.subTab = new mainMenu.MenuSubtab(this, el);
//			}
		},
		isDefault			: function(){
			return this == this.menu.defaultTab;
		},
		rollUpAll			: function(){
			if(this.subTab){
				this.subTab.rollUpAll();
			}
		}
	}
)
createClass(
	'mainMenu.MenuSubtab',
	function(tab, el){
		var me = this;
		this.parentTab = tab;
		this.subtabElement = $(el); 
//		console.log('constructor mainMenu.MenuSubtab',this);
		this.subtabElement
			.mouseover(function(ev){return me.onMouseOver(this, ev);})
			.mouseout(function(ev){return me.onMouseOut(this, ev);});
		$('.newsubmenu_item',this.subtabElement).each(function(idx, el){
			me.subElements.push(new mainMenu.MenuSubElement(me, el));
		});
	},
	{
		parentTab		: null,
		subtabElement	: null,
		subElements		: [],
		onMouseOver		: function(sender, ev){
			this.parentTab.stopSwithToDefault();
		},
		onMouseOut		: function(sender, ev){
			this.parentTab.swithToDefault();
		},
		rollUpAll		: function(){
			for(i in this.subElements){
				this.subElements[i].rollUp();
			}
		}	
	}
)

createClass(
	'mainMenu.MenuSubElement',
	function(subtabcontaineer, el){
		var me = this;
		this.containeer = subtabcontaineer;
		this.element = $(el);
		$('.newsubmenu_sub, a[href=#]', this.element).click(function(ev){return me.onSubClick(this, me);})
	},
	{
		onSubClick			: function(sender, subel){
			if(this.rolledDown()){
				this.rollUp();
			}else{
				this.rollDown();
			}
			return false;
		},
		rollDown			: function(){
			this.containeer.rollUpAll();
			$(this.element).addClass('rolled');
		},
		rollUp			: function(){
			$(this.element).removeClass('rolled');
			
		},
		rolledDown		: function(){
			return $(this.element).hasClass('rolled');
		}
	}
	
);
//createClass(
//	'mainMenu.MenuDropdown',
//	function(){},
//	{}
//	
//);
/*$(function(o){
	window.mmenu = new mainMenu.Menu({
		tabsContainer		: $('div.newmenu'),
		tabElements			: $('div.newmenu .jq_mainmenu, div.newmenu .jq_action'),
		defaultTab			: $('.jq_mainmenu.active, .jq_action.active'),
		subtabsContainer	: $('.jq_newsubmenu_hld'),
		subtabElements		: $('.jq_newsubmenu_hld .jq_newsubmenu'),
		timeout				: 0,
		subtabsDynamic		: false,
		'null':null
	});
//	console.log(window.mmenu);
})*/

