var xmlHttp = createXmlHttp();

function getMouseOverEvent(id, index)
{
	return function()
	{
		for (var i = 0; i < 5; i ++) {
			var link = document.getElementById(id + '_' + (i + 1));
			if (link) {
				link.className = (i <= index ? 'on' : '');
			}
		}
	};
}

function getMouseOutEvent(id)
{
	return function()
	{
		for (var i = 1; i <= 5; i ++) {
			var link = document.getElementById(id + '_' + i);
			if (link) {
				link.className = '';
			}
		}
	}
}

function getClickEvent(id, rating, action)
{
	return function(e)
	{
		e = e || window.event;
		if (e.preventDefault) {
			e.preventDefault();
		} else {
			e.returnValue = false;
		}
		submitRating(id, rating, action);		
		return false;
	}
}

function parseSubmitRating(id)
{
	if (xmlHttp.readyState == 4) {		
		try {
			if (xmlHttp.responseText.length) {
				var ratings = parseXML(xmlHttp.responseText);
				if (ratings) {				
					var el = ratings.getElementsByTagName('rank');
					if (el && el.length) {					
						var avg = el[0].getElementsByTagName('average');
						var cnt = el[0].getElementsByTagName('count');
						var yours = el[0].getElementsByTagName('yours');
						var valid = el[0].getElementsByTagName('valid');
						if (avg && avg.length && cnt && cnt.length && yours && yours.length && valid && valid.length) {
							var articleRank = avg[0].firstChild.nodeValue;
							var rankCount = cnt[0].firstChild.nodeValue;
							var yourRank = yours[0].firstChild.nodeValue;
							var validRank = valid[0].firstChild.nodeValue;
							if(eval(validRank) == 0)							
								document.getElementById(id + '_text').innerHTML = "You have already rated this article";
							else
								setSubmittedRating(id, articleRank, rankCount, yourRank);
						}
					}	
				}
			} else {
				throw "Invalid response";
			}
		} catch (e) {
			// Not valid XML
			var formTitle = document.getElementById(id + '_text');			
			if (formTitle) {
				formTitle.innerHTML = 'An error occurred. Please try again later.';
			}
		}
	}
}

function setSubmittedRating(id, articleRank, rankCount, yourRank)
{
	var formContainer = document.getElementById(id);
	var postedContainer = document.getElementById(id + '_post');	
	if (formContainer && postedContainer) {
		for (var i = 1; i <= 5; i ++) {
			var link = document.getElementById(id + '_post_' + i);
			if (link) {
				link.className = (i <= yourRank ? 'on' : '');
			}
			
			var link = document.getElementById(id + '_post_avg_' + i);
			if (link) {
				link.style.display = (i <= articleRank ? 'block' : 'none');
			}
		}
		
		var avgSpan = document.getElementById(id + '_post_avg');
		if (avgSpan) {
			avgSpan.innerHTML = articleRank;
		}
		
		var cntSpan = document.getElementById(id + '_post_cnt');
		if (cntSpan) {
			cntSpan.innerHTML = '(' + rankCount + ' rating' + (rankCount == 1 ? '' : 's') + ')';
		}
		
		formContainer.style.display = 'none';
		postedContainer.style.display = 'block';					
	}
}

function getParseSubmitRating(id)
{	
	return function() { parseSubmitRating(id); }
}

function submitRating(id, rating, action)
{
	var formBody = document.getElementById(id + '_form');
	var formTitle = document.getElementById(id + '_text');
	if (formBody) {
		formBody.style.visibility = 'hidden';
	}
	if (formTitle) {
		formTitle.innerHTML = 'Submitting vote...';
	}
	
	xmlHttp.open('POST', action, true);
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');	
	xmlHttp.setRequestHeader("charset", "UTF-8");
	xmlHttp.onreadystatechange = getParseSubmitRating(id);
	xmlHttp.send('rank=' + rating);	
}

function setRankingLinkActions(id, action)
{
	var container = document.getElementById(id);
	if (container) {
		if (document.all) {
			container.attachEvent('onmouseout', getMouseOutEvent(id));
		} else {
			container.addEventListener('mouseout', getMouseOutEvent(id), false);
		}

		var links = container.getElementsByTagName('LI');
		for (var i = 0; i < links.length; i ++)
		{
			// Add event handlers to each link
			if (document.all) {
				links[i].attachEvent('onmouseover', getMouseOverEvent(id, i));
				links[i].attachEvent('onclick', getClickEvent(id, i + 1, action));
			} else {
				links[i].addEventListener('mouseover', getMouseOverEvent(id, i), false);
				links[i].addEventListener('click', getClickEvent(id, i + 1, action), false);
			}
		}
	}
}

