var jonbox_close_image = "images/jonbox_close.gif";
var jonbox_close_hot_image = "images/jonbox_close_hot.gif";
var jonbox_init_once = false;

$(document).ready(init_jonbox);

function init_jonbox(e)
{
	var continue_init = false;
	$("a").each(function(i) {
		if ( $(this).attr("rel") == "jonbox" ) {
			continue_init = true;
			$(this).bind("click", {aobj: this}, open_jonbox);
		}
	});
	if ( continue_init && !jonbox_init_once )
	{
		var img1 = new Image();
		img1.src = jonbox_close_image;
		
		var img2 = new Image();
		img2.src = jonbox_close_hot_image;
		
		$("<div />").attr("id", "jonbox_jblock").appendTo("body").hide().css({
			backgroundColor: "#000",
			opacity: 0,
			width: "100%",
			position: "fixed",
			left: 0,
			top: 0,
			zIndex: 100
		});
		$("<div />").attr("id", "jonbox_loading").appendTo("body").hide().css({
			color: "#f0f0f0",
			fontFamily: "'Lucida Grande', Arial, Helvetica, sans-serif",
			fontSize: "1.5em",
			fontWeight: 700,
			position: "fixed",
			left: "-9999px",
			top: "-9999px",
			zIndex: 500
		}).html("Loading&hellip;");
		$("<div />").attr("id", "jonbox").appendTo("body").hide().css({
			backgroundColor: "#f0f0f0",
			opacity: 0,
			position: "fixed",
			left: 0,
			top: 0,
			zIndex: 1000
		});
		$("<div />").attr("id", "jonbox_titlebar").appendTo("#jonbox").css({
			background: "transparent url(images/menu_bg.png) repeat-x -15px bottom",
			textAlign: "right",
			padding: "3px"
		});
		$("<div />").attr("id", "jonbox_content").appendTo("#jonbox").css({
			background: "#ffffff",
			padding: "15px"
		});
		$("<img />").attr({
			src: jonbox_close_image,
			alt: "x",
			title: "Close"
		}).css({
			cursor: "pointer"
		}).hover(function () {
			this.src = jonbox_close_hot_image;
		}, function () {
			this.src = jonbox_close_image;
		}).appendTo("#jonbox_titlebar").click(close_jonbox);

		jonbox_init_once = true;
	}
}

function open_jonbox(e)
{
	/*if ( jQuery.browser.msie )
	{
		$("#jonbox_jblock").show().css({
			height: $(document).height(),
			opacity: 0.88
		});
		$("#jonbox_loading").show(1, function() {
			$(this).css({
				top: (($(window).height() - $(this).height()) / 3) + "px",
				left: (($(window).width() - $(this).width()) / 2) + "px"
			});
		});
		$("#jonbox_content").load(e.data.aobj.href, null, function() {
			$("#jonbox").show(1, function() {
				$(this).css({
					top: (($(window).height() - $(this).height()) / 3) + "px",
					left: (($(window).width() - $(this).width()) / 2) + "px",
					opacity: 1
				});
				$(window).resize(move_jonbox);
			});
		});
	}
	else
	{*/
		$("#jonbox_jblock").css({
			height: $(document).height()
		}).show(1, function() {
			$(this).animate({
				opacity: 0.5
			}, 200, null, function() {
				$("#jonbox_loading").show(1, function() {
					$(this).css({
						top: (($(window).height() - $(this).height()) / 3) + "px",
						left: (($(window).width() - $(this).width()) / 2) + "px"
					});
				});
				$("#jonbox_content").load(e.data.aobj.href, null, function() {
					$("#jonbox").show(1, function() {
						$(this).css({
							top: Math.max(-10, ((($(window).height() - $(this).height()) / 3) - 10)) + "px",
							left: (($(window).width() - $(this).width()) / 2) + "px"
						}).animate({
							top: Math.max(0, (($(window).height() - $(this).height()) / 3)) + "px",
							opacity: 1
						}, 200);
						$(window).resize(move_jonbox);
					});
				});
			});
		});
	//}
	return false;
}

function move_jonbox(e)
{
	$("#jonbox_loading").hide();
	$("#jonbox_jblock").css({
		height: $(document).height()
	});
	/*if ( jQuery.browser.msie )
	{
		$("#jonbox").css({
			top: (($(window).height() - $("#jonbox").height()) / 3) + "px",
			left: (($(window).width() - $("#jonbox").width()) / 2) + "px"
		});
	}
	else
	{*/
		$("#jonbox").animate({
			top: Math.max(0, (($(window).height() - $("#jonbox").height()) / 3)) + "px",
			left: (($(window).width() - $("#jonbox").width()) / 2) + "px"
		}, 200, "swing");
	//}
}

function close_jonbox(e)
{
	$(window).unbind("resize");
	$("#jonbox_loading").hide();
	/*if ( jQuery.browser.msie )
	{
		$("#jonbox").css({
			opacity: 0
		}).hide();
		$("#jonbox_jblock").css({
			opacity: 0
		}).hide();
	}
	else
	{*/
		$("#jonbox").animate({
			top: Math.max(10, ((($(window).height() - $("#jonbox").height()) / 3) + 10)) + "px",
			opacity: 0
		}, 200, null, function() {
			$(this).hide();
			$("#jonbox_jblock").animate({
				opacity: 0
			}, 200, null, function() {
				$(this).hide();
			})
		});
	//}	
	return false;
}
