// JavaScript Document

$(document).ready(function(){
	
	$("input#searchTerm").click(function(){
			$(this).val("").unbind("click");						 
		});
	
	$("div.answer").each(function(){ $(this).data("height", $(this).height()); });
});


function openComic(n)
{
	var c = new cookieObject("curComic", 1, "/", "ID");
	c.put("ID", n-1);
	c.write();
	return true;
}

function comsearch()
{
	window.location.href = "archives.php?type=search&term=" + $("input#searchTerm").val();
	return false;
}

function slide(a)
{
	if (a.is(":visible"))
	{
		a.slideUp(250);
		var bg = $("div#hugeBG");
		bg.animate( { height: (bg.height() - a.data("height"))+"px"}, 250);
		bg = $("div#midleftpng");
		bg.animate( { height: (bg.height() - a.data("height"))+"px"}, 250);
		bg = $("div#midrightpng");
		bg.animate( { height: (bg.height() - a.data("height"))+"px"}, 250);
	}
	else
	{
		a.slideDown(250);
		var bg = $("div#hugeBG");
		bg.animate( { height: (bg.height() + a.data("height"))+"px"}, 250);
		bg = $("div#midleftpng");
		bg.animate( { height: (bg.height() + a.data("height"))+"px"}, 250);
		bg = $("div#midrightpng");
		bg.animate( { height: (bg.height() + a.data("height"))+"px"}, 250);
	}
	return false;
}

function getCaptcha()
{
	$("div#captcha").hide();
	$.post("bin/genCaptcha.php", { id: 1, smeg: 2}, function(data){
		if (data.indexOf("ERROR")==-1)
		{
			$("div#captcha").html(data);
			$("div#captcha").show();
		}
		else
		{
			// error message
			$("div#captcha").html("ERROR - the server is unavilable right now.  Please refresh the page in a short while").show();
		}
	}, "text");
}

function sendCaptcha(n, guid, a)
{
	$(a).css("background-color", "#00AA00");
	// send the chosen captcha to server for validation
	$.post("bin/validateCaptcha.php", { chosen: n, ID: guid }, function(data){
		if (data=="ERROR")
		{
			$("div#captcha").html("<br/>Does that look like a furball to you?  <a href='#' onClick='getCaptcha(); return false;' style='color:#F00; text-decoration:none; cursor:pointer;'>Click Here</a> to try again");
		}
		else
		{
			// correct - send the message
			$.post("bin/sendIdea.php", { idea: $("textarea#idea").attr("value"), author: $("input#yourName").attr("value") }, function(data){
				if (data=="SUCCESS")
				{
					$("div#captcha").html("<br/>Thank you for your input - the mystic testicles will look at it shortly!");
				}
				else
				{
					$("div#captcha").html(data).after("<br />");
				}
				return false;
			}, "text");
		}
	}, "text");
	return false;
}