(function($) {
	$.htmlPopup = function(html, options) {
		var settings = $.extend({
			height: '250',
			width: '500',
			close_selector: '.close_popup',
		        top : null
		}, options);

		var $fade = $(document.createElement('div'));
		$fade
			.css('position', 'absolute')
			.css('top', '0')
			.css('z-index', '999')
			.css('background-color', '#aaa')
			.css('width', '100%')
			.css('height', $(document).height() + 'px')
			.css('opacity', '0.6')
			.hide();

		var $popup = $(document.createElement('div'));
		var pop_top = parseInt((parseInt($(window).height()) - parseInt(settings.height)) / 2);
		pop_top += parseInt($(window).scrollTop());

		if(null !== settings.top) {
			pop_top = parseInt(settings.top) + parseInt($(window).scrollTop());
		}

		var pop_left = parseInt((parseInt($('body').width()) - parseInt(settings.width)) / 2) - 26;
		$popup
			.addClass('popup')
			.css('position', 'absolute')
			.css('left', parseInt(pop_left) + 'px')
			.css('width', settings.width + 'px')
			//.css('height', settings.height + 'px')
			.css('background', '#fff')
			.css('z-index', '1000')
			.css('-moz-border-radius', '1px')
			.css('-webkit-border-radius', '1px')
			.css('top', pop_top + 'px')
			.css('opacity', '1.0')
			.prependTo($('body'))
			.hide();

		$popup.html(html);

		$popup.fadeIn('fast');
		$fade.prependTo($('body')).fadeIn('fast');

		$popup.find(settings.close_selector).click(function() {
			$popup.fadeOut('fast', function() {
				$(this).remove();
			});
			$fade.fadeOut('fast', function() {
				$(this).remove();
			});
			return false;
		});
		
		$fade.click(function() {
			$popup.fadeOut('fast', function() {
				$(this).remove();
			});
			$fade.fadeOut('fast', function() {
				$(this).remove();
			});
			return false;	
		});
	};

	$.popup = function(url, options) {
		$.get(url, null, function(data) {
			$.htmlPopup(data, options);
		}, 'html');
	};
})(jQuery);

