
window.Popup = {

	init: function() {
		$('body').append('<div id="popup-overlay"></div>');
		$('body').append('<div id="popup"></div>');
		$('#popup-overlay').css({
			background: '#000',
			opacity: '0.5',
			filter: 'alpha(opacity=50)',
			position: 'absolute',
			left: '0',
			top: '0',
			display: 'none',
			zIndex: 1000
		});
		$('#popup').css({
			position: 'absolute',
			border: '2px solid #666',
			background: '#FFF',
			zIndex: 1001,
			padding: 10,
			display: 'none'
		});
	},

	window: function() { return Popup.$window ? Popup.$window : Popup.$window = $('#popup'); },
	overlay: function() { return Popup.$overlay ? Popup.$overlay : Popup.$overlay = $('#popup-overlay'); },

	show: function(options) {
		options.width = options.width || 350;
		
		$('select:not(#popup select)').css('visibility', 'hidden');

		Popup.overlay().show();
		Popup.resizeOverlay();
		
		if(!options.modal) {
			Popup.overlay().bind('click', Popup.close);
		} else {
			Popup.overlay().unbind('click', Popup.close);
		}

		Popup.window().html('');
		
		if(options.url) {
			if(options.iframe)
			{
				Popup.window().html('<iframe scrolling="no" border="0" frameborder="0" width="100%"></iframe>');
				$('#popup iframe')[0].src = options.url;
				if(options.height) $('#popup iframe').css({ height: options.height+'px' });
			}
			else
			{
				Popup.window().addClass('loading');
				Popup.window().load(options.url, options.data, function() { Popup.window().removeClass('loading') });
			}
		}
		else if(options.html) {
			Popup.window().html(options.html);
		}

		Popup.showPopup(options.width, options.height);

	},

	showPopup: function(width, height) {
		var popup = Popup.window();
		popup.show();
		popup.css({ width: width+'px', height: height === undefined ? 'auto' : height+'px' });
		Popup.positionPopup();
	},

	positionPopup: function(width) {
		var top = $(window).scrollTop() + ($(window).height()/2) - (Popup.window().outerHeight()/2);
		Popup.window().css({ top: top , left: $(window).width() / 2 - Popup.window().outerWidth() / 2 });
	},

	resizeOverlay: function() {
		if(Popup.overlay().css('display') != 'block') return;
		Popup.overlay().css({ height: $(document).height(), width: $(document).width(), top: 0 });
		Popup.positionPopup();
	},

	close: function() {
		$('select:not(#popup select)').css('visibility', 'visible');
		Popup.window().hide();
		Popup.overlay().hide();
	},


	windowH: function() {
		return Math.max($('body').outerHeight(true), window.innerHeight || document.documentElement.clientHeight);
	},
	windowW: function() {
		return Math.max(window.innerWidth ? window.innerWidth : 0, document.documentElement.clientWidth, document.body.clientWidth);
	},
	scrollTop: function() {
		return document.body.scrollTop || document.documentElement.scrollTop;
	}

};

$(function() {
	Popup.init();
	window.onresize = Popup.resizeOverlay;

	$('a[rel=popup]').click(function() {
		Popup.show({
			iframe: true, 
			url: this.href,
			width: $(this).attr('width'),
			height: $(this).attr('height')
		});
		return false;
	});
});

