//camelize


var StarRatingWidget = Class.create();
StarRatingWidget.prototype = {
	initialize: function() {
		this.ResetScope();
		
		this._ScopeIsFlexible = true;
		this._iMaxImages = 10;
		this._sPath = "/img/icons/stars/";
		this._sImageName = "StarRating_";
		this._sClassnamePrefix = "StarRating_";
		this._sImageExt = ".gif";
	},
	
	ResetScope: function() {
		this.arScope = [];
		this._iMax = 0;
		this._iMin = 0;
	},
		
	AddToScope: function(iValue) {
		iValue = iValue * 1;
		this.arScope.push(iValue);
		this.arScope.sort(this._ArraySort);
		this._iMax = this.arScope[this.arScope.length-1];
		this._iMin = this.arScope[0];
	},
	
	_ArraySort:function(a, b) {
		return a-b;
	},
	_Undot: function(s) {
		return new String(s).replace(".", "");
	},
	
	SetImageSrcByClass: function() {
		var options = Object.extend({
      	iMin: 0
    	}, arguments[0] || {});


		var iMin = options.iMin;
		for (var a=0; a <= (this._iMaxImages/2); a+=0.5) {
			var sNum = this._Undot(a);
			var sClassname = "." + this._sClassnamePrefix + sNum;
			try {
				var sSrc;
				if (a >= iMin)
					sSrc = this._sPath + this._sImageName + sNum + this._sImageExt;
				else
					sSrc="/img/1x1.gif";
					
				//if (getCookie("LP-UniqueID")==200371516175971)  {
				//	$("debug").innerHTML = $("debug").innerHTML + sSrc + "<BR>";
				//	Element.show("debug");
				//}
				
				$$(sClassname).each(function(el){el.src=sSrc});
			} catch(e) {}
		}
	},
	
	_SetImageSrcByClass:function(sClassname, sSrc) {
	},
	
	StarRating_IsHighestValue:function(iValue) {
		return iValue == this._iMax;
	},
	StarRating_IsLowestValue:function(iValue) {
		return iValue == this._iMin;
	},
	
	StarRating_AsPercent: function(iRating) {
		return this.GetRatingAsPercentOfScope(iRating);
	},

	StarRating_AsClassname: function(iRating) {
		var iNumber = this.StarRating_AsNumber(iRating);
		return this._sClassnamePrefix + this._Undot(iNumber);
	},
	
	StarRating_AsNumber: function(iRating) {
		iRating = iRating*1;
		var fRating = this.GetRatingAsPercentOfScope(iRating);
		var fStar = parseInt(fRating * 10)/2;
		return fStar;
	},

	StarRating_AsSrc: function(iRating) {
		// There are 10 images rating from no stars to 5 stars with half points in the middle.
		var fStar = this.StarRating_AsNumber(iRating);
		return this._sPath + this._sImageName + this.Undor(fStar) + this._sImageExt;
	},
	
	StarRating_AsImg: function(iRating) {
		var sImageSrc = this.StarRating_AsSrc(iRating);
		return "<img src='"+sImageName+"'>";
	},
		
	GetRatingAsPercentOfScope: function(iRating) {			
		if (iRating > this._iMax) {
			if (this._ScopeIsFlexible) {
				this.AddToScope(iRating);
			} else 
				iRating = this._iMax;
		}
		if (iRating < this._iMin) {
			if (this._ScopeIsFlexible) {
				this.AddToScope(iRating);
			} else
				iRating = this._iMin;
		}
		
		if (this._iMax == this._iMin && this._iMax == iRating)
			return 1;
		
		var fUnits = 1/(this._iMax - this._iMin);
		
		// Express iRating as a percent of the scope 
		var fPercent = /* 1 - */ ((this._iMax - iRating)*fUnits);
		
		// Correct rounding problems.
		if (fPercent > 0.99) 
			return 1;
		else
			return fPercent;
	}
}
