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();
	}

	if(typeof initializeSayt!='undefined'){
		initializeSayt();
	}
}

function adjustContentHeight() {
	var mainWrap = document.getElementById('clearMainWrap');
	if (mainWrap==null){
		return;
	}
	
	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 correctMainWrapHeight = (overallHeight - headerHeight - 25) + "px";	// FIXME: 25px (padding of clearMainWrap)

	// set height for IE<=6; minHeight for everything else
	if (mainWrap != null){
		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 header = document.getElementById('header');
	
	if (mainWrap != null && windowWidth != null && header != null){
		if (windowWidth < header.offsetWidth) {
			var newWidth = (header.offsetWidth - mainWrap.offsetLeft);
			if (mainWrap.offsetWidth != newWidth) {
				mainWrap.style.width = newWidth + "px";
			}
		} else if (windowWidth > header.offsetWidth) {
			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";
}




/*
ORDERCENTER
*/
var ocenterRegister = new Array();

function ocenter_regComponent(id){
	ocenterRegister.push(id);
}

function playSound(soundobj) {
	 var thissound= eval("document."+soundobj);
	 try {
	   thissound.Play();
	 }
	 catch (e) {
	thissound.DoPlay();
	 }

	pause(1250);
	return true; 
}

function playSound(soundobj,wait) {
	 var thissound= eval("document."+soundobj);
	 try {
	   thissound.Play();
	 }
	 catch (e) {
	thissound.DoPlay();
	 }

	pause(wait);
	return true; 
}

function pause(millis){
	var date = new Date();
	var curDate = null;
	do { curDate = new Date(); }
	while(curDate-date < millis)
}


function sendToWebtrends(linkobj,webtrends,componentname,title){
	if(webtrends == 'true' && componentname != ''){
		//componentname = "'" + componentname + "'";
		//title = "'" + title + "'";
		//alert(componentname + ' '  + webtrends + ' ' + title);
		dcsMultiTrack('WT.grid','linklist', 'WT.CName',componentname, 'WT.Id1',linkobj.innerHTML, 'WT.Link',linkobj.href, 'WT.gp',title)
	}
}

function showDirectEdit(){
	var directEditElement = document.getElementById('directEdit');
	directEditElement.style.display = 'block';
}
function closeDirectEdit(){
	var directEditElement = document.getElementById('directEdit');
	directEditElement.style.display = 'none';
}

function printIframe(name) {
	// This function is used for the lotus notes iframes
	
	var frame = getIframe(name);
	window.open(appendPrintParameter(frame.src));
	
    function appendPrintParameter(url) {
        // Remove the following line, when the lotus notes service
    	// provides a print functionality for the print request parameter
    	return url;
    	if (url.indexOf('?') == -1) {
            return url + "?print=true";
        } else {
            return url + "&print=true";
        }
    }
	
	function getIframe(name) {
		var iframes = document.getElementsByTagName('iframe');
		for (var i = 0; i < iframes.length; i++) {
			if (iframes[i].name == name) {
				return iframes[i];
			}
		}
	}
}

jQuery(document).ready(function() {
	adjustContentDimensions();
	window.onresize = adjustContentDimensions;
});


