﻿/* 
Wondercode popup
Author: Eilev Hagen
Version: 0.2
*/

(function ($) {
    $.fn.popup = function (width, height) {
        var w = width || Math.round($(window).width() * 0.8);
        var h = height || Math.round($(window).height() * 0.8);
        WcPopup.show($(this).html(), w, h);
    };
})(jQuery);

(function ($) {
    $.fn.imagePopup = function () {
        $(this).find("a[href$='.jpg'] img[src$='.jpg'], a[href$='.jpeg'] img[src$='.jpeg'], a[href$='.png'] img[src$='.png'], a[href$='.gif'] img[src$='.gif']").parent().addClass("imagePopup");
    };
})(jQuery);

$(document).ready(function () {
    $("a.imagePopup").live("click", function () {
        WcPopup.showLoader(); // start loader
        var _this = this;
        var href = $(this).attr('href');
        var alt = $(this).find("img").attr('alt');

        var img = new Image();
        $(img).load(function () {
            var w = this.width;
            var h = this.height;

            var mw = Math.round($(window).width() * 0.9);
            var mh = Math.round($(window).height() * 0.9);

            var size = calcNewDimensions(w, h, mw, mh);
            var width = size[0];
            var mwidth = width;
            var height = size[1];
            var mheight = height;

            var nextCb, prevCb;
            var allPopups = $("a.imagePopup"); // todo: unique href
            if ($(allPopups).size() > 1) {
                // next element
                var next = $(allPopups).first();
                var prev = $(allPopups).last();

                var tmpPrev;
                $(allPopups).each(function (n, val) {
                    if (val == _this && tmpPrev) {
                        prev = tmpPrev;
                    }
                    if (tmpPrev == _this) {
                        next = val;
                    }
                    tmpPrev = val;
                });

                nextCb = function () { $(next).click(); }
                prevCb = function () { $(prev).click(); }
            }

            // remove loader
            WcPopup.show("<img src=\"" + href + "\" alt=\"" + alt + "\" style=\"width: " + mwidth + "px; height: " + height + "px; float: left;\" />", width, height, nextCb, prevCb);
        }).attr('src', href);
        return false;
    });
});

/* 
General popup
*/
var WcPopup = new function WcPupopInternal() {
    var _this = this;
    this.inEffect = false;
    this.isOpen = false;

    this.showLoader = function () {
        if (_this.inEffect)
            return false;

        if ($("#WcPopup").size() == 0) {
            $("form").append("<div id=\"WcPopup\"></div><div id=\"WcPopupNext\">&#187;</div><div id=\"WcPopupPrev\">&#171;</div><div id=\"WcPopupContent\"><a href=\"#\" id=\"WcPopupClose\">x</a><div class=\"inner\"></div></div>");

            $(window).bind('esc-click', function () { // close on esc
                _this.hide();
            });
            $("#WcPopup,#WcPopupClose").click(function () { // close on click on bg
                _this.hide();
                return false;
            });
        }

        $("#WcPopup").fadeIn();
        return true;
    };

    this.show = function (content, width, height, nextCb, prevCb) {
        if (_this.inEffect) {
            setTimeout(function () { _this.show(content, width, height, nextCb, prevCb); }, 500);
            return;
        }
        _this.isOpen = true;
        this.showLoader();

        $("#WcPopupNext,#WcPopupPrev,#close").unbind('click');
        if (nextCb) {
            $("#WcPopupNext").show();
            $("#WcPopupNext").click(function () { // close on esc
                nextCb();
            });
        }
        if (prevCb) {
            $("#WcPopupPrev").show();
            $("#WcPopupPrev").click(function () { // close on esc
                prevCb();
            });
        }

        _this.inEffect = true;
        $("#WcPopupContent .inner").html(content);
        $("#WcPopupContent")
            .css({ width: 0, height: 0, left: Math.round($(window).width() / 2) + "px", top: Math.round($(window).height() / 2) + "px" })
            .show()
            .animate({
                width: width + "px",
                height: height + "px",
                left: Math.round(($(window).width() - width) / 2) + "px",
                top: Math.round(($(window).height() - height) / 2) + "px"
            }, 500, function () {
                $("#WcPopupContent .inner").fadeIn(300, function () { _this.inEffect = false; });
            });

        return false;
    };

    this.hideContent = function (cb) {
        _this.inEffect = true;
        $("#WcPopupContent .inner").fadeOut(100);
        $('#WcPopupContent').animate({
            width: 0,
            height: 0,
            left: Math.round($(window).width() / 2) + "px",
            top: Math.round($(window).height() / 2) + "px"
        }, 500, function () {
            $('#WcPopupContent').hide();
            $("#WcPopupContent .inner").hide();
            $("#WcPopupContent .inner").html("");
            _this.inEffect = false;
        });
    }

    this.hide = function () {
        $("#WcPopupNext, #WcPopupPrev").fadeOut(300);
        $("#WcPopup").fadeOut(300, function () {
            _this.hideContent();
            _this.isOpen = false;
        });
    };
};

function calcNewDimensions(w, h, maxWidth, maxHeight) {
    if (w < maxWidth && h < maxHeight)
        return [w, h];
    var scaleW = maxWidth / w;
    var scaleH = maxHeight / h;
    if (scaleW < scaleH)
        return [Math.round(w * scaleW), Math.round(h * scaleW)];
    else
        return [Math.round(w * scaleH), Math.round(h * scaleH)];
}
