/* Copyright (c) 2009 you-vs-me.com
 *-----------------------------------------------------*/

$(document).ready(function() {
	$(".comment-submit").click(function() {
		var string = $("#commenttext").val();
		
		if(!string.length) {
			openOverlay('You can not submit an empty comment.');
			return false;	
		} else if(string.length > 255) {
			openOverlay('Is that smoke coming from the servers? Your comment is too long!');
			return false;
		}
	});
	
	if(!$("#commenttext").val()) {
		$("#commenttext").val('Add your comment...').addClass("light");
	}
	
	$("#commenttext").focus(function() {
		if($(this).hasClass("light")) {
			$("#commenttext").val('').removeClass("light");
		}
	});

	$(".comment-reply").click(function() {
		$("#comment-reply").html($(this).attr("title") + ":");
		$("#parent_id").val($(this).attr("id"));
	});
	
	$(".show-replies").click(function() {
		var id = $(this).attr("id");
		$("#comment_" + id).children(".replies").slideUp("fast");
		
		$.getJSON("/app/ajax/topic-comment-replies",
		{ topic_comment_id: id },
		function(data) {
			$("#comment_" + id).children(".replies").html(data.html).slideDown("fast");
		});
	});
	
	$("#share-on-twitter").change(function() {
		$("#twitter-credentials").slideDown();
		$(this).unbind("change");
	});
	
	$(".comment-delete").click(function() {
		deleteComment($(this).attr("id"));
	});
	
	function deleteComment(ids) {
		var params = 'ids=' + ids;
		
		jQuery.ajax({
			url: '/app/ajax/delete-comment',
			type: 'GET',
			data: params,
			complete: function(transport) {
				var response = transport.responseText || "no response";
				
				if(response == 'SUCCESS') {
					openOverlay("Your comment has been removed");
					$('#comment_' + ids).slideUp("slow");
				}
			}
		});
	}
});

function topicVoteLogin() {
	openOverlay('You must login to vote.');
}

function topicVote(topicID, wordID) {
	var params = 'topic=' + topicID + '&word=' + wordID;
 	var class1 = '';
 	var class2 = '';
 	
 	$.getJSON("/app/ajax/vote", { topic: topicID, word: wordID }, function(data) {
			if(data.voted == false) {
				if(wordID == data.ajax[0].word_id) {
					class1 = "voted";
				}
				
				if(wordID == data.ajax[1].word_id) {
					class2 = "voted";
				}
				
				var word1 = data.ajax[0].word_id;
				$("#vote-box-" + word1).children("span").attr("class", class1);
				$("#vote-box-" + word1).children("h2").html(data.ajax[0].vote_percent + "%");
				$("#vote-box-" + word1).children("h3").html(data.ajax[0].vote_count);
				
				var word2 = data.ajax[1].word_id;
				$("#vote-box-" + word2).children("span").attr("class", class2);
				$("#vote-box-" + word2).children("h2").html(data.ajax[1].vote_percent + "%");
				$("#vote-box-" + word2).children("h3").html(data.ajax[1].vote_count);
				
				if($("#vote-box-" + word1).hasClass("me")) {
					$("#progress").animate({"left": (data.ajax[0].vote_percent*1.5) + "px"}, "slow", "swing");
				} else {
					$("#progress").animate({"left": (data.ajax[1].vote_percent*1.5) + "px"}, "slow", "swing");
				}
			} else {
				openOverlay('<p class="fail">It appears your vote has already been calculated for this topic!</p>');
			}
	});
}

function commentRate(commentID, rating) {
	var params = 'comment_id=' + commentID + '&rating=' + rating;
 	
 	jQuery.ajax({
		url: '/app/ajax/commentrate',
		type: 'GET',
		data: params,
		complete: function(transport) {
			var response = transport.responseText || "no response";
			
			$('#comment-vote-' + commentID).html(response);
		}
	});
}

function topicNotices(topicId) {
 	var params = 'topic_id=' + topicId;
	
	jQuery.ajax({
		url: '/app/ajax/topic-notices',
		type: 'GET',
		data: params,
		complete: function(transport) {
			$('#updates').html(transport.responseText);
		}
	});
 }
 
function favouriteTopic(topicId) {
	var params = 'topic_id=' + topicId;
	
	jQuery.ajax({
		url: '/app/ajax/favourite-topic',
		type: 'GET',
		data: params,
		complete: function(transport) {
			var response = transport.responseText || 'null';
			
			if(response != 'null') {
				$('#follow').html(response);
			} else {
				openOverlay('You must login to add topics to your favourites.');
			}
		}
	});
 }
 
function unFavouriteTopic(topicId) {
	var params = 'topic_id=' + topicId;
 	
	jQuery.ajax({
		url: '/app/ajax/un-favourite-topic',
		type: 'GET',
		data: params,
		complete: function(transport) {
			var response = transport.responseText || 'null';
			
			if(response != 'no response') {
				$('#follow').html(response);
			}
		}
	});
}