var gbVote = new Class({
	Implements: Options,
	
    options: {
		handler				: 'gbvote',
		maxPoint			: 10,
		prefix				: 'bid',
		voteimg_l			: 'star_l.png',
		voteimg_r			: 'star_r.png'
	},
	
	initialize: function(options){
		this.setOptions(options);
		this.mkImg();
	},
	
	mkImg: function(){
		this.bound = {};
		$$('.'+this.options.handler).each(function (el,n){
			el.innerHTML = '';
			//$('uservote').setStyle('display', 'none');
			for(i=1; i <= this.options.maxPoint; i++ ) {
				if (i <= el.title){
					useimg = 'img/_'+this.options.voteimg_l;
					if(i%2 ==0) {
						useimg = 'img/_'+this.options.voteimg_r;
					}
				}else {
					useimg = 'img/'+this.options.voteimg_l;
					if(i%2 ==0) {
						useimg = 'img/'+this.options.voteimg_r;
					}
				}
				voteimg = new Element('img', {
					'id': el.id+'_'+i,
					'class': 'voteimg'+i,
					'src': useimg,
					'alt': i,
					'title': i,
					'styles': {
				    }
				}).injectInside(el);
				
				if (el.id.indexOf('getvote') < 0 ){
					// onMouseoverEvent
					this.bound.onMouseoverEvent = this.onMouseoverEvent.bindWithEvent(this, voteimg);
					voteimg.addEvent('mouseover', this.bound.onMouseoverEvent);
					// onMouseoutEvent
					this.bound.onMouseoutEvent = this.onMouseoutEvent.bindWithEvent(this, voteimg);
					voteimg.addEvent('mouseout', this.bound.onMouseoutEvent);
					// onClickEvent
					this.bound.onClickEvent = this.onClickEvent.bindWithEvent(this, voteimg);
					voteimg.addEvent('click', this.bound.onClickEvent);
				}
			}
			
			if (el.id.indexOf('getvote') < 0 ){
				// punkte anzeigen
				votetxt = new Element('span', {
					'id': el.id+'_txt_'+i,
					'class': 'votetxt',
					'html': '0 (1=low 10=high)'
				}).injectInside(el);
			}
		}.bind(this));
	},
	
	onMouseoverEvent: function (event, el){
		this.changeVote (el);
	},
	onMouseoutEvent: function (event, el){
		this.resetVote(el);
	},
	onClickEvent: function (event, el){
		this.updateVote(el);
	},
	
	distroyEvents: function (cid){
		for(i=1; i <= this.options.maxPoint; i++ ) {
			$(cid.id+'_'+i).removeEvents('mouseover');
			$(cid.id+'_'+i).removeEvents('mouseout');
			$(cid.id+'_'+i).removeEvents('click');
		}
	},
	
	changeVote: function (el){
		cid = el.getParent();
		rating = el.title;
		
		for(i=1; i <= this.options.maxPoint; i++ ) {
			
			if (i <= rating){
				useimg = 'img/__'+this.options.voteimg_l;
				if(i%2 ==0) {
					useimg = 'img/__'+this.options.voteimg_r;
				}
			}else if (i <= cid.title){
				useimg = 'img/_'+this.options.voteimg_l;
				if(i%2 ==0) {
					useimg = 'img/_'+this.options.voteimg_r;
				}
			}else {
				useimg = 'img/'+this.options.voteimg_l;
				if(i%2 ==0) {
					useimg = 'img/'+this.options.voteimg_r;
				}
			}
			$(cid.id+'_'+i).src = useimg;
		}
		$(cid.id+'_txt_'+i).innerHTML = rating+' (1=low 10=high)';
	},
	
	resetVote: function (el){
	
		cid = el.getParent();
		setid = cid.id.substr(this.options.prefix.length);
		rating = cid.title;
		if (rating == '' || rating < 1 || rating > 10){
			rating = 0;
		}
		for(i=1; i <= this.options.maxPoint; i++ ) {
			
			if (i <= cid.title){
				useimg = 'img/_'+this.options.voteimg_l;
				if(i%2 ==0) {
					useimg = 'img/_'+this.options.voteimg_r;
				}
			}else {
				useimg = 'img/'+this.options.voteimg_l;
				if(i%2 ==0) {
					useimg = 'img/'+this.options.voteimg_r;
				}
			}
			$(cid.id+'_'+i).src = useimg;
		}
		$(cid.id+'_txt_'+i).innerHTML = rating+' (1=low 10=high)';
	},
	
	updateVote: function (el){
		cid = el.getParent();
		setid = cid.id.substr(this.options.prefix.length);
		rating = el.title;
		$('uservote').value = rating;
		cid.title = Math.ceil(rating);
		this.resetVote(el);
	}
	
});
gbVote.implement(new Options);

window.addEvent('domready', function(){
	if (typeof gbVoteOpt == 'undefined'){
		gbVoteOpt = {}; 
	}
	
	gbVote = new gbVote(gbVoteOpt);
});
