/*
This is old and needs to be rewritten pretty bad.

Probably make it a JQuery plugin or a JS class. Whatever works best.

But like I said, it's old, so don't judge me to harshly :-P
*/

var popup_status = 0;
var popup_target = "popup-box";

function load_popup (location, target)
	{
	location	= (typeof (location) == 'object') ? location.href : location;
	target		= (target == null) ? popup_target : target;
	
	if (popup_status == 0)
		{
		if (location.indexOf('?') != -1)
			page_request = location + '&popup';
			else
			page_request = location + '?popup';
		
		if (page_request != "")
			{
			$.ajax ({
			type: "GET",
			url: page_request,
			async: false,
			
			success: function (content, status, XMLHttpRequest)
				{
				if (content.match (/DOCTYPE/) || XMLHttpRequest.status != 200)
					{
					window.location = location; return;
					}
				
				$("#popup-content").html (content);
				$("#popup-content #crumbs").remove ();
				
				$("#popup-background").css ({"opacity": "0.3"});
				$("#popup-background").fadeIn ("fast");
				$("#" + target).fadeIn ("fast");
				
				popup_status = 1;
				
				center_popup (target);
				
				$("#" + target + " a.hand").click (function () { disable_popup (); });
				
				if (jQuery().datepicker)
					$(".datebox").datepicker ({
						showAnim:		'fade',
						dateFormat:		'yy-mm-dd',
						duration:		'fast',
						});
				
				$("#focus,.focus").focus ();
				},
			
			complete: function (XMLHttpRequest) {
				},
			
			error: function (XMLHttpRequest, textStatus, errorThrown)
				{
				if (XMLHttpRequest.status > 0)
					alert ("AJAX Error: " + textStatus + errorThrown);
				
				return;
				},
			});
			}
		}
	
	if (typeof (location) == 'object') return false;
	}

function center_popup (target)
	{
	popup_target = (target == null) ? popup_target : target
	
	var window_height	= document.documentElement.clientHeight;
	var window_width	= document.documentElement.clientWidth;
	var popup_height	= $("#" + popup_target).height ();
	var popup_width		= $("#" + popup_target).width ();
	var position_top	= window_height / 2 - popup_height / 2;
	var position_left	= window_width / 2 - popup_width / 2;
	
	$("#" + popup_target).css ({
		"top"		: (position_top < 10) ? 10 : position_top,
		"left"		: (position_left < 10) ? 10 : position_left,
		});
	
	/* Thanks IE6 */
	$("#popup-background").css ({"height" : window_height});
	}

function disable_popup (target)
	{
	popup_target = (target == null) ? popup_target : target
	
	if (popup_status == 1)
		{
		$("#popup-background").fadeOut ("fast");
		popup_close (popup_target);
		
		if (typeof (tinyMCE) == 'object')
			tinyMCE.execCommand ("mceRemoveControl", false, "bbcode"); 
		
		popup_status = 0;
		
		return false;
		}
	}

function popup_close (target)
	{
	popup_target = (target == null) ? popup_target : target;
	
	$("#" + popup_target).fadeOut ('fast');
	
	popup_status = 0;
	}

function reload_popups ()
	{
	$(".popup-link").click (function (event) {
		event.preventDefault ();

		load_popup ($(this).attr ('href'));
		});
	}

$(document).ready (function ()
	{
	$('#popup-box .close').click (function () { disable_popup (); });
	$('#popup-background').click (function () { disable_popup (); });
	
	/* Look for the excape key */
	$(document).keypress (function (e) {
		if (e.keyCode == 27 && popupStatus == 1) disable_popup ();
		});
	});
