/******************************************************
	* jQuery plug-in
	* Easy Background Image Resizer
	* Developed by J.P. Given (http://johnpatrickgiven.com)
	* Useage: anyone so long as credit is left alone
******************************************************/

var containerObj;
var center = 0;
var idBackground = 'default';

(function($) {
	// plugin definition
	$.fn.ezBgResize = function(cntr) 
	{
		
		if (cntr) 
		{
			center = 1;
		}
		
		// First position object
		containerObj = this;
		containerObj.css("visibility","hidden");
		
		$("body").css({
			"overflow-x":"hidden"
		});
		
		$(window).load(function() {
			resizeImage();
		});
		
		$(window).bind("resize",function() {
			resizeImage();
		});
		
	};
	
	$.fn.changeBackgroundImage = function(id)
	{
		if  (id)
		{
			idBackground = id;
		}
		else
		{
			idBackground = 'default';
		}
		
		containerObj = this;
		containerObj.css("visibility","hidden");
		containerObj.children('img').css("display", "none");
		
		$("body").css({
           	 "overflow-x":"hidden"
	        });
	        
	        $(window).load(function() {
	            resizeImage();
	        });
	        
	        $(window).bind("resize",function() {
	            resizeImage();
	        });
	};
	
	function resizeImage() 
	{
		$("body").css({
			"overflow-x":"auto"
		});
		
		containerObj.css({
			"position":"fixed",
			"top":"0px",
			"left":"0px",
			"z-index":"-1",
			"overflow":"hidden",
			"width":getWindowWidth() + "px",
			"height":getWindowHeight() + "px"
		});
		
		// Resize the img object to the proper ratio of the window.
		var iw = containerObj.children('img#'+idBackground).width();
		var ih = containerObj.children('img#'+idBackground).height();
		if ($(window).width() > $(window).height()) 
		{
			if (iw > ih) 
			{
				var fRatio = iw/ih;
				containerObj.children('img#'+idBackground).css("width",$(window).width() + "px");
				containerObj.children('img#'+idBackground).css("height",Math.round($(window).width() * (1/fRatio)));

				var newIh = Math.round($(window).width() * (1/fRatio));

				if(newIh < $(window).height()) 
				{
					var fRatio = ih/iw;
					containerObj.children('img#'+idBackground).css("height",$(window).height());
					containerObj.children('img#'+idBackground).css("width",Math.round($(window).height() * (1/fRatio)));
				}
			} 
			else 
			{
				var fRatio = ih/iw;
				containerObj.children('img#'+idBackground).css("height",$(window).height());
				containerObj.children('img#'+idBackground).css("width",Math.round($(window).height() * (1/fRatio)));
			}
		} 
		else 
		{
			var fRatio = ih/iw;
			containerObj.children('img#'+idBackground).css("height",$(window).height());
			containerObj.children('img#'+idBackground).css("width",Math.round($(window).height() * (1/fRatio)));
		}
		
		containerObj.children('img#'+idBackground).css("display","inline");
		containerObj.css("visibility","visible");
		
		// Center BG Image
		if (center) 
		{
			containerObj.children('img#'+idBackground).css("position","relative");
			
			if (containerObj.children('img#'+idBackground).width() > containerObj.width()) 
			{
				var wDiff = (containerObj.children('img#'+idBackground).width() - containerObj.width()) / 2;
				containerObj.children('img#'+idBackground).css("left", "-" + wDiff + "px");
			}
		}
	}
})(jQuery);

// 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;
};
