(function($) {

	$.fn.popupbox = function(options) 
	{
		
		var options = $.extend({}, $.fn.popupbox.defaults, options); 
		  
		return this.each(function() 
		{
			  alert('teste');
		});  
		
	};
	
	$.popupbox = function(texto, options)
	{
		
		var options = $.extend({}, $.fn.popupbox.defaults, options);
		
		//console.log('Length popupbox: '+$('#popupbox').length);
		
		if ($('#popupbox').length<=0 || $('#popupbox').css('display')=='none' )
		{
			modalHeight = document.body.scrollHeight + 'px';	
			$('#transparent-modal').css({
				'opacity': 0,
				'background': '#000000',
				'position': 'absolute',
				'width': '100%',
				'height': modalHeight,
				'color': '#ffffff',
				'left': '0px',
				'top': '0px',
				'display': 'block',
				'cursor': 'pointer',
				'z-index': '50'
			}).fadeTo(250, .6).unbind('click').click(function(){
				$.popupbox.close();
			});

			$('html, body').animate({scrollTop:0}, 'slow', function()
			{
			
				$('body').append(options.popupboxHtml);
				
				$('#popupbox .content').css({ width: 0, height: 0 });
				
				popupsize_w = $('#popupbox').outerWidth();
				popupsize_h = $('#popupbox').outerHeight();
				window_w = $(window).width();
				window_h = $(window).height();
				
				mt = Math.round(0-(popupsize_h/2));
				ml = Math.round(0-(popupsize_w/2));
				
				$('#popupbox').css(
				{
					'top': '50%',
					'left': '50%',
					'margin-top': mt,
					'margin-left': ml
				}).show();				
				
			});
			
		}
		
		next_mt = 0 - Math.round((options.height / 2));
		next_ml = 0 - Math.round((options.width / 2));

		ml = $('#popupbox').css('margin-left');
		mt = $('#popupbox').css('margin-top');

		//console.log(ml+' to '+next_ml+' :: '+mt+' to '+next_mt+' ['+options.width+'x'+options.height+']');

		setTimeout(function(){

			$('#popupbox .content').stop();
			$('#popupbox').stop();
			
			$('#popupbox .content').fadeTo(125, 0, function(){
				
				$('#popupbox .content').animate(
					{
						'width': options.width, 'height': options.height
					}, 
					{
						queue: false, 
						duration: 125, 
						complete: function() {
							$(this).html(texto).fadeTo(125,1);
						} 
					}
				);
				
				$('#popupbox').animate({
					'margin-left': next_ml, 'margin-top': next_mt
				}, { queue: false, duration: 125 });

			});
			
		}, 50);
		
	};

	$.popupbox.close = function()
	{
		$('#popupbox').fadeOut(250, function()
			{ 
				$('#popupbox').remove();
				$('#transparent-modal').animate({'opacity':0}, { duration: 250, queue: false }).css('display', 'none');
			}
		);
	}
	
	$.fn.popupbox.defaults = {
		width: 500,
		height: 50,
		popupboxHtml  : '\
<div id="popupbox" style="display:none;"> \
  <div class="popup"> \
    <div class="content"> \
    </div> \
  </div> \
</div>'		
	};
	
})(jQuery);
