﻿window.$ = $telerik.$;
//$(document).ready(function() { alert("jQuery in Effect!"); });
var LeftPane = null;
var MainBody = null;
var fm = 9;
var ppem = -1;
var bWideLayout = false;
var bFloatingPage = false, bJustSwitchedToFloatingPage = false;
var bInitialResize = true;
var nLastPageWidth = 0, nLastLeftPaneClientWidth = 0, nLastBodyWidth = 0;
var nMaxPicWidth;
var nMaxLeftPaneWidth = 233;
var bFloat400Right = null;
var ResizeFuncs = [];

function BuildHeader()
{
	// Logo Mouseover
	document.getElementById("imgKnowzyLogo").onmouseover = function onmouseover(event) { this.src = '/images_core/HdrKnowzyLogo.gif'; };
	document.getElementById("imgKnowzyLogo").onmouseout = function onmouseout(event) { this.src = '/images_core/HdrKnowzyLogoBev.gif'; };

	var tdHdrBHN = document.getElementById("HdrBHN");
	var tdHdrBRBevel = document.getElementById("HdrBRBevel");
	if (tdHdrBHN != null)
	{
		var bCurrentlyWide = (tdHdrBRBevel.width == 26);

		if (bWideLayout)
			SetHeaderBackground(!bFloatingPage);

		if (bWideLayout && !bCurrentlyWide)
		{
			tdHdrBHN.width = 229;
			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 = "96.5%"
		}
		else if (!bWideLayout && bCurrentlyWide)
		{
			tdHdrBHN.width = 221;
			tdHdrBHN.style.backgroundImage = "url(/images_core/HdrBHNBackCon.gif)";
			document.getElementById("HdrTHN").width = 221;
			tdHdrBRBevel.style.backgroundImage = "url(/images_core/HdrBRBevelCon.gif)";
			tdHdrBRBevel.width = 14;
			document.getElementById("HdrTHN").style.backgroundImage = "url(/images_core/HdrTHNBack.gif)";
			document.getElementById("HdrM").style.backgroundImage = "url(/images_core/HdrBBack.gif)";
			document.getElementById("HdrTRBevel").style.backgroundImage = "url(/images_core/HdrTRBevel.gif)";
			document.getElementById("HdrTRBevel").width = 14;
			document.getElementById("HdrBRow").style.height = "127px";
			document.getElementById("divHeaderTitleRow").style.height = "127px";
			document.getElementById("tblFullHdr").width = "100%"
		}
	}
}

function SetHeaderBackground(bWhite)
{
	var tdHdrBHN = document.getElementById("HdrBHN");
	var tdHdrBRBevel = document.getElementById("HdrBRBevel");
	
	var bCurrentlyWhite = (tdHdrBHN.style.backgroundImage.indexOf("/HdrBHNBackW.gif") != -1);

	if (bWhite && !bCurrentlyWhite)
	{
		tdHdrBHN.style.backgroundImage = "url(/images_core/HdrBHNBackW.gif)";
		tdHdrBRBevel.style.backgroundImage = "url(/images_core/HdrBRBevelW.gif)";
		document.getElementById("HdrTHN").style.backgroundImage = "url(/images_core/HdrTHNBackW.gif)";
		document.getElementById("HdrTRBevel").style.backgroundImage = "url(/images_core/HdrTRBevelW.gif)";
		document.getElementById("HdrM").style.backgroundImage = "url(/images_core/HdrBBackW.gif)";
	}
	else if (!bWhite)
	{
		if (bCurrentlyWhite || tdHdrBRBevel.style.backgroundImage.indexOf("/HdrBRBevelCon.gif") != -1)
		{
			tdHdrBHN.style.backgroundImage = "url(/images_core/HdrBHNBack.gif)";
			tdHdrBRBevel.style.backgroundImage = "url(/images_core/HdrBRBevel.gif)";
			document.getElementById("HdrTHN").style.backgroundImage = "url(/images_core/HdrTHNBack.gif)";
			document.getElementById("HdrM").style.backgroundImage = "url(/images_core/HdrBBack.gif)";
			document.getElementById("HdrTRBevel").style.backgroundImage = "url(/images_core/HdrTRBevel.gif)";
		}
	}
}

function BuildFloatingPage(bPreserveSidebar)
{
	// Calc width- 105 characters
	// If Page width + sidebar width >= Client width, abort 
	// Body background blue
	// MainContent white
	// Borders, backgrounds
	// Set width of spacers: (Client width - Page width - Sidebar width) / 2
	bJustSwitchedToFloatingPage = false;

	var LeftPaneDisplay = GetCookie("LeftPaneDisplay");
	var bSidebarHidden = (LeftPaneDisplay != null && LeftPaneDisplay == "false");

	if (LeftPane == null)
		return;
		
	var nPageWidth = Math.floor(fm * 104);
	var nSplitterWidth = document.getElementById("LeftSplitter").clientWidth;

	if (document.getElementById("PageStyle") == null ||
		document.getElementById("PageStyle").value != "Article")
	{
		if (!bSidebarHidden && (bPreserveSidebar == null || bPreserveSidebar == false))
			HideShowLeftPane((Math.floor(document.body.clientWidth - (nPageWidth * .70) - nSplitterWidth) <= nMaxLeftPaneWidth), // 233
			false);

		return;
	}

	var nLeftPaneClientWidth = LeftPane.clientWidth;

	if (nLastPageWidth == nPageWidth && nLastLeftPaneClientWidth == nLeftPaneClientWidth &&
		nLastBodyWidth == document.body.clientWidth)
		return false;
	else
	{
		nLastPageWidth = nPageWidth;
		nLastLeftPaneClientWidth = nLeftPaneClientWidth;
		nLastBodyWidth = document.body.clientWidth;
	}

	if ((nPageWidth + nMaxLeftPaneWidth /*233 nLeftPaneClientWidth */ + nSplitterWidth + 10) >= document.body.clientWidth)
		RemoveFloatingPage();
	else
		bFloatingPage = true;

	if (!bSidebarHidden && (bPreserveSidebar == null || bPreserveSidebar == false))
		nLeftPaneClientWidth = HideShowLeftPane((Math.floor(document.body.clientWidth - (bFloatingPage ? nPageWidth : (nPageWidth * .70)) - nSplitterWidth) <= nMaxLeftPaneWidth), // 233
		false);
	
	if (!bFloatingPage)
		return;

	if (nLeftPaneClientWidth < nMaxLeftPaneWidth)
		nLeftPaneClientWidth = nMaxLeftPaneWidth;

	var nLeftOver = Math.floor((document.body.clientWidth - nPageWidth - nLeftPaneClientWidth - nSplitterWidth) / 2);
	
	var ContentArea = document.getElementById("ContentArea");
	var LeftSpacer = document.getElementById("LeftSpacer");
	var RightSpacer = document.getElementById("RightSpacer");
	var BottomPageEdge = document.getElementById("BottomPageEdge");
	var BottomRightPageEdge = document.getElementById("BottomRightPageEdge");

	var nLSpacerWidth = (nLeftOver - nSplitterWidth - 4);
	if (nLSpacerWidth < 0)
		nLSpacerWidth = 0;

	LeftSpacer.style.width = nLSpacerWidth + "px";
	
	if (nLeftOver > 0)
		RightSpacer.style.width = nLeftOver + "px";

	ContentArea.style.width = nPageWidth + "px";
	//LeftPane.style.width = nLeftPaneClientWidth + "px";
	if (RightSpacer.style.backgroundImage.indexOf("/PageRShad.gif") != -1)
		return;

	bJustSwitchedToFloatingPage = true;
	
	document.body.style.backgroundColor = "#CCEDFF";
	ContentArea.style.backgroundColor = "#FFFFFF";
	ContentArea.style.border = "solid 1px #000000";
	ContentArea.style.padding = "2.25em";

	RightSpacer.style.overflow = "hidden";
	RightSpacer.style.backgroundImage = "url(/images_core/PageRShad.gif)";
	RightSpacer.style.backgroundRepeat = "repeat-y";
	RightSpacer.innerHTML = "<img src='/images_core/PageTRShad.gif' alt='' width='8' height='10' />";

	LeftSpacer.style.overflow = "hidden";

	BottomPageEdge.style.height = "9px";
	BottomPageEdge.style.backgroundImage = "url(/images_core/PageBShad.gif)";
	BottomPageEdge.style.backgroundRepeat = "repeat-x";
	BottomPageEdge.innerHTML = "<img src='/images_core/PageBBLShad.gif' id='PageBBLShad' alt='' width='12' height='9' />";

	BottomRightPageEdge.innerHTML = "<img src='/images_core/PageBBRShad.gif' id='PageBBRShad' alt='' width='8' height='9' />";
}

function RemoveFloatingPage()
{
	bFloatingPage = false;
	
	var ContentArea = document.getElementById("ContentArea");
	
	if (ContentArea.style.borderWidth == "")
		return;

	var LeftSpacer = document.getElementById("LeftSpacer");
	var RightSpacer = document.getElementById("RightSpacer");
	var BottomPageEdge = document.getElementById("BottomPageEdge");
	var BottomRightPageEdge = document.getElementById("BottomRightPageEdge");

	document.body.style.backgroundColor = "#FFFFFF";

	ContentArea.style.backgroundColor = "";
	ContentArea.style.border = "";
	ContentArea.style.padding = "12px";

	RightSpacer.style.width = "";
	RightSpacer.style.overflow = "hidden";
	RightSpacer.style.backgroundImage = "";
	RightSpacer.style.backgroundRepeat = "";
	RightSpacer.innerHTML = "";

	LeftSpacer.style.width = "";
	LeftSpacer.style.overflow = "";

	BottomPageEdge.style.height = "";
	BottomPageEdge.style.backgroundImage = "";
	BottomPageEdge.style.backgroundRepeat = "";
	BottomPageEdge.innerHTML = "";

	BottomRightPageEdge.innerHTML = "";
}

function ArrangeAdvertisements()
{
	var Leaderboard = $('.Leaderboard728')[0];

	// Advertisements
	if (Leaderboard != null)
	{
		var bBodyWiderThanAd = (document.body.clientWidth > 790);
		var tdTopAd = document.getElementById("tdTopAd");
		var tdLeftPaneAdRow = document.getElementById("tdLeftPaneAdRow");
		var nMaxBodyWForAd = 810; // Puts it safely out of reach of 1024 (1046 on Chrome/Win7). Min 765 for all other browsers but WebKit: 790

		if (MainBody != null && tdTopAd != null)
		{
			if (MainBody.clientWidth > nMaxBodyWForAd && tdTopAd.colSpan != "1")
			{
				var tdSib = FindSiblingWithTag(tdTopAd, false);

				while (tdSib != null)
				{
					tdSib.style.display = "";
					tdSib = FindSiblingWithTag(tdSib, false);
				}

				FindSiblingWithTag(tdTopAd, true).style.display = "";

				tdTopAd.colSpan = "1";
			}
			else if (MainBody.clientWidth <= nMaxBodyWForAd && tdTopAd.colSpan != "5")
			{
				var tdSib = FindSiblingWithTag(tdTopAd, false);

				while (tdSib != null)
				{
					tdSib.style.display = "none";
					tdSib = FindSiblingWithTag(tdSib, false);
				}

				FindSiblingWithTag(tdTopAd, true).style.display = "none";

				tdTopAd.colSpan = "5";
			}
		}


		if (bBodyWiderThanAd && Leaderboard.style.display == "none")
			Leaderboard.style.display = "";
		else if (!bBodyWiderThanAd && Leaderboard.style.display == "")
			Leaderboard.style.display = "none";

		if (tdLeftPaneAdRow != null && LeftPane != null && 
			LeftPane.style.display == "none" && tdLeftPaneAdRow.style.display != "none")
			tdLeftPaneAdRow.style.display = "none";
	}
	
	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");
		}
	}
}


// Splitter
var dragObject = null;
var nOneInThree = 0;
var SplitDormantColor = "#F9F9F9";
var SplitHoverColor = "#EEEEEE";

function SplitterResize(ev)
{
	if (dragObject != null && dragObject.id != null && 
		dragObject.id == "LeftSplitter" && nOneInThree == 0)
	{
		ev = ev || window.event;
		var mousePos = mouseCoords(ev);

		var nNewWidth = (mousePos.x - 6) > 0 ? (mousePos.x - 6) : 0;

		if (nNewWidth <= nMaxLeftPaneWidth) // 182
			HideShowLeftPane(true);
		else
		{
			if (LeftPane.style.display == "none")
				HideShowLeftPane(false);

			LeftPane.style.width = nNewWidth + "px";
		}

		nOneInThree = 2;

		if (window.getSelection && window.getSelection() != null)
			window.getSelection().removeAllRanges();
		else if (document.selection && document.selection.createRange().text)
			document.selection.empty();

		BuildFloatingPage(true);
		BuildHeader();
		ArrangeAdvertisements();
	}
	else if (dragObject != null)
		nOneInThree = nOneInThree - 1;
}

function BeginResize(ev)
{
	ev = ev || window.event;
	var target = ev.target || ev.srcElement;
	dragObject = target;
	document.body.style.cursor = "w-resize";
	target.style.backgroundColor = SplitHoverColor;
}

function EndSplitterResize(e)
{
	if (dragObject != null && dragObject.id != null &&
		dragObject.id == "LeftSplitter")
	{
		document.body.style.cursor = "auto";
		dragObject.style.backgroundColor = "";
		
		SetSplitterCookies();
		nOneInThree = 0;
		SplitterResize(e);
		dragObject = null;
	}
}

function SplitterMouseOver(ev)
{
	if (dragObject == null)
	{
		ev = ev || window.event;
		var target = ev.target || ev.srcElement;
		target.style.backgroundColor = SplitHoverColor;
		target.style.cursor = "w-resize";
	}
}

function SplitterMouseOut(ev)
{
	if (dragObject == null)
	{
		ev = ev || window.event;
		var target = ev.target || ev.srcElement;
		target.style.backgroundColor = ""; //SplitDormantColor;
		target.style.cursor = "auto";
	}
}

function ExpandCollapseClick(target)
{
	var bCollapsed = (LeftPane.style.display == "none");
	HideShowLeftPane(!bCollapsed);
	BuildFloatingPage(true);
	BuildHeader();
	ArrangeAdvertisements();
}

function ExpandCollapseHoverOut(target, bHover)
{
	var strSrc = (target.src.indexOf("Left") != -1) ?
			"/images_core/Expand_Collapse-Left" : "/images_core/Expand_Collapse-Right";

	if (bHover)
		strSrc = strSrc + "-Hover";

	target.src = strSrc + ".gif";
}

function HideShowLeftPane(bHide, bPermanent)
{
	var LeftColExpand = document.getElementById("LeftColExpand");

	if (bPermanent == null)
		bPermanent = true;

	AddSplitterButton(!bHide, LeftColExpand, bHide);

	if (bHide)
		LeftPane.style.display = "none";
	else
		LeftPane.style.display = "";

	FindSiblingWithTag(LeftColExpand, false).style.display = bHide ? "none" : "";
	document.getElementById("LeftPaneBelow").style.display = bHide ? "none" : "";
	
	if (bPermanent)
		SetSplitterCookies();

	return LeftPane.clientWidth;
}

function AddSplitterButton(bLeft, target, bCollapsed)
{
	var strImage = bLeft ? "/images_core/Expand_Collapse-Left.gif" : "/images_core/Expand_Collapse-Right.gif";
	var strTitle = bCollapsed ? "Click to show left pane." : "Click to hide left pane.";

	target.innerHTML = '<img src="' + strImage + '" alt="" title="' + strTitle + '" width="12" height="11" onmouseover="ExpandCollapseHoverOut(this, true);" onmouseout="ExpandCollapseHoverOut(this, false)" onclick="ExpandCollapseClick(this);" />';
}

function SetSplitterCookies()
{
	var strLeftPaneDisplay = (LeftPane.style.display == "none") ? "false" : "true";
	var bWidthSet = (LeftPane.style.width != null && LeftPane.style.width != "");
	var strLeftPaneCookie = GetCookie("LeftPaneDisplay");

	// Don't set the LeftPaneDisplay cookie unless it differs from default.
	if (bWidthSet || (!bWidthSet && (strLeftPaneDisplay == "false" ||
		(strLeftPaneDisplay == "true" && strLeftPaneCookie != null))))
		AddCookie("LeftPaneDisplay", strLeftPaneDisplay);

	// Don't set when hidden!
	if (bWidthSet && strLeftPaneDisplay == "true")
		AddCookie("LeftPaneWidth", LeftPane.style.width);
}

$(document).mousemove(SplitterResize);
$(document).mouseup(EndSplitterResize);
$(document).mousemove(PictBoxResize);
$(document).mouseup(EndPictBoxResize);

function InitializeSplitters()
{
	if (LeftPaneID == null)
		return;

	LeftPane = document.getElementById(LeftPaneID);

	if (LeftPane == null)
		return;

	var strLeftPaneDisplay = GetCookie("LeftPaneDisplay");

	var bDisplayLeftPane = !(strLeftPaneDisplay != null && strLeftPaneDisplay == "false");

	if (!bDisplayLeftPane)
		HideShowLeftPane(LeftPane, false);

	AddSplitterButton(bDisplayLeftPane, document.getElementById("LeftColExpand"), !bDisplayLeftPane);

	var strLeftPaneWidth = GetCookie("LeftPaneWidth");

	if (strLeftPaneWidth != null)
		LeftPane.style.width = strLeftPaneWidth;

	if (!IsWebKit())
	{
		var LeftSplitter = document.getElementById("LeftSplitter");
		LeftSplitter.onmousedown = BeginResize;
		LeftSplitter.onmouseover = SplitterMouseOver;
		LeftSplitter.onmouseout = SplitterMouseOut;
		LeftSplitter.style.backgroundColor = ""; //SplitDormantColor;
	}
}


// Text Size

function InitializeTextSizeControls()
{
	if (TextSizePrefix == null)
		return;
	
	var TextSize_Smaller = document.getElementById(TextSizePrefix + "TextSizeSmaller"),
		TextSize_Larger = document.getElementById(TextSizePrefix + "TextSizeLarger");

	if (TextSize_Larger == null || TextSize_Smaller == null || 
		document.body.style.fontSize == null)
		return;

	TextSize_Larger.onclick = function() { IncrementTextSize(true); return false; };
	TextSize_Smaller.onclick = function() { IncrementTextSize(false); return false; };

	if (!IsIE)
	{
		TextSize_Larger.type = "button";
		TextSize_Smaller.type = "button";
	}
	//TextSize_Larger.href = "#";
	//TextSize_Smaller.href = "#";

	var nSize = GetTextSize();

	if (nSize != .85) // Default
		document.body.style.fontSize = nSize + "em";

	if (document.getElementById("TextSize_Val") != null)
		document.getElementById("TextSize_Val").innerHTML = nSize.toFixed(2) + "x";
}

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);
	fm = mf();

	//ResizeTasks(true);
}

function ChangeTextSize(nSize, bSetCookie)//, bEstimateFontWidth)
{
	//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));

//	if (bEstimateFontWidth)
//	{
//		if (fm > 7 && fm < 9)
//		{
//			if (nSize == .75)
//				fm -= 1;
//			else if (nSize == .95)
//				fm += 1;
//		}
//	}

	document.body.style.fontSize = nSize + "em";

	if (document.getElementById("TextSize_Val") != null)
		document.getElementById("TextSize_Val").innerHTML = nSize.toFixed(2) + "x";
}

function GetTextSize()
{
	var nSize = new Number(GetCookie("TextSize"));
	if (nSize == "NaN" || nSize == 0)
		nSize = GetTextSizeForWidth();

	if (nSize > 4)
	{
		nSize = GetTextSizeForWidth();
		DeleteCookie("TextSize");

		ChangeTextSize(nSize, true);
	}

	return nSize;
}

function GetTextSizeForWidth()
{
	var nWindowWidth = document.body.clientWidth;
	if (nWindowWidth >= 1200)
		return .95;
	else if (nWindowWidth > 804) //796
		return .85;
	else
		return .8;
}


// Events
function mouseCoords(ev)
{
	if (ev.pageX || ev.pageY)
	{
		var XPos = (ev.pageX >= 0) ? ev.pageX : 0;
		var YPos = (ev.pageY >= 0) ? ev.pageY : 0;
		return { x: XPos, y: YPos };
	}
	return {
		x: ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y: ev.clientY + document.body.scrollTop - document.body.clientTop
	};
}

// Controls
function CopyToControl(sourceControl, destControl, bURL)
{
	if (sourceControl == null || destControl == null)
		return;

	if (sourceControl.value != null && destControl.value != null)
	{
		if (bURL != null && bURL)
			destControl.value = MakeStringURLSafe(sourceControl.value);
		else
			destControl.value = sourceControl.value;
	}

	return true;
}

function MakeStringURLSafe(strSource)
{
	// Characters to replace with a dash
	//var strSafe = strSource.replace(/\s|_|:|\/|,/gi, "-");
	var strSafe = strSource.replace(/\s|_|:|,/gi, "-");
	
	// Characters to drop
	strSafe = strSafe.replace(/\.|\?|<|>|~|`|!|#|\$|%|\^|\*|\(|\)|=|\[|\]|\{|\}|;|"|'|\\|\|/gi, "");

	// Strip double dashes
	var re = new RegExp("--", "g");
	
	while (strSafe.indexOf("--") != -1)
		strSafe = strSafe.replace(re, "-");

	// Characters to replaces with words
	strSafe = strSafe.replace(/&/gi, "and");
	strSafe = strSafe.replace(/\+/gi, "plus");
	return strSafe.replace(/@/gi, "at").toLowerCase();
}



// DOM
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;
}

//UI
function StrikeTheMatch()
{
	if (typeof (document.body.clientWidth) != 'number')
		return;

	window.onresize = function() { ResizeTasks(); };

	InitializeSplitters();
	InitializeTextSizeControls();
	fm = mf();
	MainBody = document.getElementById("ContentArea");

	ResizeTasks(true);
}

function ResizeTasks()
{
	bWideLayout = (document.body.clientWidth >= 910);

	if (!bInitialResize)
		ChangeTextSize(GetTextSize(), false);
	else
		bInitialResize = false;

	BuildFloatingPage();
	BuildHeader();
	InitializePictBoxes();
	SizeFluidGadgets();
	ArrangeAdvertisements();
	StretchHeaderGradient();

	for (var i = 0; i < ResizeFuncs.length; i++)
		ResizeFuncs[i].call();
}

function StretchHeaderGradient()
{
	var ContentArea = document.getElementById("ContentArea");
	
	if (ContentArea == null)
		return;
	
	var Gadgets = $(".std_r_gadget_fw");
	
	if (Gadgets == null || Gadgets.length == 0)
		return;

	var nGadgetMarginLeft = Math.round(ppem * 2.25);
	var nFudgeFactor = bInitialResize ? 0 : 10;
	var nLeftSideWidth = ContentArea.clientWidth - Gadgets.outerWidth() - nGadgetMarginLeft - 36 - nFudgeFactor;

	var Headers = $(".H2Icon");

	if (Headers == null || Headers.length == 0)
		return;

	for (var i = 0; i < Headers.length; i++)
		Headers[i].style.width = nLeftSideWidth + "px";

}

function ProperlySizeRadWindow(currentWindow)
{
	if (IsIE())
	{
		currentWindow.autoSize(false);

		var bounds = currentWindow.getWindowBounds();

		if (bounds != null && bounds.height < currentWindow.BrowserWindow.document.body.clientHeight)
		{
			//alert("RadWindow- W: " + bounds.width + " H: " + bounds.height + "\nParent- W: " + nParentWidth + " H: " + nParentHeight);					var nWidthPadding = bIE ? .05 : .02;
			currentWindow.set_height(bounds.height + Math.floor(bounds.height * .05));
			currentWindow.set_width(bounds.width + 25);
		}
	}
}

function InitializePictBoxes()
{
	if (dragObject != null)
		return;
		
	if (!IsWebKit() && !IsOpera()) // Dragging the border in WebKit is problematic and Opera only uses medium quality resampling
	{
		if ($("#ContentArea").length > 0)
		{
			if (!IsFirefox())
				nMaxPicWidth = $("#ContentArea").width();
			else
				nMaxPicWidth = $("#ContentArea").width() - 11;
		}
		else
			nMaxPicWidth = 600;

		var PictBoxes = $(".pictbox");
		
		if (PictBoxes == null || PictBoxes.length == 0)
			return;

		for (var i = 0; i < PictBoxes.length; i++)
		{
			if (PictBoxes[i].className.indexOf("fr") != -1 && // Only right-floating PictBoxes are sizeable.
				PictBoxes[i].className.indexOf("gadget_fw") != -1 && // Must have a "fluid width" class.
				$(PictBoxes[i]).find(".pictbox_pict") != null)
			{
				SetPictWidthFromCookie(PictBoxes[i]);
				
				// Find all the parts of the Picture Box
				var PictBoxPict = $(PictBoxes[i]).find(".pictbox_pict")[0];
				var PictBoxPictImg = $(PictBoxPict).find("img")[0];
				
				var PictBoxBody = null;

				if ($(PictBoxes[i]).find(".pictbox_body") != null)
					PictBoxBody = $(PictBoxes[i]).find(".pictbox_body")[0];

				// If the window is being resized, we've already created the resize bar.
				// Just adjust the window height and move on.
				if ($(PictBoxes[i]).next()[0].className.indexOf("pictbox_pict_sizebar") != -1 ||
					$(PictBoxes[i]).next().find(".pictbox_pict_sizebar").length > 0)
				{
					// User has overridden default width if width style is set. No need to adjust
					// height as width hasn't changed.
					if (PictBoxes[i].style.width == "")
						SetPictBoxSizeBarHeight($(PictBoxes[i]).next(), $(PictBoxes[i]).find(".pictbox_pict"),
							$(PictBoxes[i]).find(".pictbox_body"));
				}
				else
				{
					if (PictBoxPictImg != null)
					{
						$(PictBoxPictImg).load(function(e)
						{
							if (dragObject == null)
							{
								var PictBox = GetPictBoxFromImage(e.currentTarget);

								if (PictBox == null)
									return;

								//								var PictBoxPict = $(PictBoxes[i]).find(".pictbox_pict")[0];
								//var PictBoxPictImg = $(PictBoxPict).find("img")[0];
								var PictBoxPict = $(PictBox).find(".pictbox_pict")[0];

								var PictBoxBody = null;

								if ($(PictBox).find(".pictbox_body") != null)
									PictBoxBody = $(PictBox).find(".pictbox_body")[0];
								// Create resize bar
								var PictSizeBarHTML = '<div class="box_padding fr';

								if (PictBoxBody == null)
									PictSizeBarHTML = PictSizeBarHTML + ' pictbox_pict_sizebar"></div>';
								else
									PictSizeBarHTML = PictSizeBarHTML +
										'"><div class="pictbox_pict_sizebar"></div><div class="pictbox_body_sizebar"></div></div>';

								var PictSizeBar = $(PictSizeBarHTML).insertAfter(PictBox);
								var PictTopSizeBar = null; //$(PictBoxes[i]).next();

								if (PictSizeBar[0].className.indexOf("pictbox_pict_sizebar") == -1)
									PictTopSizeBar = $(PictSizeBar).find(".pictbox_pict_sizebar");

								var PictBottomSizeBar = (PictBoxBody == null) ? null :
									$(PictTopSizeBar).next();


								// Transform PictBox elements
								$(PictBox).removeClass("box_padding");
								PictBox.style.marginTop = ".25em";
								PictBox.style.marginBottom = ".75em";
								PictBox.style.borderLeft = "0px";
								PictBoxPict.style.paddingLeft = "0px";

								if (PictBoxBody != null)
									PictBoxBody.style.paddingLeft = "0px";

								//SetPictBoxSizeBarHeight(PictSizeBar, PictBoxPict, PictBoxBody);

								//					if (PictBoxPictImg != null)
								//					{
								//						$(PictBoxPictImg).load(function() {
								//							if (dragObject == null)
								//							{
								//								var PictBoxPict = this.parentNode;

								while (PictBoxPict != null && PictBoxPict.className.indexOf("pictbox_pict") == -1)
									PictBoxPict = PictBoxPict.parentNode;

								var PictBoxBody = FindSiblingWithTag(PictBoxPict, true);

								var PictSizeBar = FindSiblingWithTag(PictBoxPict.parentNode, true);

								SetPictBoxSizeBarHeight($(PictSizeBar), PictBoxPict, PictBoxBody);

								if (PictSizeBar != null)
								{
									// Attach size bar events
									$(PictSizeBar).mousedown(BeginPictBoxResize);
									$(PictSizeBar).mouseover(PictBoxMouseOver);
									$(PictSizeBar).mouseout(PictBoxMouseOut);
								}
							}
						});
					}
					
				}				
			}
		}
		
		return;
	}
}

function SizeFluidGadgets()
{
	if (dragObject != null)
		return;

	var Gadgets = $('[class*="r_gadget_fw"]');

	if (Gadgets == null || Gadgets.length == 0)
		return;

	for (var i = 0; i < Gadgets.length; i++)
	{
		if (Gadgets[i].className.indexOf("pictbox") == -1)
		{
			if (Gadgets[i].style.width != "")
				Gadgets[i].style.width = "";

			Gadgets[i].style.width = (Gadgets[i].clientWidth + 8) + "px";
		}
	}
}

function SetPictBoxSizeBarHeight(PictSizeBar, PictBoxPict, PictBoxBody)
{
	if (PictBoxBody == null || PictBoxBody.length == 0)
	{
		$(PictBoxPict)[0].style.height = "";
		$(PictSizeBar[0]).height($(PictBoxPict).height() + 16);
		$(PictBoxPict).height($(PictSizeBar[0]).height() - 16);
	}
	else
	{
		// Clear height to get correct value from jQuery height()
		$(PictBoxBody)[0].style.height = "";
		$(PictBoxPict)[0].style.height = "";

		// Set Sizebar height, 16 is the padding (8px + 8px)
		$(PictSizeBar[0]).children(".pictbox_pict_sizebar").height($(PictBoxPict).height() + 16);
		$(PictSizeBar[0]).children(".pictbox_body_sizebar").height($(PictBoxBody).height() + 16);

		// Set PictBox height to a whole number to work around Firefox "subpixel rendering" issue
		// http://weblogs.mozillazine.org/roc/archives/2008/01/subpixel_layout.html
		$(PictBoxBody).height($(PictSizeBar[0]).children(".pictbox_body_sizebar").height() - 16);
		$(PictBoxPict).height($(PictSizeBar[0]).children(".pictbox_pict_sizebar").height() - 16);
	}
}

function SetPictWidthFromCookie(PictBox)//, bNewWidth) 
{
	// Width already set, possibly on server side
	// **Not needed. Won't consider resizing custom width images.
	//if (PictBox.style.width != "" && (bNewWidth == null || bNewWidth == false))
	//	return false;

	var strWidth = GetCookie(GetPictBoxWidthClass(PictBox) + "-width")

	if (strWidth != null && PictBox.style.width != strWidth)
	{
		PictBox.style.width = strWidth;
		SetBoxPadding((PictBox.clientWidth < nMaxPicWidth * .75), $(PictBox).next());
		//ShrinkOversizedPictBoxes(PictBox, PictBox.clientWidth); 
		return true; // width changed
	}

	return false;   //ShrinkOversizedPictBoxes(PictBox, PictBox.clientWidth);
}


function SetPictBoxWidth(PictBox, nWidth)
{
	if (nWidth < 50)
		nWidth = 50;

	if (nWidth > nMaxPicWidth)
		nWidth = nMaxPicWidth;

	SetBoxPadding((nWidth < nMaxPicWidth * .75), $(PictBox).next());
	PictBox.style.width = nWidth + "px";
}

// Not needed. Having px a on container means no fluid picture.
function ShrinkOversizedPictBoxes(PictBox, nWidth)
{
	var PictBoxImg = GetPictBoxImage(PictBox);

	if (PictBoxImg != null && PictBoxImg.clientWidth < nWidth - 16)
	{
		nWidth = PictBoxImg.clientWidth + 16;
		$(PictBox).removeClass(GetPictBoxWidthClass(PictBox));
		$(PictBoxImg).removeClass("w100");
		PictBoxImg.style.width = PictBoxImg.clientWidth + "px";
		SetBoxPadding((nWidth < nMaxPicWidth * .75), $(PictBox).next());
		PictBox.style.width = nWidth + "px";
		return true;
	}

	return false;

}

function PictBoxMouseOver(e)
{
	if (dragObject == null && e.currentTarget != null)
		e.currentTarget.style.cursor = "w-resize";
}

function PictBoxMouseOut(e)
{
	if (dragObject == null && e.currentTarget != null)
		e.currentTarget.style.cursor = "auto";
	else
		e.currentTarget.style.cursor = "w-resize";
}

function BeginPictBoxResize(e)
{
	// Get PictBox
	dragObject = FindSiblingWithTag(e.currentTarget, false);

	if (dragObject != null)
	{
		document.body.style.cursor = "w-resize";
		dragObject.nStartWidth = dragObject.clientWidth;
		dragObject.nStartXPos = e.pageX;
	}
}

function PictBoxResize(e)
{
	if (dragObject != null && dragObject.className.indexOf("pictbox") != -1)
	{
		//$(dragObject).find(".pictbox_body")[0].innerHTML = "e.pageX: " + e.pageX + " PictBox.style.width: " + dragObject.style.width + " Start Width: " + dragObject.nStartWidth + " Start Pos: " + dragObject.nStartXPos + " New Width: " + (dragObject.nStartWidth - (e.pageX - dragObject.nStartXPos));
		SetPictBoxWidth(dragObject, dragObject.nStartWidth - (e.pageX - dragObject.nStartXPos));
		SetPictBoxSizeBarHeight($(dragObject).next(), $(dragObject).find(".pictbox_pict"),
			$(dragObject).find(".pictbox_body"));

		if (window.getSelection && window.getSelection() != null)
			window.getSelection().removeAllRanges();
		else if (document.selection && document.selection.createRange().text)
			document.selection.empty();
	}
}

function SetBoxPadding(bUseBoxPadding, SizeBar)
{
	if (bUseBoxPadding && SizeBar[0].className.indexOf("box_padding_ud") != -1)
	{
		$(SizeBar).addClass("box_padding");
		$(SizeBar).removeClass("box_padding_ud");
		$(SizeBar).next().removeClass("cb");
	}
	else if (!bUseBoxPadding && SizeBar[0].className.indexOf("box_padding_ud") == -1)
	{
		$(SizeBar).removeClass("box_padding");
		$(SizeBar).addClass("box_padding_ud");
		$(SizeBar).next().addClass("cb");
	}
}

function EndPictBoxResize(e)
{
	if (dragObject != null && dragObject.className.indexOf("pictbox") != -1)
	{
		document.body.style.cursor = "auto";
		PictBoxResize(e);

		AddCookie(GetPictBoxWidthClass(dragObject) + "-width", dragObject.style.width);

		dragObject = null;
		
		// Resize the rest of the PictBoxes to new width
		var PictBoxes = $(".pictbox");

		for (var i = 0; i < PictBoxes.length; i++)
		{
			if (PictBoxes[i].className.indexOf("fr") != -1 && // Only right-floating PictBoxes are sizeable.
				$(PictBoxes[i]).find(".pictbox_pict") != null)
			{
				if (SetPictWidthFromCookie(PictBoxes[i], true))
					SetPictBoxSizeBarHeight($(PictBoxes[i]).next(), $(PictBoxes[i]).find(".pictbox_pict"),
						$(PictBoxes[i]).find(".pictbox_body"));
			}
		}
	}
}

function GetPictBoxWidthClass(PictBox)
{
	if (PictBox == null || PictBox.className == null)
		return "";

	var classes = PictBox.className.split(" ");

	for (var i = 0; i < classes.length; i++)
	{
		if (classes[i].indexOf("r_gadget_fw") != -1)
			return classes[i];
	}

	return "";
}

function GetPictBoxImage(PictBox)
{
	return PictBoxImg = $(PictBox).find("img")[0];
}

function GetPictBoxFromImage(PictBoxImage)
{
	while (PictBoxImage != null)
	{
		if (PictBoxImage.className.indexOf("pictbox ") != -1 ||
			PictBoxImage.className.indexOf("pictbox\"") != -1)
			return PictBoxImage;

		PictBoxImage = PictBoxImage.parentNode;
	}

	return null;
}

// Step-by-Step
function StepItemHover(Element)
{
	Element.style.backgroundColor = '#FFFF99';
	Element.style.border = '#333333 thin solid';
}

function StepItemOut(Element)
{
	Element.style.border = '#FFFF00 solid thin';
	Element.style.backgroundColor = '#FFFF00';
}

function mf()
{
	var LoremIpsum = "Lorem ipsum dolor sit amet, consectetur";// adipiscing elit.";// Integer vulputate consectetur aliquam.";// Pellentesque accumsan nisl sit amet eros porttitor id laoreet turpis tincidunt. Phasellus eget scelerisque urna.";
	var Widths = [];
	var TotalWidth = 0;

	for (var i = 0; i < LoremIpsum.length; i++)
	{
		var S = CreateMFDiv(LoremIpsum.charAt(i));
		document.body.insertBefore(S, document.body.lastChild);
		Widths.push(S.offsetWidth);
		TotalWidth = TotalWidth + S.offsetWidth;
		document.body.removeChild(S);
	}

	var S = CreateMFDiv('W');
	document.body.insertBefore(S, document.body.lastChild);
	ppem = S.offsetHeight; //$(S).outerHeight();
	document.body.removeChild(S);
		
	//return (Math.floor(TotalWidth / Widths.length) + 2);
	return ((TotalWidth / Widths.length) + 2);
}

function CreateMFDiv(Let)
{
	var MFDiv = document.createElement("div");

	MFDiv.innerHTML = '<span class="mf">' + Let + '</span>';

	return MFDiv.firstChild;
}

// Cookies
function GetCookie(sName)
{
	var aCookie = document.cookie.split("; ");
	for (var i = 0; i < aCookie.length; i++)
	{
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0])
			return unescape(aCrumb[1]);
	}

	return null;
}

function AddCookie(strKey, strValue, expires, strPath)
{
	var d = new Date();
	d.setTime((d.getTime() + 31536000000));

	if (strPath == null)
		strPath = "/";

	document.cookie = strKey + "=" + strValue + "; expires=" + d.toGMTString() + "; path=" + strPath;
}

function DeleteCookie(strKey)
{
	var d = new Date();
	d.setTime((d.getTime() - 86400000));
	document.cookie = strKey + "= ; expires=" + d.toGMTString();
}

// Browser Detection
function IsWebKit()
{
	return (navigator.userAgent.indexOf("WebKit") != -1);
}

function IsFirefox()
{
	return (navigator.userAgent.indexOf("Gecko/") != -1);
}

function IsOpera()
{
	return (navigator.userAgent.indexOf("Opera") != -1);
}

function IsIE()
{
	return (navigator.userAgent.indexOf("; MSIE ") != -1);
}

// URLs
function MakeURLRelative(strURL)
{
	if (strURL.indexOf("http://") != -1)
	{
		var nPostTLDSlash = 0;
		for (var i = 0, nNextIndex = 0; i < 3; i++)
		{
			nNextIndex = strURL.substring(nPostTLDSlash).indexOf("/");
			if (nNextIndex == -1)
				break;

			nPostTLDSlash += nNextIndex + 1;
		}

		return strURL.substring(nPostTLDSlash - 1);
	}
	else
		return strURL;
}

// Thanks: http://www.bloggingdeveloper.com/post/JavaScript-QueryString-ParseGet-QueryString-with-Client-Side-JavaScript.aspx
// jQuery version, when ready to embed: http://plugins.jquery.com/project/query-object
function getQueryString(key, default_)
{
	if (default_ == null) default_ = "";
	key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
	var qs = regex.exec(window.location.href);
	if (qs == null)
		return default_;
	else
		return qs[1];
}

// Util
function GetWordCount(strText)
{
	if (strText == null || strText == "" || strText.split == null)
		return 0;

	var Words = strText.split(" ");

	if (Words == null)
		return 0;

	return Words.length;
}

function UpdateWordCountLabel(LabelID, strText, nMaxCount)
{
	if (LabelID != null)
	{
		var Label = document.getElementById(LabelID);

		if (Label != null)
		{
			var nCount = GetWordCount(strText);
			var nWarningCount = nMaxCount * .9;

			if (nCount < nWarningCount)
				Label.style.color = "#000000";
			else if (nCount >= nWarningCount && nCount <= nMaxCount)
				Label.style.color = "#FF7F00";
			else
				Label.style.color = "#FF0000";

			Label.innerHTML = "<em>Word count: " + nCount + ". Max: " + nMaxCount + ".</em>";
		}
	}
}

// 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 getStyle(x, styleProp)
{
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x, null).getPropertyValue(styleProp);
	return y;
}

