/**
 * Script: Classy.FullscreenBackground
 * Author: Charles Demers
 * Version: 1.0
 * 
 * Provides: [Classy.FullscreenBackground]
 */

if(!Classy){
	var Classy = {};
}

Classy.FullscreenBackground = function(src, ratio) {
	
	this.ratio = ratio.height/ratio.width;
	
	this.c = document.createElement('div');
	this.c.id = "uifsbackground";
	var s = this.c.style;

	// s.position = "fixed";
	s.width = "100%";
	s.height = "100%";
	s.top = "0px";
	s.left = "0px";
	s.overflow = "hidden";
	s.zIndex = "0";
	
	this.img = document.createElement('img');
	this.img.style.position = "absolute";
	this.img.src = src;
	
	this.c.appendChild(this.img);
	
	document.body.style.margin = "0px";
	document.body.insertBefore(this.c,document.body.firstChild);
	
	var _this = this;
	if(window.addEventListener){
		window.addEventListener("resize",function(){
			_this.resize.call(_this);
		},false);
	} else if(window.attachEvent){
		window.attachEvent("onresize",function(){
			_this.resize.call(_this);
		});
	}
	
	this.resize();
};
Classy.FullscreenBackground.prototype = {
	resize : function(){
		var img = this.img;
		var ratio = this.ratio;
		var de = document.documentElement;
		var db = document.body;
		
		var browserwidth	= window.innerWidth || (de && de.clientWidth);
		var browserheight	= window.innerHeight || (de && de.clientHeight);
		var resizewidth 	= 0;
		var resizeheight	= 0;
		var scrollTop		= 0;
		var scrollLeft		= 0;
		
		resizewidth = browserwidth;
		resizeheight = browserheight;
		
		if(resizeheight/resizewidth > ratio){
			img.height = resizeheight;
			img.width = parseInt(resizeheight / ratio,10);
		} else {
			img.width = resizewidth;
			img.height = parseInt(resizewidth * ratio,10);
		}
		
		img.style.left = -parseInt(((img.width - browserwidth)/2), 10) + "px";
		img.style.top = -parseInt(((img.height - browserheight)/2), 10) + "px";
	}	
};
