var starImages = new Array(
			"images/misc/stars/stars_0.jpg",
			"images/misc/stars/stars_2.jpg",
			"images/misc/stars/stars_4.jpg",
			"images/misc/stars/stars_6.jpg",
			"images/misc/stars/stars_8.jpg",
			"images/misc/stars/stars_10.jpg"
	);

var starMessages = new Array(
			"",
			"",
			"",
			"",
			"",
			"",
			"Rate it",
			"",
            "&nbsp;");

var starMap = new Array(
		'0,0,0,0',
		'0,0,17,15',
		'17,0,33,15',
		'33,0,50,15',
		'50,0,66,15',
		'66,0,81,15');

var starTwinkler = new Array();
var msgTwinkler = new Array();
var rating = 0;
var isSaved = new Array();
var delayTime = 500;
var allImages = new Array();

function preloadStarImages(imgArray){
  for (i=0; i < imgArray.length ;i++){
    if (imgArray[i] != undefined) {
      allImages[i] = new Image();
      allImages[i].src = imgArray[i];
    }
  }
}

preloadStarImages(starImages);

function swapStars(pid, rating) {
    if (document.images["stars." + pid] != undefined) {
        document.images["stars." + pid].src = starImages[rating];
    }  
}

function swapStarMsgs(pid, rating) {
    if (!isSaved[pid] && document.getElementById("messages." + pid)) {
        if (rating == undefined) {
            document.getElementById("messages." + pid).innerHTML = starMessages[6];
        } else {
            document.getElementById("messages." + pid).innerHTML = starMessages[rating];
            if (rating == 7) isSaved[pid]= 1;
        }
    }
}

function starMouseOver(pid, rating){
   if (starTwinkler[pid] != 0){
    window.clearTimeout(starTwinkler[pid]);
    starTwinkler[pid] = 0;
  }
  if (msgTwinkler[pid] != 0){
    window.clearTimeout(msgTwinkler[pid]);
    msgTwinkler[pid] = 0;
  }  
  swapStars(pid, rating);
  swapStarMsgs(pid, rating);
}

function starMouseOut(pid, rating){
  // set value to send
  if(document.getElementById('starrating')){document.getElementById("starrating").value = rating;}
}

function activateStars(pid, rating){
	var starID = "stars." + pid;

    starTwinkler[pid] = 0;
    msgTwinkler[pid] = 0;

    document.write("<map style="+'"'+"cursor: pointer;cursor: hand;"+'"'+" name="+'"'+"starmap" + pid +""+'"'+">");

    var i = 0;
    for (i = 0; i < 6; i++) {
        document.write("<area shape="+'"'+"rect"+'"'+" " + 
            "coords='" + starMap[i] + "' " +
            "onmouseover="+'"'+"starMouseOver('" + pid + "'," + i + ");"+'"'+" " +
            "onmouseout="+'"'+"starMouseOut('" + pid + "'," + i + ");"+'"'+"  />");
    }
    document.write("</map>");
    document.write("<img src="+'"'+"" + starImages[rating] + ""+'"'+"");
    document.write(" valign="+'"'+"bottom"+'"'+" border="+'"'+"0"+'"'+" usemap="+'"'+"#starmap" + pid);
    document.write(""+'"'+" id="+'"'+"" + starID + ""+'"'+" />");
}

function activateStarMsgs(pid, defaultMsg){
    if (defaultMsg) {
        starMessages[6] = starMessages[defaultMsg];
    }
    document.write("<span id="+'"'+"messages." + pid + ""+'"'+" class="+'"'+"tiny"+'"'+">" + starMessages[6]  + "</span>");
}
