﻿jQuery.noConflict();
jQuery(document).ready(function()
{
	App.initialise();

});

var App =
{
	initialise: function()
	{
		jQuery(".InsidePage .Logo").each(App.bindHover);
		jQuery(".Providers ul li.Hover").each(App.bindHover);
		App.bindContent();
		App.bindFooter();
	},

	bindContent: function()
	{
		var leftNavHeight = jQuery("#LeftNav").height();
		var content = jQuery("#Content");
		var contentHeight = content.height();
		if (contentHeight != null)
		{
			content.css({ height: (leftNavHeight > contentHeight) ? leftNavHeight : contentHeight });
			var contentBottom = content.position().top + content.height();
			var tileCover = jQuery("#TileCover");
			tileCover.css({ position: "absolute", top: contentBottom + 25, left: 25 });
		}
	},
	
	bindFooter: function()
	{
		var footer = jQuery("#Footer")
		var leftContent = jQuery(".LeftContent");
		var leftContentBottom = leftContent.position().top + leftContent.height();
		var newsContent = jQuery(".CenterContent");
		if (newsContent.height() != null)
		{
			var newsContentBottom = newsContent.position().top + newsContent.height();
		}
		else
		{
			var newsContentBottom = 0;
		}
		var rightContent = jQuery(".RightContent");
		var rightContentBottom = rightContent.position().top + rightContent.height();
		var maxBottom = (leftContentBottom > newsContentBottom) ? leftContentBottom : newsContentBottom;
		
		maxBottom = (maxBottom > rightContentBottom) ? maxBottom : rightContentBottom;
		footerTop = maxBottom;
		footer.css({ position: "absolute", top: footerTop + 28, left: 0 });
	},
	
	bindHover: function()
	{
		var item = jQuery(this);
		var link = item.find("a");
		
		if (link)
		{
			if (link.attr("href") != undefined)
			{
				item.click(function(evt)
				{
					if (evt.target.tagName == "a")
					{
						var target = link.attr("target");
						if (target)
						{
							window.open(link.attr("href"), target);
						}
						else
						{
							window.location.href = link.attr("href");
						}
					}
					else
					{
						if (link.attr("href") != "")
						{
							item.css({ cursor: "pointer" });
							window.location.href = link.attr("href");
						}
					}
				});
				
			}
			else
			{
				item.css({ cursor: "default" });
			}
		}
		else
		{
			item.mouseover(function(evt)
			{
				item.css({ cursor: "default" });
			});
		}
	}
}

function openWindow(url, name, width, height, resizable, scrollbars, statusbar, menubar, toolbar)
{
	var win = null;
	var optionString = "";
	if (width) optionString += "width=" + width + ",";
	if (height) optionString += "height=" + height + ",";
	if (resizable) optionString += "resizable=1,";
	if (scrollbars) optionString += "scrollbars=1,";
	if (statusbar) optionString += "status=1,";
	if (menubar) optionString += "menubar=1,";
	if (toolbar) optionString += "toolbar=1,";
	if (optionString != "") optionString = optionString.substr(0, optionString.length - 1);
	win = window.open(url, name, optionString);
	if (win) win.focus();
	return win;
}

function printPage()
{
	if (window.print) window.print();
	else alert("Please select \"Print...\" from the File menu.");
}

function trim(theString)
{
	var newString = theString;
	while (newString.charAt(0) == " " || newString.charCodeAt(0) == 10 || newString.charCodeAt(0) == 13 || newString.charCodeAt(0) == 9)
	{
		newString = newString.substring(1,newString.length);
	}
	while (newString.charAt(newString.length - 1) == " " || newString.charCodeAt(newString.length - 1) == 10 || newString.charCodeAt(newString.length - 1) == 13 || newString.charCodeAt(newString.length - 1) == 9)
	{
		newString = newString.substring(0,newString.length - 1);
	}
	return newString;
}

function getCookie(name)
{
	var cookies = document.cookie.split(";")
	for (var i=0; i<cookies.length; i++)
	{
		var nameVal = cookies[i].split("=");
		if (nameVal.length == 2 && trim(nameVal[0]) == trim(name))
		{
			return trim(nameVal[1]);
		}
	}
	return "";
}

function setCookie(name, value, expires)
{
	document.cookie = name + "=" + value + ";path=/;" + (expires ? " expires=" + expires.toGMTString() + ";" : "");
}

function removeCookie(name)
{
	setCookie(name, "", new Date("2 January 1970"));
}

function createBookmarkLink(url, title)
{
	if (window.sidebar)
		// Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url, "");
	else if (window.external)
		// IE Favorite
		window.external.AddFavorite(url,title);
	else if (window.opera && window.print)
		// Opera Hotlist
		return true;
}
