function Rating(max,pzimg,phimg,pfimg,primg) {
  var self = this;
  this.confirm = true;
  this.readonly = false;

  this.setRating = function(elmid,rating,count,ratingurl) {
    var elm = document.getElementById(elmid);
    var ratings = getRatingHTML(elmid,rating,ratingurl);
    if(elm.firstChild)
      elm.replaceChild(ratings,elm.firstChild);
    else
      elm.appendChild(ratings);
    var telm = document.getElementById(elmid+"_txt");
    if(telm)
      telm.innerHTML = rating+"/"+max+" ("+count+" ratings)";
  }

  function getRatingHTML(elmid,rating,ratingurl) {
    var html = document.createElement("span");
    html.setAttribute('style','white-space:nowrap;');
    var i;
    for(i=1;i<=rating;i++) {
      html.appendChild(getImgHTML(pfimg,elmid,i,ratingurl));
    }
    if(rating > 0 && i<=max && (i-rating) > 0 && (i-rating) < 1) {
      html.appendChild(getImgHTML(phimg,elmid,i,ratingurl));
      i++;
    }
    for(;i<=max;i++) {
      html.appendChild(getImgHTML(pzimg,elmid,i,ratingurl));
    }
    return html;
  }

  function getImgHTML(imgurl,elmid,rating,ratingurl) {
    var val = rating;
    if(self.meaning)
      val = self.meaning[rating];
    var img = document.createElement("img");
    img.setAttribute('src',imgurl);
    if(self.readonly) {
      img.title = "iPodish Rating: "+val;
      return img;
    }
    var a = document.createElement("a");
    a.setAttribute("title","iPodish Rating : "+val+". Click To Rate!");
    a.href = 'javascript:void(0);';
    img.onmouseover = function(event) { img.src = primg; }
    img.onmouseout = function(event) { img.src = imgurl; }
    a.appendChild(img);
    a.onclick = function(event,fromError) {
      if(!ratingurl) {
        alert("Looks like you have already voted for this! You can vote for another.");
	return;
      }
      if(!fromError && self.confirm && !confirm('Are you sure you want to give a rating of "'+val+'"?'))
	return;
      var date = new Date();
      new Ajax.Request(ratingurl+rating+'&date='+date.getTime(),{ method: 'get',
            onSuccess : function(transport) {
	                  var ind = transport.responseText.indexOf(',');
			  var count = transport.responseText.substring(0,ind);
	                  var newrating = parseFloat(transport.responseText.substring(ind+1));
	                  self.setRating(elmid,newrating,count,null);
			  alert("Thank you for voting!");
	                }
	   ,onFailure : function(transport) {
	        if(transport.status < 400 && transport.status >= 500) {
	          setTimeout(function() { a.onclick(event,true); },10000);
		}
		else {
                  /* alert(transport.responseText); */
		}
	      }
	    });
      return false;
    }
    return a;
  }
}
