/******************************************************
	* jQuery plug-in
	* Easy Background Image Resizer
	* Developed by J.P. Given (http://johnpatrickgiven.com)
	* Useage: anyone so long as credit is left alone
	* Modified by aenizen
******************************************************/
(function($) {
	// plugin definition
	$.fn.ezBgResize = function(options) {
		// First position object
		this.css("position","fixed");
		this.css("z-index","-10");
		this.css("overflow","hidden");
		
		// Resize the img object to the proper ratio of the window.
		var iw = this.children('img').width();
		var ih = this.children('img').height();
		
		// plus by aenizen
		var pw, ph;
		var gw = getWindowWidth();
		var gh = getWindowHeight();
		var fRatioW = Math.round(gw * (1/(iw/ih)));
		var fRatioH = Math.round(gh * (1/(ih/iw)));
		
		if (gw > gh) {
			if (iw > ih) {
				this.children('img').css("width",gw + "px");
				this.children('img').css("height",fRatioW + "px");				
				pw = gw;
				ph = fRatioW;
				
				if(ph < gh) {
					this.children('img').css("height",gh + "px");
					this.children('img').css("width",fRatioH + "px");
					pw = fRatioH;
					ph = gh;
				}
			} else {
				this.children('img').css("height",gh + "px");
				this.children('img').css("width",fRatioH + "px");
				pw = fRatioH;
				ph = gh;
			}
		} else {
			this.children('img').css("height",gh + "px");
			this.children('img').css("width",fRatioH + "px");
			pw = fRatioH;
			ph = gh;
		}
		
		// Set obj position to the top and left of backimg - by aenizen
		if(pw > gw){
        	this.css('left', (gw - pw)/2 + "px");
		}else{
			this.css('left', "0px");
		}
		if(ph > gh){
        	this.css('top', (gh - ph)/2 + "px");
		}else{
			this.css('top', "0px");
		}
		
		// Set img cover with div - by aenizen
		$("#home").css("height",(gh-4) + "px");
	};
	
	// private function for debugging
	function debug($obj) {
		if (window.console && window.console.log) {
			window.console.log('Window Width: ' + $(window).width());
			window.console.log('Window Height: ' + $(window).height());
		}
	};
	
	// Dependable function to get Window Height
	function getWindowHeight() {
		var windowHeight = 0;
		if (typeof(window.innerHeight) == 'number') {
			windowHeight = window.innerHeight;
		}
		else {
			if (document.documentElement && document.documentElement.clientHeight) {
				windowHeight = document.documentElement.clientHeight;
			}
			else {
				if (document.body && document.body.clientHeight) {
					windowHeight = document.body.clientHeight;
				}
			}
		}
		return windowHeight;
	};
	
	// Dependable function to get Window Width
	function getWindowWidth() {
		var windowWidth = 0;
		if (typeof(window.innerWidth) == 'number') {
			windowWidth = window.innerWidth;
		}
		else {
			if (document.documentElement && document.documentElement.clientWidth) {
				windowWidth = document.documentElement.clientWidth;
			}
			else {
				if (document.body && document.body.clientWidth) {
					windowWidth = document.body.clientWidth;
				}
			}
		}
		return windowWidth;
	};
})(jQuery);
