/**
 * @author K.Kowalczykiewicz
 */

if (!Idea) var Idea = {};

Idea.PlaceRating = function(el)
{
	this.initialize(el);
};

Idea.PlaceRating.prototype = {
	element: null,
	stars: null,
	PlaceID: 0,
	currentRating: 0,
	hoveredRating: 0,
	initialize: function(el){
		if (el.tagName) 
			this.container = el;
		else
			el = this.container;
		this.currentRating = currentPlaceRating;
		this.PlaceID = currentPlaceID;
		this.element = el.getElement('.obj_note');;
		this.stars = this.element.getElements('div');
		for (var i = 0; i < this.stars.length; i++){
			this.stars[i].rating = i + 1;
			this.stars[i].addEvent('mouseenter', this.hover.bind(this));
			this.stars[i].addEvent('click', this.click.bind(this));
		}
		this.element.addEvent('mouseleave', this.leave.bind(this));		
	},
	hover: function(e){
		e = new Event(e);
		this.hoveredRating = e.target.rating;
		this.select(this.hoveredRating);
	},
	leave: function(e){
		this.select(this.currentRating);
	},
	click: function(e){
		e = new Event(e);
		this.select(e.target.rating);
		this.container.innerHTML = "<img src='/files/images/loading.gif' />";
		new Request.HTML({
			url: '/hub/ajax/place_rating.php',
			update: this.container,
			onSuccess: this.initialize.bind(this)
		}).post({PlaceID: this.PlaceID, Rating: this.hoveredRating});
	},
	select: function(rating){
		for (var i = 0; i < this.stars.length; i++){
			this.stars[i].className = rating >= this.stars[i].rating ? 'full' : (this.stars[i].rating - rating <= 0.5 ? 'half' : 'empty');
		}
	}
};

window.addEvent('domready',function(){
	activeRatingDiv = $('activeRating');
	if (activeRatingDiv)
	{
		activeRatingDiv.handler = new Idea.PlaceRating(activeRatingDiv);
	}
});