// Takes numerious buttons and applies centring ajustment to all.
function adjustIEButtons (inBtnArray)
{
	if (browser.isMicrosoft && inBtnArray.length)
	{
		for (var i = 0; i < inBtnArray.length; i++)
		{
			adjustBtnInternal (inBtnArray[i]);
		}
	}
}

function adjustIEOneBtn (inButton)
{
	if (browser.isMicrosoft)
	{
		adjustBtnInternal (inButton);
	}
}

// vertical-align = middle puts buttons w/ border too high up in IE, but middle in Mozilla.
function adjustBtnInternal (inButton)
{
	if (inButton)
	{
		inButton.style.top = "3px";
	}
}

function setVisibleOn (inElemArray)
{
	if (inElemArray.length)
	{
		for (var i = 0; i < inElemArray.length; i++)
		{
			inElemArray[i].style.visibility = "visible";
		}
	}
}

function resize()
{
	var swh =  document.getElementById("ScreenWidthHidden");
	var shh =  document.getElementById("ScreenHeightHidden");
	
	var w = 0, h = 0;
	
	if( typeof( window.innerWidth ) == 'number' )
	{
		// W3C
		w = window.innerWidth;
		h = window.innerHeight;
	}
	else if (document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
	{
		// IE 6+ in 'standards compliant mode'
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	}
	else if (document.body && ( document.body.clientWidth || document.body.clientHeight ) )
	{
		// IE 4 compatible
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}
	
	swh.value = w;
	shh.value = h;
}

function openPopupWindow (url, name)
{
	popup = window.open(url, name, 'width=450,height=580,toolbar=0,top=20,left=20,location=0,status=0,menubar=0,scrollbars=1,resizable=0');
	popup.focus();
}

// Scrolls window to show elem.
function scrollToElement (elem)
{
	if (typeof elem == "string") elem = document.getElementById(elem);
	
	var elemPos = getAbsPos (elem);
	window.scroll (0, elemPos[1]);
}

/* Floating box functions */

// onmouseover handler for sender AREA. Set in Code-behind - Render()
// id is poi id, name is sender header, encData is encoded data of body, 
// srctype is 'cover' or 'sender', x/y are local area-tag coordinates of poi.
function senderAreaMouseOver (id, name, encData, srctype, x, y)
{
	// prevent showing float box when doing rubberband zoom.
	var rubberBand = document.getElementById("MapImageCntl_rubberBand");
	if (rubberBand.style.visibility === 'visible')
	{
		return true;
	}
	var bodyList;	// array of float box bodies.
	if (srctype == "cover")
	{
		bodyList = new Array ("SFloatBoxDABPart");
	}
	else if (srctype == "sender")
	{
		// this order must match encoding order in NorkringCommon.cs
		bodyList = new Array ("SFloatBoxDABPart", "SFloatBoxFMPart", 
			"SFloatBoxTVPart", "SFloatBoxDTTPart");
	}
	setFloatingBox(id, name, encData, bodyList, x, y);
}

// onmouseoout handler for poi AREA. Set in Code-behind - Render()
function senderAreaMouseOut ()
{
	var floatBox = document.getElementById ("SenderFloatBoxDiv");
	floatBox.style.visibility = "hidden";
}

// onmouseover handler for floating box div (set in ASPX-file).
function SFloatBoxMouseOver (evt)
{
	var floatBox = document.getElementById ("SenderFloatBoxDiv");
	floatBox.style.visibility = "visible";
}

// onmouseoout handler for floating box div (set in ASPX-file).
function SFloatBoxMouseOut (evt)
{
	var floatBox = document.getElementById ("SenderFloatBoxDiv");
	floatBox.style.visibility = "hidden";
}

// Creates the sender floating box. Loads in data, positions it and shows it.
function setFloatingBox (id, name, encData, bodyList, x, y)
{
	var floatBox = document.getElementById ("SenderFloatBoxDiv");
	var boxSenderId = document.getElementById ("SFloatBoxSenderId");
	// don't reload the same data.
	if (boxSenderId.innerHTML != id)
	{
		loadSFloatData (name, encData, bodyList);
	}
	boxSenderId.innerHTML = id;
	
	positionSFloatBox (floatBox, x, y);
	floatBox.style.visibility = "visible";
}

// take the element object boxElem and the source coordinates x/y, and places
// the element appropriately. Source x/y are relative to map.
function positionSFloatBox (boxElem, srcx, srcy)
{
	if (typeof boxElem == "string") boxElem = document.getElementById(boxElem);
	
	var mapregion = document.getElementById ("MapImageOuterDiv");
	var mapWidth = mapregion.offsetWidth;
	var mapHeight = mapregion.offsetHeight;
	
	// float box is absolute within relative mapregion, so style.left/top are compared to region,
	// but comparison to screen is absolute.
	if (srcx <= (mapWidth / 2) )
	{
		boxElem.style.left = srcx - 5;
	}
	else
	{
		boxElem.style.left = srcx - boxElem.offsetWidth + 5;
	}
	if (srcy <= (mapHeight / 2) )
	{
		boxElem.style.top = srcy - 5;
	}
	else
	{
		boxElem.style.top = srcy - boxElem.offsetHeight + 5;
	}
}

// takes the data fields and load them into the box.
// encData is body parts, rows and cells. ^ separates bodies,
// ~ separates rows, # separates cells.
function loadSFloatData (name, encData, bodyList)
{
	var boxHeader = document.getElementById ("SFloatBoxHeader");
	boxHeader.innerHTML = name;
	var bodyData = encData.split ("^");
	var bodyDataLen = bodyData.length;
	for (var i = 0; i < bodyList.length; i++)
	{
		if (bodyList[i] == null)
		{
			continue;
		}
		var ithBody = document.getElementById (bodyList[i]);
		if (i < bodyDataLen && bodyData[i].length > 0)
		{
			// remove existing rows
			var numExRows = ithBody.rows.length;
			// skip row 1, the header.
			// can't use an index from 1 -> because the list gets smaller.
			// deleteRow didn't delete the correct way.
			for (var exIter = numExRows-1; exIter > 0; exIter--)
			{
				var exRow = ithBody.rows[exIter];
				ithBody.removeChild (exRow);
			}
			var dataRows = bodyData[i].split ("~");
			for (var j = 0; j < dataRows.length; j++)
			{
				var jthRow = document.createElement ("tr");
				var dataCells = dataRows[j].split ("#");
				for (var k = 0; k < dataCells.length; k++)
				{
					var cellText = dataCells[k];
					kthCell = document.createElement ("td");
					// unary + is a to-number converter
					// (http://www.jibbering.com/faq/faq_notes/type_convert.html).
					if (isNaN (+cellText))
					{
						kthCell.className = "SFloatBoxDataCell";
					}
					else
					{
						kthCell.className = "SFloatBoxDataCell SFloatBoxNumCell";
					}
					kthCell.appendChild (document.createTextNode (cellText) );
					jthRow.appendChild (kthCell);
				}
				ithBody.appendChild (jthRow);
			}
			if (dataRows.length > 0)
			{
				ithBody.style.display = "";		// default style - apparently 'table-row-group'
			}
			else
			{
				ithBody.style.display = "none";
			}
		}
		else
		{
			ithBody.style.display = "none";
		}
	}
}

// Notify ScriptManager that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

