$(document).ready(function() {
	var revertRating = $('#rating-count span').text();
	var revertStars = $('#stars-lg div').attr('class');
	$('.rate-btn').hover(
		function () {
			var rating = $(this).attr('id');
			var ratingText = $(this).attr('title');
			$('#rating-count span').text(ratingText);
			$('#stars-lg div').attr('class',rating);
		}, 
		function () {
			$('#rating-count span').text(revertRating);
			$('#stars-lg div').attr('class',revertStars);
		}
	);
	$('#rated').hover(
		function () {
			$('#rating-count span').text('You have already rated');
		}, 
		function () {
			$('#rating-count span').text(revertRating);
		}
	);
	$('.signup-comment form').submit(function() {
		var this_comment = $(this).children('textarea').val();
		var this_playlist_id = $(this).children('input[type="hidden"]').val();
		addComment(this_playlist_id,this_comment);
		return false;
	});
});
function addComment(playlistID,comment) {
	$.ajax({
		type: 'POST',
		url: '/submit/comment',
		data: 'playlist='+playlistID+'&comment='+comment,
		beforeSend: function(){
			
		},
		error: function(){
			$('.signup-comment h3 span').text('Error: try again');
		},
		success: function(msg){
			if(msg=='success'){
				$('.signup-comment form').replaceWith('<p>Thanks for commenting! Your comment has been placed into a queue and will be posted shortly.</p>');
			}
			else{
				$('.signup-comment h3 span').text('Error: refresh page');
			}
		},
		timeout: 10000
	});
	return false;
}
function setRating(playlistID,userRating) {
	$.ajax({
		type: 'POST',
		url: '/submit/rating',
		data: 'playlist='+playlistID+'&rating='+userRating,
		beforeSend: function(){
			
		},
		error: function(){
			$('#rating-count span').text('Error: try again');
		},
		success: function(msg){
			if(msg=='success'){
				$('#stars-lg div').html('<img src="/img/blank.gif" width="18" height="18" border="0" />');
				$('#rating-count span').text('Thanks for rating!');
				$('#stars-lg div').attr('class','r'+userRating+'0');
			}
			else{
				$('#stars-lg div').html('<img src="/img/blank.gif" width="18" height="18" border="0" />');
				$('#rating-count span').text('Error: refresh page');
				$('#stars-lg div').attr('class',revertStars);
			}
		},
		timeout: 10000
	});
	return false;
}