/* This is the generic modal window frame. You can pass in options 
 * to override the default values. The popContent param is what fills the modal window.
 * @param (optional): { windowTitle: value, buttonsArray: value, popContent: value }
 * requires: jQuery
 */
(function($){ 

    $.fn.modalClose = function(options){
        $('.modal-background').remove();
        $('.pop-container').remove();
    };

    $.fn.modal = function(options){
        //private vars
        var popup = document.createElement('div'),
            content = document.createElement('div'),
            background = document.createElement('div'),
            controls = document.createElement('fieldset');
            $(controls).addClass('controls');
        
        //set the default popup options
        var defaults = {
                windowTitle: '<h3 class="hdr-therapists"><span>THERAPISTS</span></h3>',
                buttonsArray: ['<button class="prev"><span>PREV</span></button>', '<button class="next"><span>NEXT</span></button>'],
                popContent: null
            };
        
        //extend options with defaults
        var options = $.extend(defaults, options);
        
        //add all the buttons
        for(var i in options.buttonsArray ){
            $(controls).append(options.buttonsArray[i]);
        }
        
        //create the opaque background
        $(background).attr('class', 'modal-background');
        $(background).css('height', $(window).height());
        
        //create the content of the popup
        $(content).attr('class', 'pop-content');
        $(content).append(options.popContent);
        
        //create the popup
        $(popup).attr('class', 'pop-container');
        $(popup).append(options.windowTitle);
        $(popup).append('<a href="#" class="close"><span>Close</span></a>');
        $(popup).append(content);
        if(options.buttonsArray !== null){
            if(options.buttonsArray.length > 0){
                $(popup).append(controls);
            }
        }
        $('body').append(background);
        $(background).css('height', $(document).height());
        
        $('body').append(popup);
        $(popup).center();
        $(popup).fadeIn('slow');

        $('.close,.modal-background').click(function(evt){
            evt.preventDefault();
            $(popup).fadeOut('slow',function(){
                $().modalClose();
            });
        });
        
        /*$(window).resize(function(){
            $(background).css('height', $(document).height());
            $(popup).css('top', (($(window).height() * .5) - ($(popup).height() * .5)));
        });*/
        
    };
    
    $.fn.modalChange = function(options){
        var defaults = {
            popHtml: null
        };
        
        //extend options with defaults
        var options = $.extend(defaults, options);
        
        $('.pop-content').html(options.popHtml);
    };
    
})(jQuery);
