// Requires jQuery - www.jquery.com
// Coded by Adam Tourkow - 2/16/2007 5:17:22 PM
// $.popup.inline('test ksjdhfkhsdf khsdkjhf kdjgh ksdghksdh gkdhsgk sdhgkjh');
// $.popup.inline('test ksjdhfkhsdf khsdkjhf kdjgh ksdghksdh gkdhsgk sdhgkjh', {width: 600, height: 600});
// $.popup.iframe('tools/dupefinder');
// $.popup.iframe('tools/dupefinder?qid=141053&question=Excuse+me', {width: 600, height: 400});
// $.popup.loader.start();
// $.popup.loader.stop();
jQuery(function($)  // function($) sets an alias as $ to use within our code
{
	$.popup =
	{
		popup_el: null,
		popup_el_bg: null,
		popup_loader_img: null,
		popup_content: null,
		create: function(settings)
		{
			// default settings
			settings = $.extend({width: 300}, settings);
			
			// The popup doesn't exist, create one
			if($('#popup').size() == 0) 
			{
				var popup_html = ''+
					'<div id="popup">'+
						'<table class="popup_top"><tr><td></td><th><span>close <img src="/images/boxes/close_x.gif" width="9" class="popup_button_close" /></span></th></tr></table>'+
						'<div class="popup_mid_left">'+
							'<div class="popup_mid_right">'+
								'<div id="popup_content"></div>'+
							'</div>'+
						'</div>'+
						'<table class="popup_bottom"><tr><td></td><th>&nbsp;</th></tr></table>'+
					'</div>'+
				'';
				this.popup_el = $(popup_html).appendTo('body');
				this.popup_el.find('.popup_top span').click(function(){ $.popup.close();});
				this.popup_el.find('.popup_top span').hover(function(){ $(this).addClass('hover')}, function(){ $(this).removeClass('hover')});
				this.popup_el_bg = $('<div id="popup_background"></div>').appendTo('body');
				this.popup_loader_img = $('<img src="/images/loading_animation.gif" alt="Loading" style="display: none; position: absolute;"/>').appendTo(this.popup_el_bg);
				this.popup_content = $('#popup_content');
				//$(window).resize( function() { $.popup.position(); }); // Seems to break ie7
			}
			this.popup_content.empty();
			if(settings.height == 'auto')
				this.popup_content.css('height', 'auto');
			else if(settings.height)
				this.popup_content.css('height', settings.height +'px');
				
			this.popup_el.css('width', settings.width +'px');

			var full_height = $('body').height() + 20 + 'px';
			this.popup_el_bg.css('height', full_height);
			
		},
		position: function()
		{
			var window_center = $(window).scrollTop() + ($(window).height() / 2);
			var max_width = $('body').width();
			var top;
			var left;

			// Center the popup
			top = window_center - (this.popup_el.height() / 2);
			left = (max_width - this.popup_el.width()) / 2;
			this.popup_el.css('top', top +'px');
			this.popup_el.css('left', left +'px');

			top = (window_center - (100 / 2));
			left = (max_width - 100) / 2;
			this.popup_loader_img.css('top', top +'px');
			this.popup_loader_img.css('left', left +'px');
		},
		inline: function(content, settings)
		{
			// default settings
			settings = $.extend({close: true}, settings);
		
			this.create(settings);
			this.popup_content.html(content);
			if(settings.close)
				this.popup_content.append('<div style="margin-top: 5px; text-align: center;"><button class="submit_secondary popup_button_close" onclick="$.popup.close();">Close</button></div>');
			this.popup_content.css('padding', '10px 20px');
			$(window).scroll( function() { $.popup.position(); });
			this.position();
			this.open();
		},
		iframe: function(url, settings, noscroll)
		{
			this.create(settings);
			this.popup_content.css('padding', '0');
			this.popup_content.append('<iframe src="'+url+'" frameborder="0" allowtransparency="true" '+(noscroll?'scrolling="no"':'')+'></iframe>');
			$(window).scroll( function() { $.popup.position(); });
			this.position();
			this.open();
		},
		loader:
		{
			start: function()
			{
				$.popup.create();
				$.popup.popup_el_bg.show();
				$.popup.popup_loader_img.show();
			},
			stop: function(content)
			{
				// If there's content, display it in the popup, if not, just close
				if(content)
				{
					$.popup.popup_content.html(content);
					$.popup.popup_content.css('padding', '10px 20px');
					$.popup.popup_el.show();
					$.popup.popup_loader_img.hide();
					return;
				}
				$.popup.close();
			}
		},
		/*
			Pass it the confirm text, and the function to run if it true or false, 
			false function is optional and will just close the window if 'No' is clicked
			$.popup.confirm('Are you sure you want to do this?', function() { alert('Yes, I do') }, function() { alert('No, I do not') });
			$.popup.confirm('Are you sure you want to do this?', function() { alert('Yes, I do') });
		*/
		confirm: function(text, fn_true, fn_false)
		{
			this.create({height: 'auto'});
			this.popup_content.css('padding', '10px 20px');
			$(window).scroll( function() { $.popup.position(); });
			this.position();

			var o_confirm = $( 
				'<div style="text-align: center;">'+
				'<p style="margin-bottom: 10px;">'+text+'</p>'+
				'</div">'
			);
			var o_true = $('<button class="ab_blue_bg" style="color: white; font-weight: bold;">Yes</button>').click(fn_true);
			var o_false = $('<button class="ab_blue_bg" style="color: white; font-weight: bold;">No</button>');
			if(fn_false)
				o_false.click(fn_false);
			else
				o_false.click(function() { $.popup.close(); });
			o_confirm.append(o_true);
			o_confirm.append('&nbsp;&nbsp;');
			o_confirm.append(o_false);
			this.popup_content.append(o_confirm);
			this.open();
		},
		open: function()
		{
			$("select, embed, object").css("visibility", "hidden");
			

			this.popup_el.show();
			this.popup_el_bg.show();
		},
		close: function()
		{
			$("select, embed, object").css("visibility", "visible");


			this.popup_el.hide();
			this.popup_el_bg.hide();
			this.popup_loader_img.hide();
			$(window).unbind("scroll");
		},
		exists: function()
		{
			return ($('#popup').size() == 0) ? false : true;
		}
	
	};
});
