// ************************************************************
// Voting Settings
// ************************************************************
var voteManager = new AjaxManager('voteManager'); // Manager for posting ratings
var voteUrl = "ajax/addVote.aspx";                // Ajax voting URL

var voteName = 'stars';         // Prefix for the span/div containing the stars
var voteImageName = 'starsImg'; // Prefix for the image to be resized

var voteWidth = 85;                     // 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 voteType = 0;       // Current mouse over type
var voteRating = 0;     // Current rating
var voteObj = null;     // Current voting div/span
var voteImageObj = null // Current voting image
var voteRedraw = false; // Allows/Denies redraw of image

var voteTimeout = -1;   // Timeout for "smoother" scrolling

// ************************************************************
// Voting MouseOver Methods
// ************************************************************


function Rating_Vote_Click_Extended()
{
	myManager.Add("ajax/getLoginXML.aspx?action=purchase", ratingVoteParser, null, null, null, null);
}
function ratingVoteParser()
{
	if(this.httpRequest.status == 200)
	{
		var html = this.httpRequest.responseText;
		
		if (html == "ok")
		{			
			Rating_Vote_Click();
		}
		else if(html == "deslogado")
		{
			//alert('not logged');
			CreateCreditNotify("Por favor efetue o Login.")
		}
	}
}
function Rating_Vote_Click() 
{
	// Updates the onmouseover event to the new constructor
	voteObj.onmouseover = new Function('Vote_OnMouseOver(' + voteId + ', ' + voteType + ', false)');
	
	frmRef = document.forms["ReviewDataForm"];
	if(frmRef.ReviewNote)
	{
		frmRef.ReviewNote.value = voteRating;
	}

	// Forces an update of the star to the saved rating
	Vote_UpdateDiv(voteRating);

	// Clears the variables
	voteId = 0;
	voteType = '';
	voteRating = 0;
	voteObj = null;
	voteImageObj = null;
	voteRedraw = false;
}

function Vote_Click_Extended()
{
	myManager.Add("ajax/getLoginXML.aspx?action=purchase", voteParser, null, null, null, null);
}
function voteParser()
{
		if(this.httpRequest.status == 200)
	{
		var html = this.httpRequest.responseText;
		
		if (html == "ok")
		{			
			Vote_Click();
		}
		else if(html == "deslogado")
		{
			//alert('not logged');
			CreateCreditNotify("Por favor efetue o Login.")
		}
	}
}

function Vote_Click() 
{
	// Updates the onmouseover event to the new constructor
	voteObj.onmouseover = new Function('Vote_OnMouseOver(' + voteId + ', ' + voteType + ', false)');
	
	// Stores the vote
	var url = voteUrl + '?rating=' + voteRating + '&id=' + voteId + '&type=' + voteType;
	var script = 'CreateVoteNotify();';
	voteManager.Add(url,new Function(script));
	
	// Forces an update of the star to the saved rating
	Vote_UpdateDiv(voteRating);
	
	// Clears the variables
	voteId = 0;
	voteType = '';
	voteRating = 0;
	voteObj = null;
	voteImageObj = null;
	voteRedraw = false;
}

// Gets the absolute left of the DIV containing the stars
function Vote_GetLeft() 
{
	if(voteObj)
	{
		var left = voteObj.offsetLeft;
		aux = voteObj.offsetParent;
		while (aux)
		{
			left += aux.offsetLeft;
			aux = aux.offsetParent;
		}
		return(left)
	}
	return(0);
}

function Vote_OnMouseOver(id, type, redraw)
{
	// Clears any pending timeouts
	if(voteTimeout>-1)
	{
		window.clearTimeout(voteTimeout);
		voteTimeout = -1;
	}

	// Clears items if we changed places
	if(voteId!=id || voteType!= type)
	{
		Vote_Clear();
	}

	// Stores the current mouseover object
	voteId = id;
	voteType = type;
	voteObj = document.getElementById(voteName + '_' + voteType + '_' + voteId);
	voteImageObj = document.getElementById(voteImageName + '_' + voteType + '_' + voteId);
	voteRedraw = redraw;

	// Enables the mousemove
	document.onmousemove = Vote_OnMouseMove;
}

function Vote_OnMouseOut()
{
	voteTimeout = window.setTimeout('Vote_Clear();',10);
}

function Vote_Clear()
{
	// Clears the current information
	if(voteRedraw)
	{
		Vote_UpdateDiv(0);
	}
	voteRating = 0;
	voteId = 0;
	voteType = '';
	voteObj = null;
	voteImageObj = null;
	voteRedraw = false;
		
	// 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
			// Declared in FloatingMenu.js
			GetMousePosition(e);
		
			// Checks what's the current mouseover star
			var newVote = Math.ceil((currentMouseX-left)/votePixels);
			
			// 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(voteImageObj && newVote>-1 && newVote<=voteStars)
	{
		// Updates the star	
		voteImageObj.style.width = votePixels * newVote + 'px';
	}
}

// ************************************************************
// Voting MouseOver Methods
// ************************************************************

var voteNotifyDiv = null;
var voteNotifyTimeout = -1;
var voteNotifyOffsetX = -60;
var voteNotifyOffsetY = -20;
var voteOnlyOne = true;

function CreateVoteNotify()
{
	// Clears any pending timeouts
	if(voteNotifyTimeout!=-1)
	{
		window.clearTimeout(voteNotifyTimeout);
	}

	if(!voteNotifyDiv)
	{
		// Creates a div
		voteNotifyDiv = document.createElement('div');
		voteNotifyDiv.className = 'loadingNotify'; //class that contains div style
		voteNotifyDiv.style.position = 'Absolute';

		document.body.appendChild(voteNotifyDiv);
	}

	// Updates the position
	voteNotifyDiv.style.left = currentMouseX + voteNotifyOffsetX + 'px';
	voteNotifyDiv.style.top  = currentMouseY + voteNotifyOffsetY + 'px';
	
	// Updates the text	
	if(voteOnlyOne)
	{
		voteNotifyDiv.innerHTML = 'Voto computado';
		voteOnlyOne = false;
	}
	else
	{
		voteNotifyDiv.innerHTML = 'Você já votou';
	}
	
	// Schedules the notification destruction
	voteNotifyTimeout = window.setTimeout('DestroyVoteNotify()',2000);
}

function DestroyVoteNotify()
{
	// Destroys the div and resets the parameters
	document.body.removeChild(voteNotifyDiv);
	voteNotifyDiv = null;
	voteNotifyTimeout = -1;
}



var myManager = new AjaxManager("myManager", false);
function deslogado()
{
		document.onmousemove = GetMousePosition;
	myManager.Add("ajax/getLoginXML.aspx?action=purchase", deslogadoParser, null, null, null,0);
}

function deslogadoParser()
{
	if(this.httpRequest.status == 200)
	{	
			CreateCreditNotify("Por favor efetue o Login.")			
	}
}


// ************************************************************
// Notify
// ************************************************************

// Starts the monitoring thread - scripts.js
document.onmousemove = GetMousePosition;

var loginNotifyDiv = null;
var loginNotifyTimeout = -1;
var loginNotifyOffsetX = -35;
var loginNotifyOffsetY = -28;

function CreateCreditNotify(text)
{
	// Clears any pending timeouts
	if(loginNotifyTimeout!=-1)
	{
		window.clearTimeout(loginNotifyTimeout);
	}

	if(!loginNotifyDiv)
	{
		// Creates a div
		loginNotifyDiv = document.createElement('div');
		loginNotifyDiv.className = 'loadingNotify2'; //class that contains div style
		loginNotifyDiv.style.position = 'Absolute';
		
		document.body.appendChild(loginNotifyDiv);
	}

	// Updates the position
	loginNotifyDiv.style.left = currentMouseX + loginNotifyOffsetX + 'px';
	loginNotifyDiv.style.top  = currentMouseY + loginNotifyOffsetY + 'px';
	
	// Updates the text	
	loginNotifyDiv.innerHTML = text;
	
	// Schedules the notification destruction
	loginNotifyTimeout = window.setTimeout('DestroyLoginNotify()', 2000);
}

function CreateCreditNotify2(text)
{
	// Clears any pending timeouts
	if(loginNotifyTimeout!=-1)
	{
		window.clearTimeout(loginNotifyTimeout);
	}

	if(!loginNotifyDiv)
	{
		// Creates a div
		loginNotifyDiv = window.opener.document.createElement('div');
		loginNotifyDiv.className = 'loadingNotify2'; //class that contains div style
		loginNotifyDiv.style.position = 'Absolute';
		
		window.opener.document.body.appendChild(loginNotifyDiv);
	}

	// Updates the position
	loginNotifyDiv.style.left = '770px';
	loginNotifyDiv.style.top  = '150px';
	
	// Updates the text	
	loginNotifyDiv.innerHTML = text;
	
	// Schedules the notification destruction
	loginNotifyTimeout = window.setTimeout('DestroyLoginNotify2()', 2000);
}

function CreateCreditNotify3()
{
	// Clears any pending timeouts
	if(loginNotifyTimeout!=-1)
	{
		window.clearTimeout(loginNotifyTimeout);
	}

	if(!loginNotifyDiv)
	{
		// Creates a div
		loginNotifyDiv = document.createElement('div');
		loginNotifyDiv.className = 'loadingNotify2'; //class that contains div style
		loginNotifyDiv.style.position = 'Absolute';
		
		document.body.appendChild(loginNotifyDiv);
	}

	// Updates the position
	loginNotifyDiv.style.left = loginNotifyOffsetX + 'px';
	loginNotifyDiv.style.top  = loginNotifyOffsetY + 'px';
	
	// Updates the text	
	loginNotifyDiv.innerHTML = "Por favor efetue o Login.";
	
	// Schedules the notification destruction
	//loginNotifyTimeout = window.setTimeout('DestroyLoginNotify()', 2000);
}
function CreateCreditNotify4()
{
	// Clears any pending timeouts
	if(loginNotifyTimeout!=-1)
	{
		window.clearTimeout(loginNotifyTimeout);
	}

	if(!loginNotifyDiv)
	{
		// Creates a div
		loginNotifyDiv = document.createElement('div');
		loginNotifyDiv.className = 'loadingNotify2'; //class that contains div style
		loginNotifyDiv.style.position = 'Absolute';
		
		document.body.appendChild(loginNotifyDiv);
	}

	// Updates the position
	loginNotifyDiv.style.left = '770px';
	loginNotifyDiv.style.top  = '150px';
	
	// Updates the text	
	loginNotifyDiv.innerHTML = "Por favor efetue o Login.";
	
	// Schedules the notification destruction
	loginNotifyTimeout = window.setTimeout('DestroyLoginNotify4()', 2000);
}
function DestroyCreditNotify()
{
	// Destroys the div and resets the parameters
	document.body.removeChild(loginNotifyDiv);
	loginNotifyDiv = null;
	loginNotifyTimeout = -1;
}
function DestroyLoginNotify4()
{
	// Destroys the div and resets the parameters
	document.body.removeChild(loginNotifyDiv);
	loginNotifyDiv = null;
	loginNotifyTimeout = -1;
}
