﻿$(document).ready(function() {
	CatChow.AjaxRating();
});

CatChow.AjaxRating = function() {
	var itemId;
	var idTypes = ["articleId", "questionId", "videoId"];
	
	
	$("body.detail a.rate").each(function() {	
		jQuery.each(idTypes, function() {
			if (jQuery.url.param(this)) {
				itemId = jQuery.url.param(this);
			}
		});
		var isRated = jQuery.cookie("rated"+itemId);
		if (isRated) {
			$("body.detail a.rate").addClass("disabled");
			$("body.detail a.rate").parent("li").addClass("rating-disabled");
		}
	});
	
    $("body.detail a.rate").click(function(event) {
		event.preventDefault();
		$this = $(this);
		var ratingType = false;
		if ($this.hasClass("disabled")) {
			return false;
		}
		if ($this.hasClass("rate-up")) {
			ratingType = true;
		}
		
		CatChow.Web.AjaxUtility.Rating(ratingType, itemId, function(result) {
			var value;
			if (result.value) {
				value = result.value;
				jQuery.cookie("rated"+itemId, true, { expires: 7});
				$("body.detail a.rate").addClass("disabled");
				$("body.detail a.rate").parent("li").addClass("rating-disabled");
			} else {
				console.log(result.error.Message);
				value = "An error occurred.";
			}
			$("body.detail .rating-stats").html(value);
		});
	});
};