
//---------------------------------------------------------------------------------------------
// FUNCTION: pkrating(rateid, image_on, image_half, image_off)
// PURPOSE: MAIN CLASS FUNCTION
//---------------------------------------------------------------------------------------------
function pkrating(rateid, image_on, image_half, image_off, image_hover) {
    this.rateid = rateid;
    this.spanid = 'pkr_'+rateid+'_booth';
    this.image_on = image_on;
    this.image_half = image_half;
    this.image_off = image_off;
    this.image_hover = image_hover;
}

//---------------------------------------------------------------------------------------------
// FUNCTION: pkrating_mouseover(rateid, rating, imageon, imageoff)
// PURPOSE: Highlights all the stars that need to be. :)
//---------------------------------------------------------------------------------------------
pkrating.prototype.pkmouseover=function(rating) {
  var imageid;
  for(var i=1; i<6; i++) {
    imageid = 'pkr_'+this.rateid+'_'+i;
    if(i <= rating) {
        document.getElementById(imageid).src = this.image_hover;
    } else {
        document.getElementById(imageid).src = this.image_off;
    }
  }
}

//---------------------------------------------------------------------------------------------
// FUNCTION: pkflipimages(rateid, averagerating)
// PURPOSE: Hilights the images according to the average rating
//---------------------------------------------------------------------------------------------
pkrating.prototype.pkflipimages=function(averagerateing) {
    var imageid;
    var remainder = averagerateing - Math.floor(averagerateing);
    for(var i=1; i<6; i++) {
      imageid = 'pkr_'+this.rateid+'_'+i;
      if (Math.floor(averagerateing) >= i) {
        document.getElementById(imageid).src = this.image_on;
      } else if ((Math.floor(averagerateing) == i-1) && (remainder >= 0.5)) {
        document.getElementById(imageid).src = this.image_half;
      } else {
        document.getElementById(imageid).src = this.image_off;
      }
    }
}

//---------------------------------------------------------------------------------------------
// FUNCTION: pkrating_click(url)
// PURPOSE: Submits the vote to the DB, and prints the Result to the screen.
//---------------------------------------------------------------------------------------------
pkrating.prototype.pkclick=function(url) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
      http_request.overrideMimeType('text/xml');
    }
  } else if (window.ActiveXObject) { // IE
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  if (!http_request) {
    alert('Giving up :( Cannot create an XMLHTTP instance');
    return false;
  }
  http_request.onreadystatechange = this.alertContents;
  http_request.open('GET', url, true);
  http_request.send(null);
}

//---------------------------------------------------------------------------------------------
// FUNCTION: alertContents()
// PURPOSE: This is the response from the above request.
//---------------------------------------------------------------------------------------------
pkrating.prototype.alertContents=function() {
  if (http_request.readyState == 4) {
    if (http_request.status == 200) {
      var response = http_request.responseText.split(";")
      var spanid = "pkr_"+response[0]+"_booth";
      eval("var myclass=PKR" + response[0]);
      var numvotes = response[1];
      var averagerateing = response[2];
      var uservote = response[3];
      var remainder = averagerateing - Math.floor(averagerateing);
      var html = "";
      for(var i=1; i<6; i++) {
        if (Math.floor(averagerateing) >= i) {
          html += "<img src='"+ myclass.image_on +"' alt='' />";
        } else if ((Math.floor(averagerateing) == i-1) && (remainder >= 0.5)) {
          html += "<img src='"+ myclass.image_half +"' alt='' />";
        } else {
          html += "<img src='"+ myclass.image_off +"' alt='' />";
        }
      }
      //alert("Your vote has been submitted.  Thank you.");
      document.getElementById(spanid).innerHTML = html;
    } else {
      alert('AJAX Error');
    }
  }
}

