/* 
* Part of Nitriques Solutions inc. (http://www.nitriques.com) jQuery plug-in bundle
* Liscence under the The Code Project Open License (CPOL)
* http://www.codeproject.com/info/cpol10.aspx

* Name: jquery.modal.js
* Date: 2010-01-08
* Version: 1.0

* Pre-requisites: 
*   jquery.center.js version 1.1

* v1.1
* Added safe block (function ($) {...})(jQuery) for noConflict
* Removed the jquery.scroll.js version 1.0 pre-requisite since this can be done usign css only

* v1.0 
* Original release

* Copyrights (c) Solutions Nitriques inc.
*/
(function ($) {
$.fn.extend({ // jQuery plugin
    ntr$modal: function(b,b2) {
        var t = $(this); // div
        var vp = 'isVisible';
        var bg;

        function setVisible(v) { t.data(vp, v); }
        function getVisible() { return t.data(vp); }

        function show() {
            var o = { opacity: 0.6 };
            var d = { duration: 800 };

            // add background to DOM
            bg.appendTo('body');
            //$(bg).click(toggle); // debug purpose

            // scroll background
            //bg.ntr$scroll();

            // show modal
            t.show();

            // center it: needs jquery.center.js
            t.ntr$center();

            // animate background
            bg.animate(o, d.duration, 'swing', function() { }); // Don't know why, but the other signature creates too much recursion

            o.opacity = 1;
            // animate modal
            t.animate(o, d);

            // Set flag
            setVisible(1);
            return this;
        }

        function hide() {
            return hide(500);
        }
        function hide(dur) {
            var o = { opacity: 0 };

            // animate background, then remove it from DOM
            bg.animate(o, dur, 'swing', function() { bg.remove() });

            // animate modal, then hide it
            t.animate(o, dur, 'swing', function() { t.hide(); });

            // Set flag
            setVisible(0);
            return this;
        }

        function toggle() {
            if (getVisible() == 1) { hide(); } else { show(); }
            return false;
        }

        function createBackground() {
            return $('<div id="ntr$modal$bg" style="width:100%;height:100%;background-color:#000000;position:absolute;top:0;left:0;z-index:999998;"></div>');
        }

        // Dynamic background
        bg = createBackground();

        hide(0); // hide with no delay to set css values

        // register toggle on bouton(s)
        $(b).click(toggle);
        if (b2) {
            alert(b2);
            $(b2).click(toggle);
        }

        //register for scroll
        /*$(window).scroll(function() {
            //bg.ntr$scroll();
            t.ntr$center();
        });*/

        //register for resize
        /*$(window).resize(function() {
            t.ntr$center();
        });*/

        return this; // return jQuery object
    }
});
})(jQuery);
