if (typeof(window.Whomix) == 'undefined') { window.Whomix = {}; }

Whomix.RatingHelper = function(opt)
{
	var _gettingRatings = {};
	var _gettingMyRatings = {};
	var _gettingRatingKeys = null;
	this.getUrl = function(op) 
	{
		var ret = [];
		if (op.resource == "ratings" && op.method == "GET" && op.keys) {
			ret.push("ratings/", op.keys);
		}
		else if (op.resource == "ratings" && op.method == "GET") {
			ret.push("ratings");
		}
		else if (op.resource == "myratings" && op.method == "GET" && op.keys) {
			ret.push("myratings/", op.keys, (op.id ? ("/"+op.id) : ""));
		}
		else if (op.resource == "myratings" && op.method == "GET") {
			ret.push("myratings");
		}
		else if (op.resource == "remixes") {
			ret.push("remixes");
		}
		else if (op.resource == "download") {
			ret.push("remix/download/", op.id, ".mp3");
		}
		else if (op.resource == "rating" && op.method == "POST" && op.id)
		{
			ret.push("rating/", op.id);
		}
		else if (op.resource == "rating_keys")
		{
			ret.push("rating_keys");
		}
		else if (op.resource == "rating" && op.id && op.keys)
		{
			ret.push("rating/", op.keys.join(","), "/", op.id);
		}
		else if (op.resource == "remix" && op.id) {
			ret.push("remix/", op.id);
		}
		return Whomix.getUrl(ret.join(''));
	}
	this.getMyRatingsForRemix = function(opts, callback) 
	{
		var theUrl = this.getUrl({resource: "myratings", method: "GET", id: opts.remixid, keys: opts.keys});
		if (_gettingMyRatings[opts.remixid]) {
//			_gettingMyRatings[opts.remixid].abort();
		}
		_gettingMyRatings[opts.remixid] = $.getJSON(theUrl, function(data,resp) {
				callback(data,resp);
//				_gettingMyRatings[opts.remixid] = null;
			});
	}
	this.getMyRatings = function(callback) {
		var theUrl = this.getUrl({resource: "myratings", method: "GET"});
		$.getJSON(theUrl, function(data, resp) {
			callback(data, resp);
		});
	}
	this.getMeanRatings = function(callback) {
		var theUrl = this.getUrl({resource: "ratings", method: "GET"});
		$.getJSON(theUrl, function(data, resp) {
			callback(data, resp);
		});
	}
	this.getRatingsForRemix = function(opts, callback) 
	{
		var theUrl = this.getUrl({resource: "rating", method: "GET", id: opts.remixid, keys: opts.keys});
		if (_gettingRatings[opts.remixid]) {
//			_gettingRatings[opts.remixid].abort();
		}
		_gettingRatings[opts.remixid] = $.getJSON(theUrl, function(data,resp) {
				callback(data,resp);
//				_gettingRatings[opts.remixid] = null;
			});
	}
	this.getRatingKeys = function(callback) 
	{
		var theUrl = this.getUrl({resource: "rating_keys"});
		if (_gettingRatingKeys) {
	//		_gettingRatingKeys.abort();
		}
		_gettingRatingKeys = $.getJSON(theUrl, function(data,resp) {
				callback(data,resp);
//				_gettingRatingKeys = null;
			});
	}
	this.postRatingForRemix = function(opts, callback)
	{
		var theUrl = this.getUrl({resource: "rating", method: "POST", id: opts.remixid});
		$.ajax({
			url: theUrl, 
			data: "keys="+opts.key+"&values="+opts.value, 
			type: "POST",
			success: function(data, resp) {
				if (callback) {
					callback(data, resp);
				}
			},
			error: function(data, resp) {
				var dowecare = false;
			}
			});
	}
}

Whomix.RemixRatings = function(targetDiv, remixId, opts) {
	this._target = $(targetDiv);
	this._remixId = remixId;
	var rhcfg = opts || {};
	this._helper = new Whomix.RatingHelper(rhcfg);
	var self = this;
	this._ratingLine = {};
	this._target.hide();

	var _refreshAverageRatings = function(keyId, notinit) {
			self._helper.getRatingsForRemix({remixid: remixId, keys: [keyId]}, 
				function(data, resp) {
					var tv = (data.ratings ? (data.ratings[keyId] || 0.5) : 0.5) * 100.0;
					$rlpb = $(".ratingLabel", self._ratingLine[keyId]);
					if (notinit) {
						$rlpb.progressbar("option", "value", tv);
					} else {
						$rlpb.progressbar({value: tv});
					}
					var isDisabled = (!data.ratings || !data.ratings[keyId]);
					$rlpb.progressbar("option", "disabled", isDisabled);	
			});
			if(!notinit && opts && opts.onComplete) {
				opts.onComplete();
			}
	}

	var _onRatingsLoaded = function(data, resp) {
		self._target.html();
		$.each(data.rating_keys, function(k, v) {
			var $rd = $("<div/>").attr('id','rating_'+self._remixId+"_"+v.id);
			var $rl = $("<div/>").attr('class','ratingLabel').append($('<span/>').css({position: "absolute"}).text(v.name));
			var $rs = $("<div/>").attr('class','ratingSlider');
			$rd.append($rl).append($rs).appendTo(self._target);
			$(".ratingSlider", $rd).slider({value: 100.0 * (data.ratings[v.id] || 0.5) });
			$(".ratingSlider", $rd).bind("slidechange", function(event, ui) {
				var fv = parseFloat(ui.value) * 0.01;
				self._helper.postRatingForRemix({remixid: remixId, key: v.id, value: fv}, function() {
					_refreshAverageRatings(v.id, true);
					$(document).trigger("rating_changed.whomix", [remixId, v.id, fv]);
				});
			});
			self._ratingLine[v.id] = $rd;
			_refreshAverageRatings(v.id);
		});
		//self._target.show();
	}
	
	var _onRatingKeysLoaded = function(data, resp) {
		var possibleKeys = [];
		$.each(data.rating_keys, function(k, v) {
			possibleKeys.push(v.id);
		})
		self._possibleKeys = possibleKeys;
		self._helper.getMyRatingsForRemix({remixid: remixId, keys: possibleKeys}, _onRatingsLoaded);
	}
	this._helper.getRatingKeys(_onRatingKeysLoaded);
}


