SubMenu = Class.create();
SubMenu.prototype = {

	menu_tree:null,
	top_menu_div:null,
	bot_menu_div:null,
	top_menu_ul:null,
	bot_menu_ul:null,
	current_page:null,	
	//event_functions
	func_h:null,
	func_c:null,
	func_p:null,	
	duration:.2,	
	myid:'SubMenu',
	browser:null,
	menu_active:false,
	menu_timer:null,
	open:false,
	initialize:function(menu_tree, browser, current_page){
		this.menu_tree     = menu_tree;
		this.current_page  = current_page;
		this.browser       = browser;
		this.top_menu_div  = $('top_nav_div');
		this.bot_menu_div  = $('bot_nav_div');
		this.top_menu_ul   = $('top_nav');
		this.bot_menu_ul   = $('bot_nav');
		this.setupStatesForTopMenu();

	},
	
	getBrowser:function(){
		return this.browser;
	},
	
	mobileDevice:function(){
		return (this.browser.isiPhone() == true) ? true : false;
	},
	
	getActivateState:function(){
		return (this.mobileDevice() == true) ? 'onClick' : 'onMouseOver';
	},
	
	bindListeners:function(){
		try{
			this.func_h = function(){window.SubMenu.closeSubMenu()};
			this.func_c = function(){window.SubMenu.closeSubMenu()};
			this.func_p = function(){window.SubMenu.closeSubMenu()};
			Event.observe($('header_margin'), 'mouseover', this.func_h);
			Event.observe($('page_content_bottom'), 'click', this.func_c);
			Event.observe($('container'), 'click', this.func_p);	
		
		}catch(e){console.log(e);}				
	},
	removeListners:function(){
		try{
			Event.stopObserving($('header_margin'), 'mouseover', this.func_h);
			Event.stopObserving($('page_content_bottom'), 'click', this.func_c);
			Event.stopObserving($('container'), 'click', this.func_p);			
		}catch(e){console.log(e);}		
	},
	
	activateMenu:function(){
		if(this.menu_active){
			//alert('canceling');
			return;
		}
/*
		var dims = Element.cumulativeOffset(this.bot_menu_div);
		//alert(dims.top);

		if(parseInt(dims.top) != 0 && parseInt(dims.top) != 111 && parseInt(dims.top) != 109){
			return;
		}else{
			//alert(dims.top);
		}
*/
		this.menu_active = true;
		this.open		 = true;
		this.menu_timer  = setTimeout(function(){this.menu_active = false;}.bind(this), 20);
//		alert('hi');
		this.bindListeners();
	//	Element.show(this.bot_menu_div);
//		new Effect.SlideDown(this.bot_menu_div, {duration:this.duration,fps:40});		
	},
	
	openSubMenu:function(menu_item){
		if(this.getMenuElements(menu_item) == false){
			return;
		}
		
		if(this.bot_menu_div){			
			try{	
				this.activateMenu();					
			}catch(e){console.log(e);}
		}else{
			alert(this.bot_menu_div+' is not a div');
		}
		
		//re-make sub-menu for currently moused over item
		this.buildMenu(menu_item);
		
	},
	
	createUL:function(item_key){
		UL 	  = document.createElement('UL');
		UL.id = item_key+'_UL';	
		UL.style.paddingTop = '5px';
		return $(UL);	
	},
	createLi:function(item, first){
		var LI    = document.createElement('LI');
		var link  = document.createElement('A');
		var bullet = document.createElement('SPAN');
		if(!first){
			bullet.innerHTML = window.sub_menu_sep_space+window.sub_menu_sep+window.sub_menu_sep_space;			
		}
		link.href = item.link;
		if(!item.link){
			return false;
		}
		link.appendChild(document.createTextNode(item.name));
		LI.appendChild(bullet);
		LI.appendChild(link);
		Element.addClassName(LI, 'menu_item');
		return $(LI);
	},
	getMenuElements:function(key){
		return (this.menu_tree[key].menu_elements.length > 0) ? this.menu_tree[key].menu_elements : false;
	},
	buildMenu:function(item_key){
		try{
			item   = this.menu_tree[item_key];
		}catch(e){	}			
			UL = this.createUL(item_key);
			menu_items = this.getMenuElements(item_key);
			var first = true;
			for(i in menu_items){
				LI = this.createLi(menu_items[i], first);
				if(LI != false){
					UL.appendChild(LI);				
					first = false;
				}
			}
			DIV = $(document.createElement('DIV'));
			DIV.appendChild(UL);
			$('bot_nav_div').innerHTML = '';
			$('bot_nav_div').appendChild(DIV);			

	},

	closeSubMenu:function(){
//		alert('in close');
		this.bot_menu_div.innerHTML = '';
//		Element.hide(this.bot_menu_div);		
		this.removeListners();
		this.open = false;
		this.menu_active = false;

		return true;

		if(this.menu_active == true || this.open == false){
			return;
		}
		try{
			var dims = Element.cumulativeOffset(this.bot_menu_div);
			if(dims.top < 105 || dims.top > 111){
				return;
			}
			this.open = false;
			this.removeListners();
			new Effect.SlideUp(this.bot_menu_div, {duration:this.duration,fps:40});						
		}catch(e){console.log(e);}
		this.menu_timer  = setTimeout(function(){this.menu_active = false;}.bind(this), 20);							
	},
	
	setupStatesForTopMenu:function(){
		switch(this.getActivateState()){
			case 'onClick':
				this.top_menu_ul.onClick = this.openSubMenu().bind(this);
			break;
			default:
				try{

					for(i in this.menu_tree){
						if(this.getMenuElements(i) == false){
							if(!$(i)){
								alert(i);
							}else{
								Event.observe($(i), 'mouseover', function(){this.closeSubMenu()}.bind(this));								
							}
						}
						var mo = function(i){
							return function(){
								window.SubMenu.openSubMenu(i);
							}
						}
						if($(i)){
							Event.observe($(i), 'mouseover', mo(i));
						}else{						
							//alert(i);							
						}	
					}
				}catch(e){console.log(e);}
			break;			
		}		
	}
	
}