var maxRating;
var isRated;
var preSet;

// Indicate the current rating selection
function rate(num){	
	maxRating = 10;
	for(i=0; i<num.parentNode.childNodes.length; i++){
		if(num.parentNode.childNodes[i].nodeName == "A"){
			maxRating++;	
		}
	}
	
	// Set classes for each rating
	if(!isRated){
		s = num.id.replace("_", "");
		refreshRate(s, true);
	}
}

// Update rating if not rated on mouseout
function rateOut(me){
	if(!isRated){
		if(!preSet){
			for(i=1; i<maxRating; i++){
				$("_"+i).className = $("_"+i).className.replace("on","");
			}
		}else{
			rate(preSet);
		}
	}
}

// Rate the item or allow re-selection of rating
function rateItem(me){
	if(!isRated){
		preSet = me;
		isRated = true;
		s = me.id.replace("_", "");
		$("rating").value = s;
		
		// Blink to confirm selection
		rateItemInterval = 60; // Blink interval
		rateItemDelay = 0;
		for(i=1; i<=6; i++){
			setTimeout('refreshRate('+s+','+(i % 2 == 0)+')', rateItemDelay)
			rateItemDelay = rateItemDelay + rateItemInterval;
		}
	}
}

// Refresh the rating based on selection
function refreshRate(num, on) {
	for(i=0; i<maxRating; i++){
		$("_"+i).style.border = "none"; // Force underline hide
		if (i>0) {
			$("_"+i).className = $("_"+i).className.replace("on","");
			if(i<=num && on){
				$("_"+i).className = $("_"+i).className+"on";
			}
		}
	}
}

// Allow re-selection of rating
function resetRate() {
	isRated = false;
}
