﻿$(document).ready(function() {		
		$("#BigForm").keyup(function() {
			if ($("#Comment").val().length > 0)
			{
				$("#PreviewComment").removeAttr("disabled");
				$("#PostComment").removeAttr("disabled");
			}
			else
			{
				$("#PreviewComment").attr("disabled", "disabled");
				$("#PostComment").attr("disabled", "disabled");
			}
		});
		
		$("#Comment").change(function() {
			if ($("#Comment").val().length > 0)
			{
				$("#PreviewComment").removeAttr("disabled");
				$("#PostComment").removeAttr("disabled");
			}
			else
			{
				$("#PreviewComment").attr("disabled", "disabled");
				$("#PostComment").attr("disabled", "disabled");
			}
		});

		$("#PreviewComment").click(function() {
			if ($("#PreviewComment").attr("disabled") != "disabled")
			{
				var pos = $(this).position();
				
				$.post("/library/previewcomment/", 
							{ SavedExpressionID: strSavedExpressionID, Comment: $("#Comment").val() },
							function(data) {
								if (data == "LogIn")
									$("#LogInDialog").modal({overlayClose:true,minHeight:0,minWidth:300,position: [pos.top + 20, pos.left + 50]});
								else {
									$("#PreviewCommentText").html(data);
									$("#PreviewCommentBox").slideDown();
								}
							});
			}
		});
		
		$("#PostComment").click(function() {
			if ($("#PostComment").attr("disabled") != "disabled")
			{
				var pos = $(this).position();
				$("#Comment").attr("disabled", "disabled");
				$("#PreviewComment").attr("disabled", "disabled");
				$("#PostComment").attr("disabled", "disabled");
				$("#PreviewCommentBox").slideUp();
				
				$.post("/library/postcomment/", 
							{ SavedExpressionID: strSavedExpressionID, Comment: $("#Comment").val() },
							function(data) {
								if (data == "LogIn")
									$("#LogInDialog").modal({overlayClose:true,minHeight:0,minWidth:300,position: [pos.top + 20, pos.left + 50]});
								else if (data == "OK") {
									$("#Comments").load("/library/comments/?SavedExpressionID=" + strSavedExpressionID + "&utc=" + new Date().getUTCMilliseconds());
									$("#Comment").val("");
									$("#Comment").removeAttr("disabled");
								}
								else {
									alert("System Error. Comment was not posted.");
									alert(data);
								}
							});
			}
		});
	
		$("#vote-up").click(function() {
			var pos = $(this).position();
			
			$.post("/library/voteup/", 
						{ SavedExpressionID: strSavedExpressionID },
						function(data) { 
							if (data == "AlreadyVoted")
								$("#AlreadyVotedDialog").modal({overlayClose:true,minHeight:0,minWidth:300,position: [pos.top + 20, pos.left + 50]});
							else if (data == "OwnVote")
								$("#OwnVoteDialog").modal({overlayClose:true,minHeight:0,minWidth:300,position: [pos.top + 20, pos.left + 50]});
							else if (data == "LogIn")
								$("#LogInDialog").modal({overlayClose:true,minHeight:0,minWidth:300,position: [pos.top + 20, pos.left + 50]});
							else {
								$("#vote-count").html(data);
								$("#vote-count").attr("title", $("#vote-count").html() + " people found this useful");
								$("#vote-up").attr("src", "/images/VotedUp.png");
							}
						});
		});

		$("#vote-down").click(function() {
			var pos = $(this).position();

			$.post("/library/votedown/", 
						{ SavedExpressionID: strSavedExpressionID },
						function(data) { 
							if (data == "AlreadyVoted")
								$("#AlreadyVotedDialog").modal({overlayClose:true,minHeight:0,minWidth:300,position: [pos.top + 20, pos.left + 50]});
							else if (data == "OwnVote")
								$("#OwnVoteDialog").modal({overlayClose:true,minHeight:0,minWidth:300,position: [pos.top + 20, pos.left + 50]});
							else if (data == "LogIn")
								$("#LogInDialog").modal({overlayClose:true,minHeight:0,minWidth:300,position: [pos.top + 20, pos.left + 50]});
							else {
								$("#vote-count").html(data); 
								$("#vote-count").attr("title", $("#vote-count").html() + " people found this useful");
								$("#vote-down").attr("src", "/images/VotedDown.png");
							}
						});
		});
		
	});
