// JavaScript Document
$(document).ready(function(){
	
	
	function nav_label(){
			
		$(".nav_label").children("ul").addClass("lev1").children("li").addClass("lev1").children("a").addClass("lev1")
		.parent().children("ul").addClass("lev2").children("li").addClass("lev2").children("a").addClass("lev2");
		
		$(".nav_label ul>li:first-child").addClass("first");
		$(".nav_label ul>li:last-child").addClass("last");
		
		
		$(".nav_label").find("li").hover(
			function(){
				$(this).addClass("selected").children("a").parent("li").parent("li").addClass("selected").end();
				
			},
			function(){
				$(this).removeClass("selected").children("a").parent("li").parent("li").removeClass("selected").end();
			}
		);
		
		var current_page = document.URL.substr(document.URL.lastIndexOf("/") + 1);
		if(current_page.search("index.html") == -1){
			$("#main_nav a").each(function(i){
				if($(this).attr("href").search(section) != -1){
					$(this).parent("li").addClass("active");
				}
			});
			$("#sub_nav a").each(function(i){
				if($(this).attr("href").search(current_page) != -1){
					$(this).parent("li").addClass("active");
				}
			});
		}
		else{
			$("#main_nav a").each(function(i){
				if($(this).attr("href").search(current_page) != -1){
					$(this).parent("li").addClass("active");
				}
			});	
		}
	}
	
	function button_over(){
		$("a.button").hover(function(){
				$(this).children("img").attr("src",$(this).children("img").attr("src").replace(".png","_over.png"));
			},
			function(){
				$(this).children("img").attr("src",$(this).children("img").attr("src").replace("_over.png",".png"));
			}
		);
	}
	
	function move_the_indicator(){
		//first we figure out which nav item is selected and set the indicator to start there
		var offset = parseInt($("#main_nav li.active").children("a").attr("class")) * 164;
		var $the_indicator = $("span#the_indicator img");
		$the_indicator.css("marginLeft", offset);
		
		//expand the indicator if it is on the last one
		if(parseInt($("#main_nav li.active").children("a").attr("class")) == 5){$the_indicator.css("width", "167px");}
		
		//now set up the roll overs on each li
		$("#main_nav ul li").each(function(){
			
			$(this).hover(function(){
								var move_to = parseInt($(this).children("a").attr("class")) * 164;
								if(parseInt($(this).children("a").attr("class")) == 5){$the_indicator.css("width", "167px");}
								else{$the_indicator.css("width", "164px");}
								$the_indicator.stop().animate({ marginLeft : move_to}, "slow");
							},
							function(){
								if(parseInt($("#main_nav li.active").children("a").attr("class")) == 5){$the_indicator.css("width", "167px");}
								else{$the_indicator.css("width", "164px");}
								$the_indicator.stop().animate({marginLeft : offset}, "slow");
							});
		});
	}
	
	
	
	
	//with all our functions defined, we call what we want to run on every page
	nav_label();
	button_over();
	move_the_indicator();
	
});
