/**
 * jQuery hot or not plugin
 */
 (function(jQuery) {


	/**
	 * Hot or Not
	 *
	 * @return jQuery
	 */
	jQuery.fn.gmc_hotOrNot = function(options) {

	    options = jQuery.extend({
	        submitUrl: this.attr('submitUrl') || ''
	    }, options || {});

		var self = this;
		var jqueryReturn = jQuery(this);

		jQuery(".hotornot .hot").click(function() {
			hotOrNot(jQuery(this).attr("rel"), "1");
			jQuery(this).click(function() { return false; });
			jQuery(".not", self).click(function() { return false; });
			return false;
		});

		jQuery(".not", self).click(function() {
			hotOrNot(jQuery(this).attr("rel"), "0");
			jQuery(this).click(function() { return false; });
			jQuery(".hot", self).click(function() { return false; });
			return false;
		});

		function hotOrNot(postId, value) {
			jQuery.ajax({
				//async: false,
				cache: false,
				data: "post=" + encodeURIComponent(postId) + "&value=" + encodeURIComponent(value),
				dataType: "json",
				error: hotOrNotError,
				success: hotOrNotSuccess,
				type: "post",
				url: options.submitUrl
			});
		}

		function hotOrNotError(XMLHttpRequest, textStatus, errorThrown) {
		}

		function hotOrNotSuccess(data, textStatus) {
			if (typeof data.success != "undefined" && data.success && data.value) {
				var value = parseInt(data.value);
				var percent = parseInt(data.percent);
				var height = Math.round(value * 100);
				var result = 0;
				if (value < 0.2) {
					result = 1;
				} else if (value < 0.4) {
					result = 2;
				} else if (value < 0.6) {
					result = 3;
				} else if (value < 0.8) {
					result = 4;
				} else if (value <= 1.0) {
					result = 5;
				}
				//jQuery(".indicator", self).height(height + "px").css("display", "block");
				jQuery(".indicator", self).addClass("indicator-" + result).removeClass("indicator");
				jQuery(".percent .value", self).text(percent);
				jQuery(".percent", self).show();
				jQuery(".hot", self).hide();
				jQuery(".or", self).hide();
				jQuery(".not", self).hide();
			} else if (typeof data.errorCode != "undefined" && data.errorCode == "cookie") {
				alert("Oooops … slow down!");	// todo: provide a proper error message
			}
		}

		return jqueryReturn;

	};


})(jQuery);



