/* Javascript: Features functionality
 * Params: none:
 *
 */
 $(document).ready(function(){
//$(function(){
 $("input[name=star2]").rating({
  callback: function(value, link){
   // 'this' is the hidden form element holding the current value
   // 'value' is the value selected
   // 'element' points to the link element that received the click.
   //alert("The value selected was '" + value + "'\n\nWith this callback function I can automatically submit the form with this code:\nthis.form.submit();");
	if(logged_in == '1'){
		$("span#rate_msg").text("Saving...").stop().css("opacity", 1).fadeIn(30);
   		$.post('/ratings/rate/', {object_type:rating_object_type, object_id:rating_object_id, rating:value}, function(data) {showrate(data);}, "json");
   		return true;
	} else {
		login.show();
		$("input[name=star2]").rating('disable');
		$("input[name=star2]").rating('select',0);
	}
   // To submit the form automatically:
   //this.form.submit();
   
   // To submit the form via ajax:
   //$(this.form).ajaxSubmit();
  }
 });
});

function showrate(data){
	//alert(data['error_code'] + data['status']+ data['new_rate']);
	//$("span#rate_msg").html(data['status']);
	if(data['error_code'] == 0){
		$("input[name=star1]").rating('enable');
		$("input[name=star1]").rating('select',data['new_rate']);
		$("input[name=star1]").rating('disable');
		$("span#rate_msg").text("Rating saved (" + data['my_rate'] + "). Thanks!").stop().css("opacity", 1).fadeIn(30);
		
		if ( user_rating == 0 )
			userbar_message('action', 'rate_game');
			
		user_rating = data['my_rate'];
		
		//$("span#rate_msg").html('Thank you for rating this game!');
		//$("span#rate_msg").text("Thank you for rating this game!").stop().css("opacity", 1).fadeIn(30);
		
		// Hide confirmation message and enable stars for "Rate this" control, after 2 sec...
		setTimeout(function(){
			$("span#rate_msg").fadeOut(1000, function(){})
		}, 2000); 
	}
	if(data['error_code'] == 1) {
		//$("input[name=star2]").rating('drain');
		$("input[name=star2]").rating('disable');
		$("span#rate_msg").html(data['status']);
	}
}