$(document).ready(function() {

	var activeStar;
	var finalSoloPickNum;
	var finalReviewPickNum;
	var userSoloClicked = false;
	var t;

	// get most of the variables I'll need once the hover starts
	// these apply to (this) rater so multiple ones can coexist
	function getEls(el){
		starRaterParent = $(el).parents(".userStarRater"); // parent userStarRater
		ratingUL = $(el).parents(".starActive"); // ul parent of li's that hold anchors
		starRatingText = $(starRaterParent).find(".starRatingText"); // element that displays to right of stars
		origRatingText = $(el).parents(".userStarRater").find(".starRatingText").html(); // original text of element that displays
		hoverStar = $(el).parent("li").attr("class"); // used to get class name of li that contains anchor being hovered over
		hoverStarLinks = $(ratingUL).find("a"); // the anchors themselves
	}
	
	$(".starActive a").hover(
		function () { // onmouseover
			getEls(this); // get most of the variables 
			$(hoverStarLinks).removeClass("active"); //remove all active links so that CSS hover states work
			userRatingText(hoverStar); // call function to change text that appears to the right
			if ( $(starRaterParent).hasClass("userSolo") ) {
				stopTimer(); // cancel the timer to flip the display back to the avg. rating if user has not yet rated in this page load
			} 
		}, 
		function () { // onmouseout
			if ( $(starRaterParent).find("a").hasClass("flag") ){ // if an anchor has been previously picked, indicated by 'flag'
				$(starRaterParent).find("a.flag").addClass("active"); // make that the active one
				hoverStar = $(starRaterParent).find("a.flag").parent("li").attr("class"); // find the class name of the li, used to by next line to determine text display
				userRatingText(hoverStar); // find the text to display
			} else {
				$(starRatingText).html(origRatingText); // if no flag exists (i.e. user hasn't make a choice), restore previous text (like, Rate Me)
			}
			if ( userSoloClicked == false ) {
				startTimer(); // if the user has hovered over stars in header but hasn't made a choice
			}
			if ( $(starRaterParent).hasClass("userSolo") ) {
				$(starRatingText).html("Your Rating"); // hard coded for header text
			} 
		}
	 );


	$(".starActive a").click(function(){
		$(starRaterParent).find("a.flag").removeClass("flag"); // remove the flag since a new one is going to be placed
		$(this).addClass("active").addClass("flag"); // adding new flag
		activeStar = $(starRaterParent).find(".active").parent("li").attr("class"); // get class name of li, has number in it
		finalPickNum = activeStar.split("-")[1]; // isolate the number, used to call other functions below
		
		if ( $(starRaterParent).hasClass("userSolo") ) {
			stopTimer(); // on click stop timer, no longer need it
			userSoloClicked = true; // prevents timer from firing again
			$(starRaterParent).hide(); // hide the rating stars temporarily
			$(starRaterParent).next(".ratingConfirm").show().delay(2000, function(){ // show rating comfirmation text
				$(starRaterParent).next(".ratingConfirm").hide(); // hide confirmation text
				$(starRaterParent).fadeIn(); // fade in rating starts again
				$(starRatingText).html("Your Rating"); // set text of header rating stars
			});
			finalSoloPickNum = finalPickNum; // varaible of rating star picked in header rater
			gameReviews_submitStarRating(finalSoloPickNum);
		} else {
			finalReviewPickNum = finalPickNum; // variable of rating star picked in Write A Review
			gameReviews_setReviewFormRating(finalReviewPickNum);
		}
		return false;
	})


	// uses class of anchor's parent 'li' to determin text to display next to stars
	function userRatingText(hoverStar) {
		if 		(hoverStar == "ratingStars-1") 	{ $(starRatingText).html("Really bad"); }
		else if 	(hoverStar == "ratingStars-2")	{ $(starRatingText).html("Needs work"); }
		else if 	(hoverStar == "ratingStars-3")	{ $(starRatingText).html("Pretty good"); }
		else if 	(hoverStar == "ratingStars-4")	{ $(starRatingText).html("Great game"); }
		else 													{ $(starRatingText).html("Fantastic!"); }
	}

	// swap out static rating stars for interactive one
	$(".ratingWrap .ratingStars").mouseover(
		function () {
			activeStar = null;
			$(this).parent().hide();
			$(this).parent().next(".userStarRater").show();
		}
	 );
	 
	// if user does not pick rating in header, restore the original static rating 
	function restoreAverageRating(){
		$(".ratingWrap").next(".userStarRater").hide();
		$(".ratingWrap").fadeIn();
	}

	// start timer to restore original static rating if user hovers over stars but none are picked
	function startTimer() {
		t=setTimeout(restoreAverageRating,500);
	}	
	function stopTimer() {
		clearTimeout(t)
	}
	
});