$(document).ready(function(){
	$('#videos-list').jScrollPane({ scrollbarOnLeft: true, dragMinHeight: 20, scrollbarWidth: 8 });
	
	$('#videos-list a').click(function(){
		$.ajax({
		   type: "POST",
		   url: urlForVideo,
		   data: "id=" + this.id,
		   success: function(html) {
	   		$('#pplayer').html(html);
		   }
		 });
	});
	
	$('.star').hover(function(){
		var currentId = parseInt(this.id.substring(5));
		$('.star').removeClass('white');
		for (var i=1;i<=currentId;i++)
		{
			$('#star-' + i).addClass('white');
		}
	});
	
	$('.star').click(function(){
		var currentId = parseInt(this.id.substring(5));
		var videoId = $('.bottom-bar').attr('id').substring(4);
		var email = $('#email-tb').val();
		
		if (email == '')
		{
			$('#email-tb').addClass('red-theme');
			$('#vote-response').html("Please insert you're email address!");
		}
		else 
		{
			$.ajax({
			   type: "POST",
			   url: validationURL,
			   data: "id=" + videoId + "&email=" + email,
			   success: function(msg){
					if (msg == 2) {
						$('#email-tb').addClass('red-theme');
						$('#vote-response').html('Invalid email address!');
					} else if (msg == 3) {
						$('#email-tb').addClass('red-theme');
						$('#vote-response').html('This email address already voted for this video!');
					} else {
						$('#vote-response').html('');
						$('#raters').html('');
						$('#currentRate').hide();
						$('#email-field').hide();
						
						$.getJSON(rateMovieUrl + "?id=" + videoId + "&val=" + currentId + "&email=" + email, function(data) {
								$('#currentRateNew').css('background-position', '0px -' + (15 * (parseInt(data.RATE))).toString() + 'px');
								$('#' + videoId + ' .currentRate1').css('background-position', '0px -' + (15 * (parseInt(data.RATE))).toString() + 'px');
								$('#currentRateNew').show();
								$('#rate-note').html(data.REAL_RATE);
								$('#' + videoId + ' .real-rate').html(data.REAL_RATE);
								
								if (data.RATE_NO == 1) {
									$('#video-votes').html(data.RATE_NO + ' vote');
									$('#' + videoId + ' .rate-no').html(data.RATE_NO + ' vote');
								} else {
									$('#video-votes').html(data.RATE_NO + ' votes');
									$('#' + videoId + ' .rate-no').html(data.RATE_NO + ' votes');
								}
							});
					}
			   }
			 });
		}
	});
	
	$('#currentRate').hover(function(){
		$('#currentRate').hide();
		$('#raters').show();
		$('#email-field').show();
		$('#email-tb').focus();
	});
	
	$('#raters').mouseleave(function(){ 
		$('#currentRate').show();
		$('#raters').hide();
	});
	
	$('#video_description').keypress(function(e){
		var length = $('#' + this.id).val().length;
 
		if (length > 200) 
		{
			if (e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40)
			{
				return false;
			}
		}
		$('#chars').html(200 - length + ' characters remaining');
	});
});

