var defaultStars = 0;
var tmpActiveStars = 0;

function hideEditForm() {
	var reviewForm = document.getElementById('reviewForm');
	reviewForm.style.display="none";
}
function showEditForm() {
	var showEditReview = document.getElementById('showEditReview');
	var reviewForm = document.getElementById('reviewForm');
	reviewForm.style.display="";
	showEditReview.style.display="none";
}
function submitReview() {
	var form = document.getElementById("reviewForm");
	var okToSubmit = 1;
	/* Verify at least a rating was present. If there is no review, that is probably ok */
	if (defaultStars == 0) {
		okToSubmit = 0;
		alert("You must choose a star rating for this show.");
	}


	if (okToSubmit == 1) {
		form.submit();
	}
}
function findPosition( oElement ) {
	if( typeof( oElement.offsetParent ) != "undefined" ) {
		for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
			posX += oElement.offsetLeft;
			posY += oElement.offsetTop;
		}
		return [ posX, posY ];
	} else {
		return [ oElement.x, oElement.y ];
	}
}
function reviewMouseMove(event, obj) {
	var starId = obj.attr('id').substring(obj.attr('id').length - 1);
	
  var p = obj.position();

  var mouseX = event.pageX;
  var objX = obj.offset().left;

	var stars = parseInt(starId) + .5; /* since the lowest you can give is 0.5 */
	if (mouseX - objX >= 20) {
		stars += .5;
	}
	tmpActiveStars = stars;
	showDefaultStars("blue", stars);

}
function fixRatingVal(setToStarVal) {
	var starVal = document.getElementById("starVal");
	if (setToStarVal == 1) {
		tmpActiveStars = starVal.value;
		defaultStars = starVal.value;
	} else {
		starVal.value = tmpActiveStars;
		defaultStars = tmpActiveStars;
	} 
	showDefaultStars("yellow", tmpActiveStars);
}
function showDefaultStars(color, stars) {
	var starCount = stars;
	for (var x=0; x < 5; x++) {
		var thisStar = 0;
		if (starCount >= 1) {	
			thisStar = 4; /* The fully filled star */
			starCount--;
		} else if (starCount == 0.5) {
			thisStar = 2; /* The half filled star */
			starCount -= .5;
		}
		var img = document.getElementById("star" + x);
		img.src = "/static/" + color + "star" + thisStar+ ".png";
	}

	var ratingVal = document.getElementById("ratingVal");
	clear(ratingVal);
	ratingVal.appendChild(document.createTextNode( stars ));
}

function ajax(url, callback) {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			callback(xmlHttp.responseText);
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function rateReview(txt) {
	if (txt.substr(0,2) == "OK") {
		var match = /OK-([\d]+)-([\d]+)-([\d]+)-([\d]+)/i.exec(txt)
		var ratingType = match[1];
		var id = match[2];
		var numPlus = match[3];
		var numTotal = match[4];
		var helpfulField = document.getElementById('reviewHelpful' + id);
		var field = document.getElementById('rateReview' + id);
		clear(field);
		clear(helpfulField);
		if (ratingType == "1") { //This was a helpful review
			field.appendChild(document.createTextNode("You marked this review as helpful."));
		} else if (ratingType == "0") { //This was not a helpful review
			field.appendChild(document.createTextNode("You marked this review as not helpful."));
		}
		helpfulField.appendChild(document.createTextNode(numPlus + " of " + numTotal + " people found this helpful"));
	} else if (txt.substr(0,5) == "DELOK") {
		var match = /DELOK-([\d]+)-([\d]+)-([\d]+)/i.exec(txt)
		var id = match[1];
		var numPlus = match[2];
		var numTotal = match[3];
		var field = document.getElementById('rateReview' + id);
		var helpfulField = document.getElementById('reviewHelpful' + id);

		clear(field);
		clear(helpfulField);
		field.appendChild(document.createTextNode("Your rating for this review has been deleted."));
		helpfulField.appendChild(document.createTextNode(numPlus + " of " + numTotal + " people found this helpful"));
	}
}

/* Preload Mouseover Images */
<!--
if (document.images) {
	var color="blue";
	pic1 = new Image(40,40);
	pic1.src = "/static/" + color + "star0.png";
	pic2 = new Image(40,40);
	pic2.src = "/static/" + color + "star1.png";
	pic3 = new Image(40,40);
	pic3.src = "/static/" + color + "star2.png";
	pic4 = new Image(40,40);
	pic4.src = "/static/" + color + "star3.png";
	pic5 = new Image(40,40);
	pic5.src = "/static/" + color + "star4.png";
}
//-->

$(document).ready(function() {
  $(".ratingStar").mousemove(function(event) {
    reviewMouseMove(event, $(this));
  });
});

