var hasVotedOnChapter = false;
var currentChapterId = 0;
var commentVotes = [];

var chapterRatingDescriptions = {
	1:"Needs a complete rewrite.",
	2:"Below Average.",
	3:"Average.",
	4:"Above Average.",
	5:"Brilliant."
}

function ToggleThumb(obj){
	if(obj.src.search(/thumb\-up\-gold\-13.gif/g) >= 0){
		obj.src = "/images/thumb-up-grey-13.gif";
	}
	else{
		obj.src = "/images/thumb-up-gold-13.gif";
	}
}

function AddCommentLikeComplete(response){
	var xp = new XmlParser();
	var xml = xp.GetXmlDocument(response);

	var root = xml.documentElement;
	try{
		var commentId = Trim(root.getElementsByTagName("CommentId")[0].firstChild.nodeValue);
		var karma = Trim(root.getElementsByTagName("CommentKarma")[0].firstChild.nodeValue);
		var likeCount = Trim(root.getElementsByTagName("CommentLikeCount")[0].firstChild.nodeValue);
		var karmaText = karma + ' karma point';
		if(karma != 1)
			karmaText += 's';

		$('#comment' + commentId + 'karma').text(karmaText);
		$('#comment' + commentId + 'likecount').text(likeCount);
		$("#commentvote" + commentId).text("Thanks!");

		commentVotes.push(commentId);
	}
	catch(ex){
		var commentId = Trim(root.getElementsByTagName("CommentId")[0].firstChild.nodeValue);
		var errorNumber = root.getElementsByTagName("ErrorNumber")[0].firstChild.nodeValue;
		if(errorNumber == ERROR_VOTE_OCCURRED_TOO_SOON){
			$('#commentvote' + commentId).text("You just liked a critique.  Wait a bit and try again.");
		}
		else if(errorNumber == ERROR_VOTE_EXISTS){
			$("#commentvote" + commentId).text("Thanks!");
		}
		else{
			$('#commentvote' + commentId).text("There was a problem recording your like.  Try again later.");
		}
	}
}

function HasVotedOnComment(commentId){
	for(i = 0; i < commentVotes.length; i++){
		if(commentVotes[i] == commentId)
			return true;
	}
	return false;
}

function AddChapterVote(chapterId, vote, isLoggedIn){
	if(!isLoggedIn){
		document.getElementById("starbar" + chapterId).innerHTML = "Please <a href=\"/login.php\">log in</a> to rate.";
		hasVotedOnChapter = true;
		return;
	}

	if(hasVotedOnChapter)
		return;

	document.getElementById("starbar" + chapterId).innerHTML = "Just a sec...";

	if(vote > 5)
		vote = 5;

	if(vote < 0)
		vote = 0;

	currentChapterId = chapterId;

	var params = new Object();
	params["chapterid"] = chapterId;
	params["vote"] = vote;

	ac = new AjaxConnection();
	ac.Send("/webservices/addvote.php", params, "AddChapterVoteComplete");
}

function AddChapterVoteComplete(response){
	var xp = new XmlParser();
	var xml = xp.GetXmlDocument(response);

	var root = xml.documentElement;
	if(root.getElementsByTagName("ErrorNumber") != undefined && root.getElementsByTagName("ErrorNumber")[0] != undefined){
		var errorNumber = root.getElementsByTagName("ErrorNumber")[0].firstChild.nodeValue;
		if(errorNumber == ERROR_VOTE_OCCURRED_TOO_SOON){
			$("#starbar" + currentChapterId).text("You just rated a work.  Wait a bit and try again.");
		}
		if(errorNumber == ERROR_VOTE_EXISTS){
			hasVotedOnChapter = true;
			$("#starbar" + currentChapterId).text("Thanks for rating!");
		}
	}
	else{
		hasVotedOnChapter = true;
		$("#starbar" + currentChapterId).text("Thanks for rating!");
	}
}

function AddCommentLike(commentId, isLoggedIn){
	if(HasVotedOnComment(commentId))
		return;

	if(!isLoggedIn){
		document.getElementById("commentvote" + commentId).innerHTML = "Please <a href=\"/login.php\">log in</a> to like this critique.";
		return;
	}

	$("#commentvote" + commentId).text("Just a sec...");

	var params = new Object();
	params["commentid"] = commentId;

	ac = new AjaxConnection();
	ac.Send("/webservices/addcommentlike.php", params, "AddCommentLikeComplete");
}

function ShowStarDescription(chapterId, rating, isLoggedIn){
	if(hasVotedOnChapter)
		return;
	if(!isLoggedIn)
		return;

	document.getElementById("starbar" + chapterId).innerHTML = chapterRatingDescriptions[rating];
}

function HideStarDescription(chapterId, isLoggedIn){
	if(hasVotedOnChapter)
		return;
	if(!isLoggedIn)
		return;

	var element = document.getElementById("starbar" + chapterId);

	for(key in chapterRatingDescriptions){
		if(element.innerHTML == chapterRatingDescriptions[key]){
			element.innerHTML = "Click to rate.";
		}
	}
}
