var popup = {
	defaults: {
		selector: '.popup',
		overlay_style: { 
			position: 'fixed',
			top: 0,
			left: 0,
			width: '100%',
			height: '100%',
			'z-index' : 50000
		},
		popup_style: {
			position: 'fixed',
			top: '50%',
			left: '50%',
			'z-index' : 50001,
			opacity: 0
		},
		popup_animate_delay: 500,
		wrap: function(img) {
			return img;
		},
		min_width: 50,
		min_height: 50
	},
	opts: {},
	overlay: {},
	popup: {},
	img: {},
	content: {},
	href: "",
	
	setup: function(opts) {
		if (!opts.selector) opts.selector = this.defaults.selector;
		if (!opts.overlay_style) opts.overlay_style = this.defaults.overlay_style;
		if (!opts.popup_style) opts.popup_style = this.defaults.popup_style;
		if (!opts.popup_animate_delay) opts.popup_animate_delay = this.defaults.popup_animate_delay;
		if (!opts.wrap) opts.wrap = this.defaults.wrap;
		if (!opts.min_width) opts.min_width = this.defaults.min_width;
		if (!opts.min_height) opts.min_height = this.defaults.min_height;
		
		this.opts = opts;
		
		this.setup_hook();
	},
	
	setup_hook: function() {
		jQuery(this.opts.selector).click(function() { popup.show(this.href); return false; });
	},
	
	show: function(href) {
		if (this.overlay.remove) this.overlay.remove();
		if (this.popup.remove) this.popup.remove();
		
		this.href = href;
		this.setup_html();	
	},

	hide: function() {
		this.popup.animate({opacity:0}, this.opts.popup_animate_delay, function() { popup.popup.remove(); popup.overlay.remove(); });
	},
	
	setup_html: function() {
		this.overlay = jQuery('<div />').css(this.opts.overlay_style);
		this.overlay.click(function() { popup.hide(); });
		this.popup = jQuery('<div />').css(this.opts.popup_style);

		jQuery('body').append(this.overlay);
		jQuery('body').append(this.popup);
		
		
		this.img = jQuery('<img />')
			.load(function() {
				popup.show_popup();
			});
		this.content = this.opts.wrap(this.img);
		this.popup.html(this.content);
				
		this.img.attr('src', this.href);
					
		this.show_popup();
	},
	
	show_popup: function() {
		var w = this.content.outerWidth();
		var h = this.content.outerHeight();
		if (w && w >= this.opts.min_width && h && h >= this.opts.min_height) {
			this.popup.css({width: w, height: w, 'margin-left': -w/2, 'margin-top': -h/2});
			this.popup.animate({opacity:1}, this.opts.popup_animate_delay, function() { jQuery(this).css({opacity: ''}); });
		}
	}
};

jQuery(document).ready(function() {
	
	var opts = {
		selector: '.popup',
		min_width: 130, /* min width and height necessary because of the border, padding, etc */
		min_height: 130,
		wrap: function(img) {
			img.css({border: 'solid 20px white'})
			
			var jq = jQuery("<div />")
						.css({
							background: "url('/images/popup.png')",
							padding: '0 13px 13px 13px'
						});
			jq.append('<div style="font-size: 11px; text-align: right; width: 100%; padding: 6px 15px 0 0; height: 21px;"><a href="javascript:popup.hide()" style="color: #636466;">Close <img src="/images/close.gif" /></a></div>');
			jq.append(img);
			
			return jq;
		}
	};
	popup.setup(opts);
});

