﻿jQuery.fn.r2iModal = function(command, el, settings) {
    switch (command) {
        case 'create':
            settings = jQuery.extend({
	            modal: $(el),
	            command: 'create'
            }, settings);
            return this.each(function(){
                if (settings.modal !== false) {
                    $(this).click(function(event){
                        event.preventDefault();
                        event.stopPropagation();
                        var viewportWidth = $(window).width();  
                        var viewportHeight = $(window).height();
                        var documentWidth = document.body.offsetWidth;  
                        var documentHeight = document.body.offsetHeight;
                        var modalBackground = $('#modalBackground-'+settings.modalBackgroundSuffix);
                        /*settings.modal.click(function(){
                        alert('foo');
                            overlay.remove();
                            modalBackground.css('display', 'none');
                            settings.modal.css('left', '-10000px');
                        });*/
                        var html = $('html');
                        var body = $('body');
                        body.prepend('<div id="overlay"></div>');
                        var overlay = $('#overlay');
                        //body.supersleight();
                        
                        settings.modal.css({
                            left: parseInt(viewportWidth / 2 - settings.modal.width() / 2),
                            top: parseInt(viewportHeight / 2 - settings.modal.height() / 2)
                        });
                        settings.modal.attr('src', settings.modal.attr('src'));
                        modalBackground.css({
                            left: parseInt(viewportWidth / 2 - settings.modal.width() / 2),
                            top: parseInt(viewportHeight / 2 - settings.modal.height() / 2)
                        });
                        overlay.css({
                            left: 0,
                            top: 0,
                            height: documentHeight,
                            width: documentWidth
                        });
                        /* non-animated */
                        overlay.css({
                            display:'block',
                            opacity: 0.6
                        });
                        modalBackground.css({
                            display:'block'
                        })
                        settings.modal.css({
                            //display:'block'
                            //left:0px
                        });
                        settings.modal[0].focus();
						if (settings.modal.contents().find('#txtEmailAddress')[0]) {
							settings.modal.contents().find('#txtEmailAddress')[0].focus();
						}
                        /*overlay.css({
                            display:'block',
                            opacity: 0.0
                        }).animate({
                            opacity: 0.6
                        }, 500, 'linear', function(){
                            modalBackground.css({
                                display:'block'
                            })
                            settings.modal.css({
                                display:'block'
                            });
                        });*/
                    });
                }
            });
            break;
        case 'remove':
            settings = jQuery.extend({
	            modal: $(this),
	            command: 'create'
            }, settings);
            return this.each(function(){
                if (settings.modal !== false) {
                    var modalBackground = $('#modalBackground-'+settings.modalBackgroundSuffix);
                    var overlay = $('#overlay');
                    modalBackground.css('display', 'none');
                    settings.modal.css('left', '-10000px');
                    overlay.remove()
                }
            });
            break;
        default:
            break;
    }
}