var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

function adjustContentDimensions() {
	adjustContentHeight();
	if (BrowserDetect.browser == "Explorer" && !(BrowserDetect.version > 6)) {
		adjustContentWidthIE();
	}
}

function adjustContentHeight() {
	var contentHeight = getElementBottom(document.getElementById('content'));
	
	var subMenuHeight = getElementBottom(document.getElementById('subNav'));
	var subBrandHeight = getSubBrandHeight();
	
	var headerHeight = getElementBottom(document.getElementById('header'));
	var windowHeight = getWindowHeight();
	
	var overallHeight = contentHeight;
	
	// the minimum height of the left column: leave room for the sub menu plus sub brand 
	var leftColumnHeight = subMenuHeight + subBrandHeight;
	
	if (leftColumnHeight > overallHeight) {
		overallHeight = leftColumnHeight;
	}
	
	if (windowHeight > overallHeight) {
		overallHeight = windowHeight;
	}
		
	var mainWrap = document.getElementById('clearMainWrap');
	
	var correctMainWrapHeight = (overallHeight - headerHeight - 25) + "px";	// FIXME: 25px (padding of clearMainWrap)  
	
	// set height for IE<=6; minHeight for everything else 
	if (BrowserDetect.browser == "Explorer" && !(BrowserDetect.version > 6)) {
		mainWrap.style.height = correctMainWrapHeight;
		mainWrap.style.minHeight = 0;
	} else {
		mainWrap.style.minHeight = correctMainWrapHeight;
	}
}

function adjustContentWidthIE() {
	var mainWrap = document.getElementById('content');
	
	var windowWidth = document.documentElement.clientWidth;
	var headerWidth = document.getElementById('header').offsetWidth;
	
	if (windowWidth < headerWidth) {
		var newWidth = (headerWidth - mainWrap.offsetLeft);
		if (mainWrap.offsetWidth != newWidth) {
			mainWrap.style.width = newWidth + "px";
		}
	} else if (windowWidth > headerWidth) {
		mainWrap.style.width = "";
	}
}

function getElementBottom(element) {
	if (!element) return 0;
	var result = element.offsetHeight;
	while (element) {
		result += element.offsetTop;
		element = element.offsetParent;
	}
	return result; 
}

function getWindowHeight() {
	if (window.innerHeight) {
		return window.innerHeight;
	}
	return document.documentElement.clientHeight;
}

function getSubBrandHeight() {
	var subBrand = document.getElementById('subBrand');
	if (subBrand == null) {
		return 0;
	}
	
	// return the SubBrand height 
	// (which includes the margin to be left free at the bottom of the page)
	return subBrand.offsetHeight;
}

/**
 * Helper for scrolling news and calendar boxes.
 * @param id the id of the element to scroll by
 * @param the amount of pixels the element is to be moved
 */
function newsboxScroller(id, copyspeed) {
	var cross_scroller = document.getElementById(id);

	var actualheight = cross_scroller.offsetHeight;
	var scrollerheight = 170; //cross_scroller.style.height;

	var currentTop = parseInt(cross_scroller.style.top);
	
	var newTop;
	if (currentTop > -actualheight) {
		newTop = currentTop - copyspeed;
	} else {
		newTop = parseInt(scrollerheight);
	}
	cross_scroller.style.top = newTop + "px";
}
