//ModalPop
//Author: Owain Lewis
//Author URL: www.Owainlewis.com
//Simple Modal Dialog for jQuery
//The idea here was to keep this plugin as lightweight and easy to customize as possible
//You are free to use this plugin for whatever you want.
//If you enjoy this plugin, I'd love to hear from you

(function () {

    jQuery.fn.modalpop = function (options) {
        var defaults = {
            speed: 500,
            center: true
        };

        var options = $.extend(defaults, options);
        var width = $(window).width();
        //Get the full page height including the scroll area
        var height = $(document).height();
        jQuery('body').prepend("<div id='mask'></div>");
        jQuery('#mask').css('height', height);
        jQuery('#mask').css('width', width);

        return this.each(function () {

            jQuery(this).click(function () {
                $this = jQuery(this);
                var id = $this.attr('href');
                //Get the window height and width
                var winH = height;
                var winW = width;

                //Set the popup window to center if required
                if (defaults.center == true) {
                    centerElement($this);
                } else {
                    $(id).css('top', 200);
                }
                $(id).css('left', winW / 2 - $(id).width() / 2);
                jQuery('#mask').fadeIn(defaults.speed);
                jQuery(id).fadeIn(defaults.speed);
                return false;
            });

            jQuery('#mask').click(function () {
                jQuery(this).fadeOut(defaults.speed);
                jQuery('.window').fadeOut(defaults.speed);
                jQuery('#mask').fadeOut(defaults.speed);
            });

            jQuery('.close').click(function () {
                jQuery('.window').fadeOut(defaults.speed);
                jQuery('#mask').fadeOut(defaults.speed);
                return false;
            });

        });
    };

})(jQuery);


var maskExists = false;
(function () {

    jQuery.fn.showDialog = function (options) {

        var defaults = {
            speed: 500,
            center: true
        };

        var options = $.extend(defaults, options);
        //Get the full page height including the scroll area
        var height = $(document).height();
        var width = $(window).width();
        if (!maskExists) {
            jQuery('body').prepend("<div id='mask'></div>");
            jQuery('#mask').css('height', height);
            jQuery('#mask').css('width', width);
            maskExists = true;
        }

        return this.each(function () {
            $this = jQuery(this);
            //Get the window height and width
            var winH = height;
            var winW = width;

            //Set the popup window to center if required
            if (defaults.center == true) {
                $this.css('top', winH / 2 - $this.height() / 2);
            }
            $this.css('left', winW / 2 - $this.width() / 2);
            jQuery('#mask').fadeIn(defaults.speed);
            $this.fadeIn(defaults.speed);

            jQuery('#mask').click(function () {
                jQuery(this).fadeOut(defaults.speed);
                jQuery('.window').fadeOut(defaults.speed);
                jQuery('#mask').fadeOut(defaults.speed);
            });

            jQuery('.close').click(function () {
                jQuery('.window').fadeOut(defaults.speed);
                jQuery('#mask').fadeOut(defaults.speed);
                return false;
            });
            jQuery(window)
            .scrollTop(0)
            .scrollLeft(0);
        });
    };

})(jQuery);

$(document).ready(function () { $(".window").prepend("<a href='#' class='close'><img src='/Scripts/jqmodal/img/close.png' alt='close icon' /></a>"); $(".window").resize(function () { setTimeout(function () { centerElement($this); }, 100); }); });

function centerElement(el) {
    //Get the full page height including the scroll area
    var height = $(window).height();
    var width = $(window).width();

    //Set the popup window to center if required
    var top = height / 2 - el.height() / 2;
    if (top < 50)
        top = 50;
    el.css('top', top);
    el.css('left', width / 2 - el.width() / 2);
    jQuery(window)
    .scrollTop(0)
    .scrollLeft(0);
}
