// variable required for navigation menu
var allow_sub_menu_hide = false;
var width_grow = 0;
var height_grow = 0;

// function to hide the sub-menu if not required
function hide_sub_menu(link_id){
	if(allow_sub_menu_hide==true){
		$("#nav_extender").animate({width:'hide'});
		$("#"+link_id).removeClass('nav_hover');
	}else{
		return false;
	}
}

$(document).ready(function() {
	$('.toggle').each(function(){
		$(this).slideUp();
	});


	// functions for using the navigation menu
	$('.expand').click(function(){
		var id = $(this).attr('id');
		
		$(this).toggleClass('expanded');
		$('#'+id+'_expand').slideToggle('slow');
		
	});
	
	$('#nav_extender').live("mouseover mouseout", function(event){
		if(event.type == "mouseover"){
			allow_sub_menu_hide = false;
		}else{
			
			// get the id of the element that needs it's hover changing
			var link_ids = $('.nav_extend_hold').attr("id");
			var array = link_ids.split('_');
			var link_id = array[0];
			
			allow_sub_menu_hide = true;
			setTimeout("hide_sub_menu('"+link_id+"')", 2000);
		}
	});
	
	$('.nav_extend_item').live('mouseover mouseout', function(event){
		if(event.type == "mouseover"){
			$(this).toggleClass("nav_extend_item_hover");
		}else{
			$(this).toggleClass("nav_extend_item_hover");
		}
	});
	
	$('.nav_extend_item_hover').live('click',function(){
		var elemt = $(this).children('a').attr('href');
		window.location.href = elemt;
	});
	
	$('.nav_item').live('mouseover mouseout', function(event){
		
		// has the menu already been shown? if not, create it and hide it
		if($('#nav_extender').size()!=1){
			$('body').append('<div id="nav_extender"></div>');
			$('#nav_extender').hide();
		}
	
		if(event.type == "mouseover"){
			// hide the extender if currrently being shown, unless it is already showing the right one
			var link_id = '';
			var req_id = $(this).attr("id");
			// variable checking if anything needs to be loaded?
			var run_animate = true;
			$('.nav_extend_hold').each(function(){
				var current_id = $(this).attr("id");
				var array = current_id.split('_');
				link_id = array[0];
				// does the one they are trying to go to, match the one being shown?
				// if not hide the current and move on
				// if so, do nothing.
				if(link_id != req_id){
					$('#nav_extender').hide();
					$('.nav_hover').each(function(){
						$(this).removeClass('nav_hover');
					});
				}else{
					if($(this).is(":visible")){
						run_animate = false;
					}else{
						$('#nav_extender').animate({width: 'show'});
					}
				}
			});
			
			
			
			$(this).addClass('nav_hover');
			
			// does a sub-menu need to be shown?
			if($(this).hasClass('extend')){
				allow_sub_menu_hide=false;
				
				// position the nav extender in the right place
				offset = $(this).offset(); 
				// set the top and left of the box so it's in the right place
				$('#nav_extender').css({top:offset.top})
				
				// get the information to put in the window
				var item_id = $(this).attr("id");
				
				// are they trying to reload the same menu, if not, then let them
				if(run_animate == true){
					// now we have the id, this can be matched to a file name, so load the file content
					var link = $(location).attr('href');
					var folders = link.split('/');
					var load_path = '';
					if(folders[folders.length-2]!="" && folders[folders.length-2]!="Airclean%202010"){
						load_path = '../';
					}
					$('#nav_extender').load(load_path+'web_nav/'+item_id+'.html', function(response, status, xhr) {
						width_grow = $('.nav_extend_hold').attr("width");
						height_grow = $('.nav_extend_hold').attr("height");
						
						// clear any existing things that are happening to this div
						$("#nav_extender").clearQueue();
						$('#nav_extender').width(width_grow);
						
						if($.browser.msie){
							// IF NOT IE, then box height will be correct
						}else{
							$('#nav_extender').height(height_grow);
						}
						$('#nav_extender').animate({width: 'show'});
					});
				}
			}
		}else{
			// the user has now left the menu item, but, are they looking at the sub-menu?
			var link_id = $(this).attr("id");
			allow_sub_menu_hide=true;
			// if it has a sub-menu, wait before closing it, else just close now
			if($(this).hasClass('extend')){
				setTimeout("hide_sub_menu('"+link_id+"')", 2000);
			}else{
				hide_sub_menu(link_id);
			}
		}
	});
	
});
