var sMax=5;	// Isthe maximum number of stars
var holder; // Is the holding pattern for clicked state
var preSet; // Is the PreSet value onces a selection has been made
var rated;
var hideRatingOnVote=true;

//Highlight on mouseover and vote
function ratingHighlight(num) {

	sMax = 0 ;
	for (n=0; n<num.parentNode.childNodes.length; n++) {
		if (num.parentNode.childNodes[n].nodeName == "A") {
			sMax++ ;	
		}
	}
	
	if (!rated) {
		s = num.id.replace("star_", '') ; // Get the selected star as an int e.g. star_1 converst to 1
		a = 0 ;
		
		for (i=1; i<=sMax; i++) {		
			if (i<=s) {
				$("star_"+i).addClassName("on") ;
				holder = a+1 ;
				a++ ;
			} else {
				$("star_"+i).removeClassName("on") ;
			}
		}
	}
	
}

//Unselect all stars when you mouseoff
function ratingOff() {

	if (!rated) {
		if (!preSet) {	
			for (i=1; i<=sMax; i++) {		
				$("star_"+i).removeClassName("on") ;
			}
		} else {
			ratingHighlight(preSet) ;
		}
	}
	
}

//When you submit a rating
function rateIt(selected, qid){

	if (!rated) {
		$("rateStatus").innerHTML = "Thank you for your vote!" ;
		preSet = selected ;
		rated = true ;
		sendRate(selected, qid) ;
		if (hideRatingOnVote) {
			ratingHide();
		}
	}
	
}

//update the questionnaire with the rating
function sendRate(selected, qid) {
	var url = "/video-voting/rater.php?question1="+selected.title+"&QuestionnaireID="+qid ;
	
	new Ajax.Request(url, {	
							method: 'get', 
							onSuccess: function(transport) {
								ratingHighlight(selected) ;	
							    }
						  } ) ;
	
}


//Highlight on mouseover and vote
function ratingHide() {
	
	for (i=1; i<=sMax; i++) {		
		$("star_"+i).addClassName("hide") ;
	}
	
}





