var newVertesana = new Class ({
	options : {
		id : false,
		prefix : '',
		limit : 10
	},
	initialize : function(options) {
		this.setOptions(options);
		this.Target = $(this.options.id);
		this.Target.setStyle('display','none');
		this.Target.removeClass(this.Target.get('class'));
		this._build();
		this.Target.getElements('option').each(this._stars,this);
//		this._calculate();
		this.y = this.maks = 0;
		this.PlaceHolder.getElements('div').each(this._actions,this);
		this._finish();
	},
	_build : function() {
		this.PlaceHolder = new Element('div',{
			'id' : this.options.prefix + 'place-holder'
		}).inject(this.Target,'after');
		this.i = 0;
	},
	_stars : function(victim) {
		if (this.i < this.options.limit) {
			this.i++;
			this.u = this.i - 1;
		}
		else {
			this.u = this.i;
		}
		this.tmp = new Element('div',{
			'class' : 'star',
			'rel' : victim.value,
			'alt' : this.i
		}).inject(this.PlaceHolder);
		if (this.u < this.options.limit) {
			this.tmp.set('title',victim.text).addClass('stp');
		}
	},
	_actions : function(victim) {
		if (this.y < this.options.limit) {
			victim.addEvents({
				'click' : this._click.bind(this)
			});
		}
		victim.addEvents({
			'mouseenter' : this._focus.bind(this),
			'mouseleave' : this._blur.bind(this)
		});
		this.y++;
	},
	_click : function(victim) {
		var p = victim.target.get('rel');
		var cc = this.PlaceHolder;
		var rc = this.Target.name;
		new Request.JSON({
			url: host + 'json.php',
			onComplete: function(resp) {
				if(resp) {
					if (resp.done == 4) {
						var tit = 'Pašlaik Tu vēl nevari vērtēt, kamēr moderators pārbaudīs Tavu izveidoto profilu. Lūdzu nedaudz pacietības!';
						new newPazinojums({pazinojums : tit});
						return;
					}
					else if (resp.done == 5) {
						var tit = 'Iespēja vērtēt atslēgta uz nenoteiktu laiku rupju un sistemātisku Noteikumu pārkāpumu dēļ';
						new newPazinojums({pazinojums : tit});
						return;
					}
					else if (resp.done == 6) {
						var tit = 'Iespēja vērtēt atslēgta līdz ' + resp.lidz + ' Noteikumu pārkāpuma dēļ';
						new newPazinojums({pazinojums : tit});
						return;
					}
					$$('.star-tip').setStyle('visibility','hidden');
					if (resp.pazinojums == 0) {
						if (resp.kopejais == 1) {
							tps = rc.split('_');
							//$(rc + '_1').set('text','');
							//$(rc + '_1').set('text',resp.vertejums);
							cc.getPrevious('h3').destroy();
							new Element('h2',{'text' : 'Tavs vērtējums: ' + resp.lietotaja_vertejums
							}).inject(cc,'before');
							new Element('h4',{'text' : (tps[0] == 'auto') ? 'Vidējais auto kopumā: ' + resp.vertejums : 'Vidējais numuram kopumā: ' + resp.vertejums
							}).inject(cc,'before');
							new Element('h5',{'text' : 'Kopā saņemtas ' + resp.balsis + ' balsis'
							}).inject(cc,'before');
							cc.destroy();
						}
						else if (resp.kopejais == 2) {
							cc.getPrevious('h3').destroy();
							new Element('h4',{'text' : 'Tavs vērtējums: ' + resp.lietotaja_vertejums
							}).inject(cc,'before');
							new Element('h4',{'text' : 'Vidējais rakstam: ' + resp.vertejums
							}).inject(cc,'before');
							new Element('h5',{'text' : 'Kopā saņemtas ' + resp.balsis + ' balsis'
							}).inject(cc,'before');
							cc.destroy();
						}
						else {
							cc.getPrevious('h3').destroy();
							new Element('h4',{'text' : 'Tavs vērtējums: ' + resp.lietotaja_vertejums
							}).inject(cc,'before');
							new Element('h4',{'text' : 'Vidējais bildei: ' + resp.vertejums
							}).inject(cc,'before');
							new Element('h5',{'text' : 'Kopā saņemtas ' + resp.balsis + ' balsis'
							}).inject(cc,'before');
							cc.destroy();
						}
						if ($chk($(rc + '_2'))) {
							$(rc + '_2').set('text',resp.balsis);
						}
					}
					else {
						new newPazinojums({pazinojums:resp.pazinojums});
					}
				}
			}
		}).post({'object' : this.Target.name,'rating' : p});
	},
	_focus : function(victim) {
		var c = victim.target.get('alt');
		var s = victim.target.getSize()
		var t = this.PlaceHolder.getSize();
		var x = (c * s.x) - t.x;
		this.PlaceHolder.setStyle('background-position',x.toInt() + 'px 0px');
	},
	_blur : function(victim) {
		var x = this.PlaceHolder.getSize().x;
		this.PlaceHolder.setStyle('background-position',-x.toInt() + 'px 0px');
	},
	_finish : function() {
		new Element('div',{'class' : 'clear'}).inject(this.PlaceHolder);
	}
});
var callVertesana = new Class ({
	options : {
		prefix : ''
	},
	initialize : function(options) {
		this.setOptions(options);
		$(document.body).getElements('select[class=' + this.options.id + ']').each(function(element) {
			new newVertesana({id : element.id, prefix : this.options.prefix, limit : element.title});
		},this);
		new Tips('.stp',{  
			className: 'star-tip',
			showDelay: 100,
			offsets : {x : 0, y : 22}
		});  
	}
});

newVertesana.implement(new Options, new Events);
callVertesana.implement(new Options);
// laižam vaļā
window.addEvent('domready', function(){
	new callVertesana({id : 'convert-vertesana-big', prefix : 'big-'});
	new callVertesana({id : 'convert-vertesana-small', prefix : 'small-'});
	new callVertesana({id : 'convert-vertesana-tiny', prefix : 'tiny-'});
});