jQuery(document).ready(function () {

	/* Popup */
	var Popup = {
		visible: false,
		node: jQuery('div.popup').appendTo(document.body),
		nodeOverlay: jQuery('div.popup-back').appendTo(document.body),
		nodeIeButton: jQuery('a.popup-button-ie6').appendTo(document.body),
		
		//Show popup
		show: function () {
			this.node.show();
			this.nodeOverlay.show();
			
			var scroll = jQuery(document).scrollTop();
			var w = [jQuery(window).width(), jQuery(window).height()];
			var p = [this.node.outerWidth(), this.node.outerHeight()];
			var b = [jQuery(document.body).width(), jQuery(document.body).height()];
			
			this.node.css({
				left: ~~((w[0]-p[0])/2) + 'px',
				top:  ~~((w[1]-p[1])/2) + scroll + 'px'
			});
			
			this.nodeOverlay.css({
				width: b[0] + 'px',
				height: b[1] + 'px'
			});
			
			if (jQuery.browser.msie && jQuery.browser.version == 6) {
				var btn = Popup.node.find('div.buttons a').offset();
				this.nodeIeButton.show().css({
					left: btn.left + 'px',
					top:  btn.top + 'px'
				});
			}
			
			this.visible = true;
		},
		
		//Hide popup
		hide: function () {
			this.node.hide();
			this.nodeOverlay.css({width: 'auto', height: 'auto'}).hide();
			this.nodeIeButton.hide();
			this.visible = false;
		},
		
		//Set message
		setMessage: function (msg) {
			this.node.find('p').html(msg);
			
			if (msg.length > 250) {
				this.node.addClass('popup-text-long');
			} else {
				this.node.removeClass('popup-text-long');
			}
			
			_typeface_js.replaceText(this.node.find('p').get(0));
			
			if ($.browser.msie && parseInt($.browser.version) <= 7) {
				var links = this.node.find('p').find('a');
					links.css('display', 'inline-block');
					
				var sup = links.find('sup');
					sup.css('display', 'inline-block');
				
				setTimeout(function () {
					sup.css('display', 'inline');
					links.css('display', 'inline');
				}, 100);
			}
		},
		
		//Constructor
		init: function () {
			Popup.node.find('div.buttons a')
			.add(Popup.nodeIeButton).click(function () {
				Popup.hide();
				return false;
			});
			
			jQuery(window).bind('resize', function () {
				if (Popup.visible) Popup.show();
			});
		}
	};
	
	Popup.init();
	window.Popup = Popup;
	
});
