var newRating=null;

/*esmiling@condenet.com -TD#49963 10/05/06*/
   try {document.execCommand("BackgroundImageCache", false, true);} 
   catch(err) {}
	
//this function sends the request
function rateItem(itemId, rating, url) {
    var cObj = YAHOO.util.Connect.asyncRequest(
	'GET', url, {success: handleRatingResponse, failure: handleFailure, scope: null}, null);
}
//is called if the ajax call is successful
function handleRatingResponse(http) {
    var results = http.responseText.split("\n");
    updateItemRating(results);
}
//is called if the ajax call is not successful
function handleFailure(errorObj){
}

//is called on load of the page and also handles interface updates after responses to ajax calls.
//its purpose is to update AVERAGE RATING stars images to match rating
function resetStarDisplay(itemId, newRating) {
    //float representing the rating: newRating when coming from ajax, innerHTML on page load
	var averageRating = newRating || document.getElementById('rating.rating.' + itemId).innerHTML;
	if(averageRating > 0){
		document.getElementById('average_rating_empty.'+itemId).style.display = 'none';
	}
	
	//array of hidden star images
	var averageRatingStars = document.getElementById('average_rating.'+itemId).getElementsByTagName('IMG');
 	
	//store the fraction
	var fractionalRating = averageRating%1;
	
	//store the floor
	var digitRating = Math.floor(averageRating);
	
	//reset src and clear display of all stars
	for(var i=0; i<averageRatingStars.length; i++){
		averageRatingStars[i].style.display = 'none';
		averageRatingStars[i].src = '/images/photo/fullstar.gif';
	}
	
	//print a star for every integer
	for(var i=0; i<digitRating; i++)
		averageRatingStars[i].style.display = 'inline';
	
	//to handle page load interface updates
	if( !newRating && fractionalRating!=0 && fractionalRating >= 0.5 ){
		averageRatingStars[i].src = averageRatingStars[i].src.replace('fullstar','halfstar');
		averageRatingStars[i].style.display = 'inline';
	}
	
	//to handle responses from the ajax calls we must update the half rating star based on its previous presence
	if( newRating && fractionalRating!=0 && fractionalRating >= 0.5 ){
		//if this new rating warrants a half of star we need to set it
		averageRatingStars[i].src = averageRatingStars[i].src.replace(/images\/photo\/.*?star/,'images\/photo\/halfstar');
		averageRatingStars[i].style.display = 'inline';
	}
	if( newRating && fractionalRating!=0 && fractionalRating < 0.5 ){
		//if this new rating doesn't warrant a half of star, we need to remove it if it previously existed
		if(averageRatingStars[i].src.match('halfstar')) averageRatingStars[i].src = averageRatingStars[i].src.replace('halfstar','emptystar');
	}
}

//is called on load of the page and handles interface update after a response to an ajax call.
//its purpose is to update USER RATING star images to match rating
function resetUserStarDisplay(itemId, userRating){
	var userRating = userRating || document.getElementById('rating.userRating.' + itemId).innerHTML;

	if( userRating.length > 1 ) return;
	
	var userRatingStars = document.getElementById('my_rate').getElementsByTagName('A');
	
	//reset all the stars
	for(var i=0; i<userRatingStars.length; i++)
		userRatingStars[i].className = '';
	
	//set all the active stars	
	for(var i=0; i<userRating; i++)
		userRatingStars[i].className = 'active';
}

//this is the handler of all interface updates resulting from a rating action
//it updates the average stars and user stars
function updateItemRating(results) {
    var itemId = results[0];
    var rating = results[1];
    var votes = results[2];
    var userRating = results[3];
	
    //update average rating stars
    resetStarDisplay(itemId, rating);
	
    //update user rating stars
    resetUserStarDisplay(itemId, userRating); 
}

function userRatingStarBehavior(){
	//grab the user rating stars
	var userRatingStars = document.getElementById('my_rate').getElementsByTagName('A');
	
	//add the behavior
	for(var i=0; i<userRatingStars.length; i++){
		userRatingStars[i].onmouseover = hoverTrailingStars;
		userRatingStars[i].onmouseout = unhoverAllStars;
	}
}
function hoverTrailingStars(userRatingStars){
	var userRatingStars = document.getElementById('my_rate').getElementsByTagName('A');
	for(var i=0; i<userRatingStars.length; i++){
		if(userRatingStars[i] != this){
			userRatingStars[i].className += ' hover';
		}else{
			break;
		}
	} 
}
function unhoverAllStars(){
	var userRatingStars = document.getElementById('my_rate').getElementsByTagName('A');
	for(var i=0; i<userRatingStars.length; i++)
		userRatingStars[i].className = userRatingStars[i].className.replace('hover','');
}
