// ************************************************************
// Voting Settings
// ************************************************************
var voteName = 'fdm_stars'
var descriptionDivName = 'RatingDescription';

var voteWidth = 60;                     // Total pixels for a "full" rating
var voteStars = 5;                      // Number of the stars
var votePixels = voteWidth / voteStars;

var voteId = 0;         // Current mouse over id
var voteRating = 0;     // Current rating
var voteObj = null;     // Current voting div/span
var voteRedraw = false; // Allows/Denies redraw of image

var voteTimeout = -1;   // Timeout for "smoother" scrolling
var lastVote = 0;

// ************************************************************
// Voting MouseOver Methods
// ************************************************************

function Vote_Click(isAuthorized)
{
	if (isAuthorized)
	{
		if(voteObj)
			SendUserRating(document.getElementById("FDM_USER_ID").value, document.getElementById("CONTENT_ID").value, voteRating);
	}
	else
	{
		alert('Para votar é necessário estar logado.');
	}
}

function SendUserRatingAjaxReturn()
{
	if(this.httpRequest.status == 200)
	{
		var msg = this.httpRequest.responseText;
		msg = msg.split("#");
		
		if (eval(msg[0]) == 0)
		{
			alert(msg[1]);
		}
		else
		{
			document.getElementById("ContentUserRating").innerHTML = msg[1];
			eval(msg[2]);
			alert(msg[3]);
		}
	}
}

function Vote_GetLeft() 
{
	if(voteObj)
	{
		var left = voteObj.offsetLeft;
		var aux = voteObj.offsetParent;
		while (aux)
		{
			left += aux.offsetLeft;
			aux = aux.offsetParent;
		}
		return(left)
	}
	return(0);
}

function Vote_OnMouseOver(id, redraw, last)
{	
	lastVote = last;
	// Clears any pending timeouts
	if(voteTimeout>-1)
	{
		window.clearTimeout(voteTimeout);
		voteTimeout = -1;
	}

	// Clears items if we changed places
	if(voteId!=id)
	{
		Vote_Clear();
	}

	// Stores the current mouseover object
	voteId = id;
	voteObj = document.getElementById('SpanStarId' + voteId);
	voteRedraw = redraw;

	// Enables the mousemove
	document.onmousemove = Vote_OnMouseMove;
}

function Vote_OnMouseOut()
{	
	//voteTimeout = window.setTimeout('Vote_Clear();',10);
	stars(userRating, userRatingDiv);
	
	document.onmousemove = null;
}

function Vote_Clear()
{	
	// Clears the current information
	if(voteRedraw)
	{
		Vote_UpdateDiv(0);
	}
	
	Vote_UpdateDiv(lastVote);
	
	voteRating = 0;
	voteId = 0;
	voteObj = null;
	voteRedraw = false;
	
	document.getElementById(descriptionDivName).innerHTML = "";

	// Disables the mousemove
	document.onmousemove = null;
}

function Vote_OnMouseMove(e)
{
	if(voteObj)
	{
		// Gets the left position of the div
		var left = Vote_GetLeft();

		if(left>=0)
		{
			// Calls the method to get the mouse position
			GetMousePosition(e);
		
			// Checks what's the current mouseover star
			var newVote = Math.ceil((currentMouseX-left)/votePixels);
			
			// Makes sure the rating is within valid limits
			if(newVote<1) newVote = 1;
			if(newVote>5) newVote = 5;
			
			// If the vote changed, updates the drawing
			if(voteRating!=newVote)
			{
				// Stores the vote for later usage
				voteRating = newVote;

				// Updates the screen if requested			
				if(voteRedraw) Vote_UpdateDiv(newVote);
			}
		}
	}
}

function Vote_UpdateDiv(newVote)
{
	if(voteObj && newVote>-1 && newVote<=voteStars)
	{
		// Updates the star	
		//voteImageObj.style.width = votePixels * newVote + 'px';
		var pos = ["-78px -14px", "-64px -15px", "-51px 0","-51px -15px","-38px 0px","-38px -15px","-25px 0","-25px -15px","-12px 0","-12px -15px","1px 0"];
		voteObj.style.backgroundPosition = pos[newVote * 2];
		
		document.getElementById(descriptionDivName).innerHTML = description[newVote - 1];
	}
}

// ************************************************************
// Voting MouseOver Methods
// ************************************************************

var voteNotifyDiv = null;
var voteNotifyTimeout = -1;
var voteNotifyOffsetX = -60;
var voteNotifyOffsetY = -60;

function ParseVote()
{
	if(this.httpRequest.status == 200)
	{
		CreateVoteNotify(this.httpRequest.responseText);
	}
}
