
// Global Variables
var storedRating;
var currentResourceImg;
var workingResourceImg;
var currentRating;

var urlPath = "http://lessonlink.library.arizona.edu/";

var ratingHTTPObject;

// Shortcut variable for "undefined".
var dd_u = "undefined";

// Define some global variables
// to be used for browser identification, etc.
var n = navigator.userAgent.toLowerCase();
var db = (document.compatMode && document.compatMode.toLowerCase() != "backcompat")?
      document.documentElement
      : (document.body || null);
var op = !!(window.opera && document.getElementById);
if(op) document.onmousedown = new Function('e',
      'if(((e = e || window.event).target || e.srcElement).tagName == "IMAGE") return false;');
var ie = !!(n.indexOf("msie") >= 0 && document.all && this.db && !this.op);
var iemac = !!(ie && n.indexOf("mac") >= 0);

var ie4 = !!(ie && !document.getElementById);
var n4 = !!(document.layers && typeof document.classes != dd_u);
var n6 = !!(typeof window.getComputedStyle != dd_u && typeof document.createRange != dd_u);
var w3c = !!(!this.op && !ie && !n6 && document.getElementById);
var ce = !!(document.captureEvents && document.releaseEvents);
var px = n4 ? '' : 'px';
var tiv = w3c ? 40 : 10;

// Preloads the images used for
// ranking, adds a function to
// determine the location of the
// user's mouse, and initializes
// the AJAX object.
function initRatingSystem()
{
   // Define OnMouseMoveHandler function
   eval("function OnMouseMoveHandler(evt) { if(typeof(evt)=='undefined'){evt=event;} displayTempRating(evt) }");

   // Add OnMouseMoveHandler to onmousemove listener
   eval("document.onmousemove = OnMouseMoveHandler;");
   
   // Preload images
   var tmpPic1 = new Image(100,25); 
   tmpPic1.src=urlPath+"layout_imgs/stars/1.gif";
   tmpPic1.src=urlPath+"layout_imgs/stars/2.gif";
   tmpPic1.src=urlPath+"layout_imgs/stars/3.gif";
   tmpPic1.src=urlPath+"layout_imgs/stars/4.gif";
   tmpPic1.src=urlPath+"layout_imgs/stars/5.gif";
   tmpPic1.src=urlPath+"layout_imgs/stars/6.gif";
   tmpPic1.src=urlPath+"layout_imgs/stars/7.gif";
   tmpPic1.src=urlPath+"layout_imgs/stars/7.gif";
   tmpPic1.src=urlPath+"layout_imgs/stars/8.gif";
   tmpPic1.src=urlPath+"layout_imgs/stars/9.gif";
   tmpPic1.src=urlPath+"layout_imgs/stars/10.gif";
   
   ratingHTTPObject = getHTTPObject();
   workingResourceImg = "";
   currentResourceImg = "";
}

// Displays a given number of stars
// based on the location of the 
// user's mouse.
function startRating(img)
{
   if(currentResourceImg != "")
      while(currentResourceImg != "");

   currentResourceImg = img;
   storedRating = img.src;
   return;
}

// Displays the current ranking when
// the user moves their mouse off of
// the stars.
//function restoreRating(img, rating)
function restoreRating(img)
{
   var votingInfo = img.className.split(" ");
   var ranking = (parseInt(votingInfo[1].substr(3))) + (parseInt(votingInfo[2].substr(3)));
   
   if(ranking != 0)
      ranking = ranking/(parseInt(votingInfo[0].substr(3)));
   
   // We need to make sure that our ranking is
   // a whole number
   ranking = Math.round(ranking);
   img.src = urlPath+"layout_imgs/stars/"+ranking+".gif";

   currentResourceImg = "";
   storedRating = "";
   return;
}

function displayTempRating(evt, img)
{
   if(storedRating && currentResourceImg)
   {
      var mousePosition = evt.clientX;
      if(ie)
         mousePosition -= 100;
      
      currentRating = Math.round((Math.abs(mousePosition - currentResourceImg.offsetLeft))/8);
      if(currentRating > 10)
         currentRating = 10;

      currentResourceImg.src = urlPath+"layout_imgs/stars/" + currentRating + ".gif";
   }
}

// Sends the user's submission to
// the database via AJAX
function setRating(img)
{
   var votingInfo = img.className.split(" ");
   var totalVoters = parseInt(votingInfo[0].substr(3));
   var totalVotes = parseInt(votingInfo[1].substr(3));

   var rID = img.id.substr(3);

   // If the user has already voted, then
   // we don't increment the number of
   // voters. Otherwise, we increase by 1.
   if( votingInfo[2].substr(0,3) == "rnk")
   {
      // Increment the number of voters
      var noOfVotersSpan = document.getElementById( (img.id + "Voters") );
      var noOfVoters = parseInt(noOfVotersSpan.innerHTML);
      noOfVotersSpan.innerHTML = noOfVoters + 1;
      totalVoters++;
   }

   var params = "id=" + rID + "&r=" + currentRating;
   var newRanking = ((totalVotes+currentRating) / (totalVoters));

   img.className = "vts"+totalVoters + " val" + totalVotes + " uvt" + currentRating;
   img.alt = "Currently ranked at " + (newRanking/2) + " stars";

   var responseFunction = function() { };
   var failureFunction = function() {  alert('Unable to add ranking.'); };
   XmlHttpPOST(ratingHTTPObject, "ajaxRating.php", params, responseFunction, failureFunction);

   restoreRating(img);

   return;
}