var formBlocked = false ;

var starSelected = [0, 0, 0, 0, 0, 0, 0] ;

function starOver (voteId, starId) {
	if(starSelected[voteId] == 0) {
		for(var i = 1; i <= 5; i++) {
			if(starId >= i) {
				document.getElementById('star'+voteId+'_'+i).src = USER.IMAGES_URL+'/star_blue.gif' ;
			}
			else {
				document.getElementById('star'+voteId+'_'+i).src = USER.IMAGES_URL+'/star_gray.gif' ;
			}
		}
	}
}

function starOut (voteId, starId) {
	for(var i = 1; i <= 5; i++) {
		if(starSelected[voteId] >= i) {
			document.getElementById('star'+voteId+'_'+i).src = USER.IMAGES_URL+'/star_blue.gif' ;
		}
		else {
			document.getElementById('star'+voteId+'_'+i).src = USER.IMAGES_URL+'/star_gray.gif' ;
		}
	}
}

function starClick (postUniqueId, voteId, starId) {
	if(starSelected[voteId] == 0) {
		starSelected [voteId] = starId ;

		$.ajax({
			type: 'POST',
			url: 'posts.php',
			data: 'action=add-vote&postUniqueId='+postUniqueId+'&voteId='+voteId+'&starId='+starId,
			success: function(msg) {
				for(var i = 1; i <= 5; i++) {
					if(document.getElementById('star'+voteId+'_'+i)) {
						document.getElementById('star'+voteId+'_'+i).style.cursor = 'default' ;
					}
				}

				$('#voteText').fadeIn('slow', function () {
					setTimeout(function () { $('#voteText').fadeOut('slow') ; }, 2000) ;
				}) ;
			}
		}) ;
	}
}

$(document).ready(function() {
	$('#commentForm').validate({
		submitHandler: function(form) {
			if(typeof formBlocked == 'undefined' || !formBlocked) {
				formBlocked = true ;

				create_loading(form.id, 'Envoi du commentaire...', {fontWeight: 'bold', fontSize: '110%'}) ;

				jQuery(form).ajaxSubmit({
					success: function (response, type, form) {
						var obj = eval("(" + response + ')') ;

						if(obj.success) {
							window.location.reload() ;
						}
						else {
							destroy_loading(form.attr('id')) ;
							alert_msg(obj.errors.reason) ;
							formBlocked = false ;
						}
					}
				});
			}
		}
	});
});
