/*
 * SU scripts
 *
 * Modified by Per Olofsson, 2009
 */

// ----- Set onload functions:
window.onload = function() {
	// Init top search
	initTopSearch();

	// Init top menu highlights
	initTopMenuHighlights();
	// Init menu highlights
	initMenuHighlights();

	// Split topic list into columns
	splitTopicsIntoColumns();

	// Disable for now - Pelle
	// Set footer
	//setFooter();
}

// ----- Get window height:
// Get document client height
function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

// --------------------------------------------------------------------------- SEARCH ----------------------------------------------------------------------------
// ----- Init top search:
// Enable search field lead text
var sTopSearchLead = '';
function initTopSearch() {
	//get lead text
	sTopSearchLead = document.getElementById('TxtHeadSearch').value;
	//attach functions
	document.getElementById('TxtHeadSearch').onfocus = function() {
		searchFocus(this);
	}
	document.getElementById('TxtHeadSearch').onblur = function() {
		searchBlur(this);
	}
}
//Clear field on focus
function searchFocus(e) {
	e.value = '';
	e.style.color = '#000000';
}
//Reset lead when leaving empty field
function searchBlur(e) {
	e.style.color = '#b1b1b1';
	if (e.value == '') {
		e.value = sTopSearchLead;
	}
	else {
		return false;	
	}
	
}
// -------------------------------------------------------------------------- /SEARCH ----------------------------------------------------------------------------

// ---------------------------------------------------------------------------- MENU -----------------------------------------------------------------------------
// ----- Init top menu highlights:
// Enable top menu highlights
function initTopMenuHighlights() {
	if (document.getElementById('HeadRowMenu')) {
		//get menu divs
		var cLIs = document.getElementById('HeadRowMenu').getElementsByTagName('li');
		for (var i=0;i<cLIs.length;i++) {
			if (cLIs[i].className != 'active') {
				//set; onmouseover
				cLIs[i].onmouseover = topMenuElementHover;
				//set; onmouseout
				cLIs[i].onmouseout = topMenuElementLeave;
			}

			////set; onclick
			if (ie6) cLIs[i].onclick = topMenuElementClick;

			if (ie6 &&
			    cLIs[i].getElementsByTagName('a').length > 0) {
				cLIs[i].style.cursor = 'pointer';
			}
		}
	}
}
//Enable onmouseover
function topMenuElementHover(e) {
	this.style.backgroundColor = '#ccd5df';
}
//Enable onmouseout
function topMenuElementLeave(e) {
	this.style.backgroundColor = '';
}
//Enable onclick
function topMenuElementClick(e) {
	this.getElementsByTagName('a')[0].focus();
	location.href = this.getElementsByTagName('a')[0].href;
}

// ----- Init menu highlights:
// Enable menu highlights
function initMenuHighlights() {
	if (document.getElementById('Menu')) {
		//get menu divs
		var cDivs = document.getElementById('Menu').getElementsByTagName('div');
		for (var i=0;i<cDivs.length;i++) {
			//set; onmouseover
			cDivs[i].onmouseover = menuElementHover;
			//set; onmouseout
			cDivs[i].onmouseout = menuElementLeave;

			//set; onclick
			if (ie6) cDivs[i].onclick = menuElementClick;

			if (ie6 &&
			    cDivs[i].getElementsByTagName('a').length > 0) {
				cDivs[i].style.cursor = 'pointer';
			}
		}
	}
}

//Enable onmouseover
function menuElementHover() {
	var e = this;
	if (e.className == 'selected') {
		return false;
	}
	else {
		if (e.parentNode.parentNode.parentNode.parentNode.tagName != 'UL') {
			e.style.backgroundColor = '#33597f';
		}
		else {
			if (e.className == 'opener') {
				e.style.backgroundColor = '#99acbf';
			}
			else {
				e.style.backgroundColor = '#ccd5df';
			}
		}
	}
}
//Enable onmouseout
function menuElementLeave() {
	this.style.backgroundColor = '';
}
//Enable onclick
function menuElementClick() {
	this.getElementsByTagName('a')[0].focus();
	window.location.href = this.getElementsByTagName('a')[0].href;
}
// --------------------------------------------------------------------------- /MENU -----------------------------------------------------------------------------

// --------------------------------------------------------------------------- LAYOUT ----------------------------------------------------------------------------
// ----- Set page footer:
// Enable set footer for Internet Explorer 7.0 and above
function setFooter() {
	if (document.getElementById('footer')) {
		if (is_ie && !ie_below7) {
			//set footer
			moveFooter();
			//enable set footer on resize
			window.onresize = function() {
				moveFooter();
			}
		}
	}
} 
// Move footer to page bottom
function moveFooter() {
	if (!ie6 && !ie7) return;

	if (document.getElementById) {
		var windowHeight = getWindowHeight();
		if (windowHeight > 0) {
			//adjust content height on resize
			if (event && event.type == 'resize') {
				var newContentHeight = (document.body.offsetHeight - (document.getElementById('footer').offsetHeight + 100));
				if (newContentHeight > document.getElementById('columns').offsetHeight) {
					document.getElementById('content').style.height = (document.body.offsetHeight - (document.getElementById('footer').offsetHeight + 100)) + 'px';
				}
			}
			//move footer
			var contentHeight = document.getElementById('content').offsetHeight;
			var footerElement = document.getElementById('footer');
			var footerHeight  = footerElement.offsetHeight;
			if (windowHeight - (contentHeight + footerHeight) >= 0) {
				footerElement.style.position = 'absolute';
				footerElement.style.top = (windowHeight - footerHeight) + 'px';
			}
			else {
				footerElement.style.position = 'static';
			}
		}
	}
}
// ----- Split topic list into columns:
// If document contains a topic list, split it into three columns
function splitTopicsIntoColumns() {
	if (document.getElementById('TopicList')) {
		//break list into columns
		//get all lists
		var cListDivs = document.getElementsByTagName('div');
		for (var i=0;i<cListDivs.length;i++) {
			if (cListDivs[i].className == 'topics') {
				//get total number of posts for each list
				var cListItems = cListDivs[i].getElementsByTagName('li');
				
				//create an array for number of posts for each column
				var aColLength = new Array(3);
				
				//handle lists with one or two posts
				if (cListItems.length <= 2) {
					aColLength[0] = 1;
					if (cListItems.length == 2) {
						aColLength[1] = 1;
					}
					else {
						aColLength[1] = 0;
					}
					aColLength[2] = 0;
				}
				//handle lists with three or more post
				else {
					//calculate base column length
					var iColLength = parseInt(cListItems.length / 3);
					aColLength[0] = iColLength;
					aColLength[1] = iColLength;
					aColLength[2] = iColLength;
					//compare number of posts to sum of base column length to calculate remaining posts
					iPostsRemaining = cListItems.length - (iColLength * 3);
					//adjust column lengths
					if (iPostsRemaining > 0) {
						//if one post is remaining
						if (iPostsRemaining == 1) {
							aColLength[0] = iColLength + 1;
							aColLength[1] = iColLength + 1;
							aColLength[2] = iColLength - 1;
						}
						//if two posts are remaining
						if (iPostsRemaining == 2) {
							aColLength[0] = iColLength + 1;
							aColLength[1] = iColLength + 1;
						}
					}
				}
				
				//set container div height
				var iContainerHeight = parseInt(aColLength[0]) * 21;
				//cListDivs[i].runtimeStyle.cssText = 'height: ' + iContainerHeight + 'px;';
				cListDivs[i].style.height = iContainerHeight + 'px';
				document.getElementById('TopicList').style.height = iContainerHeight + 'px';
				
				//set column stops
				iCol_1_stop = parseInt(aColLength[0]) - 1;
				iCol_2_stop = (parseInt(aColLength[0]) + parseInt(aColLength[1])) - 1;
				
				for (var u=0;u<cListItems.length;u++) {
					//column 1
					if (u <= iCol_1_stop) {
						//cListItems[u].runtimeStyle.cssText = 'margin-left: 0; line-height: 21px;';
						cListItems[u].style.marginLeft = 0;
						cListItems[u].style.lineHeight = '21px';
					}
					//column 2
					if (u >= iCol_1_stop + 1 && u <= iCol_2_stop) {
						if (u == iCol_1_stop + 1) {
							if (cListItems.length <= 2 && u == 1) {
								iMarginTop = 21;
							}
							else {
								iMarginTop = parseInt(aColLength[0]) * 21;
							}
							//cListItems[u].runtimeStyle.cssText = 'margin-left: 33%; line-height: 21px; margin-top: -' + iMarginTop + 'px; ';
							cListItems[u].style.marginTop = '-' + iMarginTop + 'px';
							cListItems[u].style.marginLeft = '33%';
							cListItems[u].style.lineHeight = '21px';
						}
						else {
							//cListItems[u].runtimeStyle.cssText = 'margin-left: 33%; line-height: 21px;'
							cListItems[u].style.marginLeft = '33%';
							cListItems[u].style.lineHeight = '21px';
						}
					}
					//column 3
					if (u >= iCol_2_stop + 1) {
						if (u == iCol_2_stop + 1) {
							iMarginTop = parseInt(aColLength[1]) * 21;
							//cListItems[u].runtimeStyle.cssText = 'margin-left: 66%; line-height: 21px; margin-top: -' + iMarginTop + 'px; ';
							cListItems[u].style.marginTop = '-' + iMarginTop + 'px';
							cListItems[u].style.marginLeft = '66%';
							cListItems[u].style.lineHeight = '21px';
						}
						else {
							//cListItems[u].runtimeStyle.cssText = 'margin-left: 66%; line-height: 21px;';
							cListItems[u].style.marginLeft = '66%';
							cListItems[u].style.lineHeight = '21px';
						}
					}
				}
			}
		}
		//For Internet Explorer; Adjust content height and footer placement after keyword columns has been constructed
		if (is_ie) {
			moveFooter();
		}
	}
}

