//
//create closure
//

(function($) {
    //
    // plugin definition
    //
    $.fn.modal = function(options) {


        // plugin defaults
        $.fn.modal.defaults = {
            width: 250,
            height: 250
        };

        //debug(this);
        //Create our overlay object
        var overlay = $("<div id='modal-overlay'></div>");
        var modalWindow = $("<div id='modal-window'></div>");
        var closeButton = $("<a href='#' id='btn-close'>Close</a>");

        // build main options before element iteration
        var opts = $.extend({}, $.fn.modal.defaults, options);

        // iterate and reformat each matched element
        return this.each(function() {

            $(this).click(function(e) {

                //Append the overlay to the document body
                $("body").append(overlay.click(function() { close(); }));

                //Set the css and fade in our overlay
                overlay.css("opacity", 0.8).fadeIn(800);

                //Add Model Window
                $("body").append(modalWindow);

                modalWindow.css("width", opts.width);
                modalWindow.css("height", opts.height);

                var vHref = $(this).attr("href");

                //Check for Youtube Link
                if (vHref.indexOf("http://www.youtube.com/watch") != -1) {
                    modalWindow.append("<div id='player'></div>");
                    var vVideoID = vHref.substring(vHref.indexOf("v=") + 2);
                    modalWindow.youtube({ container: 'player', width: 640, height: 393, VideoID: vVideoID });
                }

                modalWindow.center();

                modalWindow.fadeIn(1000);

                //Add Close button and position closeButton right top corner to Modal Window

                $("body").append(closeButton.click(function(ce) { ce.preventDefault(); close(); }));
                
                closeButton.css("left", $(modalWindow).position().left + ($(modalWindow).width() - $(closeButton).width()));
                closeButton.css("top", $(modalWindow).position().top - $(closeButton).height());
                
                closeButton.fadeIn('slow');
                //Prevent the anchor link from loading
                e.preventDefault();

                //Activate a listener 
                $(document).keydown(handleEscape);

            });
        });

        //For hiding the modalbox
        function close() {
            $(document).unbind("keydown", handleEscape)
            var remove = function() {
                $(this).remove();
            }
            overlay.fadeOut(800, remove);
            modalWindow.fadeOut(300, remove).empty();
            closeButton.fadeOut(200, remove).empty();

        }

        //Function listens for escape key.
        function handleEscape(e) {

            if (e.keyCode == 27) { close(); }

        }
    };

    //
    // end of closure
    //
})(jQuery);
