function submitPoll(choice){
	$("#choice1").attr("disabled","disabled");
	$("#choice2").attr("disabled","disabled");
	$("#choice3").attr("disabled","disabled");
	$("#status").css({"z-index": 3});
	$("#poll").fadeTo(1000,0.3,function(){
		$.post(ROOT_PREFIX+"poll.php", {choice : choice, id : pollid}, function(data){
			$("#status").html(data);
			setTimeout("submitPoll2()",100);
		});	
	});
}

function submitPoll2(){
	$("#status").css({
		left: $("#poll").position()['left']+$("#poll").outerWidth()/2-$("#status").outerWidth()/2,//70+$("#poll").width()/2,
		top : $("#poll").position()['top']+$("#poll").outerHeight()/2-$("#status").outerHeight()/2//10+$("#poll").height()/2,
	});
	$("#status").fadeTo(1000,1,function(){
		$("#reply").fadeTo(1000,0,function(){
			$("#reply").html("Loading new poll...");
			$("#reply").fadeTo(1000,1,function(){
				loadPoll();
			});
		});
	});
	//setTimeout("$(\"#reply\").fadeTo(1000,0)",1000);
	//setTimeout("$(\"#reply\").html(\"Loading new poll...\");$(\"#reply\").fadeTo(1000,1);",2000);
	//setTimeout("loadPoll()",3000);
}

function loadPoll(){
	$.post(ROOT_PREFIX+"poll.php",{getid: 1}, function(data){
		pollid = data;
		if(pollid > 0){
			$.post(ROOT_PREFIX+"poll.php",{load: pollid}, function(data){
				$("#status").fadeTo(1000,0,function(){
					$("#status").css({"z-index": -3})
				});
				//setTimeout("$(\"#status\").css({\"z-index\": -3})",1000);
				$("#poll").css({height: "auto"});
				$("#poll").html(data);
				$("#choice1").attr("disabled","disabled");
				$("#choice2").attr("disabled","disabled");
				$("#choice3").attr("disabled","disabled");
				$("#poll").fadeTo(1000,1,function(){
					$("#choice1").removeAttr("disabled");
					$("#choice1").click(function(){
						submitPoll(1);
					});
					$("#choice2").removeAttr("disabled");
					$("#choice2").click(function(){
						submitPoll(2);
					});
					$("#choice3").removeAttr("disabled");
					$("#choice3").click(function(){
						submitPoll(3);
					});
				});
			});
		} else {
			$("#status").fadeTo(1000,0);
			setTimeout("$(\"#status\").css({\"z-index\": 3})",1000);
			$("#poll").fadeTo(1000,1);
			$("#poll").css({height: "auto"});
			$("#poll").html("You've finished all the polls! Good job! <img src='"+ROOT_PREFIX+"images/y.png'><br/><br/>- <img src='"+ROOT_PREFIX+"images/wombathere.gif' height='20' width='20'> SmallWombat");
		}
	});	
}

function pollReport(id){
	var html = "Poll Results for Poll ID: "+id+"</br>";
	html += "<img src='poll.php?imageid="+id+"'/ border='1'><br/><input type='button' value='Quit' id='quitreport' style='border:1px solid black;font-size:9pt'>";
	$("#results").html(html);
	$("#quitreport").click(function(){
		$("#results").fadeTo(1000,0,function(){
			$("#results").css({
				"z-index": -3
			});
		});;
	});
	setTimeout("$(\"#results\").css({\"z-index\": 3});$(\"#results\").fadeTo(1000,1)",30);
}

var reportHash = "80797676";
var rpos = 0;
var num = 0;
var id = "";

$(document).ready(function(){
	var pollid = 0;
	$("body").append("<div id='status' style='text-align:center'></div>");
	$("#status").css({
		border : "1px solid black",
		"background-color" : "white",
		position: "absolute",
		opacity : 0,
		padding: "1px 10px 1px 10px",
		"font-size" : "9pt",
		"z-index" : -3
	});
	$("body").append("<div id='results'></div>");
	$("#results").css({
		border : "1px solid black",
		"background-color" : "white",
		position: "absolute",
		opacity : 0,
		padding: "20px",
		"font-size" : "9pt",
		"z-index" : -3,
		width : 300,
		left: $(window).width()/2-170,
		top: 40
	});

	$("#poll").fadeTo(0,0);
	loadPoll();

	$(document).keyup(function(event){
		if(num > 0){
			id += ""+(event.keyCode-48);
			num++;
			if(num == 3){
				pollReport(id);
				id = 0;
				num = 0;
			}
		} else if(event.keyCode == reportHash.substr(rpos,2)){
			rpos += 2;
			if(rpos == 8){
				rpos = 0;
				num = 1;
			}
		}
	});
});