/* fwStarRating.js - Pagination widget
 *
 * @Author: Ryan McGrath (redux version, library agnostic), Some other guy did the original (no clue who ;P)
 * @Requires http://images.webs.com/JS/fw.js ;P
 */

if(!fwStarRating){
	fw.StarRatingImgsLoaded = false;
}

var fwStarRating = new CompatClass({
	type: 'starRating',
	initialize: function(containerID, options){
		this.containerID = containerID;
		this.options = {
			stars: 5,
			rating: 0,
			clear: false,
			isEditable: true,
			showLogin: true,
			containerStyle: '',
			onChange: '',
			onMouseOver: '',
			onMouseOut: '',
			draw: false
		}
		fw.CompatExtend(this.options, options || {});
		this.prepare();
		if(this.options.draw)
			this.draw();
	},
	prepare: function(){
		this.rating = this.options.rating;
		
		fw.Css.load('http://images.webs.com/Styles/starRating.css');

		if(!fw.StarRatingImgsLoaded){
			i1 = new Image(); i1.src="http://images.webs.com/Images/star-on.gif";
			i2 = new Image(); i2.src="http://images.webs.com/Images/star-over.gif";
			fw.StarRatingImgsLoaded = true;
		}
		
		this.HTML = '<div id="'+this.containerID+'" class="fw-sr-container" style="width:' + (17*this.options.stars) + 'px;' + this.options.containerStyle + '">';
		for(var i=1; i<=this.options.stars; i++)
			this.HTML += '<img id="'+(this.containerID+'-star-'+i)+'" src="http://images.freewebs.com/Images/star-'+(i<=this.rating ? 'on' : 'off')+'.gif" width=17 height=16>';
		this.HTML += '</div>'
	},
	draw: function(){
		document.write(this.HTML);
		this.activate();
	},
	activate: function(){
		this.rating = this.options.rating;
		this.container = document.getElementById(this.containerID);
		this.container.StarRating = this;
		if(this.rating)
			this.resetStars();
			
		if(this.options.isEditable){
			this.container.style.cursor = 'pointer';
			this.container.onmouseout = function(){
				if(!this.options.isEditable) return;
				if(this.options.onMouseOut) {
					this.options.onMouseOut();
				}
				this.resetStars();
			}.CompatBind(this);
		}
		
		if(this.options.isEditable)
		for(var i=1; i<=this.options.stars; i++){
			var star = document.getElementById(this.containerID+'-star-'+i);
			star.i = i;
			star.StarRating = this;
			star.onclick = function(){
				if(!this.options.isEditable) return;
				this.setRating();
			}.CompatBind(this);
			star.onmouseover = function(){
				if(!this.StarRating.options.isEditable) return;
				if(this.StarRating.options.onMouseOver)
					this.StarRating.options.onMouseOver(this.i); 
				this.StarRating.setStars(this.i, 'over');
			};
		}
	},
	deActivate: function(){
		this.options.isEditable = false;
		this.container.style.cursor = 'default';
		this.resetStars();
	},
	resetStars: function(){
		if(this.disableMouseOut) return;
		this.setStars(this.rating, 'on');
		if(!this.options.isEditable)
			this.container.title = 'Average Rating: '+this.rating;
	},
	setRating: function(){
		this.disableMouseOut = true;
		if(!confirm('Give this a '+this.hoverRating+'-star rating?')){ this.disableMouseOut = false; this.resetStars(); return; }
		this.rating = this.hoverRating;
		for(var j=0; j<6; j+=2){
			window.setTimeout(function(){this.setStars(this.rating, 'over')}.CompatBind(this), j*60);
			window.setTimeout(function(){this.setStars(this.rating, 'on')}.CompatBind(this), (j+1)*60);
		}
		if(this.options.onChange) window.setTimeout(this.options.onChange, 60*4);
	},
	setStars: function(value, type){
		if(this.disableMouseOut) return;	
		var halfstar = 0;
		var rem = value % Math.floor(value);
		if(rem > .3 && rem < .7)
			halfstar = value = Math.ceil(value);
		else
			value = Math.round(value);
		for(var i=1; i<=this.options.stars; i++){
			this.hoverRating = value;
			document.getElementById(this.containerID+'-star-'+i).src = 'http://images.freewebs.com/Images/star-'+(i == halfstar ? 'half' : (i <= value ? type : 'off'))+'.gif';
		}
	}
});

