
// DOMready
$(document).ready(function() {

	$('#btn_vote').click(function (e) {
		var vid 	= $(this).attr('vid').substr(4);  // Get the video id from the vid attribute in the video vote button
		
		if (vid == '')
		{
			alert('Bad video id!');
		}
		else
		{
			// Manage the voting in AJAX and return the result in JSON format
			$.getJSON('ajax/votes.php', { mode:'vote', vid:vid }, function(j) {

				if (j[0].result == 'success')
				{			
					$('#vote_holder').html('Thank you for your vote!');		// Remove the button
					$('#vid_votes').html(j[0].count + ' Votes');			// Update the vote count
				}
				else if (j[0].result == 'avoted')
				{
					alert('You already voted.');
				}
				else if (j[0].result == 'novideo')
				{
					alert('This video doesn\'t exist.');
				}
				else if (j[0].result == 'error')
				{
					alert('No video id specified.');				
				}
				else if (j[0].result == 'notlogged')
				{
					alert('Please login to vote');
				}
			});
		}

		return false;
	});

});