/************************************************************************************
 *
 *		BGImage JS Class
 *		
 *		@author:  Tim Vogt
 *		@date:    14.02.2011
 *		@version: 1.0
 *
 *		@description: Full Screen Background Image, auto-resizing
 *
 ***********************************************************************************/

function BGImage(imagePath){
	var self = this;
	var imgPath = imagePath;
	var imgOrgW;
	var imgOrgH;
	var bgDiv;
	var bgImg;

	function init(){
		build();
	}	

	
	function build(){
		document.write('<style type="text/css">html{height:100%}</style>');
		document.body.style.height = "100%";
		bgDiv = document.createElement('div');
		bgDiv.style.overflow = "hidden";
		bgDiv.style.zIndex = "-200";
		bgDiv.style.position = "absolute";
		bgDiv.style.top = "0px";
		bgDiv.style.left = "0px";
		bgDiv.style.display = "none";
		bgDiv.style.width  = "100%";
		bgImg = new Image();
		bgImg.style.position = "fixed";
		bgImg.src = imgPath;
		bgDiv.appendChild(bgImg);
		document.body.appendChild(bgDiv);
		setTimeout(picLoaded, 50);
	}
	
	function picLoaded(){
		if(bgImg.complete){
			bgDiv.style.display = "block";
			imgOrgW = bgImg.width;
			imgOrgH = bgImg.height;
			resizeLis();
			window.onresize = resizeLis;
		} else {
			setTimeout(picLoaded, 50);
		}
	}

	function gw(){
		if (window.innerWidth) {
			return window.innerWidth;
		} else if(document.body.clientWidth){
			return document.body.clientWidth;
		} else if (document.body && document.body.offsetWidth) {
			return document.body.offsetWidth;
		} else if(document.documentElement.clientWidth){
			return document.documentElement.clientWidth;
		} else {
			return 0;
		}
	}
	
	function gh(){
		if (window.innerHeight) {
			return window.innerHeight;
		} else if(document.body.clientHeight){
			return document.body.clientHeight;
		} else if (document.body && document.body.offsetHeight) {
			return document.body.offsetHeight;
		} else if(document.documentElement.clientHeight){
			return document.documentElement.clientHeight;
		} else {			
			return 0;
		}
	}
	
	
	function resizeLis(){
		winW = gw();
		winH = gh();
		bgDiv.style.height = winH+"px";
		dImg = imgOrgW / imgOrgH;
		dWin = winW / winH;
		if(dImg > dWin){
			bgImg.style.width  = Math.floor(winH*dImg)+"px";
			bgImg.style.left   = Math.round((winW - (winH*dImg)) / 2)+"px";
			bgImg.style.height = winH+"px";
			bgImg.style.top    = "0px";
		} else {
			bgImg.style.height = Math.floor(winW*(imgOrgH / imgOrgW))+"px";
			bgImg.style.top   = Math.round((winH - (winW*(imgOrgH / imgOrgW))) / 2)+"px";
			bgImg.style.width  = winW+"px";
			bgImg.style.left = "0px";
		}
	}
	init();
}
