// JavaScript Document

///NEW MENU ITEMS - STANDALONE FUNCTIONS//
//global timer for menu
var timer;
//position flynavs
function positionNavs()
{
	$("td[id*=navOpt]").each(function()
	{
		var ind = this.id.substr(this.id.length-1, 1); //navOptX corresponds to flymenuX
		var div = $(this);
		var pos = div.offset();
		var top = pos.top + div.height();
		var left = pos.left;
		$("div#flymenu"+ind).css({"top" : top, "left" : left, "width" : div.width()-3+"px"});
	});
}

//hide all menus
function hideAllMenus()
{
	$("div[id*=flymenu]").each(function()
	{
		if($(this).is(':visible')) 
		$(this).slideUp(250);
	});
}
//check form function, works on selects and text fields
function check(frm)
{
	var requiredFields = frm.requiredFields ? frm.requiredFields.value.split(",") : null;
	var fld; 
	for(var i=0;i<requiredFields.length;i++)
	{
		fld = $("[name="+requiredFields[i]+"]");
		if(!fld.val() || fld.val() == "none selected")
		{
			alert("Please fill in the required information.");
			fld[0].focus();
			return false;
		}
	}
	return true;
}
//jQuery page load
$(function()
{
//NEW MENU ITEMS - SET STYLES AND EVENTS ON LOAD//
	//assign click handler to menu links - so it can be triggered later.
	$("a.menulink[target],a.flyoutlink[target]").click(function(e)
	{
		var params = $(this).attr("params") ? $(this).attr("params") : ""; 
		window.open($(this)[0].href, "_new", params);
		return false;
	}); 
	//links without a target property
	$("a.menulink:not([target]),a.flyoutlink:not([target])").click(function(e)
	{
		document.location.href = $(this)[0].href;
		return false;
	});
	//Assign jquery click handler to the links in the nav. new window or not is based on target prop. 
	//all the navOptions get a click handler
	$("div.navOption,div.flyout").click(function(){$(this).children().click();});  
	
	//for each flymenu, the last child div has no border on bottom
	$("div[id*=flymenu]").each(function()
	{
		$(this).find("div:last").css("border-bottom", "none");
	});
	//assign over / out for flyout menu classes
	$("div.flyout").mouseover(function()
	{
		$(this).addClass("flyouthover");
	});
	$("div.flyout").mouseout(function()
	{
		$(this).removeClass("flyouthover");
	});
	//menuoptions with flyouts - handlers - mouse over and out
	$("td[id*=navOpt]").mouseover(function()
	{
		//clear timeout, if exists
		clearTimeout(timer);
		//hide menu, if it doesn't correspond to this option
		var ind = this.id.substr(this.id.length-1, 1);
		$("div[id*=flymenu]").each(function()
		{
			if(this.id.indexOf(String(ind)) == -1) 
				$(this).slideUp(250);
		});
		//show corresponding menu
		var menu = $("div#flymenu"+ind);
		if(!menu.is(':visible')) 
			menu.slideDown(250);
	});
	
	//set a timeout for the mouse out of the flymenu or nav options divs
	$("div[id*=flymenu], td[id*=navOpt]").mouseout(function()
	{
		timer = window.setTimeout(hideAllMenus, 500);
	});
	//for flymenu, it's children, and the menuoption associated, assign over behaviour
	$("div[id*=flymenu], div[id*=flymenu]>* ").mouseover(function()
	{
		clearTimeout(timer);
	});
	
	//position the navs and set to position again on resize
	positionNavs();
	$(window).resize(positionNavs);
	
	//input fields
	$("input[type=text],textarea").focus(function(){this.select();}); //onfocus select of input fields and text areas
});