/* Example usage:  

The parameters of starfield_init are
 - the id of a type="hidden" field to store the result
 - the maximum number of stars to display
 - the src of the "white" star - the star that is turned off.
 - the src of the bright star - the star that is turned on.

The hidden field also can contain the original value.
And the parent of the hidden field will contain all the stars, so it should be in a div or span or td.

<html>
<script language="javascript" src="starfield.js">
<body onload="starfield_init('rating',10,'../starwhite.gif','../starbright.gif')">
<form>


<div>
    <input type="hidden" id="rating" name="rating" value="7">
 </div>
   
</form>

</body>


</html>
*/

function starfield_init(divid,maxstars,white,bright) {
  result =   document.getElementById(divid);
  starfield = result.parentNode; 
  starfield.star = new Array();
  starfield.time = null;
  starfield.starcount = result.value;
  starfield.white = white;
  starfield.bright = bright;
  starfield.maxstars = maxstars;
  starfield.result = result;
  for (i=1; i<=starfield.maxstars; i++) {
    starfield.star[i]=document.createElement("img")
    starfield.star[i].src=starfield.white;
    starfield.star[i].onmouseover = starfield_mouseover;
    starfield.star[i].onmouseout = starfield_mouseout;
    starfield.star[i].onclick = starfield_mouseclick;
    starfield.star[i].index = i;
    starfield.star[i].field = starfield;
    starfield.appendChild(starfield.star[i]);
    starfield.setstars = starfield_setstars;
  }
  starfield.setstars(0);
  
}

function starfield_mouseover() {
  starfield = this.field;
  clearTimeout(starfield.timer);
  starfield.timer = null;
  starnum = this.index;
  starfield.setstars(starnum);
}

function starfield_mouseout() {
  starfield=this.field;
  starfield.timer = setTimeout ("starfield.setstars(0);",500);
}


function starfield_setstars(starnum) {
  if (starnum == 0) starnum = this.starcount;
 for (i=1; i<= starnum; i++) {
    this.star[i].src=this.bright;
  }
  for (j=this.maxstars; j>starnum; j--) {
    this.star[j].src=this.white;
  }
}

function starfield_mouseclick() {
  starfield = this.field;
  starfield.starcount=this.index;
  starfield.result.value = starfield.starcount;
}
