// JavaScript Document for Star Ratings

/*
	srHasClassName tweaked from the prototype lib
	as this library shouldn't require prototype but they're useful functions.
*/
function srHasClassName(element, className) {
	if ((typeof element=="undefined") || (element==null)) return false;
	var elementClassName = element.className;
	return (elementClassName.length > 0 && (elementClassName == className ||
	new RegExp("(^|\\s)" + className + "(\\s|$)").test(elementClassName)));
}

function getElementsByTagNameSafe(tagName) {
	result=document.getElementsByTagName(tagName);
	if ( (typeof result=="undefined") || (result==null) ) {
		result=new Array();
	}
	return result;
}

function getStarRatingValue(aRef, evt) {
	evt=(evt)?evt:((event)?event:null); // Get the event depending on the browser type.
	
	if (document.all) { // For I.E.
		offsetX=evt.offsetX;
		offsetY=evt.offsetY;
	} else { // For everything else
		objX=0;
		objY=0;
		obj=aRef;
		while (obj!=null) {
			objX+=obj.offsetLeft;
			objY+=obj.offsetTop;
			obj=obj.offsetParent;
		}
		
		offsetX=evt.pageX-objX;
		offsetY=evt.pageY-objY;
	}
	
	width=aRef.clientWidth;
	stepSize=width/aRef.inputRef.maxValue;
	
	newValue=Math.ceil(offsetX/stepSize);
	
	return newValue;
}

// Offset the background
function setStarRatingBGPos(aRef, value, bHover) {
	// Make 1 and 0 both be the top
	amount=value-1;
	if (amount<0) amount=0;
	
	// Shift everything left if hovered
	offsetX=0;
	if (bHover) offsetX=aRef.offsetWidth;
	
	// Calculate the offset
	offsetY=aRef.offsetHeight*amount;

	
	// Set it
	aRef.style.backgroundPosition="-"+offsetX+"px -"+offsetY+"px";
}

// Sets the class names and inner HTML for the link to show the current values.
function setStarRatingValues(aRef) {
	if (!isNaN(parseInt(aRef.inputRef.value, 10))) { // Only process if a number is entered.
		current_rating=parseInt(aRef.inputRef.value, 10);
		max_value=aRef.inputRef.maxValue;
		
		// Stop it from going out of range
		if (current_rating>max_value) {
			current_rating=max_value;
			aRef.inputRef.value=current_rating;
		}
		if (current_rating<1) {
			current_rating=1;
			aRef.inputRef.value=current_rating;
		}
		
		setStarRatingBGPos(aRef, current_rating, false);
		
		// Set the text
		aRef.innerHTML=current_rating+"/"+max_value;
	}
}

function initStarRating(inputRef) {
	// Get the maxValue - default to 5, set to whatever follows the star_max_ class if it exists.
	inputRef.maxValue=5;
	maxPos=inputRef.className.toString().indexOf("star_max_");
	if (maxPos>-1) {
		maxString=inputRef.className.toString().substring(maxPos+9);
		inputRef.maxValue=parseInt(maxString, 10);
	}
	
	// Create the link that will display the options
	newLink=document.createElement("a"); // Create a link element
	newLink.href="#";
	newLink.className="star_rating";
	newLink=inputRef.parentNode.insertBefore(newLink, inputRef.nextSibling); // Insert after the input element
	newLink.onclick=function(evt) {
		this.inputRef.value=getStarRatingValue(this, evt);
		setStarRatingValues(this);
		return false;
	}
	
	if (!srHasClassName(inputRef, "star_rating_no_hover")) {
		newLink.onmousemove=function(evt) {
			this.inputRef.value=getStarRatingValue(this, evt);
			setStarRatingValues(this);
			//setStarRatingBGPos(this, getStarRatingValue(this, evt), true);
		}
	
		newLink.onmouseout=function(evt) {
			setStarRatingValues(this);
		}
	}
	
	newLink.inputRef=inputRef;
	setStarRatingValues(newLink);
	
	// Make the link change whenever the input changes
	inputRef.onchange=function() {
		setStarRatingValues(this.linkRef);
	}
	
	// Create a backwards link
	inputRef.linkRef=newLink;
	inputRef.className+=" star_rating_initialized";
}

function initStarRatings() {
	allInputs=getElementsByTagNameSafe("input");
	for (i=0; i<allInputs.length; i++) {
		if ( (typeof allInputs[i].className != "undefined") && (allInputs[i].className!=null) ) {
			if (srHasClassName(allInputs[i], "star_rating")) {
				initStarRating(allInputs[i]);
			}
		}
	}
}
addLoadEvent(initStarRatings);
