var votingLocked = false;
var VIDEO_VOTING_BLINK_RATE	  = 100;

function HiliteStars( rank, numStars, pathImg, starOffFilename, starOnFilename, videoID, votingStatic )
{
	if ( votingLocked || votingStatic )
		return;

	DrawStars( rank, numStars, pathImg, starOffFilename, starOnFilename, videoID );
}

function UnhiliteStars( defRank, numStars, pathImg, starOffFilename, starOnFilename, videoID, votingStatic )
{
	if ( votingLocked || votingStatic )
		return;

	DrawStars( defRank, numStars, pathImg, starOffFilename, starOnFilename, videoID );
}

function DrawStars( rank, numStars, pathImg, starOffFilename, starOnFilename, videoID )
{
	for ( i = 1; i <= numStars; ++i )
	{
		var hilited = ( i <= rank );

		var ele = document.getElementById( "star_" + videoID + "_" + i );

		if ( hilited )
			ele.src = pathImg + "/" + starOnFilename;
		else
			ele.src = pathImg + "/" + starOffFilename;
	}
}

function VoteForVideo( videoID, vote, numStars, pathImg, starOffFilename, starOnFilename, votingStatic )
{
	if ( votingStatic )
		return;

	if ( votingLocked )
	{
		alert( "You have already voted for this video!" );
		return;
	}

	votingLocked = true;
	DrawStars( vote, numStars, pathImg, starOffFilename, starOnFilename, videoID );
	VoteSuccess();

	var url = '/index.php?show=ajaxVoteForVideo';
	new Ajax.Request(url, {
		  method: 'post',
		  parameters: "videoID=" + videoID + "&vote=" + vote,
	 	  onSuccess: function(transport) {
		  }
	});	
}

function VoteSuccess()
{
	BlinkStarRater( false );
	IncNumRatingsDiv();
	SetThanksText();
}

function BlinkStarRater( stop )
{
	var ele = document.getElementById( "starRater" );

	if ( ele == undefined )
		return;

	if ( ele.style.visibility == "hidden" )
	{
		ele.style.visibility = "visible";
		ele.style.display	 = "inline";
	}
	else
	{
		ele.style.visibility = "hidden";
		ele.style.display	 = "none";
	}

	if ( !stop )
		setTimeout( "BlinkStarRater(true);", VIDEO_VOTING_BLINK_RATE );
}

function SetThanksText()
{
	var ele = document.getElementById( "clickToVoteDiv" );

	if ( ele == undefined )
		return;

	ele.innerHTML = "Thanks for Voting!";
}

function IncNumRatingsDiv()
{
	var ele = document.getElementById( "numRatingsDiv" );

	if ( ele == undefined )
		return;

	var numRatings = ele.innerHTML;
	++numRatings;
	ele.innerHTML = numRatings;
}
