// ///// GENERAL BEHAVIOUR

var Page = {

	// PAGE SETUP

	onload : null, // set this to a function on the page if you want custom code executed by the body.onload event

	init : function () { // onload handler (executed in the context of window obj)
		if(Page.isPopup) {
			Page.setupPopup();
		}
		else {
			Page.adjustLayout();
		}
		// check if the page has registered body.onload handlers that should be executed
		if(!Page.onload && typeof PageOnload == "function") {
			Page.onload = PageOnload;
		}
		if(typeof Page.onload == "function") {
			Page.onload();
		}
	},

	// LAYOUT CONTROL

	adjustLayout : function() { // adjust page layout // resize handler (executed in the context of window obj)
		Page.adjustContentTopPosition();
		Page.adjustShadowLayout();
		Page.adjustLeftcolHeight();
	},
	adjustContentTopPosition : function() { // adjust top content position when no 2nd tabrow
		var has2ndTab = false;
		var has2lines = false;
		if (Navigation.getById("tabnav").getViewById("tabmenu").isinit) {
			var navigation = document.getElementById("tabNavRoot");
			var leftCol = document.getElementById("leftCol");
			var mainArea = document.getElementById("mainArea");
			if(navigation && leftCol && mainArea) {
				var items = navigation.getElementsByTagName("ul");
				if (items[0]) {
					var childs = items[0].childNodes;
					if (childs) {
						for(var i = 0; i < childs.length; i++) {
							if (childs[i].nodeName.toLowerCase() == "li") {
								if (childs[i].className.indexOf("current") > -1) {
									var subnav = childs[i].childNodes;
									if (subnav) {
										for(var j = 0; j < subnav.length; j++) {
											if (childs[i].childNodes[j].nodeName.toLowerCase() == "ul"){
												has2ndTab = true;
											}
											if (childs[i].childNodes[j].innerHTML && childs[i].childNodes[j].innerHTML.toLowerCase().indexOf("<br") > -1) {
												has2lines = true;
											}
										}
									}
								}
							}
						}
						if(has2ndTab) {
							if (has2lines) { //if 2nd tabrow and 2 lines
								leftCol.style.marginTop = "3.75em";
								mainArea.style.marginTop = "3.75em";
							}
						}
						else { //if no 2nd tabrow
							leftCol.style.marginTop = "0.83333333em";
							mainArea.style.marginTop = "0.83333333em";
						}
					}
				}
			}
		}
	},
	adjustShadowLayout : function() { // adjust page shadow design
		var cnt_height = document.getElementById("content").offsetHeight;
		var win_height = (window.innerHeight)? window.innerHeight : document.body.clientHeight;
		var cnt_height = parseFloat(cnt_height);
		var win_height = parseFloat(win_height);
		var height = ((cnt_height > win_height)? cnt_height : win_height) + "px";

		var sl = document.getElementById("shadowLeft");
		if(sl) sl.style.height = height;
		var sr = document.getElementById("shadowRight");
		if(sr) sr.style.height = height;
	},
	adjustLeftcolHeight : function() {// stretch leftcol height if no lefthandnav
		var leftcol = document.getElementById("leftCol");
		var navroot = document.getElementById("lefthandNavRoot");
		if(leftcol && navroot) { // popups have no leftcol ...
			var navmenus = navroot.getElementsByTagName("ul");
			if(!(navmenus.length > 0)) {
				var mar_height = document.getElementById("mainArea").offsetHeight;
				mar_height = ((mar_height - 15) / 12) + "em";
				leftcol.style.height = mar_height;
			}
		}
	},

	// POPUPS

	DEFAULT_POPUP_HEIGHT : 500,

	ENUM_POPUPSIZES_IE : { inter_small : 465, inter_large : 647, intra_small : 336, intra_large : 633, apps_small : 336, apps_medium : 451, apps_large : 633, apps_xlarge : 865 },
	ENUM_POPUPSIZES_OTHER : { inter_small : 460, inter_large : 642, intra_small : 331, intra_large : 628, apps_small : 331, apps_medium : 446, apps_large : 628, apps_xlarge : 860 },

	openPopup : function(pop_url, pop_name, pop_type) { // adjust popup height
		if(browser.agent == AbstractBrowser.AGENT_IE) {
			var popupSizes = this.ENUM_POPUPSIZES_IE;
		}
		else {
			var popupSizes = this.ENUM_POPUPSIZES_OTHER;
		}
		if(!pop_type) { // open in new browser window
			var pop_params = "";
		}
		else if(popupSizes[pop_type]) { // open standard size popup
		var pop_width = popupSizes[pop_type];
		var pop_params = "width=" + pop_width + ",height=" + this.DEFAULT_POPUP_HEIGHT + ",scrollbars=1,resizable=1";
		}
		else if(pop_type.match(/\d{1,4},\d{1,4}/) != ""){ // open custom size popup
			var dims = pop_type.split(",");
			var pop_width = dims[0];
			var pop_params = "width=" + pop_width + ",height=" + dims[1] + ",scrollbars=1,resizable=1";
		}
		pop_name = (pop_name != "")? pop_name : ("pop_" + new Date().getTime()); // open in new popup if no name provided

		var popup = window.open(pop_url, pop_name, pop_params);
		if(pop_width) popup.pop_width = pop_width;
	},
	setupPopup : function() { // adjust popup height // resize handler (executed in the context of window obj)
		// get layout type
		var maclass = document.getElementById("mainArea").className;
		// standard formats
		if(maclass.indexOf("content2Col") > -1) {
			var PopID = "popup2Col";
		}
		else if(maclass.indexOf("contentMedium") > -1) {
			var PopID = "popupMedium";
		}
		else if(maclass.indexOf("contentWide") > -1) {
			var PopID = "popupWide";
		}
		// special formats
		else if(maclass.indexOf("MainWeather") > -1) {
			var PopID = "popupWeather";
		}
		else if(maclass.indexOf("MainOrgChart") > -1) {
			var PopID = "popupOrgChart";
		}
		else { // use this with inpage popup styles and "width,height" call of openPopup
			var PopID = "popupSpecial";
		}
		// setup styles
		var body = document.getElementsByTagName("body")[0];
		with(body) {
			style.textAlign = "left";
			style.background = "none";
			style.backgroundColor = "#ffffff";
			setAttribute("id", PopID);
			className = className + " popup"; // ie fix: forces css recalc and repaint
		}
		// adjust window height
		if(self.pop_width) {
			var content = document.getElementById("content");
			content.style.height = "auto";
			content.style.minHeight = "1px";

			var cheight = content.offsetHeight;
			var dheight = document.getElementById("disclaimer").offsetHeight;
			var h_factor = (browser.agent == AbstractBrowser.AGENT_MZ)? 15 : 0;
			var pop_height = cheight + dheight + h_factor;
			var popup_width = parseInt(self.pop_width) + 10;

			if(pop_height < this.DEFAULT_POPUP_HEIGHT) {
				window.resizeTo(popup_width, pop_height);
			}
		}
		self.focus();
	},

	// LANGUAGE MANAGEMENT

	ENUM_LANGUAGES : { "en" : ["English", "Language"], "de" : ["Deutsch", "Sprache"], "fr" : ["Fran&ccedil;ais", "Langue"], "it" : ["Italiano", "Lingua"], "es" : ["Espa&ntilde;ol", "Idioma"]},

	changeLanguage : function(newlang) { // change page language
		var langs = "";
		for(var l in this.ENUM_LANGUAGES) langs += l + "|";
		langs = langs.substring(0, langs.length - 1);

		var url_rexp = new RegExp("(.[\./])(" + langs + ")([\./])"); // match lang as: /de/ or as: .de.
		var doc_rexp = new RegExp("_(" + langs + ")\."); // match lang as: language=de
		var prm_rexp = new RegExp("language=(" + langs + ")"); // match lang as: language=de

		var url = document.location.pathname;

		var match = null;
		match = url.match(url_rexp);
		if(match) {
			var url = url.replace(url_rexp, (RegExp.$1 + newlang + RegExp.$3));
		}
		var match = null;
		match = url.match(doc_rexp);
		if(match) {
			var url = url.replace(doc_rexp, ("_" + newlang + "."));
		}
		var match = null;
		match = url.match(prm_rexp);
		if(match) {
			var url = url.replace(prm_rexp, ("language=" + newlang));
		}

		if(url != document.location.pathname) document.location.href = url;
	},
	writeLanguageSelector : function(pagelangs) { // write language selector
		// don't collide with "Settings" link
		if(window["SettingsLabel"] && SettingsLabel.length > 0) return;
		// get actual language
		if(!this.act_lang) {
			var langs = "";
			for(var l in this.ENUM_LANGUAGES) langs += l + "|";
			langs = langs.substring(0, langs.length - 1);

			var url = document.location.pathname;
			var url_rexp = new RegExp(".[\./](" + langs + ")[\./]"); // match lang as: /de/ or as: .de.
			var prm_rexp = new RegExp("language=(" + langs + ")"); // match lang as: language=de
			url_rexp.exec(url);
			if(RegExp.$1 && RegExp.$1.length == 2) {
				var match = RegExp.$1;
			}
			else {
				prm_rexp.exec(url);
				if(RegExp.$1 && RegExp.$1.length == 2) {
					var match = RegExp.$1;
				}
			}
			this.act_lang = match;
		}
		// write element
		var html_langsel = "";
		html_langsel += this.ENUM_LANGUAGES[this.act_lang][1] + " ";

		var pagelangs = pagelangs.split(".");
		for(var i = 0; i < pagelangs.length; i++) {
			if(pagelangs[i] == this.act_lang) {
				html_langsel += "<span class=\"active\" title=\"" + this.ENUM_LANGUAGES[pagelangs[i]][0] + "\">" + pagelangs[i].toUpperCase() + "</span> ";
			}
			else {
				html_langsel += "<a class=\"lang\" href=\"javascript:Page.changeLanguage('" + pagelangs[i] + "');\" title=\"" + this.ENUM_LANGUAGES[pagelangs[i]][0] + "\">" + pagelangs[i].toUpperCase() + "</a> ";
			}
		}
		document.write(html_langsel);
	},

	// OPTIONAL ELEMENTS

	writeElement : function(item, param1, param2) {

		if(item && item.length && item.length > 0) {
			if(!param1 && !param2) {
				// if array
				if(item.constructor && item.constructor.toString().toLowerCase().indexOf("array") > -1) {
					var html = "";
					for(var i = 0; i < item.length; i++) {
						if(i != 0) html += "/ ";
						html += item[i] + " ";
					}
				}
				// if simple string field
				else var html = item;
			}
			else if(param1 && !param2) {
				// if array
				if(item.constructor && item.constructor.toString().toLowerCase().indexOf("array") > -1) {
				}
				// if single field and html code
				else {
					if(param1.indexOf("{$}") > -1) {
						var html = param1.replace(/\{\$\}/, item);
					}
					else { // if label/url pair
						var html = "<a href=\"" + param1 + "\">" + item + "</a>";
					}
				}
			}
			else if(param1 && param2) {
				// if array
				if(item.constructor && item.constructor.toString().toLowerCase().indexOf("array") > -1) {
					if(param2.indexOf("{$}") > -1) {
						var html = "";
						for(var i = 0; i < item.length; i++) {
							var link = "<a href=\"" + ((param1[i])? param1[i] : "#") + "\">" + item[i] + "</a>";
							html += param2.replace(/\{\$\}/, link);
						}
					}
				}
				// if label/url pair and html code
				else {
					if(param2.indexOf("{$}") > -1) {
						var html = "<a href=\"" + param1 + "\">" + item + "</a>";
						html = param2.replace(/\{\$\}/, html);
					}
				}
			}
			if(html) document.write(html);
		}
	},
	writeStatefullElement : function(param1, param2) { // write preferences/settings
		var html = " <a class=\"prefs\" href=\"" + param2 + "\">" + param1 + "</a>";
		if(param2.length==0){
			html = "<span class=\"active\">" + param1 + "</span>";
		}
		document.write(html);
	},
	writeFooterInfo : function(date, datehtml, owner, ownerurl, ownerhtml) {
		if((date && date.length > 0) || (owner && owner.length > 0)) {

			datehtml = datehtml.replace(/\{\$\}/, date);
			ownerhtml = ownerhtml.replace(/\{\$\}/, "<a href=\"" + ownerurl + "\">" + owner + "</a>");

			if(date && date.length > 0) {
				ownerhtml = ownerhtml.replace(/<dl class="info">/, "");
			}
			else datehtml = "";
			if(owner && owner.length > 0) {
				datehtml = datehtml.replace(/<\/dl>/, "");
			}
			else ownerhtml = "";
			document.write(datehtml + ownerhtml);
		}
	},

	// PRINT

	print : function() {
		if(window.print) {
			window.print();
		}
		else {
			alert("Your browser does't support this feature.\nPlease, use your browser's normal print command.");
		}
	}

};

// ///// REGISTER PAGE INIT HANDLER
// see navigations.js Navigation object for further onload time handlers

window.onload = Page.init;
if(self.name == "") {
	window.onresize = Page.adjustLayout;
}
