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"></div>');
 
/* Popup plugin used across the website */
 
(function($) {
	$(function() {
		positioner.prependTo('body').click(function() {
			if (container.is(':visible')) {
				close();				
			}
		});
		
		closeButton.prependTo(positioner).css('opacity', 0).click(function() {
			close();
			return false;
		});
		
		container.prependTo(positioner);
		
		dimmer.prependTo('body').css('opacity', $.fn.optPopup.defaults.opacity).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();				
			});
		});
 
		$.fn.optPopup.initializeLinks();
	});
		
	function close() {
		positioner.fadeOut(800, function() {
			container.html('');
			container.hide();
			// closeButton.hide().offset({ top: 0, left: 0 });
		}).hide();
		dimmer.fadeOut(800);		

		$('html').css('overflow-y', '');
		$('body').css('overflow', '');
	}
	
	$.fn.optPopup = function(url, options, callback) {
		if (jQuery.isFunction( url )) {
			callback = url;
			options = null;
			url = $(this).attr('href');
		}
		else if (jQuery.isFunction( options )) {
			callback = options;
			options = null;
		}			
		else if (!callback) {
			callback = function() {};
		}
		
		var settings = $.extend({}, $.fn.optPopup.defaults, options);
		
		function popup() {
			$('html').css('overflow-y', 'scroll');
			$('body').css('overflow', 'hidden');
			
			container.animate({ opacity: 0 }, 50).show();
			dimmer.fadeIn('fast');
			positioner.fadeIn('fast');
			closeButton.css('opacity', 0);
			
			if ($.browser.msie && $.browser.version.substr(0,1) < 7) {
				positioner.css('top', $(window).scrollTop() + 'px');
			}
			
			// if (settings.closeButton === true || settings.iframe === true ) {
			// 	closeButton.fadeIn('fast');				
			// }
			
			container.width(settings.width).height(settings.height);
 
			$('#popup_container a.magnify').remove();
 
			if (settings.iframe === true) {
				container.html('<iframe src="' + url + '" width="' + settings.width + '" height="' + settings.height + '" scrolling="auto" frameborder="0"></iframe>').css('overflow', 'hidden');
 
				container.css('margin-left', settings.width / -2);
				container.css('margin-top', settings.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);
										
					$('#print_block a').click(function() {
						window.print();
					});
					
					if (settings.width == 'auto' || settings.height == 'auto') {
						var popup = container.find('div');
						
						var contentWidth = popup.outerWidth({ margin: true });
						var contentHeight = popup.outerHeight({ margin: true });
 
						var popWidth = contentWidth < settings.maxWidth ? contentWidth : settings.maxWidth;
						var popHeight = contentHeight < settings.maxHeight ? contentHeight : settings.maxHeight;
 
						if (settings.width == 'auto') {
							container.width(popup.outerWidth({ margin: true }));							
						}
						if (settings.height == 'auto') {
							container.height(popup.outerHeight({ margin: true }));											
						}
					}
 
					container.css('opacity', 0).show();
					container.animate({ opacity: 1 }, 500);

					// Move the close button to be inside the popup
					closeButton.offset({ top: container.offset().top - 12, left: container.offset().left - 10 }).css('opacity', 1);
					
					$.fn.optPopup.initializeLinks();

					callback.call();						
				});
			}
						
			return false;		
		}
		
		return this.unbind('click').click(function() { popup(); return false; });
	};
 
	$.fn.optPopup.defaults = {
		width: 'auto',
		height: 'auto',
		maxWidth: 600,
		maxHeight: 600,
		opacity: 0.85,
		draggable: false,
		iframe: false,
		closeButton: true,
		animateIn: 'fadeIn',
		animateOut: 'fadeOut'
	};
 
	$.fn.optPopup.initializeLinks = function() {
		$('a[rel^=popup]').each(function() {
			$(this).optPopup($(this).attr('href'));
		});	
	};
})(jQuery);