CavMenu = Class.create();
CavMenu.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:'CavMenu',
	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{
//			Event.observe($('header'), 'mouseover',  function(){window.CavMenu.closeSubMenu();}.bind(this));									
//			Event.observe($('content'), 'click',  function(){window.CavMenu.closeSubMenu();}.bind(this));
//			Event.observe($('parent_div'), 'click',  function(){window.CavMenu.closeSubMenu();}.bind(this));
			this.func_h = function(){window.CavMenu.closeSubMenu()};
			this.func_c = function(){window.CavMenu.closeSubMenu()};
			this.func_p = function(){window.CavMenu.closeSubMenu()};
			Event.observe($('header'), 'mouseover', this.func_h);
			Event.observe($('content'), 'mouseover', this.func_c);
			Event.observe($('logo_div_main'), 'mouseover', this.func_h);
//			Event.observe($('parent_div'), 'click', this.func_p);			
		}catch(e){console.log(e);}				
	},
	removeListners:function(){
		try{
			Event.stopObserving($('header'), 'mouseover', this.func_h);
			Event.stopObserving($('content'), 'mouseover', this.func_c);
			Event.stopObserving($('logo_div_main'), 'mouseover', this.func_h);
//			Event.stopObserving($('parent_div'), 'click', this.func_p);			
			//Event.stopObserving($('header'), 'mouseover',  function(){window.CavMenu.closeSubMenu();}.bind(this));									
			//Event.stopObserving($('content'), 'click',  function(){window.CavMenu.closeSubMenu();}.bind(this));
			//Event.stopObserving($('parent_div'), 'click',  function(){window.CavMenu.closeSubMenu();}.bind(this));					
		}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) != 91 && parseInt(dims.top) != 89){
			return;
		}else{
			//alert(dims.top);
		}
		this.menu_active = true;
		this.open		 = true;
		this.menu_timer  = setTimeout(function(){this.menu_active = false;}.bind(this), 800);
		this.bindListeners(); 
		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 = '3px';
		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 = '&bull;';			
		}
		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(){
		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), 500);							
	},
	
	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){
							Event.observe($(i), 'mouseover', function(){this.closeSubMenu()}.bind(this));
						}
						var mo = function(i){
							return function(){
								window.CavMenu.openSubMenu(i);
							}
						}

						Event.observe($(i), 'mouseover', mo(i));
					}
				}catch(e){
					//alert(e.message);
				}
			break;			
		}		
	}
	
}