var timeout	= 500;
var closeL1Timer	= null;
var closeL2Timer	= null;
var lastL1DIV	= "";
var lastL1MenuId = "";
var currentL1MenuId = "-";
var lastL2DIV	= "";
var lastL2MenuId = "";
var currentL2MenuId = "-";
var IEBrowser = 0;

function HideLastDIVs()
{
	HideLastL1DIV();
	HideLastL2DIV();
}

function HideLastL1DIV()
{
	// close old layer
	if(lastL1DIV != "")
	{
		//window.status = 'HideLastL1DIV('+lastL1DIV+')';
		$('#'+lastL1DIV).hide();

	}
	if(lastL1MenuId != "" && lastL1MenuId != currentL1MenuId)
		$('#'+lastL1MenuId).attr("class","ddMenuOff");

}

function HideLastL2DIV()
{
	// close old layer
	if(lastL2DIV != "")
	{
		//window.status = 'HideLastL2DIV('+lastL2DIV+')';
		$('#'+lastL2DIV).hide();
	}

	if(lastL2MenuId != "" && lastL2MenuId != currentL2MenuId)
		$('#'+lastL2MenuId).attr("class","sgMenuOff");
}

function HideDIV(d) 
{
	//window.status = 'HideDIV('+d+')';
	$('#'+d).hide();
}

function DisplayDIV(d, top, left)
{
	$('#'+d).css('position','absolute')
		    .css('visibility','visible')
			.css('display','block')
			.css('left', left + 'px')
			.css('top',top + 'px')
			.onScreen(top)
			.show();

		//$('#'+d).onScreen();
}

function DisplayL1DIV(d,top,left) 
{	
	cancelCloseL1Timer(); // cancel close timer
	cancelCloseL2Timer(); // cancel close timer
	HideLastL1DIV(); // Hide Last L1 DIV
	HideLastL2DIV(); // Hide Last L2 DIV
	DisplayDIV(d,top,left); // Display new L1 DIV
}

function DisplayL2DIV(d,top,left) 
{	
	cancelCloseL2Timer(); // cancel close timer
	HideLastL2DIV(); // Hide Last L2 DIV
	DisplayDIV(d,top,left); // Display new L2 DIV
}


function HideLoading()
{
	var div = document.getElementById("LoadingDiv");
	div.style.visibility = "hidden"; 	
}

function startCloseTimers()
{
	startCloseL1Timer();
	startCloseL2Timer();
}

function cancelCloseTimers()
{
	cancelCloseL1Timer();
	cancelCloseL2Timer();	
}

function startCloseL1Timer()
{
    //func = "hideDIV(\'"+div+"\')";
	if (closeL1Timer == null)
		closeL1Timer = window.setTimeout('HideLastL1DIV()', timeout);
}


function cancelCloseL1Timer()
{
	if(closeL1Timer)
	{
		window.clearTimeout(closeL1Timer);
		closeL1Timer = null;
	}
}

function startCloseL2Timer()
{
    //func = "hideDIV(\'"+div+"\')";
	if (closeL2Timer == null)
		closeL2Timer = window.setTimeout('HideLastL2DIV()', timeout);
}


function cancelCloseL2Timer()
{
	if(closeL2Timer)
	{
		window.clearTimeout(closeL2Timer);
		closeL2Timer = null;
	}
}

function SelectSearchCat(value) {
  var searchCat = document.getElementById("cat");
  
  value = value.replace("%20"," ");
  value = value.replace("%26","&");
  value = value.replace("%27","'");
  for(index = 0; 
    index < searchCat.length; 
    index++) {
   if(searchCat[index].value == value)
     searchCat.selectedIndex = index;
   }
}


function MenuInit()
{
	closeL1Timer	= null;
	closeL2Timer	= null;
	lastL1DIV	= "";
	lastL1MenuId = "";
	currentL1MenuId = "-";
	lastL2DIV	= "";
	lastL2MenuId = "";
	currentL2MenuId = "-";

	//Define a mouseenter for all objects with CSS class of ddMenuOff.
	$('.ddMenuOff').mouseenter
	(
		function() 
		{
			var menuItem = $(this);
			currentL1MenuId = menuItem.attr("id");

			var divId = currentL1MenuId.replace('GM','SM'); //if Group Menu (GMn-n) display Submenu (SMn-n)
			divId = divId.replace('MM','GroupMenu');  // if Main Menu (MMn) display GroupMenun

			if ($('#'+divId).length == 0)
			{
				divId = currentL1MenuId.replace('MM','SM');
				if ($('#'+divId).length == 0)
					divId = currentL1MenuId.replace('MM','SM')+'-1';
			}

			//if (lastL1MenuId != currentL1MenuId)
			if (lastL1MenuId != "")
				$('#'.lastL1MenuId).attr("class","ddMenuOff");

			menuItem.attr("class","ddMenuOn");
			var menuOffset = menuItem.offset();
			var left = menuOffset.left + menuItem.width() - 1;
			var top = menuOffset.top + 2;
			DisplayL1DIV(divId,top,left);
			lastL1DIV = divId;
			lastL1MenuId = currentL1MenuId;
		}
	).mouseleave
	(
		function() 
		{
			currentL1MenuId = "-"; //makes the last highlited item unhighlight when move off the menu
			startCloseL1Timer();
		}
	);


	$('.sgMenuOff').mouseenter
	(
		function() 
		{
			var menuItem = $(this);
			currentL2MenuId = menuItem.attr("id");

			var divId = currentL2MenuId.replace('GM','SM'); //if Group Menu (GMn-n) display Submenu (SMn-n)

			//if (lastL2MenuId != currentL2MenuId)
			if (lastL2MenuId != "")
				$(lastL2MenuId).attr("class","sgMenuOff");
			menuItem.attr("class","sgMenuOn");
			var menuOffset = menuItem.offset();
			var left = menuOffset.left + menuItem.width() + 0;
			var top = menuOffset.top + 1;
			DisplayL2DIV(divId,top,left);
			lastL2DIV = divId;
			lastL2MenuId = currentL2MenuId;
		}
	).mouseleave
	(
		function() 
		{
			currentL2MenuId = "-"; //makes the last highlited item unhighlight when move off the menu
			startCloseL2Timer();
		}
	);


	//Define a mouseenter for all objects with CSS class of FlyoutMenu.
	$('.FlyoutMenu').mouseenter
	(
		function() 
		{
			cancelCloseTimers();
			//window.setTimeout('cancelCloseTimers();', 3000);
		}
	).mouseleave
	(
		function() 
		{
			startCloseTimers();
		}
	);

	$('.FlyoutMenu2').mouseenter
	(
		function() 
		{
			cancelCloseTimers();		
		}
	).mouseleave
	(
		function() 
		{
			startCloseTimers();
		}
	);

	$('.FlyoutMenu3').mouseenter
	(
		function() 
		{
			cancelCloseTimers();		
		}
	).mouseleave
	(
		function() 
		{
			startCloseTimers();
		}
	);

	$('.FlyoutGroupMenu').mouseenter
	(
		function() 
		{
			cancelCloseTimers();		
		}
	).mouseleave
	(
		function() 
		{
			startCloseTimers();
		}
	);


	$('.FlyoutMenuAdmin').mouseenter
	(
		function() 
		{
			cancelCloseTimers();		
		}
	).mouseleave
	(
		function() 
		{
			HideLastDIVs();
		}
	);
}

(function($)
{
	// Center a div on the screen
	$.fn.center = function () { 

		// To call this function: $(element).center(); 

		this.css("position","absolute"); 
		this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px"); 
		this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px"); 
		//window.status = 'Height: '+this.height()+'  Width: '+this.width();
		return this; 
	};
})(jQuery);

(function($)
{
	// Center a div on the screen   (used when on Mobile device to center H & V)
	$.fn.centerPreview = function () { 

		// To call this function: $(element).centerVertically(); 

		this.css("position","absolute"); 
		var windowHeight = $(window).height();
		var thisHeight = this.height();


		// Shrink image inside of DIV is DIV is bigger than screen - 50 pixels.
		if (thisHeight > windowHeight-25)
		{
			thisHeight = windowHeight-25;
		   $(this).find("#preview-image").height(thisHeight);
		}

		var windowWidth = $(window).width();
		var thisWidth = this.width();

		// Shrink image inside of DIV is DIV is bigger than screen - 50 pixels.
		if (thisWidth > windowWidth-25)
		{
			thisWidth = windowWidth-25;
		   $(this).find("#preview-image").width(thisWidth);
		}

		thisHeight = this.height();
		thisWidth = this.width();
		//window.status = 'preview thisHeight: '+thisHeight+'  Width: '+this.width();
		//alert('preview thisHeight: '+thisHeight+'  Width: '+this.width());
		
		// Size unknown or 125x125 when rel not the same as src, so show in upper-right
		if ((thisHeight < 130) || (thisWidth <130))
		{
			this.css("top", 20 + +$(window).scrollTop() + "px"); 
			this.css("left", 10 + $(window).scrollLeft() + "px");
		}
		else
		{
			this.css("top", (( windowHeight - thisHeight ) / 2+$(window).scrollTop() + 20) + "px"); 
			this.css("left", ( windowWidth - thisWidth ) / 2+$(window).scrollLeft() + "px");
		}
		return this; 
	};
})(jQuery);

(function($)
{
	// Center a div on the screen (used on non-mobile to center vertically)
	$.fn.centerVertically = function () { 

		// To call this function: $(element).centerVertically(); 

		this.css("position","absolute"); 
		var windowHeight = $(window).height();
		var thisHeight = this.height();

		// Shrink image inside of DIV is DIV is bigger than screen - 120 pixels.
		if (thisHeight > windowHeight-120)
		{
			thisHeight = windowHeight-120;
		   $(this).find("#preview-image").height(thisHeight);
		}
		this.css("top", ( windowHeight - this.height() ) / 2+$(window).scrollTop() + "px"); 
		//window.status = 'new thisHeight: '+thisHeight+'  Width: '+this.width();
		return this; 
	};
})(jQuery);

(function($)
{
	// Make shure a div is visible on the screen by sliding up or down if needed
	$.fn.onScreen = function (desiredTop) { 

		// To call this function: $(element).onScreen(desired-top-position); 
		var thisBottom =  desiredTop + this.height();
		var windowBottom = ($(window).scrollTop() + $(window).height());
		var newTop = ( $(window).scrollTop() + $(window).height() - this.height());

		if ( thisBottom > windowBottom )
		{
			this.css("position","absolute"); 
			this.css("top", newTop + "px"); 
		}
		//window.status = 'Height: '+this.height()+'  Width: '+this.width();
		return this; 
	};
})(jQuery);


