(function(root, $, undefined) {
    
    var ScalingImage = function(src, originalSize, load) {

        this.originalSize = originalSize;
        this.src = src;
        var self = this;
        this.load = function() {
            $(window).trigger("resize");
            self.scale();
            self.html.parent().css({
                position: root.B.needOverflowY ? "fixed" : "absolute"
            });
            load && load.call(this);
        };
        
        this.html = $("<img/>", {
            src: this.src,
            load: this.load,
            css:{
                position: "relative"
            }
        });
        
    };
    
    ScalingImage.prototype = {
        
        scale: function() {
            
            var windowWidth = $(window).width();
            var windowHeight = $(window).height();
            
            var width = this.getNaturalWidth();
            var height = this.getNaturalHeight();
            
            var aspect = width / height;
            var windowAspect = windowWidth / windowHeight;
            
            this.html.parent().stop();
            
            var minHeight = 100;
            var minWidth = minHeight * aspect;
            
            if(windowWidth < minWidth && windowHeight < minHeight ) {
                this.html.css({
                    height: minHeight,
                    width: "auto"
                });
                this.html.parent().animate({
                    left:0,
                    top:0
                }, 100);
                root.B.hasOverflowY && $("body, html").css("overflow", "scroll");
                return;
            }
            
            //root.B.hasOverflowY && $("body, html").css("overflow", "hidden");
            root.B.needOverflowY && $("body, html").css({"overflow": "auto", "overflow-y": "scroll"});

            if(height * aspect - windowHeight * aspect < width - windowWidth) {
                                   
                this.html.css({
                    height: windowHeight,
                    width: "auto"
                });
                
            } else {

                this.html.css({
                    height: "auto",
                    width: windowWidth
                });
                
            }
            
            if(windowWidth < minWidth || windowHeight < minHeight) {
            
                var self = this;
            
                $("body, html").css({
                    "min-width": minWidth,
                    "min-height": minHeight,
                    "overflow": "scroll"
                });    
                
                this.html.parent().animate({
                    left:0,
                    top:0
                }, 150);
                
                return;
                
            }
            
            this.center.call(this.html.parent());
            
        },
        
        center: function() {
            
            var windowWidth = $(window).width();
            var windowHeight = $(window).height();
            var width = this.css("width");
            var height = this.css("height");
            
            if(width === "100%") {
                return;
            }
            //console.log(width, height, this);
            //this.css("top", -((parseInt(height, 10) - parseInt(windowHeight, 10)) / 2));
            this.css("left", -((parseInt(width, 10) - parseInt(windowWidth, 10)) / 2));
            this.css("top", 0);
            
        },
        
        getNaturalWidth: function() {
            
            return this.originalSize.width;
            
        },

        getNaturalHeight: function() {
            
            return this.originalSize.height;

        }
        
    };
    
    root.B.ScalingImage = ScalingImage;
    
})(this, jQuery);
