//comments.js

var xmlHttp;

function show_new_comment()
{
	document.getElementById('new_comment').style.display = 'block';
	location.hash = "#comments";
}

//Initiate AJAX command to server to set the rating for this instance
function set_rating(iid, score)
{
	document.getElementById('ajax-loader').style.display = 'inline';

	createXMLHttpRequest();
	
	var url = "index.php?module=TAG&func=update_rating&type=contributor&iid=" + iid  + "&rating=" + score + "&theme=Blank";
	
	xmlHttp.onreadystatechange = handleStateChangeRating;
	xmlHttp.open("GET", url, true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(null);
}

function createXMLHttpRequest(which)
{
	if(window.ActiveXObject)
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	else
		xmlHttp = new XMLHttpRequest();
}

//Handle state changes in the XMLHttpRequest object
function handleStateChangeRating()
{
	if(xmlHttp.readyState == 4)
	{
		if(xmlHttp.status == 200)
		{
			//Do stuff with the returned data
			if(xmlHttp.responseXML)
				update_rating(xmlHttp.responseXML.documentElement);
		}
	}
}

//Update display elements with result of AJAX request
function update_rating(xml)
{
	document.getElementById('ajax-loader').style.display = 'none';
	
	var my_rating, instance_avg, instance_count; 
	
	if(xml.getElementsByTagName('NewUserRating')[0].firstChild)
		my_rating = xml.getElementsByTagName('NewUserRating')[0].firstChild.nodeValue;
		
	if(xml.getElementsByTagName('NewInstanceAvg')[0].firstChild)
		instance_avg = xml.getElementsByTagName('NewInstanceAvg')[0].firstChild.nodeValue;
		
	if(xml.getElementsByTagName('NewInstanceCount')[0].firstChild)
		instance_count = xml.getElementsByTagName('NewInstanceCount')[0].firstChild.nodeValue;
		
	var new_star_class;
		
	precise_instance_avg = instance_avg;
	instance_avg = Math.round(instance_avg);
		
	if(my_rating < 2)								new_star_class = "onestar";
	else if(my_rating >= 2 && my_rating < 3)		new_star_class = "twostar";
	else if(my_rating >= 3 && my_rating < 4)		new_star_class = "threestar";
	else if(my_rating >=4 && my_rating < 5)			new_star_class = "fourstar";
	else 											new_star_class = "fivestar";
	
	if(instance_avg < 2)							new_avg_class = "onestar";
	else if(instance_avg >= 2 && instance_avg < 3)	new_avg_class = "twostar";
	else if(instance_avg >= 3 && instance_avg < 4)	new_avg_class = "threestar";
	else if(instance_avg >=4 && instance_avg < 5)	new_avg_class = "fourstar";
	else 											new_avg_class = "fivestar";
		
	var star_elt = document.getElementById('TAG_user_rating');
	var avg_elt = document.getElementById('TAG_avg_rating');
	
	star_elt.className = "rating " + new_star_class;
	avg_elt.className = "staticrating " + new_avg_class;
		
	set_node_text('numeric_avg', precise_instance_avg);
	set_node_text('rating_count', instance_count);
}

//Replaces the first child of the given element with a new text node containing new_text,
//or adds a new text node if there are no children
function set_node_text(elt_id, new_text)
{
	elt = document.getElementById(elt_id);
	
	if(!elt)
	{
//		Logger.info("set_node_text failed to find element " + elt_id);
		return;
	}
	
	newTextNode = document.createTextNode(new_text);
	
	if(elt.childNodes[0])
		elt.replaceChild(newTextNode, elt.childNodes[0]);
	else
		elt.appendChild(newTextNode);
}