/**
 * Returns the innerWidth & innerHeight values in a cross-browser way.
 * 
 * @return array[innerWidth, innerHeight]
 */
function getInnerSize() {
	var myWidth = 0, myHeight = 0;
	
	if(typeof(window.innerWidth) == 'number') {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {   
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [myWidth, myHeight];
}

function initializeTAF() {
	Event.observe('taf', 'mouseover', function(e) { toggleButton(e, 'tell-a-friend-ro.gif'); });
	Event.observe('taf', 'mouseout', function(e) { toggleButton(e, 'tell-a-friend.gif'); });
}

function openTAF() {
	var width = 748;
	var height = 553;
	
	var popupLeft = (window.screen.width / 2) - (width / 2);
	var popupTop = (window.screen.height / 2) - (height / 2);
	
	var url = 'http://www.mini.de/de/de/general/silo/ecard_taf/taf_frameset.jsp?targetPageUrl=%2Fde%2Fde%2Fwebcam%2Fcontent.jsp&module_name=Das+neue+MINI+Cabrio';
	
	window.open(url, 'tafPopup', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=' + width + ',height=' + height + ",left=" + popupLeft + ',top=' + popupTop + ",screenX=" + popupLeft + ',screenY=' + popupTop);
}

function toggleButton(ev, name) {
	var element = ev.element();
	
	var baseName = element.src.substr(0, element.src.lastIndexOf('/') + 1);
	
	if(ev.type == 'mouseover') {
		element.src = baseName + name;
	} else {
		element.src = baseName + name;
	}
}

function convertToPx(value) {
	if(value.endsWith('px')) {
		return value.substr(0, value.length - 2);
	} else if(value.endsWith('em')) {
		// TODO add calculation
		// fspt = document.body.fontSize
		// fsbodybx = fspt * 1.3333
		// fspx = fsbodypx * fsem
		return "#todo#";
	} else if(value.endsWith('pt')) {
		// TODO add calculation:
		// fspt = document.body.fontSize;
		// fspx = 1.3333 * fspt 
		return "#todo#";
	} else if(value.endsWith('%')) {
		return "#todo#";
	}
	
	return value;
}

function getLang(type) {
    var lang;
    if (typeof navigator.userLanguage != "undefined") {
        lang = navigator.userLanguage.toUpperCase();
    } else if (typeof navigator.language != "undefined") {
        lang = navigator.language.toUpperCase();
    }
    return (lang && lang.indexOf(type.toUpperCase()) == 0)
}

function getParam(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if( results == null )
		return "";
	else
		return results[1];
}

function getUrlBasename() {
	var base = window.location.pathname;
	
	if(window.location.protocol.indexOf('http') > -1) {
		base = 'http://' + window.location.host + base;
	} else {
		base = base.substring(1);
		base = decodeURI(base);
	}
	
	base = base.substring(0, base.lastIndexOf('/') + 1);
	
	return base;
}