var MAX_WIDTH = 600;
var MAX_HEIGHT = 600;
var OPACITY = .8;
var container = $('<div id="popup_container"></div>');
var dimmer = $('<div id="dimmer"></div>');
var positioner = $('<div id="positioner"></div>');
var closeButton = $('<div id="close_button">&#x2297; <a href="#">Close</a></div>');

/* Popup plugin used across the website */

(function($) {
	$(function() {
		positioner.prependTo('body').click(function() {
			if (container.is(':visible')) {
				close();				
			}
		});
		closeButton.prependTo(positioner).css('opacity', .85).click(function() {
			close();
			return false;
		});
		container.prependTo(positioner);
		dimmer.prependTo('body').css('opacity', .85).click(function() {
			if (container.is(':visible')) {
				close();				
			}
		});
		
		// Mimicking fixed positining for IE6
		if ($.browser.msie && $.browser.version.substr(0,1) < 7) {	
			dimmer.height($(document).height());
			positioner.height($(window).height());
			
			$(window).resize(function() {
				positioner.height($(window).height());				
			});
					
			$(window).scroll(function() {
				positioner.css('top', $(window).scrollTop() + 'px');
			});
		}

		// Hide the container and the dimmer
		positioner.hide();
		dimmer.hide();
		
		container.hover(function() {
			positioner.unbind('click');
		}, function() {
			positioner.click(function() {
				close();				
			});
		});
	});
	
	// function initializeLinks() {
	// 	$('a[rel^=popup]').each(function() {
	// 		$(this).optPopup($(this).attr('href'), $(this).metadata());
	// 	});		
	// }
	
	function close() {
		positioner.fadeOut(800, function() {
			container.html('');
			container.hide();
		}).hide();
		dimmer.fadeOut(800);
	}
	
	$.fn.optPopup = function(url, options) {		
		options = jQuery.extend({
			width: 'auto',
			height: 'auto',
			draggable: false,
			iframe: false,
			closeButton: true,
			animateIn: 'fadeIn',
			animateOut: 'fadeOut',
			parameters: {}	
		}, options || {});
		
		function _initialize() {
			container.animate({ opacity: 0 }, 200).show();
			dimmer.fadeIn('fast');
			positioner.fadeIn('fast');
			
			if ($.browser.msie && $.browser.version.substr(0,1) < 7) {
				positioner.css('top', $(window).scrollTop() + 'px');
			}
			// loader.fadeIn('fast');
			
			if (options.closeButton == true || options.iframe == true ) {
				closeButton.fadeIn('fast');				
			}
			
			container.width(options.width).height(options.height);

			if (options.iframe == true) {
				container.html('<iframe src="' + url + '" width="' + options.width + '" height="' + options.height + '" scrolling="auto" frameborder="0"></iframe>').css('overflow', 'hidden');

				container.css('margin-left', options.width / -2);
				container.css('margin-top', options.height / -2);

				container.css('opacity', 0).show();
				container.animate({ opacity: 1 }, 500);
			}
			else {
				$.get(url, function(data) {
					container.html(data);

					container.css('margin-left', container.width() / -2);
					container.css('margin-top', container.height() / -2);
					
//					initialize();
					
					$('#print_block a').click(function() {
						window.print();
					});
					
					if (options.width == 'auto' || options.height == 'auto') {
						var popup = container.find('div');
						
						var contentWidth = popup.outerWidth({ margin: true });
						var contentHeight = popup.outerHeight({ margin: true });

						var popWidth = contentWidth < MAX_WIDTH ? contentWidth : MAX_WIDTH;
						var popHeight = contentHeight < MAX_HEIGHT ? contentHeight : MAX_HEIGHT;

						if (options.width == 'auto')
							container.width(popup.outerWidth({ margin: true }));
						if (options.height == 'auto')
							container.height(popup.outerHeight({ margin: true }));				
					}
					
					// alert(container.find('div:first').outerHeight({ }) + ', ' + container.height());
					container.css('opacity', 0).show();
					container.animate({ opacity: 1 }, 500);
				});
			}
			
			return false;
		}
		return this.unbind('click').click(_initialize);
	};
})(jQuery);
