/******************************************************
	* jQuery plug-in
	* Easy Background Image Resizer
	* Developed by J.P. Given (http://johnpatrickgiven.com)
	* Useage: anyone so long as credit is left alone
******************************************************/
(function($) {
	// plugin definition
	$.fn.ezBgResize = function(options) {
		// First position object
		this.css("position","fixed");
		this.css("top","0px");
		this.css("left","0px");
		this.css("z-index","0");
		this.css("overflow","hidden");
		this.css("visibility", "hidden");
		
		// Set obj to the width and height of window
		this.css("width",getWindowWidth() + "px");
		this.css("height",getWindowHeight() + "px");
		
		// Resize the img object to the proper ratio of the window.
		var iw = this.children('img').width();
		var ih = this.children('img').height();
		
		if($.browser.msie && $.browser.version == 6)
		{
		    this.css("position", "absolute");
		    this.css("top", $(window).scrollTop() + "px");   
		}
		
		// Just because ie thinks the image starts with a width and a height
		
		if(!isImageLoaded(this.children('img')[0]))
		    return false;

		if (getWindowWidth() > getWindowHeight()) {
			if (iw > ih) {
				var fRatio = iw/ih;
				this.children('img').css("width",getWindowWidth() + "px");
				this.children('img').css("height",Math.round(getWindowWidth() * (1/fRatio)));
				
				var newIh = Math.round(getWindowWidth() * (1/fRatio));
				
				if(newIh < getWindowHeight()) {
					var fRatio = ih/iw;
					this.children('img').css("height",getWindowHeight());
					this.children('img').css("width",Math.round(getWindowHeight() * (1/fRatio)));
				}
			} else {
				var fRatio = ih/iw;
				this.children('img').css("height",getWindowHeight());
				this.children('img').css("width",Math.round(getWindowHeight() * (1/fRatio)));
			}
		} else {
			var fRatio = ih/iw;
			this.children('img').css("height",getWindowHeight());
			this.children('img').css("width",Math.round(getWindowHeight() * (1/fRatio)));
		}
		this.css("visibility", "visible");
		return true;
	};
	
	// 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;
	};
	
	function isImageLoaded(img) {
        // During the onload event, IE correctly identifies any images that
        // weren’t downloaded as not complete. Others should too. Gecko-based
        // browsers act like NS4 in that they report this incorrectly.
        if (!img.complete)
            return false;

        // However, they do have two very useful properties: naturalWidth and
        // naturalHeight. These give the true size of the image. If it failed
        // to load, either of these should be zero.

        if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0)
            return false;

        // No other way of checking: assume it’s ok.
        return true;
    };
})(jQuery);