var CtRating = Class.create(CtComponent_Abstract, {

	entityName: null,
	entityId: null,
	isVoted: null,
	buttonList: null,


	initialize: function(htmlElement) {
		this.element = $(htmlElement);
		this.id = this.element.identify();

		try {
			var json = this.element.readAttribute('rel').evalJSON();
			this.element.writeAttribute('rel', null);
			if (json.length != 3) {
				return;
			}
			this.entityName = json[0];
			this.entityId = json[1];
			this.isVoted = json[2];
		} catch (ex) {
			return;
		}

		this.element.addClassName('ctRating-' + this.entityName + '-' + this.entityId);

		this.buttonList = this.element.select('a.ctRating-button');

		if (!this.isVoted) {
			for (var i = 0; i < this.buttonList.length; i++) {
				this.buttonList[i].observe('click', this.clickHandler.bindAsEventListener(this, i + 1));
				this.buttonList[i].observe('mouseover', this.mouseoverHandler.bindAsEventListener(this, i + 1));
				this.buttonList[i].observe('mouseout', this.mouseoutHandler.bindAsEventListener(this, i + 1));
			}
		}

		CtPage.addComponent(this);
	},


	clickHandler: function(event, rating) {
		Event.stop(event);

		if (this.isVoted) {
			return;
		}

		var request = CtPage.getRequest(this.id, 'ajax');
		request.url = '/rating';
		request.options.parameters = {t: this.entityName, id: this.entityId, r: rating};

		request.send();
	},


	mouseoverHandler: function(event, rating) {
		Event.stop(event);

		if (this.isVoted) {
			return;
		}

		for (var i = 0; i < this.buttonList.length; i++) {
			if ((i + 1) <= rating) {
				this.buttonList[i].addClassName('ctRating-hover');
			} else {
				this.buttonList[i].removeClassName('ctRating-hover');
			}
		}
	},


	mouseoutHandler: function(event, rating) {
		Event.stop(event);

		if (this.isVoted) {
			return;
		}

		for (var i = 0; i < this.buttonList.length; i++) {
			this.buttonList[i].removeClassName('ctRating-hover');
		}
	},


	refresh: function(response) {
		if (!response || !response.json || !response.json.rating || !response.json.count_vote) {
			return;
		}

		for (var i = 0; i < this.buttonList.length; i++) {
			this.buttonList[i].removeClassName('ctRating-hover');
		}

		$$('.ctRating-' + this.entityName + '-' + this.entityId).each(function(element) {
			CtPage.getComponent(element.identify()).vote(response.json.rating);
		});

		$$('.ctRating-count-vote-' + this.entityName + '-' + this.entityId).each(function(element) {
			element.update(response.json.count_vote);
		});
	},


	vote: function(rating) {
		this.isVoted = true;
		this.element.writeAttribute('title', 'Вы уже проголосовали');
		this.element.addClassName('ctRating-voted');

		for (var i = 0; i < this.buttonList.length; i++) {
			if ((i + 1) <= rating) {
				this.buttonList[i].removeClassName('ctRating-unchecked');
				this.buttonList[i].addClassName('ctRating-checked');
			} else {
				this.buttonList[i].addClassName('ctRating-unchecked');
				this.buttonList[i].removeClassName('ctRating-checked');
			}
		}
	}

});



var CtRating_Factory = new (Class.create({
	initialize: function() {
		Event.observe(document, 'dom:loaded', function() { this.refresh() }.bind(this));
	},

	refresh: function(htmlElement) {
		var elements = [];
		if (htmlElement) {
			elements = $(htmlElement).select('.ctRating');
		} else {
			elements = $$('.ctRating');
		}
		for (var i = 0; i < elements.length; i++) {
			elements[i].removeClassName('ctRating');
			new CtRating(elements[i]);
		}
	}
}));

CtPage.registerScript("CtRating");