// JavaScript Document
$(document).ready(function()
{
	$(".vote input").attr("checked", "");
	
	$(".vote div").click(function()
	{
		var table = $(this).closest(".question-table");
		if($(this).css("background-position") == "0px -40px")
		{
			$(".vote div").css("background-position", "0px 0px");
			$(this).css("background-position", "0px 0px");
			$("#answer").attr("value", "");
		}
		else
		{
			$(".vote div").css("background-position", "0px 0px");
			$(this).css("background-position", "0px -40px");
			if($(this).attr("class") == "vote-1")
			{
				$("#answer").attr("value", "1");
			}
			else
			{
				$("#answer").attr("value", "2");
			}
		}
	});
	
	$("#vote-button input").click(function()
	{
		if($("#answer").val() != "")
		{
			var answer = $("#answer").val();
			var mode = $("#mode").val();
			var modeValue = $("#modeValue").val();
			var questionNumber = $("#question-number").val();
			var previous = $("#questionNumbers").val();
			$(this).attr("src", "/_images/saving.png");
			var slider = $("#vote-slider");
	
			$.ajax(
			{
				url: '/save-vote.php',
				type: 'GET',
				async: false,
				cache: false,
				timeout: 5000,
				data: { questionNumber: questionNumber, answer: answer, mode: mode, modeValue: modeValue, previous: previous },
				error: function(data)
				{
            		
        		},
        		success: function(data)
				{ 
					$(slider).hide("slide", { direction: "up" }, 1000);
					var bits = data.split("~SEP~");
					setTimeout(function()
					{
						$("#answer").attr("value", "");
						$("#question-number").attr("value", bits[0]);
						$("#question-number-display").html(bits[0]);
						var q1 = $("#question-1").html();
						var q2 = $("#question-2").html();
						$("#question-1").html(bits[1]);
						$("#question-2").html(bits[2]);
						$(".vote div").css("background-position", "0px 0px");
						$("#vote-button input").attr("src", "/_images/vote.png");
						/* var url = $("#facebook-share").attr("href");
						var urlBits = url.split('=');
						var urlBits2 = urlBits[1].split('.co.uk/');
												
						/* FACEBOOK */
						/* var fbsharesrc = urlBits[0] + "=" + urlBits2[0] + ".co.uk/" + bits[0] + "&t=" + urlBits[2];
						$("#facebook-share").attr("href", fbsharesrc); */
						
						$("#facebook-share").attr("src", "http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwouldyourather.co.uk/" + bits[0] + "&layout=button_count&show_faces=false&width=95&action=like&colorscheme=light&height=21");
						
						/* SHORT URL */
						$("#shortURL-share").html(bits[5]).attr("href", bits[5]);
						
						/* TWITTER */
						$("#twitter-share iframe").attr("src", bits[6]);

						/* MODE VALUE */
						$("#modeValue").attr("value", bits[7]);
						
						
						var votes1 = parseInt(bits[3]);
						var votes2 = parseInt(bits[4]);
						var totalVotes = votes1 + votes2;
						
						if(answer == 1)
						{
							var rather = q1;
							var than = q2;
							var ratherVotes = Math.round((votes1 / totalVotes)*100);
							var thanVotes = Math.round((votes2 / totalVotes)*100);;
						}
						else
						{
							var rather = q2;
							var than = q1;
							var ratherVotes = Math.round((votes2 / totalVotes)*100);;
							var thanVotes = Math.round((votes1 / totalVotes)*100);;
						}
						
						var sep = ","; 
						if($("#answerNumbers").attr("value") == "")
						{
							sep = "";	
						}
						$("#answerNumbers").attr("value", $("#answerNumbers").attr("value") + sep + answer);
						$("#questionNumbers").attr("value", $("#questionNumbers").attr("value") + sep + questionNumber);
						$("#answer-list").prepend("<table class=\"answer\"><tr><td>You would rather " + rather + "...</td><td class=\"right\"><div style=\"position: relative;\"><div class=\"percent\" style=\"width: " + (ratherVotes*1.4) + "px;\">&nbsp;</div><p>(" + ratherVotes + "% of " + totalVotes + " Votes)</p></div></td></tr><tr class=\"bottom-row\"><td>than " + than + "</td><td class=\"right\"><div style=\"position: relative;\"><div class=\"percent\" style=\"width: " + (thanVotes*1.4) + "px\">&nbsp;</div><p>(" + thanVotes + "% of " + totalVotes + " Votes)</p></div></td></tr></table>");
						$("#youwould").slideDown(2000);
					}, 1000);
        		}
    		});

			$(slider).show("slide", { direction: "up" }, 1000);
		}
	});

	$("#skip-button input").click(function()
	{
		var mode = $("#mode").val();
		var modeValue = $("#modeValue").val();
		var questionNumber = $("#question-number").val();
		var previous = $("#questionNumbers").val();
		$(this).attr("src", "/_images/skipping.png");
		var slider = $("#vote-slider");

		$.ajax(
		{
			url: '/skip-vote.php',
			type: 'GET',
			async: false,
			cache: false,
			timeout: 5000,
			data: { questionNumber: questionNumber, mode: mode, modeValue: modeValue, previous: previous },
			error: function(data)
			{
        		
    		},
    		success: function(data)
			{ 
				$(slider).hide("slide", { direction: "up" }, 1000);
				var bits = data.split("~SEP~");
				setTimeout(function()
				{
					$("#answer").attr("value", "");
					$("#question-number").attr("value", bits[0]);
					$("#question-number-display").html(bits[0]);
					var q1 = $("#question-1").html();
					var q2 = $("#question-2").html();
					$("#question-1").html(bits[1]);
					$("#question-2").html(bits[2]);
					$(".vote div").css("background-position", "0px 0px");
					$("#skip-button input").attr("src", "/_images/skip.png");

					$("#facebook-share").attr("src", "http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwouldyourather.co.uk/" + bits[0] + "&layout=button_count&show_faces=false&width=95&action=like&colorscheme=light&height=21");
										
					/* SHORT URL */
					$("#shortURL-share").html(bits[5]).attr("href", bits[5]);
					
					/* TWITTER */
					$("#twitter-share iframe").attr("src", bits[6]);

					/* MODE VALUE */
					$("#modeValue").attr("value", bits[7]);
					
					
					/* var votes1 = parseInt(bits[3]);
					var votes2 = parseInt(bits[4]);
					var totalVotes = votes1 + votes2;
					
					if(answer == 1)
					{
						var rather = q1;
						var than = q2;
						var ratherVotes = Math.round((votes1 / totalVotes)*100);
						var thanVotes = Math.round((votes2 / totalVotes)*100);;
					}
					else
					{
						var rather = q2;
						var than = q1;
						var ratherVotes = Math.round((votes2 / totalVotes)*100);;
						var thanVotes = Math.round((votes1 / totalVotes)*100);;
					}
					
					var sep = ","; 
					if($("#answerNumbers").attr("value") == "")
					{
						sep = "";	
					}
					$("#answerNumbers").attr("value", $("#answerNumbers").attr("value") + sep + answer);
					$("#questionNumbers").attr("value", $("#questionNumbers").attr("value") + sep + questionNumber);
					$("#answer-list").prepend("<table class=\"answer\"><tr><td>You would rather " + rather + "...</td><td class=\"right\"><div style=\"position: relative;\"><div class=\"percent\" style=\"width: " + (ratherVotes*1.4) + "px;\">&nbsp;</div><p>(" + ratherVotes + "% of " + totalVotes + " Votes)</p></div></td></tr><tr class=\"bottom-row\"><td>than " + than + "</td><td class=\"right\"><div style=\"position: relative;\"><div class=\"percent\" style=\"width: " + (thanVotes*1.4) + "px\">&nbsp;</div><p>(" + thanVotes + "% of " + totalVotes + " Votes)</p></div></td></tr></table>");
					$("#youwould").slideDown(2000); */
				}, 1000);
    		}
		});

		$(slider).show("slide", { direction: "up" }, 1000);
	});

	
	$(".submit-trigger").click(function()
	{
		$("#overlay").height($(document).height()).show();
		$("#submit-lightbox").show();
		$("#submit-form input").attr("value", "");
		$("#submit-result").html("");
		$("#submit-question-button").attr("src", "/_images/submit.png");
		return false;
	});
	
	$(".submit-close").click(function()
	{
		$("#overlay").height(0).fadeOut("slow");
		$("#submit-lightbox").fadeOut("slow");
		return false;
	});
	
	$("#submit-question-button").click(function()
	{
		var validates = true;
		$("#submit-question-button").attr("src", "/_images/saving.png");
		if($("#submit-question1").attr("value") == "")
		{
			validates = false;
		}
		
		if($("#submit-question2").attr("value") == "")
		{
			validates = false;
		}
		
		if($("#submit-email").attr("value") == "")
		{
			validates = false;
		}
		
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var address = $("#submit-email").attr("value");
		if(reg.test(address) == false)
		{
			validates = false;
		}

		
		if(validates)
		{
			$.ajax(
			{
				url: 'submit-question.php',
				type: 'GET',
				async: false,
				cache: false,
				timeout: 30000,
				data: { question1: $("#submit-question1").attr("value"), question2: $("#submit-question2").attr("value"), email: $("#submit-email").attr("value") },
				error: function(data)
				{
					
				},
				success: function(data)
				{ 
					if(data == "OK")
					{
						$("#submit-result").html("Dilemma submitted.<br />We will email you when it is approved!");
					}
					else
					{
						$("#submit-result").html("Error!");
					}
					
					setTimeout(function()
					{
						$("#overlay").height(0).fadeOut("slow");
						$("#submit-lightbox").fadeOut("slow");
					}, 2000);
				}
			});
		}
		else
		{
			$("#submit-result").html("Please complete all the fields!");
			$("#submit-question-button").attr("src", "/_images/submit.png");
		}
		return false;
	});
});
