/**
*
*  Copyright 2005 www.AjaxLine.com - NaikonSoft 
*  Author Igor Kononuchenko
*
*  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
*  file except in compliance with the License. You may obtain a copy of the License at
*
*         http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software distributed under the
*  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
*  either express or implied. See the License for the specific language governing permissions
*  and limitations under the License.
**/


var Rating = Class.create({
	initialize: function(parent, options) {
		this.parent = parent;
		
		var defaultOptions = {
			prompt: "promptText",
			emptyImageUrl: basePath + 'images/star3.png',
			hoverImageUrl: basePath + 'images/star1.png',
			votedImageUrl: basePath + 'images/star2.png',
			afterYouVotedImageUrl: basePath + 'images/star2.png',
			starsCount: 10,
			isEnabled: true,
			afterVoteText: 'afterVoteText',
			value: -1,
			hints: "",
			staticRating: false
		};
		
		Object.extend(this, defaultOptions);
		Object.extend(this, options);
		
		var ratingsValue = this.parent.down('.ratingsValue') || null;
		
		if (ratingsValue && ratingsValue.value) {
			this.value = ratingsValue.value;
		}
		
		this.container = parent.down('.ratingsSlider');
		
		if (this.parent.down('.starCount'))
			this.starsCount = parent.down('.starCount').value;
			
		this.createStars();
	},
	
	createStars: function() { 
		this.stars = new Array();
		
		for (var i = 0; i < this.starsCount; i++) {
			this.stars.push(new Star(this, i, this.staticRating));
		} 
		
		this.setVoted(this.value);
	},

	notifyMouseOver: function(star) {
		this.currentValue = star.index;
		this.setHover(true, star.index);
	},
	
	notifyMouseOut: function(star) {

		this.currentValue = star.index;
		setTimeout(this.setUnHover.bind(this), 100);

		if (this.currentValue!=star.index)  
			this.setHover(true, this.currentValue);
	},
	
	setUnHover: function() {
		this.setHover(false, this.starsCount - 1);
		this.setVoted(  this.value );
	},
	
	notifyClick: function(star) {
		this.setAfterYouVoted(star.index);
		//this.isEnabled = false;
	},
	
	setHover: function(isSetHover,index) {
		for(var i = 0; i < index + 1; i++) {
			this.stars[i].setHover(isSetHover);
		} 
	},

	setVoted: function(index) {
		for(var i = 0; i < index; i++) {
			if (i - (index + 1) > -1)  
				this.stars[i].setVoted(this.votedHalfImageUrl); 
			else
				this.stars[i].setVoted(this.votedImageUrl);
		}
	},

	setAfterYouVoted: function(index) {
		for(var i = 0; i < index + 1; i++) {
			if (i - (index + 1) > -1)  
				this.stars[i].setVoted(this.afterYouVotedHalfImageUrl); 
			else
				this.stars[i].setVoted(this.afterYouVotedImageUrl);
		} 
		
		this.value = index + 1;
		
		this.parent.select('.ratingsValue')[0].value = this.value;
	}
});

var Star = Class.create({
	initialize: function(parent, index, staticRating) {
		this.container = parent.container;
		this.parent = parent;
		this.img = null;
		this.index = index; 
		this.staticRating = staticRating;
		this.createDOM();
	},
	
	createDOM: function() {
		this.img = document.createElement('img');
		this.img.src = this.parent.emptyImageUrl; 
		this.img.className = 'ratingsStar';
		this.img.alt = this.img.title = this.index + 1;
		this.parent.container.appendChild(this.img);
		
		if (this.parent.hints)
			this.img.title = this.parent.hints[this.index];
			
		if (!this.staticRating) {
			this.img.onclick = this.onclick.bindAsEventListener(this);
			this.img.onmouseover = this.onmouseover.bindAsEventListener(this);
			this.img.onmouseout = this.onmouseout.bindAsEventListener(this);
		}
	}, 
	
	setHover:function(isSetHover) {
		this.img.src =!isSetHover?this.parent.emptyImageUrl:this.parent.hoverImageUrl;
	},
	
	setVoted: function(url) {
		this.setImage(url);
		this.isVoted = true;
	},
	
	setAfterYouVoted: function() {
		this.img.src =this.parent.afterYouVotedImageUrl; 
		this.isVoted = true;
	},
	
	setImage: function(url) {
		this.img.src = url;
	},
	
	onmouseover:function(e) { 
		if (this.parent.isEnabled) {
			this.img.style.cursor ='pointer';
			this.parent.notifyMouseOver(this);
		}
	},
	
	onmouseout: function(e) { 
		if (this.parent.isEnabled) {
			this.img.style.cursor ='default'; 

			if (!this.isNextElemStar(e)) {
				this.parent.notifyMouseOut(this) 
			}
			else {  
				if (this.isVoted && this.parent.value>this.index) 
					this.setVoted(this.getVotedImageUrl()); 
				else
					this.img.src = this.parent.emptyImageUrl;   
			}
		}
	},
	
	getVotedImageUrl: function() {
		return (this.parent.value - (this.index) > -1 && this.parent.value - (this.index) < 0) ? this.parent.votedHalfImageUrl : this.parent.votedImageUrl;
	},
	
	isNextElemStar: function(e) {
		isIE = navigator.userAgent.toLowerCase().indexOf("ie") != -1;
		
		if(isIE)
			return e.toElement.tagName == 'IMG';
		else
			return e.relatedTarget != null && e.relatedTarget.tagName == 'IMG';
	},
	
	onclick: function(e) { 
		if (this.parent.isEnabled)  {
			this.img.style.cursor ='default'; 
			this.parent.notifyClick(this);
			this.parent.notifyMouseOut(this);
		}
	}
});