﻿$(document).ready(function()
{
	$("a").click(function() { this.blur(); });

	$(".mtareas").click(function()
	{
		ExecHover.call(this);
		return false;
	});

	$("#mtareas").hover(
				function(evt)
				{
					ExecHover.call(this);
				},
				function(evt)
				{
					hovered = false;
					setTimeout("hideMenu()", 200);
				});
	$("#ddMenu").hover(
				function()
				{
					hovered = true;
				},
				function()
				{
					hovered = false;
					setTimeout("hideMenu()", 200);
				});
	
	
	var skip = (typeof skipSubmenus != "undefined" && skipSubmenus == true);
	if (!skip)
	{
		$("a").each(function()
		{
			if (this.href.indexOf(window.location.pathname) > 0 && window.location.pathname != "/")
			{
				var th = $(this);
				if (th.hasClass("btn") || th.hasClass("nomenu")) return;
				var fnt = th.css("font-size");

				if ((fnt == "11px" || fnt == "10px"))
				{
					th.addClass("currentLink");
				}
				else
				{
					SetupSelectedMenu(th);
				}
			}
		});
		if (window.location.pathname == "/")
		{
			SetupSelectedMenu($("#mhome"));
		}
	}
});

function SetupSelectedMenu(th)
{
	if (th.css("background-image").indexOf("I/menu/") > 0)
	{
		th.css("background-image", th.css("background-image").replace(".gif", "_.gif"));
	}
}

var hovered = false;

function ExecHover()
{
	if (hovered) return;
	hovered = true;
	var pos = $(this).offset();
	var dd = $("#ddMenu");
	dd.css("left", pos.left);
	dd.css("top", $("#centerContent").offset().top - 9);
	dd.fadeIn(200);
}

function hideMenu()
{
	if (hovered) return;
	var dd = $("#ddMenu");
	dd.fadeOut(200);
}

//override dropdownlist validator to use with "jQuery UI selectmenu" plugin
function ValidatorOnChange(event)
{
	if (!event)
	{
		event = window.event;
	}
	Page_InvalidControlToBeFocused = null;
	var targetedControl;
	if ((typeof (event.srcElement) != "undefined") && (event.srcElement != null))
	{
		targetedControl = event.srcElement;
	}
	else
	{
		targetedControl = event.target;
	}
	var vals;
	if (typeof (targetedControl.Validators) != "undefined")
	{
		vals = targetedControl.Validators;
	}
	else
	{
		if (targetedControl.tagName.toLowerCase() == "label")
		{
			targetedControl = document.getElementById(targetedControl.htmlFor);
			vals = targetedControl.Validators;
		}
		else
		{
			if (targetedControl.tagName.toLowerCase() == "a")
			{
				var tid = targetedControl.parentElement.parentElement.id;
				if (tid == "" || tid == null) return;
				
				tid = tid.substr(0, tid.lastIndexOf("-menu"))
				$("#" + tid)[0];
				vals = $("#" + tid)[0].Validators;
			}
		}
	}
	var i;
	for (i = 0; i < vals.length; i++)
	{
		ValidatorValidate(vals[i], null, event);
	}
	ValidatorUpdateIsValid();
}

function RunDDLMenu()
{
	ExecHover.call($('#mtareas'));
	hovered = false;
	setTimeout('hideMenu()', 1500);
}
