/**
 * JavaScript code to detect available availability of a 
 * particular font in a browser using JavaScript and CSS. 
 * 
 * Author : Lalit Patel
 * Website: http://www.lalit.org/lab/jsoncookies
 * License: Creative Commons Attribution-ShareAlike 2.5
 *          http://creativecommons.org/licenses/by-sa/2.5/
 * Version: 0.15 
 *          changed comparision font to serif from sans-serif, 
 *          as in FF3.0 font of child element didn't fallback 
 *          to parent element if the font is missing.
 * Updated: 09 July 2009 10:52pm
 * 
 */

/**
 * Actual function that does all the work. Returns an array with all the info.
 * This test will fail for the font set as the default serif font.
 * 
 * Usage: d = new Detector();
 *        d.test('font_name');
 */
var Detector = function(){
	var h = document.getElementsByTagName("BODY")[0];
	var d = document.createElement("DIV");
	var s = document.createElement("SPAN");
	d.appendChild(s);
	d.style.fontFamily = "sans";			//font for the parent element DIV.
	s.style.fontFamily = "sans";			//serif font used as a comparator.
	s.style.fontSize   = "72px";			//we test using 72px font size, we may use any size. I guess larger the better.
	s.innerHTML        = "mmmmmmmmmmlil";		//we use m or w because these two characters take up the maximum width. And we use a L so that the same matching fonts can get separated
	h.appendChild(d);
	var defaultWidth   = s.offsetWidth;		//now we have the defaultWidth
	var defaultHeight  = s.offsetHeight;	//and the defaultHeight, we compare other fonts with these.
	h.removeChild(d);
	/* test
	 * params:
	 * font - name of the font you wish to detect
	 * return: 
	 * f[0] - Input font name.
	 * f[1] - Computed width.
	 * f[2] - Computed height.
	 * f[3] - Detected? (true/false).
	 */
	function debug(font) {
		h.appendChild(d);
		var f = [];
		f[0] = s.style.fontFamily = font;	// Name of the font
		f[1] = s.offsetWidth;				// Width
		f[2] = s.offsetHeight;				// Height
		h.removeChild(d);
		font = font.toLowerCase();
		if (font == "serif") {
			f[3] = true;	// to set arial and sans-serif true
		} else {
			f[3] = (f[1] != defaultWidth || f[2] != defaultHeight);	// Detected?
		}
		return f;
	}
	function test(font){
		f = debug(font);
		return f[3];
	}
	this.detailedTest = debug;
	this.test = test;	
}
/*
 * replaceWithImg - replace everything inside objId with the image at imgUrl.
 * if linkHref is != '' the image will be surrounded by a link to linkHref.
 */
function replaceWithImg(objId, imgUrl, linkHref){
	var obj = document.getElementById(objId);
	var stripedText = obj.innerHTML.replace(/<br>/ig, ' ');
	stripedText = stripedText.replace(/(<([^>]+)>)/ig, '');
	var img = document.createElement('img');
	img.src = imgUrl;
	img.alt = stripedText;
	img.title = stripedText;
	if ( obj.hasChildNodes() ) {
		while ( obj.childNodes.length >= 1 ) {
			obj.removeChild( obj.firstChild );       
		} 
	}
	img.style.paddingTop = '5px';
	if(linkHref == ''){
		obj.appendChild(img);
	} else {
		var anchor = document.createElement('a');
		anchor.href=linkHref;
		anchor.appendChild(img);
		obj.appendChild(anchor);
	}
	
}


function loadflir(path){
  document.getElementById('address').style.letterSpacing = '0';
  document.getElementById('groupname').style.letterSpacing = '0';
  var flirscript = document.createElement('script');
  flirscript.setAttribute('type', 'text/javascript');
  flirscript.setAttribute('src', path + 'flirmin_cust.js');
  document.getElementsByTagName('head')[0].appendChild(flirscript);
  //These are manual replacements. We do them because FLIR produces an ugly result for this.
  replaceWithImg('address','/themes/pfadi10/images/address.png', 'http://wien83.schickes.at');
  replaceWithImg('groupname','/themes/pfadi10/images/groupname.png', '');
  document.getElementById('address').style.paddingRight = '29px';
}

var head_height = 141;
var referenceObjectId = 'cont_left'; //Object to use as a reference point for distance from top etc.
/**
 * fixToBottom - fix an object whose position is set to fixed to the bottom of the visible page
 * This is needed to ensure that:
 * - The element is visible when the browser window is smaller then the size of the layout
 * - The element is at the bottom of the layout when the browser window is larger than the
 * 		layout. In that case we want to prevent the element from "sliding out of the layout".
 */
function fixToBottom(obj_id){
	var obj = document.getElementById(obj_id);
	var reference = document.getElementById(referenceObjectId);
	//We have to reset the position, otherwise the if clause would not lead to the correct result
	// (since we fixed offsetHeight by setting style.top).
	obj.style.top = 'auto';
	obj.style.bottom = '0';
	var style_top = reference.offsetTop + reference.clientHeight - obj.clientHeight;
	if(obj.offsetTop - reference.offsetTop >= reference.clientHeight - obj.clientHeight) {
		obj.style.top = style_top+'px';
		obj.style.bottom = 'auto';
	}
}

function fixedObjects(){
	fixToBottom('layout_footer_left');
	fixToBottom('layout_footer_middle');
	fixToBottom('layout_footer_right');
	
	var reference = document.getElementById(referenceObjectId);
	
	//Handle bottom layer that keeps text from showing up (layout_hide_overflow)
	//bottom padding of main content depends on size of middle ornament
	var paddingBottom = document.getElementById('layout_footer_middle').clientHeight;
	var criticalHeight = reference.offsetTop + reference.clientHeight;
	var window_height = 0;
	if(typeof window.innerHeight != 'undefined'){
		window_height = window.innerHeight;
	} else if(typeof window.document.documentElement != 'undefined'){
		window_height = window.document.documentElement.clientHeight;
	} else {
		window_height = window.document.body.clientHeight;
	}
	var hiddenAreaHeight = Math.max(window_height - criticalHeight, 0);
	document.getElementById('cont_center').style.paddingBottom = hiddenAreaHeight + paddingBottom + 'px';
	document.getElementById('layout_hide_overflow').style.height = hiddenAreaHeight + 'px';
}

function updateLayout(){
	fixedObjects();
	window.setTimeout('fixedObjects()', 500); //wait for browser to finish resize, cleanup if necessary.
}
function initLayout(){
	if(navigator.appName && navigator.appVersion 
		&& navigator.appName == 'Microsoft Internet Explorer' 
		&& navigator.appVersion.substring(0, 1) <= 6){
		//MSIE <=6 is not supported.
		return;
	}
	var d = new Detector();
	if(!d.test('Monotype Corsiva')){
		loadflir('/assets/flir/');
	}
	if(navigator.userAgent.indexOf('Safari') != -1){ //Safari does not center correctly
		var refLeft = document.getElementById('outer').offsetLeft;
		document.getElementById('layout_footer_left').style.left = refLeft + 'px';
		document.getElementById('layout_footer_middle').style.left = refLeft + 'px';
		document.getElementById('layout_footer_right').style.left = refLeft + 20 + 'px';
	}
	updateLayout();
}

