// Global variables
// t is a global time set to clear the rating's message window
var timerRatings;
var timerClearRatings;

// From http://jehiah.cz/archive/javascript-isdefined-function
function isDefined(variable)
{
return (!(!( variable||false )));
}

function alreadyRatedMessage(id) {
	window.clearTimeout(timerRatings);
	var rt = document.getElementById(id);
	rt.innerHTML = '<span style="font-size: 100%;color:rgb(255,0,0);">You have already rated.</span>';
	timerRatings = window.setTimeout('clearMessage("'+id+'")', 2000);  
}

function ratingSuccess(parentElemID, origRating, id) {
	window.clearTimeout(timerClearRatings);
	var rt = document.getElementById(id);
	rt.innerHTML = '<span style="font-size: 100%;color:rgb(0,51,153)">Thanks for rating!</span>';
	timerClearRatings = window.setTimeout('clearStars("'+parentElemID+'", "'+origRating+'", "'+id+'")', 10000);
}

function loginMessage(id) {
	window.clearTimeout(timerRatings);
	var rt = document.getElementById(id);
	rt.innerHTML = '<span class="regLink"><a href="login">Log in</a></span> to rate';
	timerRatings = window.setTimeout('clearMessage("'+id+'")', 7000); 
}

function clearMessage(id) {
	var rt = document.getElementById(id);
	rt.innerHTML = origMessage;
}

function showStars(thisElem, numStars, tbid) {

// Show the number of stars based on which star the mouse is 
// currently over [== numstars]

// Get the parent node of this image element
   var rb = thisElem.parentNode;
   var nodes = rb.childNodes;
   var totalStars = 0;
   for(var i = 0; i < nodes.length; i++) {
// Assumes that the child nodes are cycled through from the first star image to the last
	if (nodes[i].className == 'stars') {  
		if (totalStars < numStars) {
			nodes[i].src = "images/full_star_big.gif";
			//nodes[i].setAttribute(src, 'images/full_star_big.gif');
			nodes[i].setAttribute('alt', 'Full star');
			totalStars++;
		} else {
			nodes[i].src = "images/empty_star_big.gif";
			//nodes[i].setAttribute(src, 'images/empty_star_big.gif');
			nodes[i].setAttribute('alt', 'Empty star');
			totalStars++;
		}
	}  
   }

	var rt = document.getElementById(tbid);
	   switch (numStars)
	   {
		case 5:
			rt.innerHTML = '<span class="smallprint">Excellent</span>';
	  		break;
		case 4:
  			rt.innerHTML = '<span class="smallprint">Pretty good</span>';
	  		break;
		case 3:
  			rt.innerHTML = '<span class="smallprint">Worth seeing</span>';
	  		break;
		case 2:
  			rt.innerHTML = '<span class="smallprint">Acceptable</span>';
	  		break;
		case 1:
  			rt.innerHTML = '<span class="smallprint">Poor</span>';
	  		break;
		default:
  			rt.innerHTML = '<span class="smallprint">Choose a rating</span>';
	}

}	


// Show the original rating
function clearStars(thisElem, rating, tbid) {
// check if thisElem refers to a String (i.e. the parent id sent by ratingSuccess)
// or a DOM img object (i.e. from the web page)
   var rb;
   if (typeof(thisElem) == 'string') {
	rb = document.getElementById(thisElem);
	//if (!isDefined(thisElem)) { return; }
	//if (thisElem == null) { return; }
   } else {
// Get the parent node of this image element
   	rb = thisElem.parentNode;
   }

// convert rating into the correct star pattern
   var numFullStars = Math.floor(rating);
   var numHalfStars = rating - numFullStars;
   if (numHalfStars >= 0.5) 
	numHalfStars = 1;
   else	
	numHalfStars = 0;


   var nodes = rb.childNodes;
   var totalStars = 0;
   for(var i = 0; i < nodes.length; i++) {
	if (nodes[i].className == 'stars') {  
		if (totalStars <= (numFullStars-1)) {
			nodes[i].src = "images/full_star_big.gif";
			//nodes[i].setAttribute(src, 'images/full_star_big.gif');
			nodes[i].setAttribute('alt', 'Full star');
			totalStars++;
		} else {
			if (totalStars == (numFullStars + numHalfStars - 1)) {
				nodes[i].src = "images/half_star_big.gif";
				//nodes[i].setAttribute(src, 'images/half_star_big.gif');
				nodes[i].setAttribute('alt', 'Half star');
				totalStars++;			
			} else {
				nodes[i].src = "images/empty_star_big.gif";
				//nodes[i].setAttribute(src, 'images/empty_star_big.gif');
				nodes[i].setAttribute('alt', 'No star');
				totalStars++;
			}
		}  
	} 
   } 
   clearMessage(tbid);  

}

function updateRatingInfo(data) {
// check for returned data
   if ((data == "") || (data == null)) {
   	//Don't worry about it
	return;
   } else if (data.indexOf("Problem") != -1) {
   	//Don't worry about it
	return;
   } else if (data.indexOf("Exception") != -1) {
   	//Don't worry about it
	return;
   } else if (data.indexOf("Already rated") != -1) {
   	//Don't worry about it
	return;
   } else {
	origMessage = "<span class=\"smallprint\">" + data + " Ratings</span>";
   }

}


function storeRating(thisElem, rating, userNickname, tID, ctID, tsID, origRating, tbid) { 

// send rating, user name and tsID to database
   JGetRatingsInfo.addNicknameRating(userNickname, tID, ctID, tsID, rating, updateRatingInfo);

// Create anonymous function fo display the already rated message function
   var functionDemo = new Function("alreadyRatedMessage('"+tbid+"');")

// Remove onmouseover and onmouseout functions for the individual images in this rating block
   var rb = thisElem.parentNode;
   var nodes = rb.childNodes;
   for(var i = 0; i < nodes.length; i++) {
	if (nodes[i].className == 'stars') { 
		nodes[i].setAttribute("onclick", "");
		nodes[i].setAttribute("onmouseover", functionDemo);
		nodes[i].setAttribute("onmouseout", "");
	} 
   }

// show thank you for rating message
   ratingSuccess(rb.id, origRating, tbid); 

}


