/*
===============================================
Preload Images
===============================================
*/
URLpath='http://www.sneakersgalore.com/';

var emptyStar = new Image();
emptyStar.src=URLpath+"func/rater/imgz/smallemptystar.png";

var fullStar = new Image();
fullStar.src=URLpath+"func/rater/imgz/smallstargreen.png";


/*
===============================================
changes images moving over
===============================================
*/
function swapRatingStar(idStar)
	{
	var numId=idStar.substr(4,1);
	for(i=1;i<6;i++)
		{
		idOther='star'+i;

		if(numId>=i)
			{
			srcImg=URLpath+"func/rater/imgz/smallstargreen.png";
			}
		else
			{
			srcImg=URLpath+"func/rater/imgz/smallemptystar.png";
			}

		$(idOther).src=srcImg;
		}

	}
	
/*
===============================================
onmouseout setting back stars to original values
===============================================
*/	
function clearStars(exRate)
	{
	for(i=1;i<6;i++)
		{
		idOther='star'+i;
		if(i<=exRate)
			{
			srcImg=URLpath+"func/rater/imgz/smallstargreen.png";
			}
		else
			{
			srcImg=URLpath+"func/rater/imgz/smallemptystar.png";
			}
		$(idOther).src=srcImg;
		}
	}
	

/*
===============================================
firing ajax-request
===============================================
*/
function updateRating(idUpdate,sId)
	{
	
	if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
		{
			var pathurl=URLpath+"func/rater/rater.php";
			var ratingValue=idUpdate.substr(4,1);
			var param="sid=" + sId + "&rate="+ratingValue;
			xmlHttp.open("POST",pathurl, true);
			xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			xmlHttp.onreadystatechange = fireNewRating;
			xmlHttp.send(param);

		}
	else
		{
		setTimeout('updateRating(idUpdate,sId)',100);
		}
	
	}


/*
===============================================
unpacking xmlResponse
===============================================
*/	
function fireNewRating(transport)
	{
	if(xmlHttp.readyState == 4)
		{
		if(xmlHttp.status == 200)
			{
			xmlResponse = xmlHttp.responseXML;
			xmlRate=xmlResponse.getElementsByTagName('newAverage');
			xmlCount=xmlResponse.getElementsByTagName('newCount');
			
			var newRate;
				for (var loop = 0; loop < xmlRate.length; loop++) 
					{
					xmlFirst=xmlRate[loop];
					newRate=xmlFirst.firstChild.nodeValue;
					} 
					
			var newCount;
				for (var loop = 0; loop < xmlCount.length; loop++) 
					{
					xmlFirst=xmlCount[loop];
					newCount=xmlFirst.firstChild.nodeValue;
					} 		

			createNewStars(newRate, newCount);
			
			}
		else
			{
			alert("Problem accessing Server: " + xmlHttp.statusText);
			}
		}

	}
	
/*
===============================================
building new dom-object and inject it
===============================================
*/	
function createNewStars(newRate, newCount)
	{
	roundedAvg=Math.round(newRate);
	rateHtml='';
	
	for(i=1;i<6;i++)
		{
		if(i<=roundedAvg)
			{
			img=URLpath+"func/rater/imgz/smallstargreen.png";
			}
		else
			{
			img=URLpath+"func/rater/imgz/smallemptystar.png";
			}
		rateHtml=rateHtml+'<img id="star'+i+'" src="'+img+'" width="30" height="29"/>';
		}
	
	rateHtml=rateHtml+'<br>&#160;&#160;&#216;-Bewertung: '+newRate;
	
	if(newCount>2)
		{
		rateHtml=rateHtml+'<br />&#160;&#160;('+newCount+' Stimmen)';
		}
		
	$('rate').update(rateHtml);	
	}	
