/* 
	Created by Yannick Van Avermaet
	v1.1 09/12/2009 12:05:53
	All rights reserved © 2009
*/

var cIsIE = document.all ? true : false;
//if(!cIsIE)
//	document.captureEvents(Event.MOUSEMOVE);

var cStars = Class.create({
	initialize: function(element, number, stars, options){
		this.elementId = element;
		this.element = $(element);
		this.number = number || 10;
		this.stars = stars || {};
		this.options = options || {};
		this.remember = 0;
		
		if(typeof(this.stars.full) == 'undefined' || typeof(this.stars.empty) === 'undefined'){
			alert("You should pass 'full' and 'empty' in order for this to work!");
			return;	
		}
		
		if(this.options.allowHalf && typeof(this.stars.half) === 'undefined'){
			alert("You should pass 'half' or put 'allowHalf' to false for this to work!");	
			return;
		}
		
		this._create();
		
		if(this.options.prefill > 0 && this.options.prefill <= number){
			this.remember = this.options.prefill;
			this.draw('auto');
		}
	},
	
	_create: function(){
		for(var i = 0; i < this.number; i++){
			this.img = document.createElement('img');
			Element.extend(this.img);
			this.img.src = this.stars.empty || '';
			this.img.id = this.elementId+'_star'+(i+1);
			
			this.handler = this.draw.bind(this);

			if(!this.options.disabled){
				this.img.observe('mouseover',this.handler,false);
				this.img.observe('mousemove',this.handler,false);
				this.img.observe('mouseout',this.handler,false);
				this.img.observe('click',this.handler,false);
			}
			
			this.element.appendChild(this.img);
		}
	},
	
	draw: function(e){
		if(e.type != 'mouseout' && e != 'auto'){
			var elm = e.element();
			var previousImages = elm.previousSiblings();
			var nextImages = elm.nextSiblings();
			// Get X/Y of the mouse
			this._mousePos(e);
			
			// Get where the image is located
			var offsets = elm.viewportOffset();//Element.positionedOffset(elm); // positionedOffset doesn't work correctly in IE
			var left = offsets[0];
			var top = offsets[1];
			
			// Get width of the image
			var elmWidth = elm.getWidth();
			
			var half = false;
			if(this.x > left+(elmWidth/2) || !this.options.allowHalf){
				elm.src = this.stars.full;	
				if(e.type == 'click')
					this.remember = 1;
			}
			else{
				elm.src = this.stars.half;	
				if(e.type == 'click')
					this.remember = 0.5;
			}
			
			
			
			var aantal = previousImages.length;
			if(e.type == 'click')
				this.remember += aantal;
			
			for(var i = 0; i < aantal; i++)
				previousImages[i].src = this.stars.full;
			
			aantal = nextImages.length;
			for(var i = 0; i < aantal; i++)
				nextImages[i].src = this.stars.empty;
				
			
			if(e.type == 'click' && typeof(this.options.mouseClick) !== 'undefined' && this.options.mouseClick != ''){
				elm.value = this.remember;
				if(this.options.extra)
					elm.extra = this.options.extra;
				eval(this.options.mouseClick+'(elm)');
			} else if(e.type != 'click' && typeof(this.options.mouseHover) !== 'undefined' && this.options.mouseHover != ''){
				eval(this.options.mouseHover+'()');	
			}
		} else{
			var half = false;
			if(this.remember % 1 == 0.5 && this.options.allowHalf){
				half = true;
				this.remember -= 0.5;
			}
			
			var lastElm;
			var childImages = this.element.descendants();
			var aantal = childImages.length;
			for(var i = 0; i < aantal; i++){
				if((i+1) <= this.remember){
					childImages[i].src = this.stars.full;
					lastElm = childImages[i];
				}
				else{
					childImages[i].src = this.stars.empty;
				}
			}
			
			var tmp;
			if(typeof(lastElm) !== 'undefined')
				tmp = lastElm.next(0);
			else
				tmp = childImages[0];
			if(half && this.options.allowHalf){
				tmp.src = this.stars.half;
				this.remember += 0.5;
			}	
			if(e == 'auto'){
				if(typeof(this.options.mouseClick) !== 'undefined' && this.options.mouseClick != ''){
					//alert(tmp);
					tmp.value = this.options.prefill;
					if(this.options.extra)
						tmp.extra = this.options.extra;					
					eval(this.options.mouseClick+'(tmp)');
				}
			}
			else{
				if(typeof(this.options.mouseExit) !== 'undefined' && this.options.mouseExit != ''){
					eval(this.options.mouseExit+'()');	
				}
			}
		}
	},
	
	_mousePos: function(e){
		var tempX, tempY;
		if(cIsIE){
			tempX = event.clientX + document.body.scrollLeft;
			tempY = event.clientY + document.body.scrollTop;
		}
		else{
			tempX = e.pageX;
			tempY = e.pageY;
		}  
		if(tempX < 0)
			tempX = 0;
		if (tempY < 0)
			tempY = 0;  
			
		this.x = tempX;
		this.y = tempY;
	}
});