var totalScore = new Array();
var bClicked = new Array();
var bUserRating = new Array();
var barTimer = new Array();
var barTimerName = new Array();

//image arrays for each iamge
var arrImg0 = new Array();
var arrImg1 = new Array();
var arrImg2 = new Array();
var arrImg3 = new Array();
var arrImg4 = new Array();
var arrImg5 = new Array();

var MAX_SCORE = 5;
var currentImage;
var currentScore;

/*
this sets a user rated bar - telling the javascript that it began in a 'yellow' bar
*/
function setUserBar(titleid, startingScore){
	// set bUserRating
	bUserRating[titleid] = true;
	totalScore[titleid] = startingScore;
}

function setTotalBar(titleid, startingScore){
	// only set the original score
	totalScore[titleid] = startingScore;
	//alert("title: " + titleid +" starting score: " + startingScore)
}

function loadBar(titleid, img0,img1,img2,img3,img4,img5,score,avgUserRating,sColourPath,isUserBar,isRedirect){
	var imageNA = sColourPath + "rating_wht_na.gif"
	var image1 = sColourPath + "rating_wht_full.gif"
    var image2 = sColourPath + "rating_wht_full.gif"
    var image3 = sColourPath + "rating_wht_full.gif"
    var image4 = sColourPath + "rating_wht_full.gif"
    var image5 = sColourPath + "rating_wht_full.gif"
	
	switch (score){
		case "0":
			imageNA = sColourPath + "rating_yel_na.gif"
			break;
        case "1":
            image1 = sColourPath + "rating_yel_full.gif"
            break;
        case "2":
            image1 = sColourPath + "rating_yel_full.gif"
            image2 = sColourPath + "rating_yel_full.gif"
            break;
        case "3":
			image1 = sColourPath + "rating_yel_full.gif"
            image2 = sColourPath + "rating_yel_full.gif"
            image3 = sColourPath + "rating_yel_full.gif"
            break;
        case "4":
            image1 = sColourPath + "rating_yel_full.gif"
            image2 = sColourPath + "rating_yel_full.gif"
            image3 = sColourPath + "rating_yel_full.gif"
            image4 = sColourPath + "rating_yel_full.gif"
            break;
        case "5":
            image1 = sColourPath + "rating_yel_full.gif"
            image2 = sColourPath + "rating_yel_full.gif"
            image3 = sColourPath + "rating_yel_full.gif"
            image4 = sColourPath + "rating_yel_full.gif"
            image5 = sColourPath + "rating_yel_full.gif"
            break;
    }
    
    if (isRedirect){
		document.write("<img src='" + imageNA + "' width='13' height='16' id='" + img0 + "' onmouseover='rollover(" + titleid + ", 0);' onclick='redirect();'><img src='" + image1 + "' id='" + img1 + "' width='13' height='16' onmouseover='rollover(" + titleid + ", 1);' onclick='redirect();'><img src='" + image2 + "' id='" + img2 + "' width='13' height='16' onmouseover='rollover(" + titleid + ", 2);' onclick='redirect();'><img src='" + image3 + "' id='" + img3 + "' width='13' height='16' onmouseover='rollover(" + titleid + ", 3);' onclick='redirect();'><img src='" + image4 + "' id='" + img4 + "' width='13' height='16' onmouseover='rollover(" + titleid + ", 4);' onclick='redirect();'><img src='" + image5 + "' id='" + img5 + "' width='13' height='16' onmouseover='rollover(" + titleid + ", 5);' onclick='redirect();'>");
    }else{
		document.write("<img src='" + imageNA + "' width='13' height='16' id='" + img0 + "' onmouseover='rollover(" + titleid + ", 0);' onclick='clickedStar(" + titleid + ");'><img src='" + image1 + "' id='" + img1 + "' width='13' height='16' onmouseover='rollover(" + titleid + ", 1);' onclick='clickedStar(" + titleid + ");'><img src='" + image2 + "' id='" + img2 + "' width='13' height='16' onmouseover='rollover(" + titleid + ", 2);' onclick='clickedStar(" + titleid + ");'><img src='" + image3 + "' id='" + img3 + "' width='13' height='16' onmouseover='rollover(" + titleid + ", 3);' onclick='clickedStar(" + titleid + ");'><img src='" + image4 + "' id='" + img4 + "' width='13' height='16' onmouseover='rollover(" + titleid + ", 4);' onclick='clickedStar(" + titleid + ");'><img src='" + image5 + "' id='" + img5 + "' width='13' height='16' onmouseover='rollover(" + titleid + ", 5);' onclick='clickedStar(" + titleid + ");'>");
    }
    
    arrImg0[titleid] = document.getElementById(img0);
	arrImg1[titleid] = document.getElementById(img1);
	arrImg2[titleid] = document.getElementById(img2);
	arrImg3[titleid] = document.getElementById(img3);
	arrImg4[titleid] = document.getElementById(img4);
	arrImg5[titleid] = document.getElementById(img5);
	
	if (isUserBar == 1) {
		setUserBar(titleid,score);
	}else{
		if (isUserBar == 0) {
			setTotalBar(titleid,avgUserRating);
			resetBar(titleid);
		}
	}
}

//This draws the full size Rating bar
function loadLargeBar(titleid, img0,img1,img2,img3,img4,img5,score,avgUserRating,sColourPath,isUserBar,isRedirect){
	var imageNA = sColourPath + "rating_wht_NA.gif"
	var image1 = sColourPath + "rating_wht_full.gif"
    var image2 = sColourPath + "rating_wht_full.gif"
    var image3 = sColourPath + "rating_wht_full.gif"
    var image4 = sColourPath + "rating_wht_full.gif"
    var image5 = sColourPath + "rating_wht_full.gif"
	
	switch (score){
		case "0":
			imageNA = sColourPath + "rating_yel_na.gif"
			break;
        case "1":
            image1 = sColourPath + "rating_yel_full.gif"
            break;
        case "2":
            image1 = sColourPath + "rating_yel_full.gif"
            image2 = sColourPath + "rating_yel_full.gif"
            break;
        case "3":
			image1 = sColourPath + "rating_yel_full.gif"
            image2 = sColourPath + "rating_yel_full.gif"
            image3 = sColourPath + "rating_yel_full.gif"
            break;
        case "4":
            image1 = sColourPath + "rating_yel_full.gif"
            image2 = sColourPath + "rating_yel_full.gif"
            image3 = sColourPath + "rating_yel_full.gif"
            image4 = sColourPath + "rating_yel_full.gif"
            break;
        case "5":
            image1 = sColourPath + "rating_yel_full.gif"
            image2 = sColourPath + "rating_yel_full.gif"
            image3 = sColourPath + "rating_yel_full.gif"
            image4 = sColourPath + "rating_yel_full.gif"
            image5 = sColourPath + "rating_yel_full.gif"
            break;
    }
    
    if (isRedirect){
		document.write("<img src='" + imageNA + "' id='" + img0 + "' onmouseover='rollover(" + titleid + ", 0);' onclick='redirect();'><img src='" + image1 + "' id='" + img1 + "' onmouseover='rollover(" + titleid + ", 1);' onclick='redirect();'><img src='" + image2 + "' id='" + img2 + "' onmouseover='rollover(" + titleid + ", 2);' onclick='redirect();'><img src='" + image3 + "' id='" + img3 + "' onmouseover='rollover(" + titleid + ", 3);' onclick='redirect();'><img src='" + image4 + "' id='" + img4 + "' onmouseover='rollover(" + titleid + ", 4);' onclick='redirect();'><img src='" + image5 + "' id='" + img5 + "' onmouseover='rollover(" + titleid + ", 5);' onclick='redirect();'>");
    }else{
	    document.write("<img src='" + imageNA + "' id='" + img0 + "' onmouseover='rollover(" + titleid + ", 0);' onclick='clickedStar(" + titleid + ");'><img src='" + image1 + "' id='" + img1 + "' onmouseover='rollover(" + titleid + ", 1);' onclick='clickedStar(" + titleid + ");'><img src='" + image2 + "' id='" + img2 + "' onmouseover='rollover(" + titleid + ", 2);' onclick='clickedStar(" + titleid + ");'><img src='" + image3 + "' id='" + img3 + "' onmouseover='rollover(" + titleid + ", 3);' onclick='clickedStar(" + titleid + ");'><img src='" + image4 + "' id='" + img4 + "' onmouseover='rollover(" + titleid + ", 4);' onclick='clickedStar(" + titleid + ");'><img src='" + image5 + "' id='" + img5 + "' onmouseover='rollover(" + titleid + ", 5);' onclick='clickedStar(" + titleid + ");'>");
    }
    
    arrImg0[titleid] = document.getElementById(img0);
	arrImg1[titleid] = document.getElementById(img1);
	arrImg2[titleid] = document.getElementById(img2);
	arrImg3[titleid] = document.getElementById(img3);
	arrImg4[titleid] = document.getElementById(img4);
	arrImg5[titleid] = document.getElementById(img5);
	
	if (isUserBar == 1) {
		setUserBar(titleid,score);
	}else{
		if (isUserBar == 0) {
			setTotalBar(titleid,avgUserRating);
			resetBar(titleid);
		}
	}
}

function redirect(){window.location.href='../join_step1.aspx?message=true';}

// the proper panel should be set
function rollover(titleid, score){
	// switch the panels to the proper yellow panels
	// set the current image score we are on
	// clear timmer
	window.clearTimeout(barTimer[titleid]);
	currentImage = eval("arrImg" + score + "[" + titleid + "]");
	currentScore = score;
	
	// set to hand
	currentImage.style.cursor = "hand";
	
		
	// get the image we are rolling over
	path = getPathOfImage(eval("arrImg" + score + "[" + titleid + "]")); 
	// now rollover the images - first reset the bar then 
	currentImage.src = path + "rating_yel_full.gif";
	// set first 4 other images based on score.
	for(i=0; i<=score; i++){
		objScoreImage = eval("arrImg" + i + "[" + titleid + "]");
		//alert(objScoreImage.id);
		path = getPathOfImage(objScoreImage);
		if(i==0){
			if(currentScore == 0){
				objScoreImage.src = path + "rating_yel_na.gif";	
			} else {
				objScoreImage.src = path + "rating_wht_na.gif";
			}
		} else {
			objScoreImage.src = path + "rating_yel_full.gif";
		}	
	}
	
	if(score < 5){
		for(i = score + 1; i<=MAX_SCORE; i++){
			objScoreImage = eval("arrImg" + i + "[" + titleid + "]");
			//alert(objScoreImage.id);
			path = getPathOfImage(objScoreImage);
			objScoreImage.src = path + "rating_wht_full.gif";	
		}
	}
}

function clickedStar(titleid){
	// score the click to the iframe
	//try iframes
	oInput = window.userRankingFrame.document.getElementById("inScore");
	// set the current score
	oInput.value = currentScore;
	oInput = window.userRankingFrame.document.getElementById("inTitleID");
	oInput.value = titleid;
	// submit form
	window.userRankingFrame.document.rankingForm.submit();
	// set info so that the bar doesn't reset back to the 'red' bar if it was clicked from a redbar 
	bClicked[titleid] = true;
	totalScore[titleid] = currentScore;
}

function clearVote(titleid, systemScore){
	// score the click to the iframe
	//try iframes
	oInput = window.userRankingFrame.document.getElementById("inScore");
	// set the current score
	oInput.value = -1;
	oInput = window.userRankingFrame.document.getElementById("inTitleID");
	oInput.value = titleid;
	// submit form
	window.userRankingFrame.document.rankingForm.submit();
	
	// set info so that the bar does reset back to the 'red' bar
	bClicked[titleid] = false;
	bUserRating[titleid] = false;
	totalScore[titleid] = systemScore;
	
	//reset the bar!
	resetBar(titleid)
}

function rolloutOfDiv(titleid){
	// set timmer 
	barTimer[titleid] = window.setTimeout("resetBar(" + titleid + ")", 50);
	barTimerName[titleid] = "resetBar(" + titleid + ")"
}

var bSetFirstPartial = false;

function resetBar(titleid){
	// check to see if it's a userbar/or been clicked
	// if not then it's a titlebar
	if(bUserRating[titleid] || bClicked[titleid]){
		// keep it yellow
		for(i = 0; i <= MAX_SCORE; i++){
			// GET IMAGE
			objScoreImage = eval("arrImg" + i + "[" + titleid + "]");
			path = getPathOfImage(objScoreImage);
			
			// if current is lower then total score (or equal) set it to
			// yellow
			if(i <= totalScore[titleid]){

				if(i==0){
					if(totalScore[titleid] == 0){
						objScoreImage.src = path + "rating_yel_na.gif";	
					} else {
						objScoreImage.src = path + "rating_wht_na.gif";
					}
				} else {
					objScoreImage.src = path + "rating_yel_full.gif";
				}	
			} else {
				// else set it to white
				objScoreImage.src = path + "rating_wht_full.gif";
			}
		}
	} else {
		// reset to red 
		bSetFirstPartial = false;
		for(i = 0; i <= MAX_SCORE; i++){
			// GET IMAGE
			objScoreImage = eval("arrImg" + i + "[" + titleid + "]");
			path = getPathOfImage(objScoreImage);
			
			// Unlike yellow, we are now dealing with 'double' values.
			// this means we need to revert to the closest red value (rounding up)
			// to the totalScore
			//alert("TitleId: " + titleid + " compare: " + Math.floor(totalScore[titleid]) + " score: " + totalScore[titleid]);
			if(Math.floor(totalScore[titleid]) >= i){
				// print a full red star
				if(i==0){
					if(totalScore[titleid] == 0){
						objScoreImage.src = path + "rating_red_na.gif";	
					} else {
						objScoreImage.src = path + "rating_wht_na.gif";
					}
				} else {
					objScoreImage.src = path + "rating_red_full.gif";
				}	
			} else {
				if(!bSetFirstPartial){
					//alert ("setting partial white");
					// the first pass is the first star that requires a partial
					// red star
					remainder = totalScore[titleid]-Math.floor(totalScore[titleid]);
					// find the closest that is greater then 0)
					imagePostFix = getClosestRedImage(remainder);
					//alert(imagePostFix + " " + totalScore[titleid]);
					if(imagePostFix == 0 && i==MAX_SCORE){
						//objScoreImage.src = path + "rating_red_" + imagePostFix + "_end.gif";
						objScoreImage.src = path + "rating_wht_full.gif";
					} else if(imagePostFix == 0){
						objScoreImage.src = path + "rating_wht_full.gif";
					} else if(imagePostFix != undefined) {

						//alert(path + "rating_red_" + imagePostFix + ".gif");
						objScoreImage.src = path + "rating_red_" + imagePostFix + ".gif";
					}
					bSetFirstPartial = true;
				} else {
					//alert ("setting full white");
					// since we have set the first partial star the rest are empty stars
					objScoreImage.src = path + "rating_wht_full.gif";
				}
			}
		}
	}
}

function getClosestRedImage(remainder){
	closestMatch=100;
	matchLoop=100;
	for(j=0;j<5;j=j+0.125){
		current = Math.abs(remainder - j);
		//alert("current: " + current + " closestMatch: " + closestMatch);
		//alert(current);
		if(closestMatch<current){
			// return the current value.  Since we want
			// a value like: {125,250,375,500,625,750,875} multiply look value by 1000x
			if (matchLoop*1000 > 875){
				return 875;
			}
			//alert ("Exit:" + matchLoop*1000);
			return matchLoop*1000;
		} else {
			closestMatch = current;
			matchLoop = j;
		}
	} 
}

function hoverClear(img, titleid){
	// get all the img tags inside the link
	objImg = document.getElementById(img);
	pos = objImg.src.lastIndexOf('/');
	imgName = objImg.src.substring(pos + 1,objImg.src.length);
	// Path - required to build string to replace images.
	path = objImg.src.substring(0, pos) + "/";
	// now see if '-over' is in the na  If it isn't add it.
	// if it is
	//alert(imgNalastIndexOf('-over'));
	sSearch = imgNalastIndexOf('-over');
	if(sSearch < 0){
			// no 'over' attached - lets put one in!
			arrImgName = imgNasplit('.');
			// set image source
			// now cut the '_on' or the '_off' off the end
			objImg.src = path + arrImgName[0] + "-over." + arrImgName[1];
	} else {
			// strip the '-over' from the image name
			arrImgName = imgNasplit('-over.');
			objImg.src = path + arrImgName[0] + "." + arrImgName[1];
	}	
}