/*
	(c) 2007 and later by Antonov Club Avianna. Created by Roman Bigler.
	Do not copy or use this code without our prior written permission.
	
	Parts of this file might be exluded from the above Copyright, this is
	noted in-place.
*/

/*	The following Code (Object BrowserDetect, until "EOF PD CODE") is directly 
	taken from  quirksmode.org. Their copyright applies.
	*/
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: "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();
/* **** EOF PD CODE **** */

/*	Again, incorrect Javascript implementations force
	us to read out innerWidth and -height with different methods. 
	Code of the following two functions (until "EOF PD CODE")is derived from quirksmode.org
	resources, their Copyright applies. */
function inHeight() {
	if (self.innerHeight) // all except Explorer
	{
		return self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		return document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		return document.body.clientHeight;
	}
}
function inWidth() {
	if (self.innerHeight) // all except Explorer
	{
		return self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		return document.documentElement.clientWidth;
	}
	else if (document.body) // other Explorers
	{
		return document.body.clientWidth;
	}
}
/* **** EOF PD CODE **** */

/* 	This is the piece that makes our site scale well.
	It's not perfect yet, but produces stunning results.
	Yep, this might be one of the very first really 
	resolution-indepenent designs. (Font-Size excepted...yet :) ) */
//var SCALE_FACTOR 	= 0.28; //for the old site
var SCALE_FACTOR 	= 0.24; //bigger means less height
var RESIZE_FONT		= false;

var IE6_COR_FACTOR	= 1.01;  //bigger means less height
var IE6_POS_SIZE	= '32%'; //42

var IE5_COR_FACTOR	= 1.01; //bigger means less height
var IE5_POS_SIZE	= '31%'; //41

function fixTextBlock() {
	var textBlock = document.getElementById("textbody");
	var availHeight = inHeight();
	var availWidth = inWidth();
	if(BrowserDetect.browser == "Explorer" && BrowserDetect.version < 7) {
		var textPositioner = document.getElementById('textpositioner');
		/*
			IE >5.5, <7
		*/
		if(BrowserDetect.version > 5.5) {
			textPositioner.style.width = IE6_POS_SIZE;
			textBlock.style.height = Math.round( (availHeight) - (availWidth * (SCALE_FACTOR*IE6_COR_FACTOR) ) ) + "px";			
			/* 
				Degrade gracefully upon images inside the box:
				Images that are wider than the text box' width will be switched
				to a relative size, which should automatically constrain
				the dimensions to be within the surrounding box. They'll
				look a bit uglier, however.
			*/
			allImageNodes = document.getElementsByTagName("img");
			for (var i = 0; i < allImageNodes.length; i++) {
				var curNode = allImageNodes[ i];
				var parNode = curNode.parentNode;
				while(parNode != null) {
					if (parNode == textBlock) {
						var found = true;
						break;
					} else {
						parNode = parNode.parentNode;
					}
				}
				if(found) {
					parentWidth = textBlock.style.height.substring(0,textBlock.style.height.length-2);
					childWidth = curNode.width;
				  	if(childWidth > parentWidth) {
						// Algo 1
						newWidth = childWidth * 0.7;
						factor = curNode.height / curNode.width;
						curNode.width = newWidth;
						curNode.height = curNode.width * factor;
						
						// Algo 2
						/*curNode.style.width = newWidth;
						curNode.width = "";
						curNode.height = "";*/
						
						found = false;
					}
				}
			}
		/*
			IE <5.5
		*/
		} else {
			textPositioner.style.width = IE5_POS_SIZE;
			textBlock.style.height = Math.round( (availHeight) - (availWidth * (SCALE_FACTOR*IE5_COR_FACTOR) ) ) + "px";
		}
	/* 
		All other Browsers
	*/
	} else {
		var textOuterBlock = document.getElementById("textouter");
		var backLayer = document.getElementById("backlayer");
		var textPosBlock = document.getElementById("textpositioner");
		textBlock.style.height = (textOuterBlock.offsetHeight - textPosBlock.offsetHeight) + "px";
		//textBlock.style.height = Math.round( (availHeight) - (availWidth*SCALE_FACTOR) ) + "px"; //old style, not as accurate
		
		//TODO: Make this available to IE's
		if(((availHeight / availWidth) < 0.47) || availHeight < 680 || availHeight < 580) {
			/*
				Not enough vertical space available to create a nice view. In that case,
				lock content area height and fix menus
			*/
		
			switchToFixedLayout();
		} else {
			// check if we need to flip back from fixed Layout...
			switchToFlexibleLayout();
		}
		backLayer.style.height = (textOuterBlock.offsetHeight) + "px";
		
		if(BrowserDetect.browser == "Firefox") {
			textPosBlock.style.width = "29%";
		}
	}
}

var LAYSWITCH_MENU_OLD_TOP = -1;

function switchToFlexibleLayout() {
	//TODO: Implement as separate stylesheet
	var textOuterBlock = document.getElementById("textouter");
	var backLayer = document.getElementById("backlayer");
	var textPosBlock = document.getElementById("textpositioner");
	var textBlock = document.getElementById("textbody");
	var availHeight = inHeight();
	var availWidth = inWidth();
	
	/*window.status="flexible / h: " + availHeight;*/
	
	// revert changes from function below as necessary
	if (textOuterBlock.style.height != "100%") textOuterBlock.style.height = "100%";
	
	var divElements = document.getElementsByTagName("div");
	for (var i = 0; i < divElements.length; i++) {
		var curDiv = divElements[ i];
		var classVals = curDiv.getAttribute('class');
		if (classVals) {
			if (classVals.indexOf("menu") > -1) {
				curDiv.style.position = "absolute";
				curDiv.style.top = LAYSWITCH_MENU_OLD_TOP;
			}
		}
	}
}

function switchToFixedLayout() {
	//TODO: Implement as separate stylesheet
	var textOuterBlock = document.getElementById("textouter");
	var backLayer = document.getElementById("backlayer");
	var textPosBlock = document.getElementById("textpositioner");
	var textBlock = document.getElementById("textbody");
	var availHeight = inHeight();
	var availWidth = inWidth();

	/*window.status="fixed / h: " + availHeight;*/
	
	textOuterBlock.style.height="900px";
	textBlock.style.height = (textOuterBlock.offsetHeight - textPosBlock.offsetHeight) + "px";
	
	var divElements = document.getElementsByTagName("div");
	for (var i = 0; i < divElements.length; i++) {
		var curDiv = divElements[ i];
		var classVals = curDiv.getAttribute('class');
		if (classVals) {
			if (classVals.indexOf("menu") > -1) {
				// we can't really make menus fixed in case of a cheap godawful widescreen res.
				if (availHeight > 570) {
					curDiv.style.position = "fixed";
				} else {
					curDiv.style.position = "absolute";
				}
				if(availHeight < 670) {
					if (LAYSWITCH_MENU_OLD_TOP == -1) LAYSWITCH_MENU_OLD_TOP = curDiv.style.top;
					curDiv.style.top = "50px";
				}
			}
		}
	}

}

/*	Fixes an errorneous behaviour of Firefox
*/
var FF_TXTBODY_TOPFIX_MARGIN	=	"-3%";
function fixFFMargin() {
	var textBlock = document.getElementById("textouter");
	var availHeight = inHeight();
	var availWidth = inWidth();
	textBlock.style.marginTop = FF_TXTBODY_TOPFIX_MARGIN;
}

/*	This replaces pixel-based images by their vector-based SVG
	counterparts, given that they have the right CSS class.
	Note that there are still browsers out there which don't
	implement SVG sufficiently, so pay attention you call this
	function. It's not babysitting the browser. */
var SVG_CSS_REPLACE_CLASS 	= "svgReplace";
var SVG_IMGEL_REMOVE_ID		= "svgToBeRemoved";
function loadSVG() {
	var imageElements = document.getElementsByTagName("img");
	for (var i = 0; i < imageElements.length; i++) {
		var imgEl = imageElements[i];
		var classAttr = imgEl.getAttribute("class");
		if (classAttr != null && classAttr.search(SVG_CSS_REPLACE_CLASS) >= 0) {
			var newURL = imgEl.getAttribute("src");
			/*newURL = newURL.replace(/(.+\.).+/, "$1svg");
			var parNode = imgEl.parentNode;
			*/
		}
	}
}

/*	This function takes care of the strange behaviour IE6
	exposes when second-level menu-items are displayed initially. 
	Funny though, IE6 interprets the very same style annotations 
	correctly when set afterwards by this script. */
var IE6_MNUFIX_ELNAME		=	"menu-2";
var IE6_MNUFIX_PRE_MARGIN 	=	"0%";
var IE6_MNUFIX_PRE_PADLEFT 	=	"1%"; 
var IE6_MNUFIX_POST_MARGIN 		=	"2%";
var IE6_MNUFIX_POST_PADLEFT 	=	"10%"; 
function fix_IE6_2ndlvl(init) {
	var els2fix = document.getElementsByName(IE6_MNUFIX_ELNAME);
	if (init) {
		for(var i = 0; i < els2fix.length; i++) {
			els2fix[i].style.margin 		= IE6_MNUFIX_PRE_MARGIN;
			els2fix[i].style.paddingLeft	= IE6_MNUFIX_PRE_PADLEFT;
		}
	} else {
		for(var i = 0; i < els2fix.length; i++) {
			els2fix[i].style.margin 		= IE6_MNUFIX_POST_MARGIN;
			els2fix[i].style.paddingLeft	= IE6_MNUFIX_POST_PADLEFT;
		}
	}
}
/*	This function kicks IE6 in the head to force correct
	content overflow handling of the text-body. */
function fix_IE6_overflow() {
	var textBlock = document.getElementById("textbody"); 
	textBlock.width
}
/*	Removes the Wing-Logo at the top. The logo, in conjunction with
	the PNG fix, causes IE < 7 to violently rip the whole layout apart. */
function fix_IE56_rm_logo() {
	var logo = document.getElementById("toplogo");
	if(logo != null) {
		var prnt = logo.parentNode;
		prnt.removeChild(logo);
	}
}
/* 	This function will warn the user in case he's using an
	ancient Browser, but only if it's known to be old (NOT just
	unkown, that's dangerous!).
*/
function warnUponOldBrowser() {
	var debugBrowserWarn = false;
	// Don't even bother if the Browser is current enough
	if(
		(
			(BrowserDetect.browser == "Explorer" && BrowserDetect.version > 6) ||
			(BrowserDetect.browser == "Firefox" && BrowserDetect.version > 3) ||
			(BrowserDetect.browser == "Safari" && BrowserDetect.version > 1) 
		) &&
			(debugBrowserWarn != "true")
		) {
		return
	}	
	
	// Default Message
	var msg_en = "You are using an old browser! It is possible that the site doesn't look correctly.";
	
	// Notification Template
	var noti_div_el = document.createElement("div");
	noti_div_el.setAttribute('class','ancient-notification');
	noti_div_el.style.fontFamily	=	'Verdana, sans-serif';
	noti_div_el.style.fontWeight	=	'bold';
	noti_div_el.style.paddingLeft	=	'22pt';
	noti_div_el.style.fontSize	=	'9pt';
	noti_div_el.style.position	=	'absolute';
	noti_div_el.style.top		=	'0px';
	noti_div_el.style.left		=	'0px';
	noti_div_el.style.width		=	'100%';
	noti_div_el.style.height	=	'10px';
	noti_div_el.style.background	=	'FFE100';
	noti_div_el.style.color		=	'black';
	noti_div_el.style.zIndex	=	'1';
	
	
	// IE
	if(BrowserDetect.browser == "Explorer" && BrowserDetect.version < 6) {
		var textEl = document.createTextNode(msg_en);
		noti_div_el.appendChild(textEl);
		document.getElementsByTagName("body")[0].appendChild(noti_div_el);
	}
}

/* Popup functions for calendar. You may use that as you will, this code is public domain. */
function ShowPopup(hoveritem,info)
{
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	} else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	
	hp = document.getElementById("hoverpopup");
	hp.style.top = (posy) + "px";
	hp.style.left = (posx) + "px";
	hp.style.visibility = "visible";
}

function HidePopup()
{
	hp = document.getElementById("hoverpopup");
	hp.style.visibility = "hidden";
}

