var _imageid = '0';

function ToggleComments(imageid)
{
	if(imageid.length > 0)
	{
		if (document.getElementById('commentContainer' + imageid).style.display == "none" || document.getElementById('commentContainer' + imageid).style.display == "")
		{
			document.getElementById('commentContainer' + imageid).style.display = "block";
			document.getElementById('showCommentLink' + imageid).innerHTML='Hide Comments';
			getImageComments(imageid);
		}
		else
		{
			document.getElementById('commentContainer' + imageid).style.display = "none";
			document.getElementById('showCommentLink' + imageid).innerHTML='View Comments';
			getImageCommentCount(imageid);
		}
	}
}
			
function ToggleAddComment(imageid)
{
	if(imageid.length > 0)
	{
		if (document.getElementById('addImageCommentContainer' + imageid).style.display == "none" || document.getElementById('addImageCommentContainer' + imageid).style.display == "")
		{
			document.getElementById('addImageCommentContainer' + imageid).style.display = "block";
			document.getElementById('addCommentLink' + imageid).innerHTML='Cancel Add Comment';
			document.getElementById('txtName' + imageid).focus();
		}
		else
		{
			document.getElementById('addImageCommentContainer' + imageid).style.display = "none";
			document.getElementById('addCommentLink' + imageid).innerHTML='Add Comment';			
		}
	}
}
			
function AddComment(imageid)
{
	if (imageid.length > 0)
	{
		if(document.getElementById('txtName' + imageid).value.length > 0)
		{
			if(document.getElementById('txtComment' + imageid).value.length > 0)
			{
				addImageComment(imageid, document.getElementById('txtName' + imageid).value, document.getElementById('txtComment' + imageid).value);
				document.getElementById('txtName' + imageid).value = '';
				document.getElementById('txtComment' + imageid).value = '';
			}
			else
			{
				alert('Please enter a comment');
			}
		}
		else
		{
			alert('Please enter a name');
		}
	}
}
			
function ToggleAddRating(imageid)
{
	if(imageid.length > 0)
		{
		if (document.getElementById('addImageRating' + imageid).style.display == "none" || document.getElementById('addImageRating' + imageid).style.display == "")
		{
			document.getElementById('addImageRating' + imageid).style.display = "block";
			document.getElementById('cmbRating' + imageid).value = 0;
			document.getElementById('imageRating' + imageid).style.display = "none";
		}
		else
		{
			document.getElementById('addImageRating' + imageid).style.display = "none";
			document.getElementById('imageRating' + imageid).style.display = "block";
		}
	}
}
			
function AddRating(imageid)
{
	if (imageid.length > 0)
	{
		if(document.getElementById('cmbRating' + imageid).value > 0)
		{
			addImageRating(imageid, document.getElementById('cmbRating' + imageid).value);
		}
		else
		{
			alert('Please select a rating');
		}
	}			
}






function getImageComments(imageid) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
	  	return;
	} 
	
	_imageid = imageid;
	document.getElementById("imageCommentContainer" + _imageid).innerHTML="Loading comments..."
	var url="/JSScripts/ajax/getImageComments.php";
	url=url+"?imageid="+imageid;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=getCommentsStateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function addImageComment(imageid, name, comment) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
	  	return;
	} 
	
	_imageid = imageid;
	var url="JSScripts/ajax/addImageComment.php";
	url=url+"?imageid="+imageid;
	url=url+"&name="+name;
	url=url+"&comment="+comment;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=addCommentStateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function getImageCommentCount(imageid) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
	  	return;
	} 
	
	_imageid = imageid;
	var url="JSScripts/ajax/getImageCommentCount.php";
	url=url+"?imageid="+imageid;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=getCommentCountStateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function addImageRating(imageid, ratingid) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
	  	return;
	} 
	
	_imageid = imageid;
	var url="JSScripts/ajax/addImageRating.php";
	url=url+"?imageid="+imageid;
	url=url+"&ratingid="+ratingid;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=addRatingStateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function getImageRatingCount(imageid) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
	  	return;
	} 
	
	_imageid = imageid;
	var url="JSScripts/ajax/getImageRatingCount.php";
	url=url+"?imageid="+imageid;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=getRatingCountStateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function getImageRatingAverage(imageid) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
	  	return;
	} 
	
	_imageid = imageid;
	var url="JSScripts/ajax/getImageRatingAverage.php";
	url=url+"?imageid="+imageid;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=getRatingAverageStateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


function getCommentsStateChanged() {
	if (xmlHttp.readyState==4) { 
		document.getElementById("imageCommentContainer" + _imageid).innerHTML=xmlHttp.responseText;
	}
}

function addCommentStateChanged() {
	if (xmlHttp.readyState==4) {
		if (xmlHttp.responseText == "true") {
			alert("Comment added");
			ToggleAddComment(_imageid);
			getImageComments(_imageid);
			if (document.getElementById('commentContainer' + _imageid).style.display == "none" || document.getElementById('commentContainer' + _imageid).style.display == "") {
				getImageCommentCount(_imageid);
			}
		} else {
			alert("Failed to add comment");	
		}
	}
}

function getCommentCountStateChanged() {
	if (xmlHttp.readyState==4) { 
		document.getElementById("showCommentLink" + _imageid).innerHTML='View Comments (' + xmlHttp.responseText + ')';
	}
}

function addRatingStateChanged() {
	if (xmlHttp.readyState==4) {
		if (xmlHttp.responseText == "true") {
			alert("Rating added");
			ToggleAddRating(_imageid);
			getImageRatingCount(_imageid);
		} else if (xmlHttp.responseText == "invalid") {
			alert("You have already rated this image and cannot rate it again");
			ToggleAddRating(_imageid);
		} else {
			alert("Failed to add rating");	
		}
	}
}

function getRatingCountStateChanged() {
	if (xmlHttp.readyState==4) {
		var plural="s";
		if (xmlHttp.responseText == "1") { plural = ""; }
		document.getElementById("ratingCount" + _imageid).innerHTML= xmlHttp.responseText + ' Rating' + plural;
		getImageRatingAverage(_imageid);
	}
}

function getRatingAverageStateChanged() {
	if (xmlHttp.readyState==4) {
		document.getElementById("imgRating" + _imageid).src = "JSScripts/utils/starrating/rating.php?rating=" + xmlHttp.responseText;
		document.getElementById("imgRating" + _imageid).alt = "Average Rating: " + xmlHttp.responseText;
	}
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try
	{
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	}
	catch (e)
  	{
  		// Internet Explorer
  		try
    	{
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
  		catch (e)
    	{
    	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
  	}
	return xmlHttp;
}
