var TextSizeLbl = null;
var LeftBar = null;
var MainBody = null;
var Leaderboard = null;
var nLayoutStyle = 1;
var bWideLayout;
var bFloat400Right = null;
var xmlhttp;
var ResizeFuncs = [];

function ArrangeColumns2(LeftBarID, MainBodyID, TextSizeLblID, LeaderboardID)
{
	if (typeof(document.body.clientWidth) != 'number')
		return;
	
	//alert("Body Width = " + document.body.clientWidth);
	
	SetPageSections(LeftBarID, MainBodyID, TextSizeLblID, LeaderboardID);
	
	ResizeTasks();
	EqualLinkAndBodyHeight(true);
	
//	alert(MainBody.clientWidth);
	if (MainBody != null)
		AddCookie("L" + nLayoutStyle + "BodyWidth", MainBody.clientWidth, null, "/");
}

function SizeColumns(nLeftWidth, nBodyWidth, nFontSize, strBodyPadding)
{
	alert("sizecolumn");
	if (LeftBar != null)
		LeftBar.style.width = (nLeftWidth) + "px";
		
	if (MainBody != null)
	{
		// -1 for the width of the right border of the left bar (see .LeftBar)
		MainBody.style.width = (nBodyWidth - 1) + "px";
		if (strBodyPadding != null && strBodyPadding != "")
			changecss(".MainBodyInCell", "padding", strBodyPadding);
	}

	EqualLinkAndBodyHeight(true);
	
	// This is only necessary when the width cookies are not set on Firefox
	// It is a last resort, since it is impercise at setting the scroll position
	if (StripAnchorFromURL() != "" && document.getElementById(StripAnchorFromURL()) != null &&
		(GetCookie("L" + nLayoutStyle + "LeftBar") == null || 
		GetCookie("L" + nLayoutStyle + "BodyWidth") == null))
		window.scrollTo(0, (getEltPageTop(document.getElementById(StripAnchorFromURL()))) - 40);
	
	AddCookie("L" + nLayoutStyle + "LeftBar", nLeftWidth, null, "/");
	AddCookie("L" + nLayoutStyle + "BodyWidth", nBodyWidth - 1, null, "/");
}

function DeleteCookies()
{
	//alert("Deleting Cookies");
	var d = new Date();
	d.setTime((d.getTime() - 1000));
	document.cookie = "L1LeftBar= ; expires=" + d.toGMTString();
	document.cookie = "L1Body= ; expires=" + d.toGMTString();
	document.cookie = "L1AdArea= ; expires=" + d.toGMTString();
	document.cookie = "L1BodyWidth= ; expires=" + d.toGMTString();
}

function IncrementTextSize(bIncrease)
{
	var nSize = new Number(GetTextSize());
	if (bIncrease)
		nSize+= new Number(.05);
	else
	{
		nSize-= new Number(.05);
		if (nSize <= 0)
			nSize = new Number(.05);
	}
	
	ChangeTextSize(nSize, true);
}

function ChangeTextSize(nSize, bSetCookie)
{
	//alert(GetTextSize());
	if (document.body == null || document.body.style == null || 
		document.body.style.fontSize == null || document.cookie == null)
		return;
	
	if (GetCookie("TextSize") != null && nSize == GetTextSize())
		return;
	
	if (bSetCookie == true)
		AddCookie("TextSize", nSize.toFixed(2), null, "/");

	document.body.style.fontSize = nSize + "em";
	
	if (TextSizeLbl != null)
		TextSizeLbl.innerHTML = nSize.toFixed(2) + "x";

	EqualLinkAndBodyHeight(true);
}

function GetTextSize()
{
	var nSize = new Number(GetCookie("TextSize"));
	if (nSize == "NaN" || nSize == 0)
		nSize = GetTextSizeForWidth();

	if (nSize > 3)
	{
		nSize = GetTextSizeForWidth();
		d.setTime((d.getTime() - 1000));
		document.cookie = "TextSize= ; expires=" + d.toGMTString();

		ChangeTextSize(nSize, true);
	}

	return nSize;
}

function GetTextSizeForWidth()
{
	var nWindowWidth = document.body.clientWidth;
	if (nWindowWidth >= 1200)
		return .9;
	else if (nWindowWidth > 804) //796
		return .85;
	else
		return .75;
}

function StripAnchorFromURL()
{
	if (document.URL == null || document.URL.indexOf('#') == -1)
		return "";

	return document.URL.substring((document.URL.indexOf('#')) + 1, document.URL.indexOf('?') == -1 ?
		document.URL.length : document.URL.indexOf('?'));
}

function ArrangeColumns()
{
	// > 800 - main body 517
	// >= 640 - main body 300
	// <= 320 - main body 300, ads under main body
	// <= 300 - 
	if (document.getElementById("divLeftBar") == null)
		return;
	
	var AdArea = document.getElementById("divAdArea");
	var nTwoColWidth = (document.getElementById("divLeftBar").clientWidth + document.getElementById("divMainBody").clientWidth);
	//alert("body width: " + document.body.clientWidth + "\nTwo Col Width + 140: " + (nTwoColWidth + 140));
	if (document.body.clientWidth > nTwoColWidth + 240)
	{
		// wide enough to accommodate all three areas
		EqualLinkAndBodyHeight(true);
		document.getElementById("divMainContent").insertBefore(document.getElementById("divLeftBar"), document.getElementById("divMainBody"));
		AdArea.style.width = (document.body.clientWidth - nTwoColWidth - 6) + "px";
		//alert("Width: " + (document.body.clientWidth - nTwoColWidth - 6) + "\nstyle.width: " + AdArea.style.width);
		if (AdArea.clientHeight < (document.getElementById("divMainBody").clientHeight))
			AdArea.style.height = (document.getElementById("divMainBody").clientHeight) + "px";
		else
			AdArea.style.height = "";
	}
	else 
	{
		//AdArea.style.height = "480px";
		//AdArea.style.cssFloat = "none";
		if (document.body.clientWidth >= nTwoColWidth)
		{
			// Left bar and body, side-by-side
			EqualLinkAndBodyHeight(true);
			document.getElementById("divMainContent").insertBefore(document.getElementById("divLeftBar"), document.getElementById("divMainBody"));
			AdArea.style.width = (nTwoColWidth) + "px";
		}
		else
		{
			// Left bar moved underneath body
			EqualLinkAndBodyHeight(false);
			AdArea.style.width = (document.getElementById("divMainBody").clientWidth) + "px";
			document.getElementById("divMainContent").insertBefore(document.getElementById("divLeftBar"), document.getElementById("divAdArea"));
		}
		
	}
	
	//divAdArea.style.height = divMainBody.clientHeight - 9;
}

function EqualLinkAndBodyHeight(bSetEqual)
{
	if (LeftBar == null || MainBody == null)
		return;

	if (bSetEqual)
	{
		//alert("Setting " + divMainBody.clientHeight + ", " + divLeftBar.clientHeight);
		if (LeftBar.clientHeight >= MainBody.clientHeight)
		{
			MainBody.style.height = LeftBar.clientHeight + "px";
			var BodyTable = document.getElementById("BodyTable");
			if (BodyTable != null && BodyTable.rows != null)
				BodyTable.rows[1].cells[1].style.height = (LeftBar.clientHeight - 16) + "px";
		}
		else
		{
			MainBody.style.height = "";
			LeftBar.style.height = (MainBody.clientHeight - 1) + "px"; // -3 of .LeftBar bottom border
		}
	}
	else
	{
		MainBody.style.height = "";
		LeftBar.style.height = "100%";
	}
}

function ChangeColor()
{
	return;
}

function ResizeTasks()
{
	if (typeof(document.body.clientWidth) != 'number')
		return;
		
	var nWindowWidth = document.body.clientWidth;
	var divMainBodyInCell = document.getElementById("divMainBodyInCell");
	var bArticlePage = (document.getElementById("divPageBody") != null);
	bWideLayout = (nWindowWidth >= 920);
	
	if (GetCookie("TextSize") == null)
		ChangeTextSize(GetTextSizeForWidth(), false);

	BuildHeader(bWideLayout, bArticlePage);
	ArrangeAdvertisements();
	BuildPageShadow(bWideLayout);
	
	for (var i = 0; i < ResizeFuncs.length; i++)
		ResizeFuncs[i].call();
}

function BuildHeader(bWideLayout, bArticlePage)
{
	// Logo Mouseover
	document.getElementById("imgKnowzyLogo").onmouseover = function onmouseover(event) { this.src = '/images/HdrKnowzyLogo.gif'; };
	document.getElementById("imgKnowzyLogo").onmouseout = function onmouseout(event) { this.src = '/images/HdrKnowzyLogoBev.gif'; };
	
	var tdHdrBHN = document.getElementById("HdrBHN");
	var tdHdrBRBevel = document.getElementById("HdrBRBevel");
	if (tdHdrBHN != null)
	{
		if (bWideLayout && tdHdrBHN.width <= 221)
		{
			tdHdrBHN.width = 229;
			if (bArticlePage)
			{
				tdHdrBHN.style.backgroundImage = "url(/images/HdrBHNBack.gif)";
				tdHdrBRBevel.style.backgroundImage = "url(/images/HdrBRBevel.gif)";
				document.getElementById("HdrTHN").style.backgroundImage = "url(/images/HdrTHNBack.gif)";
				document.getElementById("HdrM").style.backgroundImage = "url(/images/HdrBBack.gif)";
				document.getElementById("HdrTRBevel").style.backgroundImage = "url(/images/HdrTRBevel.gif)";
			}
			else
			{
				tdHdrBHN.style.backgroundImage = "url(/images/HdrBHNBackW.gif)";
				tdHdrBRBevel.style.backgroundImage = "url(/images/HdrBRBevelW.gif)";
				document.getElementById("HdrTHN").style.backgroundImage = "url(/images/HdrTHNBackW.gif)";
				document.getElementById("HdrTRBevel").style.backgroundImage = "url(/images/HdrTRBevelW.gif)";
				document.getElementById("HdrM").style.backgroundImage = "url(/images/HdrBBackW.gif)";
			}
			
			document.getElementById("HdrTHN").width = 229;
			tdHdrBRBevel.width = 26;
			document.getElementById("HdrTRBevel").width = 26;
			document.getElementById("HdrBRow").style.height = "140px";
			document.getElementById("divHeaderTitleRow").style.height = "140px";
			document.getElementById("tblFullHdr").width = "94%"
		}
		else if (!bWideLayout && tdHdrBHN.width >= 229)
		{
			tdHdrBHN.width = 221;
			tdHdrBHN.style.backgroundImage = "url(/images/HdrBHNBackCon.gif)";
			document.getElementById("HdrTHN").width = 221;
			var tdHdrBRBevel = document.getElementById("HdrBRBevel");
			tdHdrBRBevel.style.backgroundImage = "url(/images/HdrBRBevelCon.gif)";
			tdHdrBRBevel.width = 14;
			document.getElementById("HdrTRBevel").width = 14;
			document.getElementById("HdrBRow").style.height = "127px";
			document.getElementById("divHeaderTitleRow").style.height = "127px";
			document.getElementById("tblFullHdr").width = "100%"
		}
	}
}

function BuildPageShadow(bWideLayout)
{
	// If divPageBody element is present, this is an article page.
	var divPageBody = document.getElementById("divPageBody");
	if (divPageBody == null)
		return;
		
	var divPageRShadCnt = document.getElementById("divPageRShadCnt"), divMainBodyInCell = document.getElementById("divMainBodyInCell"),
		divPageBShadCnt = document.getElementById("divPageBShadCnt"), divPageBodyIn = document.getElementById("divPageBodyIn");
	
	if (bWideLayout)
	{
		//document.getElementById("divMainContent").style.marginTop = "11px";

		// Build the page
		document.body.style.backgroundColor = "#CCEDFF";
		changecss(".FooterContainer", "backgroundColor", "#CCEDFF");

		divMainBodyInCell.style.padding = "0em 1.1em 1.1em 1.1em";
		divPageBodyIn.style.padding = "2.25em"; // 1.4em

		divPageBody.style.margin = "0px 8px 0px 0px";
		divPageBody.style.width = "100%";
		divPageBody.style.overflow = "hidden";
		divPageBody.style.backgroundColor = "#FFFFFF";
		divPageBody.style.border = "1px solid #000000";

		// Build the shadow
		if (navigator.family == "ie4" || navigator.family == "gecko")
		{
			divPageRShadCnt.style.width = "8px";
			divPageRShadCnt.style.margin = "0px 0px 0px -8px";

			// Right shadow
			document.getElementById("divPageRShadT").style.backgroundImage = "url(/images/PageTRShad.gif)";
			document.getElementById("divPageRShadT").style.backgroundRepeat = "no-repeat";
			document.getElementById("divPageRShadT").style.backgroundPosition = "bottom";
			document.getElementById("divPageRShadT").style.height = "10px";

			document.getElementById("divPageRShadM").style.backgroundImage = "url(/images/PageRShad.gif)";
			document.getElementById("divPageRShadM").style.backgroundRepeat = "repeat-y";
			document.getElementById("divPageRShadM").style.height = (divPageBody.clientHeight - 10) + "px";

			document.getElementById("divPageRShadB").style.backgroundImage = "url(/images/PageBRShad.gif)";
			document.getElementById("divPageRShadB").style.backgroundRepeat = "no-repeat";
			document.getElementById("divPageRShadB").style.height = "2px";

			// Bottom shadow
			document.getElementById("imgPageBShadL").src = "/images/PageBBLShad.gif";
			document.getElementById("imgPageBShadL").width = "12";
			document.getElementById("imgPageBShadR").src = "/images/PageBBRShad.gif";
			document.getElementById("imgPageBShadR").width = "14";
			divPageBShadCnt.style.backgroundImage = "url(/images/PageBShad.gif)";
			divPageBShadCnt.style.backgroundRepeat = "repeat-x";
			divPageBShadCnt.style.height = "9px";
			divPageBShadCnt.style.width = (divPageBody.clientWidth + 10) + "px";
			if (MSIE6OrLess())
				divPageBShadCnt.style.margin = "-1.53em 0px 0px 0px";

		}
	}
	else
	{
		//document.getElementById("divMainContent").style.marginTop = "11px";
		document.body.style.backgroundColor = "#FFFFFF";
		changecss(".FooterContainer", "backgroundColor", "#FFFFFF");
		divMainBodyInCell.style.padding = "0px 12px 12px 12px";
		divPageBody.style.padding = "";
		divPageBody.style.backgroundColor = "";
		divPageBody.style.border = "";
		divPageBodyIn.style.padding = "0em";
		document.getElementById("divPageRShadT").style.backgroundImage = "";
		document.getElementById("divPageRShadM").style.backgroundImage = "";
		document.getElementById("divPageRShadB").style.backgroundImage = "";
		document.getElementById("imgPageBShadL").src = "/ico/one_px.gif";
		document.getElementById("imgPageBShadL").width = "";
		document.getElementById("imgPageBShadR").src = "/ico/one_px.gif";
		document.getElementById("imgPageBShadR").width = "";
		divPageBShadCnt.style.backgroundImage = "";
		divPageBShadCnt.style.height = "";
	}
}

function ArrangeAdvertisements()
{
	var divMainBody = document.getElementById("divMainBody");

	// Advertisements
	if (Leaderboard != null)
	{
		if (bWideLayout)
			Leaderboard.style.margin = "4px auto 0px auto";
		else
			Leaderboard.style.margin = "0px auto 0px auto";
	}
	
	if (MainBody != null)
	{
		if ((bFloat400Right == null || bFloat400Right) && (MainBody.clientWidth - 375) < 200)
		{
			bFloat400Right = false;
			changecss(".Float400R", "styleFloat", "none");
			changecss(".Float400R", "cssFloat", "none");
			changecss(".Float400R", "margin", "auto");
			changecss(".Float400R", "clear", "both");
		}
		else if (!bFloat400Right && (MainBody.clientWidth - 375) >= 200)
		{
			bFloat400Right = true;
			changecss(".Float400R", "margin", ".25em auto 1.5em 1.5em");
			changecss(".Float400R", "styleFloat", "right");
			changecss(".Float400R", "cssFloat", "right");
			changecss(".Float400R", "clear", "right");
		}
	}
}


// Retrieve the value of the cookie with the specified name.
function GetCookie(sName)
{
	// cookies are separated by semicolons
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++)
	{
		// a name/value pair (a crumb) is separated by an equal sign
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0]) 
		return unescape(aCrumb[1]);
	}

	// a cookie with the requested name does not exist
	return null;
}

function AddCookie(strKey, strValue, expires, strPath)
{
	var d = new Date();
	d.setTime((d.getTime() + 31536000000));

	document.cookie = strKey + "=" + strValue + "; expires=" + d.toGMTString() + "; path=" + strPath;
}

function SetPageSections(LeftBarID, MainBodyID, TextSizeLblID, LeaderboardID)
{
	TextSizeLbl = document.getElementById(TextSizeLblID);
	LeftBar = document.getElementById(LeftBarID);
	MainBody = document.getElementById(MainBodyID);
	Leaderboard = document.getElementById(LeaderboardID + "_GraphicAdContainer");
	//alert(MainBody.id);
}

function FindSiblingWithTag(node, bDirection)
{
	if (bDirection)
		node = node.nextSibling;
	else
		node = node.previousSibling;

	if (node == null)
		return null;

	// For Firefox's sake: skip over line feeds, other whitespace
	while (node.nodeType != 1)
	{
		if (bDirection)
			node = node.nextSibling;
		else
			node = node.previousSibling;

		if (node == null)
			return null;
	}

	return node;
}

function ReadHttpFile(url, callback)
{
	xmlhttp = null;
	if (window.XMLHttpRequest)
		xmlhttp = new XMLHttpRequest();
	else if (window.ActiveXObject)
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

	if (xmlhttp != null)
	{
		var bUseCallback = (typeof(callback) == "function");
		if (bUseCallback)
			xmlhttp.onreadystatechange = callback;

		xmlhttp.open("GET", url, bUseCallback);
		xmlhttp.send(null);
		
		if (bUseCallback && xmlhttp.readyState == 4 && xmlhttp.status == 200)
			return xmlhttp.responseText;
	}
}

// thanks jerom!
// http://forums.devshed.com/javascript-development-115/dynamically-change-a-css-class-definition-435990.html
function changecss(myclass,element,value) {
	if (! document.styleSheets) return
	var CSSRules;  // IE uses rules...
	for (var i = 0; i < document.styleSheets.length; i++) {
		(document.styleSheets[0].cssRules) ? CSSRules = 'cssRules' : CSSRules = 'rules';
		for (var j = 0; j < document.styleSheets[i][CSSRules].length; j++) {
			if (document.styleSheets[i][CSSRules][j].selectorText == myclass) {
				document.styleSheets[i][CSSRules][j].style[element] = value;
			}
		}	
	}
}

function MSIE6OrLess()
{
	return (navigator.AppName = "Microsoft Internet Explorer" && 
		(navigator.appVersion.indexOf("MSIE 6.") != -1 || navigator.appVersion.indexOf("MSIE 5.") != -1 || 
		navigator.appVersion.indexOf("MSIE 4.") != -1));
}

function MSIE7OrBetter()
{
	return (navigator.AppName = "Microsoft Internet Explorer" && 
		(navigator.appVersion.indexOf("MSIE 7.") != -1 || navigator.appVersion.indexOf("MSIE 8.") != -1 || 
		navigator.appVersion.indexOf("MSIE 9.") != -1));
}

function IsWebKit()
{
	return (navigator.userAgent.indexOf("WebKit") != -1);
}

function IsOpera()
{
	return (navigator.userAgent.indexOf("Opera") != -1);
}