// ************************************************************
// Voting Methods
// ************************************************************
var voteurl = "addVote.aspx";
function Vote_Add(content, rating)
{
	this.xmlhttp = CreateXmlHttpRequest();
	if(this.xmlhttp != null)
	{
		try
		{
			this.xmlhttp.open("GET", voteurl + "?contentid=" + content + "&rating=" + rating);
			this.xmlhttp.send(null);
		}
		catch(e)
		{
			alert('Could not open URL');
			alert(e);
		}
	}
	else
	{
		alert('XmlHttpRequest object could not be loaded');
	}
}

// ************************************************************
// Voting Settings
// ************************************************************
var divVoteName = 'voting';
var divVoteImageName = 'votingImage';
var divVoteLeft = -1;
var divVoteWidth = 45;
var divVoteStars = 5;
var currentVote = 0;
var currentVoteLocked = false;
var currentTimer = -1;

// ************************************************************
// Public Voting Methods
// ************************************************************
function Vote_GetCurrent()
{

		if(playerIg)
		{
			return(playerIg.getCurrentMediaId());
		}

}

function Vote_Click()
{
	// Locks the vote in place
	Vote_Lock();

	// Saves the vote
	Vote_Add(Vote_GetCurrent(),currentVote);
}

function Vote_Lock()
{
	// Locks the voting
	currentVoteLocked = true;

	// Disables the mousemove
	document.onmousemove = null;
}

function Vote_Unlock()
{
	// Unlocks the vote
	currentVoteLocked = false;
	
	// Resets to initial state
	Vote_UpdateDiv(0);
}

// ************************************************************
// Voting MouseOver Methods
// ************************************************************

// Gets the absolute left of the DIV containing the stars
function Vote_GetLeft() 
{
	if(divVoteLeft<0)
	{
		var obj = document.getElementById(divVoteName);
		if(obj)
		{
			divVoteLeft = obj.offsetLeft;
			aux = obj.offsetParent;
			while (aux)
			{
				divVoteLeft += aux.offsetLeft;
				aux = aux.offsetParent;
			}
		}
	}
}

function Vote_OnMouseOver(e)
{
	// Does not allow changes if it's locked
	if(!currentVoteLocked)
	{
		// Does not allow mouseover if there's no content
		if(Vote_GetCurrent()>0)
		{
			var obj = document.getElementById(divVoteName);
			if(obj)
			{
				// Blocks the div clearing from executing
				if(currentTimer>0) window.clearTimeout(currentTimer);

				// Enables the mousemove
				document.onmousemove = Vote_OnMouseMove;
			}
		}
	}
}

function Vote_OnMouseOut(e)
{
	// Does not allow changes if it's locked
	if(!currentVoteLocked)
	{
		var obj = document.getElementById(divVoteName);
		if(obj)
		{
			// Clears the vote
			currentTimer = window.setTimeout('Vote_UpdateDiv(0)',50);

			// Disables the mousemove
			document.onmousemove = null;
		}
	}
}

function Vote_OnMouseMove(e)
{
	var obj = document.getElementById(divVoteName);
	if(obj)
	{
		// Gets the left position of the div
		Vote_GetLeft();

		if(divVoteLeft>=0)
		{
			// Gets the current mouse X position
			var nn6 = document.getElementById && !document.all;
			var mouseX = nn6 ? e.clientX : event.clientX;
			
			// Gets the size in pixels of a star
			var starPixels = divVoteWidth / divVoteStars;

			// Checks what's the current mouseover star
			var newVote = Math.ceil((mouseX-divVoteLeft)/starPixels);
			
			// If the vote changed, updates the drawing
			if(currentVote!=newVote)
			{
				Vote_UpdateDiv(newVote);
			}
		}
	}
}

var count = 0;
function Vote_UpdateDiv(newVote)
{
	// Stores the vote for later usage
	currentVote = newVote;
	
	var img = document.getElementById(divVoteImageName);
	if(img)
	{
		// Gets the size in pixels of a star
		var starPixels = divVoteWidth / divVoteStars;

		// Updates the star	
		img.style.width = starPixels * currentVote + 'px';
	}
}