﻿$.browser.msie6 = $.browser.msie && /MSIE 6\.0/i.test(window.navigator.userAgent) && !/MSIE 7\.0/i.test(window.navigator.userAgent);
$.fn.vCenter = function(options) {
    var pos = {
        sTop: function() {
            return window.pageYOffset || $.boxModel && document.documentElement.scrollTop || document.body.scrollTop;
        },
        wHeight: function() {
            if ($.browser.opera || ($.browser.safari && parseInt($.browser.version) > 520)) {
                return window.innerHeight - (($(document).height() > window.innerHeight) ? getScrollbarWidth() : 0);
            } else if ($.browser.safari) {
                return window.innerHeight;
            } else {
                return $.boxModel && document.documentElement.clientHeight || document.body.clientHeight;
            }
        }
    };

    return this.each(function(index) {
        if (index == 0) {
            var $this = $(this);
            var elHeight = $this.outerHeight();
            $this.css({
                position: 'absolute',
                marginTop: '0',
                top: pos.sTop() + (pos.wHeight() / 2) - (elHeight / 2)
            });
        }
    });
};

$.fn.modalWindow = function(options) {
    var pluginDefault = $.extend({}, $.fn.modalWindow.defaults, options);

    /* The modalWindow element */
    var $thisMW = $('<div />').attr('class', pluginDefault.classname);

    /* The modalWindow element */
    var $content = $('<div />').attr('class', pluginDefault.contentClassname).html(pluginDefault.html).appendTo($thisMW);

    /* The shadow behind the modalWindow */
    var $shadow = $('<div />').attr('id', 'modal-shadow').css('opacity', '0.5').appendTo('body');

    /* The close-button */
    var $close = $('<a />').attr('class', 'modal-close').text('Stäng').appendTo($thisMW);

    if (pluginDefault.roundedCorners === true) {
        var $btr = $('<div />').attr('class', 'btr');
        var $bbr = $('<div />').attr('class', 'bbr');
        var $btl = $('<div />').attr('class', 'btl').appendTo($btr);
        var $bbl = $('<div />').attr('class', 'bbl').appendTo($bbr);
        $thisMW.prepend($btr);
        $thisMW.append($bbr);
    }

    $thisMW.css('width', pluginDefault.contentWidth).prependTo('body');
    pluginDefault.onOpen.call($thisMW[0]);

    if (pluginDefault.center === true) {
        var width = $thisMW.outerWidth();
        var height = $thisMW.outerHeight();
        $thisMW.css({
            position: 'fixed',
            top: '50%',
            left: '50%',
            marginLeft: '-' + (Math.ceil(width / 2)) + 'px',
            marginTop: '-' + (Math.ceil(height / 2)) + 'px'
        });

        /* IE6 Support for vertical-centering (can't use position:fixed) */
        if ($.browser.msie6) {
            $thisMW.vCenter();
            $(window).bind('scroll', function() {
                $thisMW.vCenter();
            });
        }
    }

    $close.add($shadow).bind('click', function() {
        $thisMW.add($shadow).remove();
        pluginDefault.onClose.call($thisMW[0]);
    });

    return $thisMW;
};

$.fn.modalWindow.defaults =
{
    classname: 'modal-window',
    contentClassname: 'modal-content',
    contentWidth: 'auto',
    center: true,
    roundedCorners: true,
    html: '',
    onOpen: function() { },
    onClose: function() { }
};


jQuery(function() {

    $('.thickbox').bind('click', function() {
        $.fn.modalWindow({
            html: '<img src="' + this + '" onload="setSize(this)"/>'
            //,contentWidth: '550px'
        });
        return false;
    });
});
function setSize(objImg) {
    var w = objImg.width;
    var h = objImg.height;
    var ww = $(window).width();  //wWidth();
    var wh = $(window).height(); //wHeight();
    var margin = 50;
    // proportions
    var wide = w >= h ? true : false;
    var ratio = h / w;
    if (wide && w > ww - margin) {
        w = ww - margin;
        h = h * ratio;
    }
    else if (!wide && h > wh - margin) {
        h = wh - margin;
        w = h / ratio;
    }
    
    objImg.width = w;
    objImg.height = h;
    
    var width = $(".modal-window").outerWidth();
    var height = $(".modal-window").outerHeight();

    
    $(".modal-window").css({
        position: 'fixed',
        top: '50%',
        left: '50%',
        marginLeft: '-' + (Math.ceil(w / 2)) + 'px',
        marginTop: '-' + (Math.ceil(h / 2)) + 'px'
    });
    if($.browser.msie6)
        $(".modal-window").vCenter();
    $(".modal-window").fadeIn("slow");
}
function wHeight() {
    if ($.browser.opera || ($.browser.safari && parseInt($.browser.version) > 520)) {
        return window.innerHeight - (($(document).height() > window.innerHeight) ? getScrollbarWidth() : 0);
    } else if ($.browser.safari) {
        return window.innerHeight;
    } else {
        return $.boxModel && document.documentElement.clientHeight || document.body.clientHeight;
    }
}
function wWidth() {
    if ($.browser.opera || ($.browser.safari && parseInt($.browser.version) > 520)) {
        return window.innerWidth - (($(document).width() > window.innerWidth) ? getScrollbarWidth() : 0);
    } else if ($.browser.safari) {
        return window.innerWidth;
    } else {
        return $.boxModel && document.documentElement.clientWidth || document.body.clientWidth;
    }
}