/*
* Copyright (c) 2007, Gabor Toth - me@tothgabor.hu
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the <organization> nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

function Rating(hiddenId,starCount,picPath) {

	var div;
	var hidden;
	var fullStar;
	var emptyStar;
	var halfStar;
	var coloredStar;
	var starCount;

	var selected;

	this.starCount = starCount;
	this.div = document.getElementById(hiddenId + "divid");
	this.hidden = document.getElementById(hiddenId);

	this.fullStar	=	picPath + "fullstar.gif";
	this.emptyStar	=	picPath + "emptystar.gif";
	this.halfStar	=	picPath + "halfstar.gif";
	this.coloredStar =	picPath + "coloredstar.gif";

	if(this.div && this.hidden) {

		this.selected = parseFloat(this.hidden.value);
		var imageSrc;
		for(var i = 0; i<this.starCount; ++i) {
			if( (i < this.selected && i+1 <= this.selected) || (i+1 == this.selected )) {
				imageSrc = this.fullStar;
			}
			else if(i < this.selected && i+1 > this.selected) {
				imageSrc = this.halfStar;
			}
			else {
				imageSrc = this.emptyStar;
			}
			this.div.innerHTML += '<img onClick="object_' + hiddenId + '.Click(' + (i+1) +');" onMouseOver="object_' + hiddenId + '.MouseOver(event);" onMouseOut="object_' + hiddenId + '.MouseOut();" style="border: 0px;" id="' + hiddenId +  '_star' + i + '" src="' + imageSrc +  '" alt="' + (i+1) + '"></a>';
		}


	}
	else {
		alert("Rating: Div element not found : " + hiddenId);
	}


	this.Click = Click;
	this.MouseOver = MouseOver;
	this.MouseOut = MouseOut;
	this.getEventSource = getEventSource;

	function getEventSource(event) {
		if(event) {
			if(event.srcElement) {
				return event.srcElement;
			}
			else if(event.target) {
				return event.target;
			}
			else {
				alert("Rating: There is no Target of Event");
			}
		}
		else {
			alert("Rating: There is no Event Object");
		}
	}

	function Click(clicked) {
		this.selected = clicked;
		this.hidden.value = clicked;
		for(var i=0; i < this.starCount; ++i) {
			var element = document.getElementById(this.hidden.name + '_star'+ i);
			if(element) {
				if(i < this.selected) {
					element.src = this.fullStar;
				}
				else {
					element.src = this.emptyStar;
				}
			}
			else {
				alert("Stars Not Found: " + this.hidden.name + '_star'+ i)
			}
		}
	}

	function MouseOver(event) {
		var overPicture = this.getEventSource(event);
		var color = 1;
		for(i=0; i < this.starCount; ++i) {
			var element = document.getElementById(this.hidden.name + '_star'+ i);
			if(element) {
				if(  color == 1 ) {
					element.src = this.coloredStar;
				}
				else {
					element.src = this.emptyStar;
				}

				if(element == overPicture) {
					color = 0;
				}

			}
			else {
				alert("Stars Not Found: " + this.hidden.name + '_star'+ i)
			}
		}
	}

	function MouseOut() {
		for(i=0; i < this.starCount; ++i) {
			var element = document.getElementById(this.hidden.name + '_star'+ i);
			if(element) {
				if( (i < this.selected && i+1 <= this.selected) || (i+1 == this.selected )) {
					element.src = this.fullStar;
				}
				else if(i < this.selected && i+1 > this.selected) {
					element.src = this.halfStar;
				}
				else {
					element.src = this.emptyStar;
				}
			}
			else {
				alert("Stars Not Found: " + this.hidden.name + '_star'+ i)
			}
		}
	}

}
