if(top.location != location) top.location.href = location.href;

var showtimer = null;
var hidetimer = null;
var mouseX = 0;
var mouseY = 0;

function getWindowWidth()
{
    if(window.innerWidth)
    {
	return window.innerWidth;
    } 
		        
    return document.body.clientWidth;
}

$().mousemove( function(e) { mouseX = e.pageX; mouseY = e.pageY; } );

function addBlurbHandlers(parent)
{
	$("a.likelink", parent).click(function(e)
	{
		$(this).parent().append("<strong>Thank you for your rating!</strong>");
		$("strong:last", $(this).parent()).hide();
		$(this).hide("fast");
		$("strong:last", $(this).parent()).show("fast");

		var archive_id = this.id.substr(4);
		
		$.post("dataquery.php", {aid: archive_id, op: 'like'}, function(htmldata)
		{
		});
		
		e.preventDefault();
	});

	$("a.tagdel", parent).click(function(e)
	{
		e.preventDefault();

		var archive_id = this.id.substr(6, this.id.indexOf("_")-6);

		var tag_id = this.id.substr(this.id.indexOf("_")+1);
		
		// make a query to delete tag_id from archive_id
		$.post("dataquery.php", {aid: archive_id, tag: tag_id, op: 'del'}, function(htmldata)
		{
			taglist = $("#taglist" + archive_id);
			taglist.html(htmldata);
	
			addBlurbHandlers(taglist);
		});
	});
	
	$("input.tagaddbut", parent).click(function(e)
	{
		e.preventDefault();

		var archive_id = this.id.substr(6);

		var tag_id = $("#tagadd" + archive_id).find('option').filter(':selected').val();

		// make a query to add tag_name to archive_id
		$.post("dataquery.php", {aid: archive_id, tag: tag_id, op: 'add'}, function(htmldata)
		{
			taglist = $("#taglist" + archive_id);
			taglist.html(htmldata);
	
			addBlurbHandlers(taglist);
		});
	});

	$("input.close", parent).click(
	function(e)
	{
		e.preventDefault();
		$(".td").hide();
	});
}

function showblurb(id)
{
	$(".td").hide();

	var tooltip = $("#" + id + "t");
	tooltip.hide();
        tooltip.html("<p align=\"center\"><img src=\"loading.gif\"></p>");

	x = mouseX + 100;
	if(x + tooltip.width() > getWindowWidth()-50)
		x = getWindowWidth() - tooltip.width()-50;
		
	tooltip.css( { position: "absolute",
				top: mouseY + 10, left: x });
	tooltip.fadeIn("fast");

	showtimer = null;

	$.post("tooltip.php", {id: id.slice(1)}, function(htmldata)
	{
		var tooltip = $("#" + id + "t");
		tooltip.html(htmldata);
		htmldata = "";
	
		addBlurbHandlers(tooltip);
	});
}

function hideblurb(id)
{
	var tooltip = $("#" + id + "t");
	tooltip.fadeOut("fast", function() { tooltip.text("") });
}

function addHandlers()
{
	searchdiv = $(".searchdiv:visible");

	searchdiv.hide();

	clouddiv = $(".clouddiv:visible");

	clouddiv.hide();
	
	$("input").filter(".cloudopen").click(
	function(event)
	{
		event.preventDefault();
		
		clouddiv.css( { position: "absolute",
			       top: $(this).offset().top + $(this).height() + 20 + "px", left: $(this).offset().left - clouddiv.width() - 20 + "px" });
		clouddiv.slideDown("fast");

		searchdiv = $(".searchdiv");
		
		searchdiv.slideUp("fast");
	});
	
	$("a").filter(".rl").click(function(e)
	{
		var archive_id = this.id.substr(1);

		$(this).html("<img src=\"rated.png\" alt=\"You have rated this game\" />");
		
		$.post("dataquery.php", {aid: archive_id, op: 'like'}, function(htmldata)
		{
		});
		
		e.preventDefault();
	});

	$("a").filter(".cloudclose").click(
	function(event)
	{
		event.preventDefault();
		clouddiv = $(".clouddiv");
		
		clouddiv.slideUp("fast");
	});
		
	$("a").filter(".searchlink").click(
	function(event)
	{
		event.preventDefault();
		
		searchdiv.css( { position: "absolute",
			       top: $(this).offset().top + $(this).height() + 20 + "px", left: $(this).offset().left - searchdiv.width() - 20 + "px" });
		searchdiv.slideDown("fast");
		
		clouddiv = $(".clouddiv");
		
		clouddiv.slideUp("fast");
	});
	
	$("a").filter(".cloudlink").click(
	function(event)
	{
		event.preventDefault();
		
		clouddiv.css( { position: "absolute",
			       top: $(this).offset().top + $(this).height() + 20 + "px", left: $(this).offset().left - clouddiv.width() - 20 + "px" });
		clouddiv.slideDown("fast");
		
		searchdiv = $(".searchdiv");
		
		searchdiv.slideUp("fast");
	});

	$("input").filter(".searchopen").click(
	function(event)
	{
		event.preventDefault();
		
		searchdiv.css( { position: "absolute",
			       top: $(this).offset().top + $(this).height() + 20 + "px", left: $(this).offset().left - searchdiv.width() - 20 + "px" });
		searchdiv.slideDown("fast");
		
		clouddiv = $(".clouddiv");
		
		clouddiv.slideUp("fast");
	});

	$("a").filter(".searchclose").click(
	function(event)
	{
		event.preventDefault();
		searchdiv = $(".searchdiv");
		
		searchdiv.slideUp("fast");
	});
	
	$("a").filter(".cloudclose").click(
	function(event)
	{
		event.preventDefault();
		clouddiv = $(".clouddiv");
		
		clouddiv.slideUp("fast");
	});

	$("img").filter(".popup").hide();

	$("span").filter(".tl").hover(
	function()
	{
		$(this).find("img:first").show();
		$(this).find("img:last").hide();
	},
	function()
	{
		$(this).find("img:last").show();
		$(this).find("img:first").hide();
	}
	);
		
	$("img").filter(".popup").click(
	function(e)
	{
		idin = "x" + $(this).parent().find("a:first").attr("id").slice(1);
		showblurb(idin);
	});
}

$(document).ready(addHandlers);
