// JavaScript Document

var MENU_TIMER = null;
var MENU_HIDE_DELAY = 250;
var MENU_OPEN_SPEED = 200;

jQuery( function($) {
	

	// ======================================================
	// See if we need to shift the bg of the left column to below any content
	// if there is something other than just white space - move the background image down below the content
	// =====================================================

	jQuery("<img>").attr("src", "/images/template/menu_button_bg.png");	// force a preload of the drop down menu button background
	
	
	// ======================================================
	// Cancel clicks on buttons with sub menus
	// ======================================================
	$("#navBar > a").click(function() {
		var menuSelector = $(this).attr("rel");
		if (menuSelector && $(menuSelector).length > 0) {
			return false	
		}
	});
	
	// ======================================================
	// show the relevant drop down menu in a pretty slidy way when you hover over a nav button
	// ======================================================
	$("#navBar > a").hover(
		function() {											// mouse-over function
			var menuSelector = $(this).attr("rel");
			if (menuSelector && ($(menuSelector).length > 0)) {	// does this button have a menu
				var menu = $(menuSelector);
				if ($(".dropDownMenu:not(#" + menu.attr("id") + "):visible").length > 0) {	// is there already a menu displayed that isn't this one...
					$(".dropDownMenu:animated").stop(true, true);	// if a menu is currently opening, stop it first...
					hideAllMenus();									// turn off everything, so we can start from the beginning
				}
				
				window.clearTimeout(MENU_TIMER);					// stop the timer
				
				menu.css("left", ($(this).offset().left));			// position the menu under the button
				menu.css("top", ($(this).offset().top) + $(this).height());
	
				$(menuSelector + ":hidden").slideDown(MENU_OPEN_SPEED);	// don't re do the opening animation if this is already visible (e.g. if you hovered from the menu back up tot he button)
				//$(menuSelector + ":hidden").show(MENU_OPEN_SPEED);	
			}
			
			$(this).css("background-position", "0 0");// 'on'		// make sure the nav button is 'on' regardless of if we have have menu
		}, 
		
		function () {											// mouse-out function
			var menuSelector = $(this).attr("rel");
			if (menuSelector && ($(menuSelector).length > 0)) {		// does this button have a menu?
				MENU_TIMER = window.setTimeout("hideAllMenus()", MENU_HIDE_DELAY);	// start the timer to turn it off
			} else {
				if (!$(this).hasClass("navButtonOn")) {	// if this is an 'on' button, it stays on no mater what
					$(this).css("background-position", "0 -40px");	// 'off'		// no menu - just turn off the button
				}
			}
		}

	);
	
	
	// ======================================================
	// Make sure the drop down menus don't vanish while you're pointing at them
	// ======================================================
	$(".dropDownMenu").hover(	
		function(){
			window.clearTimeout(MENU_TIMER);	// cancel the menu hiding
		},
		function(){
			MENU_TIMER = window.setTimeout("hideAllMenus()", MENU_HIDE_DELAY);	// restart the menu hiding
		}
	);
	
	
	// ======================================================
	// Add the roll over styling to the ui buttons
	// ======================================================
	$(".ui-state-default").live("mouseover", function() {$(this).addClass("ui-state-hover")});
	$(".ui-state-default").live("mouseout", function() {$(this).removeClass("ui-state-hover")});
	
	
	// ======================================================
	// Make linkButtons actual links
	// ======================================================
	$(".linkButton").live("click", function() {window.location=$(this).attr("rel")});
	
	
	// ======================================================
	// Remove the bottom line of border from any clientTablewe have
	// and add the odd/even styles...
	// ======================================================
	$("#clientTable tr:odd td").addClass("odd");
	$("#clientTable tr:even td").addClass("even");
	
	$("#clientTable tr:last td").css("border", "none");
	
	
	// ======================================================
	// horrid hack to make the right hand nav button a pixel larger then the others
	// ======================================================
	$("#navBar a:last").css("width", "132px").css("background-image", "url(/images/template/nav_button_wider.png)");
	

	// ======================================================
	// A horid hack for Safari
	// ======================================================
	// to make the text of the 'back to clients' button show in the right place in Safari
	// normally I would fix this with line height instead of top padding and reduced height,
	// but this doesn't work when the text wraps
	if ($.browser.safari) {
		if (navigator.userAgent.toLowerCase().indexOf("chrome") == -1) {	// if we're not chrome
			$("#leftBox a.leftButton").css("padding-top", "6px");	
			$("#leftBox a.leftButton").css("height", "38px");	
			
			$("textarea").css("resize","none");
		}
	}
	
	
});	// end of jQuery ready() function


function hideAllMenus() {
	$(".dropDownMenu:visible").each(function(){
		var id=$(this).attr("id");		// turn off all the nav buttons that have visible menus
		$("#navBar > a[rel=#" + id + "]:not(.navButtonOn)").css("background-position", "0 -40px");
		
	});
	$(".dropDownMenu:visible").hide();								// hide all the menus
}

function selectButton(name) {
	jQuery( function($) {		// wrap it all in JQuery in case we called this function before the page is ready...
		var button = $("#navBar > a[name=" + name + "Button]");
		button.addClass("navButtonOn");// 'on'		// find a button based on the name of the link and turn it on.
		
		// if this button has a sub menu - clone it and bung at the left hand side.
		
		var menuSelector = button.attr("rel");
		
		if (menuSelector) {	// does this button have a menu
			var menu = $(menuSelector);
			var leftMenu = $(menu.clone());
			
			leftMenu.css("position", "relative");
			leftMenu.attr("id", "leftMenu");
			
			leftMenu.addClass("dropDownMenuLeftClone");
			leftMenu.removeClass("dropDownMenu");
			
			$("#leftContent").append(leftMenu);	// don't show() before append() otherwise Safari doesn't show();
			leftMenu.show();
			
		}
		
	});
}