/******************************************************
    * jQuery plug-in
    * Easy Background Image Resizer
    * Developed by J.P. Given (http://johnpatrickgiven.com)
    * Some modifcations by Hay Kranen < http://www.haykranen.nl >
    * Useage: anyone so long as credit is left alone
******************************************************/

(function($) {
    var containerObj, center = true;

    // plugin definition
    $.fn.ezBgResize = function(center) {

        //center = center || false;
        var center = true;
        // First position object
        containerObj = this;

        containerObj.css("visibility","hidden");

        $("body").css({
            "overflow-x":"hidden"
        });

        $(window).load(function() {
            resizeImage();
        });

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


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

    };

    function resizeImage() {

        $("body").css({
            "overflow-x":"auto"
        });

		var offset_top = 0;
		var offset_left = 0;

		var dimension_width = getWindowWidth();
		var dimension_height = getWindowHeight();

		if ($('#body-background').hasClass('project-image-view')) {
			offset_top = 120;
			offset_left = 360;
			dimension_width = (dimension_width-380);
			dimension_height = (dimension_height-175);
		}
		if(!$.browser.msie || $.browser.msie && $.browser.version!="6.0") {
    	    containerObj.css({
    			"position":"fixed"
    	    });
		}

        // Resize the img object to the proper ratio of the window.
        var iw = containerObj.children('img').width();
        var ih = containerObj.children('img').height();
        
        if (iw > ih && dimension_width < 580) {
        	dimension_width = 580;
			containerObj.css({position: 'absolute'});
		}

        containerObj.css({
            "top":offset_top+"px",
            "left":offset_left+"px",
            "z-index":"-1",
            "overflow":"hidden",
            "width": dimension_width + "px",
            "height":dimension_height + "px"
        });
        
        if ($(window).width() > $(window).height()) {

            if (iw > ih) {

                var fRatio = iw/ih;
                containerObj.children('img').css("width",$(containerObj).width() + "px");
                containerObj.children('img').css("height",Math.round($(containerObj).width() * (1/fRatio)));

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

                if(newIh < $(window).height()) {
                	
                    var fRatio = ih/iw;
                    containerObj.children('img').css("height",$(containerObj).height());
                    containerObj.children('img').css("width",Math.round($(containerObj).height() * (1/fRatio)));
                }

            } else {

                var fRatio = ih/iw;
                containerObj.children('img').css("height",$(containerObj).height());
                containerObj.children('img').css("width",Math.round($(containerObj).height() * (1/fRatio)));

            }
        } else {

            var fRatio = ih/iw;
            containerObj.children('img').css("height",$(containerObj).height());
            containerObj.children('img').css("width",Math.round($(containerObj).height() * (1/fRatio)));

        }



		if ($('#body-background').hasClass('project-image-view')) {

			var reposition = 360;
			if (containerObj.children('img').width() < containerObj.children('img').height()) {
				reposition = (((getWindowWidth()-containerObj.children('img').width())/2)+170);
				if (reposition<360) {
					reposition = 360;
				}
			}
			containerObj.css("left", reposition+"px");
		}

        containerObj.css("visibility","visible");
				
        // Center BG Image
        if (center) {

		if ($('#body-background').hasClass('project-image-view')) {
			containerObj.children('img').css("position","relative");
        } else {
	        containerObj.children('img').css("position","absolute");
        }

				var wDiff = 0;

                if (containerObj.children('img').width() > containerObj.width()) {
	                var wDiff = (containerObj.children('img').width() - containerObj.width()) / 2;
    	            containerObj.children('img').css("left", "-" + wDiff + "px");
    	        } else {
    	        	containerObj.children('img').css("left", "0px");
    	        }

                if (containerObj.children('img').height() > containerObj.height()) {
                	var wDiff = (containerObj.children('img').height() - containerObj.height()) / 2;
                	containerObj.children('img').css("top", "-" + wDiff + "px");
    	        } else {
    	        	containerObj.children('img').css("top", "0px");
    	        }

        }
    }

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