/*
 * ImageResizer.js
 *
 * @type Object
 * @author Thomas Monzel | Apparat-Hamburg
 * @description Skalieren von Bildern
 */

var ImageResizer = (function() {
    var core = function() {}

    core.scaleAroundCenter = function(image) {
        var dim = this.getResizedDim(image);
        
        // Bild einstellen
        image.css({height: dim.height, width: dim.width, top: dim.top, left: dim.left});

        return dim;
    }

    core.getResizedDim = function(image) {
        // Proportionale Ratio des Browserfensters errechnen
        var navWidth = $(window).width();
        var navHeight = $(window).height();
        var navRatio = navWidth / navHeight;

        // Proportionale Ratio des Bildes errechnen
        if (image.width() > 1) picWidth = image.width();
        if (image.height() > 1) picHeight = image.height();
        picRatio = picWidth / picHeight;

        var newWidth = 0;
        var newHeight = 0;

        if (navRatio > picRatio) {
            //console.log('oben');
            newHeight = (navWidth / picWidth) * picHeight;
            newWidth = navWidth;
        } else {
            //console.log('unten');
            newHeight = navHeight;
            newWidth = (navHeight / picHeight) * picWidth;
        }

        // Zentrieren des Bildes
        newTop = 0 - ((newHeight - navHeight) / 2);
        newLeft =  0 - ((newWidth - navWidth) / 2);

        return {left:newLeft, top:newTop, width:newWidth, height:newHeight};
    }

    return core;
})();
