// JavaScript Document
$(document).ready(function () {
	var browser = jQuery.browser;
	

	function ie7hack1(menu) {
		// IE 7 rollovers fail when animation stops
		if(browser['msie'] && Number(browser['version']) >= 7)
		{
			menu.children('li').stop(true, true);
		}
	}
	
	function ie7hack2(menu) {
		// IE 7 rollovers fail when animation stops
		if(browser['msie'] && Number(browser['version']) >= 7)
		{
			menu.children('li').animate({'opacity': 1}, 9999999);
		}
	}
	

	function showMenu(menu) {
		ie7hack1(menu);
		menu.css({'opacity': 1});
		menu.children('li').css({'opacity': 0});
		menu.slideDown('normal', function () {
			menu.children('li').animate({'opacity': 1}, 'normal', function () {
				ie7hack2(menu);
			});
		});
	}
	
	$("#innerNav li a").click(function () {
		$(this).blur();
		var url = $(this).attr('href');
		$("#innerNav li a").not("a.on").removeClass('current');
		$(this).addClass('current');
		var submenu = $(this).parent().children("ul");
		var faded = false;
		if($(this).is(".why-tnp"))
		{
			showMenu(submenu);
			return false;
		}
		$("#innerNav li ul").not('#innerNav li.on ul').each(function () {
			if($(this).is(":visible")) {
				$(this).animate({'opacity': 0}, "normal", function () {
					$(this).slideUp('normal', function () {
						if(faded === false) {
							showMenu(submenu);
						}
						faded = true;
					});
				});
			}
			
		});
		
		// if we made it this far and still = false no sub menu
		// was visible so we slide down anyways
		if(faded === false) {
			showMenu(submenu);
		}
		
		// don't load links that weren't meant to be loaded
		if(url == '#') {
			return false;
		}
		else {
			window.location = url;	
		}
	});
	
	// get current page
	var current_page = window.location.href;
	
	// make current page "ON"
	$("#innerNav a").each(function () {
		var current_link = $(this);
		if(current_link.attr('href') == current_page) {
			current_link.addClass('current on');
			current_link.parent().parent().addClass('current on');
			current_link.parent().parent().parent().children("a:first").addClass('current on');
			current_link.parent().parent().parent().parent().parent().children("a:first").addClass('current on');
		}
	});
	
	
	// change down arrows to right arrows when needed
	$("a.on").each(function () {
		$(this).addClass('active');
		$(this).closest('li').addClass('current on');
		// does this have a sub menu?
		if($(this).parent().find('ul').length) {
			$(this).addClass("down");
		}
		
		// move arrow back if a child of this is not selected but instead this is selected
		if($(this).attr('href') == current_page) {
			$(this).removeClass("down");
		}
		
	});
	
	// hover effect {
	$("#innerNav li a img").hover(
							function () { 
								$(this).attr('src', $(this).attr('src').replace('.gif', 'B.gif')); 
							},
							function () { 
								$(this).attr('src', $(this).attr('src').replace('B.gif', '.gif')); 
							}
	);
	
	$("#innerNav li a.why_tnp").click(function () {
		$(this).closest('li').find('ul li:first a').click();									
	});
});