var menu=function(){
	var t=15,z=800,s=1,a;
	function dd(n){this.n=n; this.h=[]; this.c=[]}
	dd.prototype.init=function(p,c){
		a=c; var w=document.getElementById(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
		for(i;i<l;i++){
			var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
			h.onmouseover=new Function(this.n+'.st('+i+',true)');
			h.onmouseout=new Function(this.n+'.st('+i+')');
		}
	}
	dd.prototype.st=function(x,f){
		var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0];
		//clearInterval(c.t); 
		//c.style.overflow='hidden';
		if(f){
			clearInterval(c.t);
			p.className+=' '+a;
			if(!c.mh){
				c.style.display='block'; 
				
			//	c.style.height=''; 
			//	c.mh=c.offsetHeight; 
			//	c.style.height=0;
				c.style.overflow='visible';
				c.style.zIndex=z;
				z++;
			}
			//if(c.mh==c.offsetHeight){
			//	
			//}
			//else{
				
				//c.t=setInterval(function(){sl(c,1)},t);
			//}
		}else{
			c.t=setInterval(function(){
				p.className=p.className.replace(a,'');
				//c.t=setInterval(function(){sl(c,-1)},t);
				c.style.overflow='hidden';
				c.style.display='none';
			},t);
		}
	}
	function sl(c,f){
		var h=c.offsetHeight;
		if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
			if(f==1){c.style.filter=''; c.style.opacity=1; c.style.overflow='visible'}
			clearInterval(c.t); return
		}
		var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
		c.style.opacity=o; c.style.filter='alpha(opacity='+(o*100)+')';
		c.style.height=h+(d*f)+'px'
	}
	return{dd:dd};
}();







var Nav = function(){
	var runner;
	var root_runner;
	var hide_menu;
	var show_root_menu = '';
	var show_child_menu = '';
	var show_parent_menu = '';
	//Create Events
	var Observables = function() {
		this.addEvents({
			'evt1' : true
		});
	};
	Ext.extend(Observables, Ext.util.Observable);
	
	return{
		//Events
		Evts : new Observables(),
		
		init : function(){
			
			//this.setListeners();
//			
//			this.runner = new Ext.util.DelayedTask();
//			this.root_runner = new Ext.util.DelayedTask();
		}
		
		, setListeners : function(){
			Ext.select('div.nav_item_container', true).each(function(el){el.on('mouseover', function(){Nav.showMenu(el.id, 1)});});
			Ext.select('div.nav_menu', true).each(function(el){el.on('mouseover', function(){Nav.showMenu(el.id, 3)});});
			Ext.select('div.nav_menu_child', true).each(function(el){el.on('mouseover', function(){Nav.showMenu(el.id, 4)});});
			Ext.select('div.nav_menu_item_parent', true).each(function(el){el.on('mouseover', function(){Nav.showMenu(el.id, 2)});});
			
			Ext.select('div.nav_item_container').on('mouseout',
				function(e, el){
					var menu_arr = new Array();
					var menu;
					var tg = e.getTarget();
					var reltg = e.getRelatedTarget();
					menu = tg.parentNode.id.replace('nav_','').replace('menu_','').replace('child_','');
					menu_arr = menu.split('_');
					
					if(reltg.id == 'body_splash_shadow_overlay_top') return;
					//Move over non menu item, close child and parent
					if(reltg.className != 'nav_menu_item' && reltg.className != 'nav_menu_item_parent' && reltg.className != 'nav_menu_item_parent_current'){
						Nav.hideMenu2('menu_'+menu_arr[0]);
						//Nav.hideMenu2(Nav.show_parent_menu);
						return;
					}
				}
			);
			
			Ext.select('div.nav_menu_item').on('mouseout',
				function(e, el){
					var menu_arr = new Array();
					var menu;
					var tg = e.getTarget();
					var reltg = e.getRelatedTarget();
					menu = tg.parentNode.id.replace('nav_','').replace('menu_','').replace('child_','');
					menu_arr = menu.split('_');
					
					//Same Menu, close child
					if(tg.parentNode.id == reltg.parentNode.id) return;
					
					//Move over non menu item, close child and parent
					if(tg.parentNode.id != reltg.parentNode.id && reltg.className != 'nav_menu_item' && reltg.className != 'nav_menu_item_parent' && reltg.className != 'nav_menu_item_parent_current'){
						//Nav.hideMenu2(Nav.show_child_menu);
						//Nav.hideMenu2(Nav.show_parent_menu);
						Nav.hideMenu2(tg.parentNode.id);
						Nav.hideMenu2('menu_'+menu_arr[0]);
						//Ext.get(tg.id).replaceClass('nav_menu_item_parent_current', 'nav_menu_item_parent');
						
						return;
					}
					
					//Hide menu
					Nav.hideMenu2(tg.parentNode.id);
					//Nav.hideMenu2(Nav.show_child_menu);	
				}
			);
			
			Ext.select('div.nav_menu_item_parent').on('mouseout',
				function(e, el){
					var menu_arr = new Array();
					var menu;
					var tg = e.getTarget();
					var reltg = e.getRelatedTarget();
					menu = tg.parentNode.id.replace('nav_','').replace('menu_','').replace('child_','');
					menu_arr = menu.split('_');
					
					//Same Menu or shadow overlay, hide open child menu
					if(tg.parentNode.id == reltg.parentNode.id || reltg.id == 'body_splash_shadow_overlay_top'){
						Nav.hideMenu2('menu_child_'+tg.id);
						Ext.get(tg.id).replaceClass('nav_menu_item_parent_current', 'nav_menu_item_parent');
						return;
					}
					
					//Move over child menu item, ignore
					if(tg.parentNode.id != reltg.parentNode.id && reltg.className == 'nav_menu_item') return;
					
					//Move over non-menu, close child and parent
					if(tg.parentNode.id != reltg.parentNode.id && reltg.className != 'nav_menu_item'){
						//Nav.hideMenu2(Nav.show_child_menu);
						//Nav.hideMenu2(Nav.show_parent_menu);
						Nav.hideMenu2('menu_child_'+tg.id);
						Nav.hideMenu2(tg.parentNode.id);
						Ext.get(tg.id).replaceClass('nav_menu_item_parent_current', 'nav_menu_item_parent');
						return;
					}
				}
			);
			
			
			//Nav Item Listeners
			Ext.get('nav_construction').on('click',function(){Nav.goTo('products/construction solutions.php');});
			Ext.get('nav_surveying').on('click',function(){Nav.goTo('products/survey solutions.php');});
			Ext.get('nav_pm').on('click',function(){Nav.goTo('project management/index.php');});
			Ext.get('nav_support').on('click',function(){Nav.goTo('support/index.php');});
			Ext.get('nav_spi').on('click',function(){Nav.goTo('company/index.php');});
			
			//Menu Item Listeners
			Ext.get('construction_machine_control').on('click',function(){Nav.goTo('products/machine control.php');});
			Ext.get('construction_instruments').on('click',function(){Nav.goTo('products/instruments.php');});
			Ext.get('construction_software').on('click',function(){Nav.goTo('products/software.php');});
			Ext.get('construction_grading_systems').on('click',function(){Nav.goTo('products/grading systems.php');});
			Ext.get('construction_agriculture').on('click',function(){Nav.goTo('products/agriculture.php');});
			Ext.get('construction_supplies').on('click',function(){Nav.goTo('products/supplies.php');});
			
			Ext.get('construction_machine_control_gps_positioning').on('click',function(){Nav.goTo('products/mc gps positioning.php');});
			Ext.get('construction_machine_control_gps_rovers').on('click',function(){Nav.goTo('products/GPS Rovers.php');});
			Ext.get('construction_machine_control_laser_automation').on('click',function(){Nav.goTo('products/Laser Automation.php');});
			Ext.get('construction_machine_control_sonic_automation').on('click',function(){Nav.goTo('products/Sonic Automation.php');});
			
			Ext.get('construction_instruments_gc_lasers').on('click',function(){Nav.goTo('products/GC Lasers.php');});
			Ext.get('construction_instruments_laser_receivers').on('click',function(){Nav.goTo('products/Laser Receivers.php');});
			Ext.get('construction_instruments_handheld_tools').on('click',function(){Nav.goTo('products/Handheld Tools.php');});
			Ext.get('construction_instruments_interior_lasers').on('click',function(){Nav.goTo('products/Interior Lasers.php');});
			Ext.get('construction_instruments_pipe_lasers').on('click',function(){Nav.goTo('products/Pipe Lasers.php');});
			Ext.get('construction_instruments_total_stations').on('click',function(){Nav.goTo('products/ci Total Stations.php');});
			Ext.get('construction_instruments_data_collectors').on('click',function(){Nav.goTo('products/ci Data Collectors.php');});
			Ext.get('construction_instruments_auto_levels').on('click',function(){Nav.goTo('products/Auto Levels.php');});
			Ext.get('construction_instruments_transits').on('click',function(){Nav.goTo('products/Transits.php');});
			Ext.get('construction_instruments_theodolites').on('click',function(){Nav.goTo('products/Theodolites.php');});
			Ext.get('construction_instruments_site_layout').on('click',function(){Nav.goTo('products/Site Layout.php');});
			
			Ext.get('construction_software_site_design').on('click',function(){Nav.goTo('products/Site Design.php');});
			Ext.get('construction_software_estimation').on('click',function(){Nav.goTo('products/Estimation.php');});
			Ext.get('construction_software_special_applications').on('click',function(){Nav.goTo('products/Special Applications.php');});
			
			Ext.get('construction_grading_systems_3_point_hitch').on('click',function(){Nav.goTo('products/3-Point Hitch.php');});
			Ext.get('construction_grading_systems_skid_steer').on('click',function(){Nav.goTo('products/Skid-Steer.php');});
			Ext.get('construction_grading_systems_pull_type').on('click',function(){Nav.goTo('products/Pull-Type.php');});
			Ext.get('construction_grading_systems_components').on('click',function(){Nav.goTo('products/Components.php');});
			
			Ext.get('construction_agriculture_steering').on('click',function(){Nav.goTo('products/Steering and Guidance.php');});
			Ext.get('construction_agriculture_water_management').on('click',function(){Nav.goTo('products/Water Management.php');});
			
			Ext.get('construction_supplies_consumables').on('click',function(){Nav.goTo('products/Consumables.php');});
			Ext.get('construction_supplies_tripods_and_rods').on('click',function(){Nav.goTo('products/Tripods and Rods.php');});
			Ext.get('construction_supplies_measuring_tape').on('click',function(){Nav.goTo('products/Measuring Tapes.php');});
			Ext.get('construction_supplies_field_accessories').on('click',function(){Nav.goTo('products/Field Accessories.php');});
			
			Ext.get('surveying_gps_positioning').on('click',function(){Nav.goTo('products/gps positioning.php');});
			Ext.get('surveying_robotic_positioning').on('click',function(){Nav.goTo('products/surveying_robotic_positioning.php');});
			Ext.get('surveying_total_stations').on('click',function(){Nav.goTo('products/Total Stations.php');});
			Ext.get('surveying_data_collectors').on('click',function(){Nav.goTo('products/Data Collectors.php');});
			Ext.get('surveying_data_laser_scanners').on('click',function(){Nav.goTo('products/Laser Scanners.php');});
			Ext.get('surveying_software').on('click',function(){Nav.goTo('products/survey software.php');});
			Ext.get('surveying_supplies').on('click',function(){Nav.goTo('products/survey supplies.php');});
			
			Ext.get('surveying_software_site_design').on('click',function(){Nav.goTo('products/Survey Site Design.php');});
			Ext.get('surveying_software_estimation').on('click',function(){Nav.goTo('products/Survey Estimation.php');});
			Ext.get('surveying_software_special_applications').on('click',function(){Nav.goTo('products/Survey Special Applications.php');});
			
			Ext.get('surveying_supplies_consumables').on('click',function(){Nav.goTo('products/Survey Consumables.php');});
			Ext.get('surveying_supplies_tripods_and_rods').on('click',function(){Nav.goTo('products/Survey Tripods and Rods.php');});
			Ext.get('surveying_supplies_measuring_tapes').on('click',function(){Nav.goTo('products/Survey Measuring Tapes.php');});
			Ext.get('surveying_supplies_field_accessories').on('click',function(){Nav.goTo('products/Survey Field Accessories.php');});
			
			Ext.get('support_service_and_repair').on('click',function(){Nav.goTo('support/index.php');});
			Ext.get('support_training').on('click',function(){Nav.goTo('support/training.php');});
			Ext.get('support_updates_and_manuals').on('click',function(){Nav.goTo('support/updates.php');});
			
			Ext.get('pm_products').on('click',function(){Nav.goTo('project management/products.php');});
			Ext.get('pm_professional_services').on('click',function(){Nav.goTo('project management/professional services.php');});
			
			
			Ext.get('company_about_spi').on('click',function(){Nav.goTo('company/index.php');});
			Ext.get('company_contact').on('click',function(){Nav.goTo('contact/index.php');});
			Ext.get('company_news_and_events').on('click',function(){Nav.goTo('company/news.php');});
			Ext.get('company_jobs').on('click',function(){Nav.goTo('company/jobs.php');});			
		}
		
		, showMenu : function(menu_full, level){
			var root = '';
			var parent_menu = '';
			var menu_arr = new Array();
			var menu;
			
			//alert(menu);
			menu = menu_full.replace('nav_','').replace('menu_','').replace('child_','');
			menu_arr = menu.split('_');
			//if(menu_arr.length() > 0){
			root_menu = 'nav_'+menu;
			
			switch(level){
				case 1:
					child_menu = 'menu_'+menu;
					parent_menu = root_menu;
					//if(!Ext.get(parent_menu).isDisplayed()){
//						!Ext.isIE ? Ext.get(parent_menu).enableDisplayMode('block').setLeftTop((Ext.get(root_menu).getLeft() + (Ext.get(root_menu).getWidth() * (level-1))), (Ext.get(root_menu).getTop())).show() : Ext.get(parent_menu).enableDisplayMode('block').setLeftTop((Ext.get(root_menu).getLeft() + (Ext.get(root_menu).getWidth() * (level-1)) - 2), (Ext.get(root_menu).getTop() - 2)).show();
//					}
					if(!Ext.get(child_menu).isDisplayed()){
						!Ext.isIE ? Ext.get(child_menu).enableDisplayMode('block').setLeftTop((Ext.get(parent_menu).getLeft() + (Ext.get(parent_menu).getWidth() * (level-1)-20)), (Ext.get(parent_menu).getTop() + 42)).show() : Ext.get(child_menu).enableDisplayMode('block').setLeftTop((Ext.get(parent_menu).getLeft() + (Ext.get(parent_menu).getWidth() * (level-1)) - 22), (Ext.get(parent_menu).getTop() + 40)).show();
					}
					
					//this.show_root_menu = '';
					this.show_parent_menu = child_menu;
					this.show_child_menu = '';
					break;
					
				//case 1:
//					parent_menu = root;
//					break;
					
				case 2:
					child_menu = 'menu_child_'+menu;
					parent_menu = 'menu_'+menu_arr[0];
					//if(!Ext.get(parent_menu).isDisplayed()){
//						!Ext.isIE ? Ext.get(parent_menu).enableDisplayMode('block').setLeftTop((Ext.get(root_menu).getLeft() + (Ext.get(root_menu).getWidth() * (level-1))), (Ext.get(root_menu).getTop())).show() : Ext.get(parent_menu).enableDisplayMode('block').setLeftTop((Ext.get(root_menu).getLeft() + (Ext.get(root_menu).getWidth() * (level-1)) - 2), (Ext.get(root_menu).getTop() - 2)).show();
//					}
					if(!Ext.get(child_menu).isDisplayed()){
						var sh = new Ext.Layer({
							'shadow': true
						});
						!Ext.isIE ? Ext.get(child_menu).enableDisplayMode('block').setLeftTop((Ext.get(parent_menu).getLeft() + (Ext.get(parent_menu).getWidth() * (level-1))), (Ext.get(menu_full).getTop()-1)).show() : Ext.get(child_menu).enableDisplayMode('block').setLeftTop((Ext.get(parent_menu).getLeft() + (Ext.get(parent_menu).getWidth() * (level-1)) - 2), (Ext.get(menu_full).getTop() - 3)).show();
						//!Ext.isIE ? sh.enableDisplayMode('block').setLeftTop((Ext.get(parent_menu).getLeft() + (Ext.get(parent_menu).getWidth() * (level-1))), (Ext.get(menu_full).getTop())).show() : sh.enableDisplayMode('block').setLeftTop((Ext.get(parent_menu).getLeft() + (Ext.get(parent_menu).getWidth() * (level-1)) - 2), (Ext.get(menu_full).getTop() - 2)).show();
					//	sh.setBounds(Ext.get(child_menu).getLeft(), Ext.get(child_menu).getTop(), Ext.get(child_menu).getWidth(), Ext.get(child_menu).getHeight());
						Ext.get(menu_full).replaceClass('nav_menu_item_parent', 'nav_menu_item_parent_current');	
					}
					
					//this.show_root_menu = root_menu;
					this.show_parent_menu = parent_menu;
					this.show_child_menu = child_menu;
					break;
					
				case 3:
					child_menu = menu_full;
					parent_menu = 'menu_'+menu_arr[0];
					//if(!Ext.get(parent_menu).isDisplayed()){
//						!Ext.isIE ? Ext.get(parent_menu).enableDisplayMode('block').setLeftTop((Ext.get(root_menu).getLeft() + (Ext.get(root_menu).getWidth() * (level-1))), (Ext.get(root_menu).getTop())).show() : Ext.get(parent_menu).enableDisplayMode('block').setLeftTop((Ext.get(root_menu).getLeft() + (Ext.get(root_menu).getWidth() * (level-1)) - 2), (Ext.get(root_menu).getTop() - 2)).show();
//					}
					if(!Ext.get(child_menu).isDisplayed()){
						!Ext.isIE ? Ext.get(child_menu).enableDisplayMode('block').setLeftTop((Ext.get(parent_menu).getLeft() + (Ext.get(parent_menu).getWidth() * (level-1))), (Ext.get(parent_menu).getTop())).show() : Ext.get(child_menu).enableDisplayMode('block').setLeftTop((Ext.get(parent_menu).getLeft() + (Ext.get(parent_menu).getWidth() * (level-1)) - 2), (Ext.get(parent_menu).getTop() - 2)).show();
					}
					
					//this.show_root_menu = root_menu;
					this.show_parent_menu = parent_menu;
					this.show_child_menu = child_menu;
					break;
					
				case 4:
					child_menu = menu_full;
					parent_menu = 'menu_'+menu_arr[0];
					//if(!Ext.get(parent_menu).isDisplayed()){
//						!Ext.isIE ? Ext.get(parent_menu).enableDisplayMode('block').setLeftTop((Ext.get(root_menu).getLeft() + (Ext.get(root_menu).getWidth() * (level-1))), (Ext.get(root_menu).getTop())).show() : Ext.get(parent_menu).enableDisplayMode('block').setLeftTop((Ext.get(root_menu).getLeft() + (Ext.get(root_menu).getWidth() * (level-1)) - 2), (Ext.get(root_menu).getTop() - 2)).show();
//					}
					if(!Ext.get(child_menu).isDisplayed()){
						!Ext.isIE ? Ext.get(child_menu).enableDisplayMode('block').setLeftTop((Ext.get(parent_menu).getLeft() + (Ext.get(parent_menu).getWidth() * (level-1))), (Ext.get(parent_menu).getTop())).show() : Ext.get(child_menu).enableDisplayMode('block').setLeftTop((Ext.get(parent_menu).getLeft() + (Ext.get(parent_menu).getWidth() * (level-1)) - 2), (Ext.get(parent_menu).getTop() - 2)).show();
					}
					
					//this.show_root_menu = root_menu;
					this.show_parent_menu = parent_menu;
					this.show_child_menu = child_menu;
					break;
			}
			
			//if(this.hide_menu == 'menu_'+menu){
			//this.runner.cancel();
			//}
			
			
		}
			
		, hideMenu : function(menu_full, level){
			var root = '';
			var parent_menu = '';
			var menu_arr = new Array();
			var menu;
			var hide_menu;
			
			//alert(menu);
			menu = menu_full.replace('nav_','').replace('menu_','').replace('child_','');
			menu_arr = menu.split('_');
			//if(menu_arr.length() > 0){
			
			switch(level){
				case 1:
					child_menu = 'menu_'+menu;
					parent_menu = root;
					
					this.hide_menu = child_menu;
			
					this.root_runner.delay(100,
						function(){
							if(Ext.get(Nav.show_parent_menu).isDisplayed()){
								Ext.get(Nav.show_parent_menu).enableDisplayMode('block').hide();
							}
						}
						,Nav
					);
					break;
					
				//case 1:
//					parent_menu = root;
//					break;
					
				case 2:
					child_menu = menu_full;
					parent_menu = 'menu_'+menu_arr[0];
					
					this.hide_menu = child_menu;
			
					this.runner.delay(100,
						function(amenu){
							if(Ext.get(Nav.show_child_menu).isDisplayed()){
								Ext.get(Nav.show_child_menu).enableDisplayMode('block').hide();
							}
							
							if(Ext.get(Nav.show_parent_menu).isDisplayed()){
								Ext.get(Nav.show_parent_menu).enableDisplayMode('block').hide();
							}
							
							//if(Ext.get(Nav.show_root_menu).isDisplayed()){
//								Ext.get(Nav.show_root_menu).enableDisplayMode('block').hide();
//							}
						}
						,Nav
					);
					break;
			}
		}
			
		, hideMenu2 : function(menu_full){
			
			//this.root_runner.delay(100,
//				function(){
					if(Ext.get(menu_full)){
						if(Ext.get(menu_full).isDisplayed()){
							Ext.get(menu_full).enableDisplayMode('block').hide();
						}
					}
				//}
//				,Nav
//			);
		}
		
		, goTo : function(loc){
			document.location.href=Page.root_path+loc;
		}
	};
}();


//Fix PNG's for Internet Explorer
//function jsf_fixPNG(){
//	var arVersion = navigator.appVersion.split("MSIE")
//	var version = parseFloat(arVersion[1])
//	
//	if ((version >= 5.5) && (document.body.filters)) 
//	{
//	   for(var i=0; i<document.images.length; i++)
//	   {
//	      var img = document.images[i]
//	      var imgName = img.src.toUpperCase()
//	      if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
//	      {
//	         var imgID = (img.id) ? "id='" + img.id + "' " : ""
//	         var imgClass = (img.className) ? "class='" + img.className + "' " : ""
//	         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
//	         var imgStyle = "display:inline-block;" + img.style.cssText 
//	         if (img.align == "left") imgStyle = "float:left;" + imgStyle
//	         if (img.align == "right") imgStyle = "float:right;" + imgStyle
//	         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
//	         var strNewHTML = "<span " + imgID + imgClass + imgTitle
//	         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
//	         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
//	         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
//	         img.outerHTML = strNewHTML
//	         i = i-1
//	      }
//	   }
//	}
//}




