/*
 * Modal - jQuery plugin
 * Clean and simple lightbox / modal dialog alternative
 *
 * Copyright (c) 2011-2012 Redflow Inc.
 *
 * Requires: jQuery v1.6+
 */

;(function($) {
    // Namespace object
    $.modal = {};

    // Flag to see if the container exists
    $.modal.isContainerCreated = false;

    // jQuery object of the container's div element
    $.modal.container = undefined;

    // Register for the resize event to re-center the dialog
    $(window).resize(function() {
        if ($.modal.isContainerCreated) {
            $.modal.container.children().center();
        }
    });

    /*
    * New jQuery function to open a dialog upon click on an anchor element
    */
    $.fn.modal = function() {
        var href = $(this).attr("href");

        if (!$.modal.isContainerCreated) {
            var containerID = "dialog";

            $("body").append("<div id=\"" + containerID + "\">\n<div class=\"content\"></div>\n");

            // Store the jQuery object of the container for later use
            $.modal.container = $("#" + containerID);

            // Make the container invisible for now
            $.modal.container.hide();
        }

        if (href && href.length) {
            var errorMessage = "<p>Valitettavasti pyydettyä sisältöä ei voida ladata. Ole hyvä ja yritä myöhemmin uudelleen.</p>";

            if ($.modal.isContainerCreated) {
                // Make AJAX call to load the content to the first child (ie. the content div)
                $.modal.container.children().first().children("#person").load(href + " #person > *", function(response, status, xhr) {
                    if (status == "error") {
                        $(this).html(errorMessage);
                    }

                    // Once the AJAX is done, show the dialog
                    $.modal.show();
                });
            } else {
                // Make AJAX call to load the content to the first child (ie. the content div)
                $.modal.container.children().first().load(href, function(response, status, xhr) {
                    if (status == "error") {
                        $(this).html(errorMessage);
                    }

                    // Once the AJAX is done, show the dialog
                    $.modal.show();
                });
            }
        }
    };

    // Add to jQuery so it is reusable (cannot be chained!)
    $.fn.center = function() {
        this.css("position", "absolute");
        this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
        this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");

        return this;
    };

    /*
    * Close the modal dialog.
    */
    $.modal.close = function() {
        if (!$.modal.container) {
            return;
        }

        // Fade out and delete the container
        $.modal.container.stop().fadeOut(function() {
            // Delete once fade out is done
            $.modal.container.remove();
        });

        $.modal.isContainerCreated = false;
    };

    /*
    * Show the modal dialog.
    */
    $.modal.show = function() {
        if ($.modal.isContainerCreated) {
            return;
        }

        $.modal.container.children().first().prepend("<a href=\"#\" class=\"close\" title=\"Close\">Close</a>\n");
        $.modal.container.fadeIn();
        $.modal.isContainerCreated = true;
    };
})(jQuery);

