var SIGN_IN_LINK = "/profile/default.aspx?Promo=rec0001";
var MOUSEOVER_TITLES = new Array();
MOUSEOVER_TITLES[0] = "Hated it!";
MOUSEOVER_TITLES[1] = "Did not like it.";
MOUSEOVER_TITLES[2] = "Almost into it.";
MOUSEOVER_TITLES[3] = "Liked it.";
MOUSEOVER_TITLES[4] = "Really enjoyed it.";
MOUSEOVER_TITLES[5] = "Loved it.";
MOUSEOVER_TITLES[6] = "Haven't seen it.";
MOUSEOVER_TITLES[7] = "Not interested in it.";


var sets = new Array();
var recognizedUser = false;
SIDEBAR_FRAME = "frmSide";

function InitWidget(WidgetType, MovieID, RatingValue, RatingType, RecognizedUser, ClientID) {
	//Store values
	sets[MovieID] = WidgetType; 
	recognizedUser = RecognizedUser;

	//Create placeholder images
	var thisHtml =  "<div class=\"widget_" + WidgetType + "\">" ;
	thisHtml +=		"<div class=\"ratingsWidget\">" ;
	thisHtml +=		"<div class=\"starbox\">" ;
	thisHtml +=		"<ul>" ;

	for (var i = 0; i <= 5; i++)
	{
	cssclass = (i==0)?" class=\"star0\"":"";
	atitle = (RecognizedUser)?" title=\"" + MOUSEOVER_TITLES[i] + "\"":" title=\"You must sign in to rate movies.\"";
	thisHtml += "<li" + cssclass + "><a href=\"javascript:setRating('" + MovieID + "','" + i + "','" + ClientID + "')\"" + atitle + ">";
	thisHtml += "<img id=\"" + MovieID + "_img" + i + "\" border='0'  ";
	thisHtml += " onMouseOver=\"doMouseOver('" + MovieID + "','" + i + "');\" ";
	thisHtml += " onMouseOut=\"doMouseOut('" + MovieID + "');\" />";
	thisHtml += "</a></li>";
	}
	thisHtml += "</ul>"; //close starbar
	
	thisHtml += "<div class=\"starignore\">" ;
	atitle = (RecognizedUser)?" title=\"" + MOUSEOVER_TITLES[7] + "\"":" title=\"You must sign in to rate movies.\"";
	thisHtml += "<a href=\"javascript:setRating('" + MovieID + "','7','" + ClientID + "')\"" + atitle + ">";
	thisHtml += "<img id=\"" + MovieID + "_img7\" border='0'  ";
	thisHtml += " onMouseOver=\"doMouseOver('" + MovieID + "','7');\" ";
	thisHtml += " onMouseOut=\"doMouseOut('" + MovieID + "');\" />";
	thisHtml += "</a>";
	thisHtml += "</div></div>"; //close starbox

	
	thisHtml +=	"<DIV class=\"staroptions\"><ul>" ;
	if (RecognizedUser) {
		//one checkbox
		atitle = (RecognizedUser)?" title=\"" + MOUSEOVER_TITLES[6] + "\"":" title=\"You must sign in to rate movies.\"";
		thisHtml += "<li><a href=\"javascript:setRating('" + MovieID + "','6','" + ClientID + "')\"" + atitle + ">";
		thisHtml += "<img id=\"" + MovieID + "_img6\" border='0'  ";
		thisHtml += " onMouseOver=\"doMouseOver('" + MovieID + "','6');\" ";
		thisHtml += " onMouseOut=\"doMouseOut('" + MovieID + "');\" />";
		thisHtml += "</a></li>";
		
	}
	else {
		//sign in link
		thisHtml += "<li class=\"unrec\"><p><img src=\"images/ratingStars/icon_arrow.gif\" width=\"8\" height=\"8\"><a href=\"" + SIGN_IN_LINK+ "\" title=\"Register to rate movies.\">Register and Rate Movies.</a></p></li>";
		thisHtml += "<li class=\"unrec\"><p><img src=\"images/ratingStars/icon_arrow.gif\" width=\"8\" height=\"8\"><a href=\"" + SIGN_IN_LINK + "\" title=\"Sign in to rate movies.\">Have an Account? Sign In.</a></p></li>";
	}	
	thisHtml +=	"</ul></div>"; //close staroptions
	thisHtml += "<div class=\"starguide\">";
	thisHtml += "<a href=\"/support/help_rate_rec.aspx\"><img src=\"images/ratingStars/icon_btn_guide.gif\" width=\"19\" height=\"20\" name=\"guide\" alt=\"Help\" title=\"Help\" border=\"0\"></a>";
	thisHtml += "</div>"; //close starguide
	

	thisHtml += "<div class=\"bloat\"><hr></div>";
	thisHtml += "</div></div>"; //close ratingsWidget and widget_c/e

	document.write(thisHtml);
	displayRating(MovieID, RatingValue, RatingType, true);

}


function doMouseOver(MovieID, RatingValue) {
	displayRating(MovieID, RatingValue, 1, false);
}

function doMouseOut(MovieID) {
	if (sets[MovieID] == "expanded" && recognizedUser) { nImages=7; } else {nImages=7;}
	for (var i = 0; i <= nImages; i++) {
		img = getImage(MovieID + "_img" + i);
		img.src = img.defsrc;
	}
}
function setRating(MovieID, RatingValue, ClientID) {
	displayRating(MovieID, RatingValue, 1, true);
	setRatingImage(ClientID);
	//Submit rating
	MID = MovieID.substring(MovieID.lastIndexOf("_")+1, MovieID.length);
	submitURL = "/util/submitRating.aspx?mid=" + MID + "&r=" + RatingValue + "&source=" + window.location.pathname + window.location.search;
	window.location.href = submitURL;
	//Refresh iframe sidebar
	if (document.getElementById(SIDEBAR_FRAME) ) {
		document.getElementById(SIDEBAR_FRAME).src = document.getElementById(SIDEBAR_FRAME).src;
	}
}

function displayRating(MovieID, RatingValue, RatingType, SetAsDefault) {
	if (sets[MovieID] == "expanded" && recognizedUser) { nImages=7; } else {nImages=7;}
	
	if (RatingType > 3) {
		//unavailable or unknown - all off
		for (var i = 0; i <= nImages; i++) {
			if (i==0) { imgType="0" } else if (i==6) { imgType="nosee" } else if (i==7) { imgType="nointerest" } else { imgType="star" }
			offImage = "rating_" + imgType + "_off.gif";
			setImgSrc(MovieID+"_img" + i, offImage, SetAsDefault);
		}
	}
	else {
		//Predicted and community can be fractions
		var RatingInt = parseInt(RatingValue);
		var RatingFraction = "";
		if (RatingType > 1) {
			sRatingValue = RatingValue + ''; //string
			if(sRatingValue.indexOf('.')>0){
				RatingFraction = sRatingValue.substring((sRatingValue.indexOf('.')+1) , sRatingValue.length);
				if (RatingFraction.length == 1) {RatingFraction += "0";}
			}
		}
		// non-star selected = special image
		if (RatingValue == 0 || RatingValue > 5) {
			//set selected image on
			onImage = "rating_" + ((RatingValue==0)?"0":(RatingValue==6)?"nosee":"nointerest") + "_on.gif";
			setImgSrc(MovieID+"_img" + RatingValue, onImage, SetAsDefault);
			//set all others off
			for (var i = 0; i <= nImages; i++) {
				if (i != RatingValue) {
				if (i==0) { imgType="0" } else if (i==6) { imgType="nosee" } else if (i==7) { imgType="nointerest" } else { imgType="star" }
				offImage = "rating_" + imgType + "_off.gif";
				setImgSrc(MovieID+"_img" + i, offImage, SetAsDefault);
				}
			}
		}
		//Star selected
		else
		{
			//Special images off
			setImgSrc(MovieID+"_img0", "rating_0_off.gif", SetAsDefault);
			if (nImages > 5) {
				setImgSrc(MovieID+"_img6", "rating_nosee_off.gif", SetAsDefault);
				setImgSrc(MovieID+"_img7", "rating_nointerest_off.gif", SetAsDefault);
			}
			//Loop through stars
			for (var i = 1; i <=5 ; i++) {
				//on
				if (i <= RatingValue) {
					setImgSrc(MovieID+"_img" + i, "rating_star" + RatingType + "_on.gif", SetAsDefault);
				}
				else {
					//fractional
					if (RatingFraction != "" && (RatingInt == i-1) ) {
						setImgSrc(MovieID+"_img" + i, "rating_star" + RatingType + "_on" + RatingFraction + ".gif", SetAsDefault);
					}
					//off
					else {
						setImgSrc(MovieID+"_img" + i, "rating_star_off.gif", SetAsDefault);					
					}
				}
			}
		}
	}
}

function setImgSrc(ImgID, Src, SetAsDefault) {
	img = getImage(ImgID);
	if (img) {
		MovieID = ImgID.substring(0, ImgID.lastIndexOf("_"));
		Src = "images/ratingStars/" + sets[MovieID] + "/" + Src;
		img.src = Src;
		if (SetAsDefault) {
			img.defsrc = Src;
		}
	}
}

function getImage(ImgID) {
	return document.getElementById(ImgID);
}

function setRatingImage(ClientID) {
	if (recognizedUser)
	{
		var imgID = ClientID+"_ratingImage";
		var ratingImage = document.getElementById(imgID);
		if (ratingImage)
		{
			ratingImage.src="images/lbl_rate_your.gif";
			ratingImage.alt="Your Rating";
		}
	}
}

