(function($) {

	var jqez = {};

	$.fn.ezBgResize = function(options) {
		jqez = options;
		if (!$.isArray(jqez.img)) {jqez.img = [jqez.img]}

		$("<img/>").attr("src", jqez.img).load(function() {
			if($("#jq_ez_bg").length) return;
			jqez.width = this.width;
			jqez.height = this.height;
			$("body").append('<div id="jq_ez_bg"></div>');
			$("#jq_ez_bg").html('<img src="' + jqez.img[0] + '" border="0">');
			$("#jq_ez_bg").css("visibility", "hidden");
			resizeImage();
			resizeImage();
			resizeImage();
		}).each(function() {
			if(this.complete) {
				$(this).trigger('load');
			}
		});
	};

	function resizeImage() {

		var elm = $('body');

		$('#site_content').css({ 'height' : 'auto' });
		if($(window).height() > elm.height()) {
			$('#site_content').css({ 'height' : $(window).height() - 131 });
		}

		$("#jq_ez_bg").css({
			"position": "absolute",
			"top": "0px",
			"left": "0px",
			"z-index": "-1",
			"overflow": "hidden",
			"width": elm.width() + "px",
			"height": elm.height() + "px",
			"opacity" : jqez.opacity
		});

		$("#jq_ez_bg").children('img').css("position", "relative");

		var iw = jqez.width;
		var ih = jqez.height;

		var bWidthRatio = elm.width() / iw;
		var bHeightRatio = elm.height() / ih;

		if(bWidthRatio <= bHeightRatio) {
			$("#jq_ez_bg").children('img').css("height", elm.height()).css("width", 'auto' );
		} else {
			$("#jq_ez_bg").children('img').css("width", elm.width()).css("height", 'auto' );
		}

		if (typeof(jqez.center) == 'undefined' || jqez.center) {
			if ($("#jq_ez_bg").children('img').width() > elm.width()) {
				var this_left = ($("#jq_ez_bg").children('img').width() - elm.width()) / 2;
				$("#jq_ez_bg").children('img').css({ "top"  : 0, "left" : -this_left });
			}
			if ($("#jq_ez_bg").children('img').height() > elm.height()) {
				var this_height = ($("#jq_ez_bg").children('img').height() - elm.height()) / 2;
				$("#jq_ez_bg").children('img').css({ "left" : 0, "top" : -this_height });
			}
		}

		$("#jq_ez_bg").css({"visibility" : "visible"});
		$("body").css({"overflow":"auto"});
	}

	$(window).bind("resize", function() {resizeImage();});

})(jQuery);
