// main rollover script / event binds
$(function() {
	
	// get id of parentPage image
	parentPage = $('a.' + parentPage).parent();
	
	// preload images
	$('img').each(function() {
		rosrc = $(this).attr('src');
		ro_over = rosrc.replace('_off','_on');
		$('<img>').attr('src', ro_over);
	});
	
	// remove css hovers and setup javascript menu if javascript is enabled (looks ugly in ie6)
	if (!$.browser.msie || $.browser.version > "6.0") {
		
		$('#mainNav li').removeClass('highlight').find('a').append('<span class="hover" />').each(function () {
			$('> span.hover', this).css('opacity', 0);
		});
		
		// put parent page button in "on" state
		$('#' + parentPage.attr('id') + ' a span.hover').css({ 'opacity': 1 });
	
		// mouseovers for non dropdown menus
	  $('#mainNav li.norm span.hover').hover(function () {
	      // on hover
		    $("div[id*='tnav']").each(function() {
					if ($(this).css('display') != 'none') {
						hideMenu($(this).attr('id').replace('_menu', ''));
					};
				});
				if ($('#' + parentPage.attr('id') + ' a').attr('class') != $(this).parent().attr('class')) {
					$(this).stop().fadeTo(200, 1);	
				}
	  }, function () {
	      // off hover
	      if ($('#' + parentPage.attr('id') + ' a').attr('class') != $(this).parent().attr('class')) {
	      	$(this).stop().fadeTo(200, 0);
      	}
	  });
	                    
		// dropdown menu event binds
		$('#mainNav li.sub span.hover').mousemove(function() {
			showSubMenu($(this).parent().parent().attr('id'));
		});
		
		// keep dropdown open binds
		$('.subMenu').mousemove(function() {
			showSubMenu($(this).attr('id').replace("_menu", ""));
		});
		
		// close dropdown binds
		$('.subMenu').mouseout(function() {
			timeToHide($(this).attr('id').replace("_menu", ""));
		});
		$('#mainNav li.sub span.hover').mouseout(function() {
			timeToHide($(this).parent().parent().attr('id'));
		});
	}
	// side bar binds
	$('#sideBar div ul li a.snav').mousemove(function() {
		showSubMenu($(this).parent().attr('id'));
	});
	$('#sideBar div ul li a.snav').mouseout(function() {
		timeToHide($(this).parent().attr('id'));
	});

});


// dropdown menus

var menuTimeout;

function showSubMenu(menuParentID) {
	var menuParent = $('#' + menuParentID);
	var menu = $('#' + menuParent.attr('id') + '_menu');
	var coords = menuParent.offset();
	// if you can't already see the menu
	if (menu.css('display') == 'none') {
		// if it's on the top
		if (menuParent.attr('id').charAt(0) == 't') {
			// if the button is for the parent page we want it to stay in the "on state"
			if (!$.browser.msie || $.browser.version > "6.0") {
				if (menuParent.attr('id') != parentPage.attr('id')) {
					$('#' + menuParentID + ' span.hover').stop().fadeTo(200, 1);
				}
			}
			// set coords for the menu
			var s_top = coords.top + menuParent.height();
			var s_left;
			if (menuParent.width() > menu.width()) {
				s_left = coords.left + ((menuParent.width() - menu.width()) / 2);
			}
			else if (menuParent.width() < menu.width()) {
				s_left = coords.left - ((menu.width() - menuParent.width()) / 2);
			}
			else {
				s_left = coords.left;
			}
			menu.css({
				'top':			s_top + 6,
				'left':			s_left,
				'opacity':	0
			});
			$(".subMenu").each(function() {
				if ($(this).css('display') != 'none') {
					hideMenu($(this).attr('id').replace('_menu', ''));
				};
			});
			menu.show().animate({
				'top':			s_top,
				'opacity':	1
			});
		}
		// if it's a side menu our job is easier
		else if (menuParent.attr('id').charAt(0) == 's') {
			$('#' + menuParent.attr('id') + ' a').addClass('active').removeClass('hover');
			var s_top = coords.top - 20;
			var s_left = coords.left + menuParent.width() - 5;
			menu.css({
				'top':			s_top,
				'left':			s_left + 6,
				'opacity':	0
			});
			$(".subMenu").each(function() {
				if ($(this).css('display') != 'none') {
					hideMenu($(this).attr('id').replace('_menu', ''));
				};
			});
			menu.show().animate({
				'left':			s_left,
				'opacity':	1
			});
		}
	}
	clearTimeout(menuTimeout);
};

// faster delay for when the mouse leaves
function timeToHide(menuParentID) {
	clearTimeout(menuTimeout);
	menuTimeout = setTimeout('hideMenu("' + menuParentID + '")', 667);
};

function hideMenu(menuParentID) {
	var menuParent = $('#' + menuParentID);
	if (menuParent.attr('id').charAt(0) == 't') {
		if (menuParent.attr('id') != parentPage.attr('id')) {
			if (!$.browser.msie || $.browser.version > "6.0") {
				$('#' + menuParentID + ' span.hover').stop().fadeTo(200, 0);
			}
		}
	}
	else if (menuParent.attr('id').charAt(0) == 's') {
		$('#' + menuParent.attr('id') + ' a').addClass('hover').removeClass('active');
	}
	$('.subMenu').stop().fadeOut(150);
};
