/**
должны быть определены параметры:
_php_ajax_url - урл скрипта для простановки / получения оценок
_php_photo_id - id фотографии
_php_cookie_name - имя куки, куда кладуться id-и после проголосования
_php_container_name - контейнер, содержащий звездочки. имеет св-во onmouseuot
_php_pix_path - путь к файлам звездочек
**/


var Vote = {
	currentPoint: 0,
	position: 0,
	enabled: navigator.cookieEnabled,
	imgList: [],
	ajaxUrl: _php_ajax_url,
	photoId: _php_photo_id,
	cookieName: _php_cookie_name,
	containerName: _php_container_name,
	pixPath: _php_pix_path,

	init: function(event)
	{
		this.show(event, this.currentPoint);
		this.position = 0;

		if (!this.checkCookie() && this.enabled) {
			this.enabled = false;
			$(this.containerName).style.cursor = null;
			Event.stopObserving($(this.containerName), 'mouseout', this.init.bindAsEventListener(this));
			for(var i = 1; i<6; i++) {
				Event.stopObserving($('star_'+i).parentNode, 'mouseover', this.show.bindAsEventListener(this, i, true));
				Event.stopObserving($('star_'+i).parentNode, 'click', this.set.bindAsEventListener(this, i));
			}
		}
	},

	setCurrent: function()
	{
		if (!this.enabled)
			return false;

		new Ajax.Request(this.ajaxUrl+'&id='+this.photoId,
			{
				onSuccess: function(transport) {
					var response = transport.responseText.evalJSON(true);
					Vote.currentPoint = response.average;
					Event.observe($(Vote.containerName), 'mouseout', Vote.init.bindAsEventListener(Vote));
					for(var i = 1; i<6; i++) {
						Event.observe($('star_'+i).parentNode, 'mouseover', Vote.show.bindAsEventListener(Vote, i,true));
						Event.observe($('star_'+i).parentNode, 'click', Vote.set.bindAsEventListener(Vote, i));
					}
					$(Vote.containerName).style.cursor ='pointer';

					Vote.init();
				}
			}
		);
	},

	show: function(event, point, action)
	{
		if (this.enabled) {

			if (action == true) {
				if (point == this.position)
					return;
				this.position = point;
			}

			for (var i = 1; i < 6; ++i)
				this._updateImage(i, (i <= point ? point : 0), action);
		}
	},

	set: function(event, point)
	{
		if (!this.checkCookie()) {
			return false;
		}

		new Ajax.Request(this.ajaxUrl+'&id='+this.photoId+'&action=set&points='+point,
			{
				onSuccess: function(transport) {
					var response = transport.responseText.evalJSON(true);
					Vote.currentPoint = response.average;
					Vote.init();
				}
			}
		);

		return true;
	},

	checkCookie: function()
	{
		if (photoCookie = Cookie.get(this.cookieName)) {

			if (-1 == photoCookie.indexOf(','))
				photoCookie = [photoCookie];
			else
				photoCookie = photoCookie.split(',');

			if (photoCookie.in_array(this.photoId))
				return false;
		}

		return true;
	},

	_updateImage: function(id, point, action)
	{
		if (!this.imgList[id])
			this.imgList[id] = $('star_' + id);

		if (this.imgList[id])
			this.imgList[id].src = this.pixPath+ (point == 0 ? 'star_off' : (action == true ? 'star_mouse' : 'star_on'))+ '.gif';
	},

	_isSaveCookie: function()
	{
		if (photoCookie = Cookie.get(this.cookieName)) {

			if (-1 == photoCookie.indexOf(','))
				photoCookie = [photoCookie];
			else
				photoCookie = photoCookie.split(',');

			if (photoCookie.in_array(this.photoId))
				return false;
			else
				photoCookie.push(this.photoId);

		} else
			photoCookie = [this.photoId];

		Cookie.set(this.cookieName, photoCookie, 30);

		return true;
	}
};
