var Rating = Class.create();

Rating.prototype = {
	initialize: function(target, in_params)
	{
		this.params = {
			readOnly: false,
			max:10,
			nrate:'',
			markImg:{'width':16, 'height': 48},
			image:'/i/rating/star.gif',
			//  	image2:'/i/rating/star_gr.jpg',
			onrate: null
		};
		
		this.params = $H(this.params).merge(in_params);
		
		this.el = $(target);
		if(!this.el)
			throw('Rating.initialization: Target element not found');

		this.el.setStyle({
		'background':'url("' + this.params.image + '")',
		//		'background':'green',
		width: this.params.max * this.params.markImg.width + 'px',
		height: (this.params.markImg.height / 3) + 'px'
		});
		this.actEl = $(document.createElement('div'));
		this.nrateEl = $(document.createElement('div'));
		this.el.appendChild(this.actEl);
		this.el.appendChild(this.nrateEl);
		
		this.actEl.setStyle(
		{
		'position':'absolute',
		'width':'0px',
		'height':(this.params.markImg.height / 3)+'px',
		'background':'url("' + this.params.image + '")',
		//		'background':'red',
		//		'border':'red 1px solid',
		'background-position-y': (this.params.markImg.height / 3) + 'px',
		'background-position':'0px ' + (this.params.markImg.height / 3) + 'px'
		});
		
		//	this.actEl.innerHTML = '&nbsp;';

		Event.observe(this.el, 'mousemove', this.mouseMove.bindAsEventListener(this));
		//	Event.observe(this.actEl, 'mousemove', this.mouseMove.bindAsEventListener(this));
		Event.observe(this.el, 'click', this.mouseClick.bindAsEventListener(this));
		Event.observe(this.el, 'mouseover', this.mouseover.bindAsEventListener(this));
		Event.observe(this.el, 'mouseout', this.mouseout.bindAsEventListener(this));
	},
	//Properties
	params: null,
	el: null,
	actEl: null,
	fixedMark: 0,
	//////////////////////
	setMark: function(mark) {
		this.fixedMark = mark;
		this.actEl.setStyle({'width': mark * this.params.markImg.width + 'px'});
		this.setNrate();
	},
	
	setNrate: function() {
		if (this.params.nrate !== '')
		{
			this.nrateEl.innerHTML='Голосов:&nbsp;'+this.params.nrate;
			this.nrateEl.setStyle({'margin-left': 10 + 5 * this.params.markImg.width + 'px'});
			this.nrateEl.setStyle({'color': '#C0C0C0'});
		}
	},
	
	getMark: function(){
		return parseInt(this.actEl.getStyle('width')) / this.params.markImg.width;
	},
	mouseMove: function(event){
		if(this.params.readOnly)
		return;
		var elxy = Position.page(this.el);
		var mark = Math.floor((Event.pointerX(event) - elxy[0])/this.params.markImg.width) + 1;
		if(mark <= this.params.max)
		this.actEl.setStyle({'width':mark * this.params.markImg.width + 'px'});
	},
	mouseClick: function(event){
		if(this.params.readOnly)
		return;
		this.params.readOnly = true;
		this.setMark(this.getMark());
		this.actEl.setStyle(
		{
		'background-position-y':2*this.params.markImg.height / 3 + 'px',
		'background-position': '0px ' + 2*this.params.markImg.height / 3 + 'px'
		});
		var v = this.getMark();
		this.el.setAttribute('value', v);
		var mark = v;
		eval(this.params.onrate);
		if (this.params.nrate !== '')
		{
			this.params.nrate++;
		}
		this.setNrate();
	},
	mouseout: function(event){
		this.setMark(this.fixedMark);
	},
	mouseover: function(event){
		if(this.params.readOnly)
		return;
		var elxy = Position.page(this.el);
		var mark = Math.floor((Event.pointerX(event) - elxy[0])/this.params.markImg.width) + 1;
		if(mark <= this.params.max)
		this.actEl.setStyle({'width':mark * this.params.markImg.width + 'px'});
	}
}
var t = 1.3;

var init = function(){
	$A($$('.rating')).each(function(el)
	{
		var par = {};
		var t;

		el = $(el);
		
		t = el.readAttribute('max');
		if(t)
		par.max = t;
		t = el.readAttribute('onrate');
		if(t)
		{
			par.onrate = t;
		}
		
		t = el.readAttribute('nrate');
		if(t)
		par.nrate = t;

		var r = new Rating(el, par);

		t = el.readAttribute('value');
		if(t)
		r.setMark(t);
	});
}

if(window.addEventListener) // Mozilla, Netscape, Firefox
window.addEventListener('load', init, false);
else  // IE
window.attachEvent('onload', init);

