/* Cast positive vote for a rhyme */
function positiveRhymeVote(rid) {
	$('.loading-vote-' + rid).fadeIn('slow', function() {
		$.post('/rhymes/updateRhymeVotePost/',
			{vote: 1, rid: rid},
			function(data) {
				$('.loading-vote-' + rid).fadeOut('fast', function() {
					$('.vote-current-' + rid).html('You voted... <img src="/public/img/check.png" title="Sweet!" alt="Sweet!" />');
					$('.vote-message-' + rid).html('<strong>Re-vote?</strong>');
				});
			});
	});
}

/* Cast negative vote for a rhyme */
function negativeRhymeVote(rid) {
	$('.loading-vote-' + rid).fadeIn('slow', function() {
		$.post('/rhymes/updateRhymeVotePost/',
			{vote: -1, rid: rid},
			function(data) {
				$('.loading-vote-' + rid).fadeOut('fast', function() {
					$('.vote-current-' + rid).html('You voted... <img src="/public/img/cross.png" title="Weak!" alt="Weak!" />');
					$('.vote-message-' + rid).html('<strong>Re-vote?</strong>');
				});
			});
	});
}

// Create reply form for comments section
function createReplyForm(qid, comment_id, parent) {
	$.post('/rhymes/createReplyForm',
		{qid: qid, comment_id: comment_id, parent: parent},
		function(data) {
			if (data != '') {
				$('#reply_barrier_' + comment_id).show().html(data);
				$('#comment_button_reply_' + comment_id).attr('disabled','disabled').text('Replying...');
				$('.comment_button_cancel_' + comment_id)
					.bind('click', function() {
						$('#form_comment_reply_' + comment_id).remove();
						$('#comment_button_reply_' + comment_id).removeAttr('disabled').text('Reply');
					});
			}
		});
}

// User comment reply form
function commentReplyForm(form) {
	var reply = form.reply;
	var qid = form.qid;
	var comment_id = form.comment_id;
	var parent = form.parent;
	var userid = form.userid;
	var name = form.name;
	
	// Reply field empty
	if(trim(reply.value) == "") {
		$('#' + form.id).find('.form_msg').fadeOut('slow', function() {
			$(this).html('Please enter a reply.').addClass('error').fadeIn('slow');
		});
		return false;
	
	// Success!
	} else {
		$('#' + form.id + ' :button').attr('disabled','disabled');
		$.post('/rhymes/commentReply',
			{reply: reply.value, qid: qid.value, comment_id: comment_id.value, parent: parent.value, userid: userid.value, name: name.value},
			function(data) {
//				$('#reply_barrier_' + comment_id.value).html('Thanks for your comment!').removeClass('error').addClass('success').fadeIn('slow');
				$('#' + form.id).find('.form_msg').html('Thanks for your comment!')
					.removeClass('error').addClass('success').fadeIn('slow');
				$('#' + form.id + ' :textarea').attr('disabled','disabled');
				$('#comment_button_reply_' + comment_id.value).text('Replied');
			});
	}

	return false;
}

// User comment form
function commentForm(form) {
	var comment = form.comment;
	var qid = form.qid;
	var userid = form.userid;
	var name = form.name;
	
	// Comment field empty
	if(trim(comment.value) == "") {
		$('#' + form.id).find('.form_msg').fadeOut('slow', function() {
			$(this).html('Please enter a comment.').addClass('error').fadeIn('slow');
		});
		return false;
	
	// Success!
	} else {
		$('#' + form.id + ' :button').attr('disabled','disabled');
		$.post('/rhymes/comment',
			{comment: comment.value, qid: qid.value, userid: userid.value, name: name.value},
			function(data) {
				$('#' + form.id).find('.form_msg').html('Thanks for your comment!')
					.removeClass('error').addClass('success').fadeIn('slow');
				$('#' + form.id + ' :textarea').attr('disabled','disabled');
			});
	}

	return false;
}

// Build replies block under comment
function build_replies(qid, comment_id) {
	$('#comment_row_' + comment_id).find('.view_replies').removeAttr('onclick');
	
	$.post('/rhymes/buildReplies',
		{qid: qid, comment_id: comment_id},
		function(data) {
			$('#comment_row_' + comment_id).append(data).find('.view_replies').bind('click', function() {
					hide_replies(comment_id);
				}).text('Hide replies').show();
		});
}

// Hide reply block under comment
function hide_replies(comment_id) {
	$('#comment_row_' + comment_id).find('.view_replies').unbind('click');
	$('#comment_row_' + comment_id).find('.view_replies').bind('click', function() {
			show_replies(comment_id);
		}).text('Show replies');
	$('#reply_block_' + comment_id).hide();
}

// Show reply block under comment
function show_replies(comment_id) {
	$('#comment_row_' + comment_id).find('.view_replies').unbind('click');
	$('#comment_row_' + comment_id).find('.view_replies').bind('click', function() {
			hide_replies(comment_id);
		}).text('Hide replies');;
	$('#reply_block_' + comment_id).show();
}

// Rate a comment
function vote(qid, comment_id, rating) {
	$.post('/content/commentVote',
		{qid: qid, comment_id: comment_id, rating: rating},
		function(data) {
			var current_score = parseInt($('#comment_post_score_' + comment_id).text());
			if (rating > 0) {
				$('#comment_post_score_' + comment_id).text((current_score + 1));
				$('#comment_vote_positive_' + comment_id).html('<img src="/public/img/check.png" alt="Sweet!" />');
 				$('#comment_vote_negative_' + comment_id).html('<img src="/public/img/cross_grey.png" alt="Weak!" />');
			} else {
				$('#comment_post_score_' + comment_id).text((current_score - 1));
				$('#comment_vote_positive_' + comment_id).html('<img src="/public/img/check_grey.png" alt="Sweet!" />');
				$('#comment_vote_negative_' + comment_id).html('<img src="/public/img/cross.png" alt="Weak!" />');
			}
		});
}

// Remove a rhyme
function removeRhyme(rid) {
	var answer = confirm("Are you sure you want to remove this rhyme?\n(Warning: all comments and votes associated with this rhyme will disappear as well.)");
	
	if (answer) {
		$.post('/rhymes/removeRhymePost',
		{rid: rid},
		function(data){
			$('#rhyme-' + rid).fadeOut('slow');
		});
	}
	
	return false;
}

$(document).ready( function() {
	
//	// Set the path to the SWF file
//	ZeroClipboard.setMoviePath('/surveysays/public/swf/ZeroClipboard.swf');
//	
//	// setup single ZeroClipboard object for all our elements
//	var clip = new ZeroClipboard.Client();
//	clip.setHandCursor(true);
//	
//	// assign a common mouseover function for all elements using jQuery
//	$('.clipboard').mouseover( function() {
//		// set the clip text to our innerHTML
//		clip.setText(this.name);
//		
//		// reposition the movie over our element
//		// or create it if this is the first time
//		if (clip.div) {
//			clip.receiveEvent('mouseout', null);
//			clip.reposition(this);
//		}
//		else clip.glue(this);
//
//		// gotta force these events due to the Flash movie
//		// moving all around.  This insures the CSS effects
//		// are properly updated.
//		clip.receiveEvent('mouseover', null);
//	});
	
	/* jEditable - edit artist */
	$('.edit_artist').editable('/rhymes/editRhymeArtistPost', {
		type: 		'text',
		width: 		'200px',
		event: 		'dblclick',
		submit: 	'Update',
		cancel: 	'Nevermind',
		indicator: 	'Saving...',
		tooltip: 	'Double click to edit...',
		style: 		'inherit'
	});
	
	/* jEditable - edit album */
	$('.edit_album').editable('/rhymes/editRhymeAlbumPost', {
		type: 		'text',
		width: 		'200px',
		event: 		'dblclick',
		submit: 	'Update',
		cancel: 	'Nevermind',
		indicator: 	'Saving...',
		tooltip: 	'Double click to edit...',
		style: 		'inherit'
	});
	
	/* jEditable - edit song */
	$('.edit_song').editable('/rhymes/editRhymeSongPost', {
		type: 		'text',
		width: 		'200px',
		event: 		'dblclick',
		submit: 	'Update',
		cancel: 	'Nevermind',
		indicator: 	'Saving...',
		tooltip: 	'Double click to edit...',
		style: 		'inherit'
	});
	
	/* jEditable - edit rhyme */
	$('.edit_rhyme').editable('/rhymes/editRhymeQuotePost', {
		type: 		'textarea',
		event: 		'dblclick',
		submit: 	'Update',
		cancel: 	'Nevermind',
		indicator: 	'Saving...',
		tooltip: 	'Double click to edit...'
	});
});
