Object.extend(Position, {
	windowGetWidth:function(){
		if(!Prototype.BrowserFeatures.XPath && Prototype.Browser.WebKit ){
			return this.innerWidth;
		}
		if(Prototype.Browser.Opera){
			return document.body.clientWidth;
		}
		//return document.documentElement.clientWidth;
		return document.documentElement.offsetWidth;
	},
	windowGetHeight:function(){
		if(!Prototype.BrowserFeatures.XPath && Prototype.Browser.WebKit){
			return this.innerHeight;
		}
		if(Prototype.Browser.Opera){
			return document.body.clientHeight;
		}
		//return document.documentElement.clientHeight;
		//return document.documentElement.offsetHeight;
		return document.body.parentNode.clientHeight
	},
	windowGetScrollTop:function() {
		return this.pageYOffset||document.documentElement.scrollTop;
	}
});

var FullScreenBG = Class.create();
FullScreenBG.prototype = {
	bg_element: null,
	ratio: 0,
	img_src: null,
	initialize: function(src) {
		this.img_src = src;
		Event.observe(window, "load", this.createBackground.bindAsEventListener(this));
	},
	createBackground: function() {
		if(this.bg_element===null) {
			this.bg_element = Element.extend(document.createElement("img"));
			this.bg_element.id="FullScreenBGImg";
			this.bg_element.setStyle({ position:"fixed", overflow:"hidden", zIndex:-1, top: "0px", left: "0px", visibility:"hidden" });
			this.bg_element.src = this.img_src;
			Event.observe( this.bg_element, "load", this.imgInit.bindAsEventListener(this));
			document.body.appendChild(this.bg_element);
			if(Prototype.Browser.IE && !window.XMLHttpRequest) {
				this.bg_element.setStyle({position:"absolute", top:Position.windowGetScrollTop()+"px"} );
				Event.observe(window, "scroll", function() {
					this.bg_element.setStyle({top: Position.windowGetScrollTop()+"px"});
				}.bind(this));
			}
		}
	},
	imgInit: function(){
		var w=parseInt(this.bg_element.getStyle("width"),10);
		var h=parseInt(this.bg_element.getStyle("height"),10);
		this.ratio=w/h;
		this.scale();
		this.bg_element.setStyle({visibility:"visible"});
		Event.observe(window, "resize", this.scale.bindAsEventListener(this));
	},
	scale: function() {
		
		var width, height, n_width, n_height, ratio;
		width = Position.windowGetWidth();
		height = Position.windowGetHeight();
		ratio = this.ratio;
		
		if (width/height>ratio){
			n_width=width;
			n_height=Math.round(width/ratio);
		}
		else {
			n_width=Math.round(height*ratio);
			n_height=height;
		}
		//debug("width: "+width+" height: "+height+" ratio: "+ratio);
		//debug("n_width: "+n_width+" n_height: "+n_height);
		this.bg_element.width=n_width;
		this.bg_element.height=n_height;
	}
}
