var divSize = { w:0, h:0 };
var timer = null;

function init() {
	if (document.getElementById) {
		timer = setInterval("detectScroll()", 200);
	}
}

// Background image functions start

function setDivSize(w, h) {
	
	document.getElementById("bgImageContainer").style.width = w + "px";
	document.getElementById("bgImageContainer").style.height = h + "px";
	
	if (w > 1024) {
		document.getElementById("bgImage").style.width = "100%";
		document.getElementById("bgImage").style.height = "auto";
	}
	else {
		document.getElementById("bgImage").style.width = "auto";
		document.getElementById("bgImage").style.height = "auto";
	}
}

function detectScroll() {
	
	var newX = getVisibleSize('w') + getScrolledBy('l');
	var newY = getVisibleSize('h') + getScrolledBy('t');
	
	// Vertical scrollbar is definitly visible
	if (getScrolledBy('t') > 0) {
		newX -= 16;
	}
	
	divSize.x = newX;
	divSize.y = newY;
	
	setDivSize(divSize.x, divSize.y);
}

function getScrolledBy(side) {

	var scrolledBy = { l:0, t:0 };
	
	if (typeof(window.innerWidth) == 'number') {
		// Non-ie
		scrolledBy.l = window.pageXOffset;
		scrolledBy.t = window.pageYOffset;
	}
	else if (document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft)) {
		//IE 6+ in 'standards compliant mode'
		scrolledBy.l = document.documentElement.scrollLeft;
		scrolledBy.t = document.documentElement.scrollTop;
	}
	else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		scrolledBy.l = document.body.scrollLeft;
		scrolledBy.t = document.body.scrollTop;
	}
	
	//window.status = "w:" + scrolledBy.l + ", h: " + scrolledBy.t;
	
	if (side == 't') {
		return scrolledBy.t;
	}
	else {
		return scrolledBy.l;
	}
}

function getVisibleSize(side) {
	
	var winSize = { w:0, h:0 };
	
	if (typeof(window.innerWidth) == 'number') {
		// Non-ie
		winSize.w = window.innerWidth;
		winSize.h = window.innerHeight;
	}
	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		//IE 6+ in 'standards compliant mode'
		winSize.w = document.documentElement.clientWidth;
		winSize.h = document.documentElement.clientHeight;
	}
	else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		//IE 4 compatible
		winSize.w = document.body.clientWidth;
		winSize.h = document.body.clientHeight;
	}
		
	if (side == 'w') {
		return winSize.w;
	}
	else {
		return winSize.h;
	}
}

function openYouTubePopUp(videoId, lang) {

	var youTubePopUp = window.open("/youTubePopUp.php?videoId=" + videoId + "&lang=" + lang, 'uturnyoutubepop', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1,width=500,height=460,left = 590,top = 345');
	youTubePopUp.focus();

}

function showMediaPopUpImageFullsize(imageId) {

	// We do it this way to mmake sure that the centering go ok
	document.getElementById('mediaPopUpFullsizeWrapper_' + imageId).style.display = 'block';
	document.getElementById('mediaPopUpFullsizeWrapper_' + imageId).style.backgroundColor = 'white';
	centerElement('mediaPopUpFullsizeWrapper_' + imageId);
	document.getElementById('mediaPopUpFullsizeInnerWrapper_' + imageId).style.visibility = 'visible';

	// Set dimensions of cover based on mainelements dimensions
	var mainElm = new element('main');
	var coverElm = new element('mediaPopUpCover');
	coverElm.setH(mainElm.getH() + 10 + 'px');
	
	// Show cover element
	document.getElementById('mediaPopUpCover').style.display = 'block';

}

function hideMediaPopUpImageFullsize(imageId) {

	document.getElementById('mediaPopUpFullsizeWrapper_' + imageId).style.display = 'none';
	document.getElementById('mediaPopUpFullsizeWrapper_' + imageId).style.backgroundColor = 'transparent';
	document.getElementById('mediaPopUpFullsizeInnerWrapper_' + imageId).style.visibility = 'hidden';
	document.getElementById('mediaPopUpCover').style.display = 'none';

}

/**
* Get the current y position of the top of the body element
*/
function getYOfBodyTop() {
	
	return (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
	
}

function centerElement(elmId) {

	browserDims = getBrowserDimensions();
	
	var elm = new element(elmId);
	
	// Divide the browser width by 2 and subtract half the width of the element
	var marginLeft = Math.round((browserDims[0]/2) - (elm.getW()/2));
	
	// Divide the browser height by 2 and subtract half the height of the element
	// and then add the scrolloffset
	var marginTop = Math.round((browserDims[1]/2) - (elm.getH()/2)) + getYOfBodyTop();
	
	// Lets make sure we wont end up outside the browser window
	marginLeft < 0 ? marginLeft = 0 : '';
	marginTop < 0 ? marginTop = 0 : '';
	
	document.getElementById(elmId).style.marginLeft = marginLeft + 'px';
	document.getElementById(elmId).style.marginTop = marginTop + 'px';

}

function getBrowserDimensions() {

	outcome = new Array();
	outcome[0] = false;
	outcome[1] = false;
	
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined')
	{
	
		outcome[0] = window.innerWidth;
		outcome[1] = window.innerHeight;
		
	} else if (typeof document.documentElement != 'undefined'
	 && typeof document.documentElement.clientWidth !=
	 'undefined' && document.documentElement.clientWidth != 0) { 
	 
	 	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)	
		outcome[0] = document.documentElement.clientWidth;
		outcome[1] = document.documentElement.clientHeight;
		
	} else { // older versions of IE
	
		outcome[0] = document.getElementsByTagName('body')[0].clientWidth;
		outcome[1] = document.getElementsByTagName('body')[0].clientHeight;
		
	}
	
	return outcome;

}

 

// Background image functions end

//window.onload = init();