/* 
 *   jQuery Easy ModalBox v0.1
 *
 *   Authors:
 *   Danilo Freitas - contato@danilofreitas.com - http://www.danilofreitas.com
 *   Vitor Hidalgo - vitor_hidalgo@hotmail.com - http://www.vitorhidalgo.com.br
 *
 *   Copyright (c) 2009 Danilo Freitas & Vitor Hidalgo
 *
 *   Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */
(function($){
    $.fn.extend({   
        easymodalbox: function(options) {  
   
            var defaults = {  
                bgColor: '#000',
				bgOpacity : 0.5
            };  
               
            var options = $.extend(defaults, options);  
           
            return this.each(function() {  
												
				var htmlOverflowX = $('body').css('overflowX');

				$('html').css('overflowX', 'hidden');
						 
				var o =options;  
				var modalObj = $(this);                  
				var modalObjWidth = modalObj.width();
				var modalObjHeight = modalObj.height();
		
				var documentWidth = $(document).width();
				var documentHeight = $(document).height();

				var cssHtmlRestore = {
					'overflowX' : htmlOverflowX 
				}

				var cssModalObj = {
					'position' : 'absolute',
					'widht' : modalObjWidth + 'px',
					'height': modalObjHeight + 'px',
					'marginLeft' : '-' + modalObjWidth/2 + 'px',
					'marginTop' : '-' + modalObjHeight/2 + 'px',
					'left' : '50%',
					'top' : '50%',
					'zIndex' : '999'
				}
				   
				var cssBoard = {
					'background-color' : o.bgColor,
					'color' : 'rgb(0,40,244)',
					'width' : documentWidth + 'px',
					'height' : documentHeight + 'px',
					'position' : 'absolute',
					'display' : 'none',
					'zIndex' : '998',
					'opacity' : o.bgOpacity,
					'top' : '0px',
					'left' : '0px'
				}
				  
				$('body').prepend('<div id="modalBoard"></div>');
				$('#modalBoard').css(cssBoard).fadeIn('fast');
					
				$(modalObj).css(cssModalObj).fadeIn(function () {
					$(modalObj).animate({
							top: y_fixo+$(document).scrollTop()+"px"
						},{duration:500,queue:false}
					);
				});
					
				var y_fixo = modalObj.offset().top+modalObjHeight/2;
				$(window).scroll(function () {
					$(modalObj).animate({
							top: y_fixo+$(document).scrollTop()+"px"
						},{duration:500,queue:false}
					);
				});
				
				$('a[rel="modalClose"]').click(function(e) {
					$("div#modalBoard").hide('fast').remove();
					$(modalObj).hide();
					$('html').css(cssHtmlRestore);
					e.preventDefault();
				});
				  
            });  
        }  
    });  
})(jQuery);