/*
###
Needs this HTML to work on (380 is the Article-ID):
<div class="ratingbox" >';
	<img src="fileadmin/rating/stars/starFull.gif" id="380-1"  onmouseover="saveRating(this);showMousePos(this, 1);" onmouseout="restoreRating(this);" onclick="rate(this, 380, 1)" />';
	<img src="fileadmin/rating/stars/starFull.gif" id="380-2"  onmouseover="saveRating(this);showMousePos(this, 2);" onmouseout="restoreRating(this);" onclick="rate(this, 380, 2)" />';
	<img src="fileadmin/rating/stars/starFull.gif" id="380-3"  onmouseover="saveRating(this);showMousePos(this, 3);" onmouseout="restoreRating(this);" onclick="rate(this, 380, 3)" />';
	<img src="fileadmin/rating/stars/starHalf.gif" id="380-4"  onmouseover="saveRating(this);showMousePos(this, 4);" onmouseout="restoreRating(this);" onclick="rate(this, 380, 4)" />';
	<img src="fileadmin/rating/stars/starEmpty.gif" id="380-5"  onmouseover="saveRating(this);showMousePos(this, 5);" onmouseout="restoreRating(this);" onclick="rate(this, 380, 5)" />';
</div> <!-- class="ratingbox" -->';
###
*/
var theurl = "fileadmin/rating/TippTrickRating.php";
var actDiv;
var theRes;
var textStateSave;
starStateSave = new Array ();

// ### Init AJAX communication
var xmlHttpObject = false;
// if XMLHttpRequest-class available: create request object for IE7, Firefox, etc.
if (typeof XMLHttpRequest != 'undefined') 
{
    xmlHttpObject = new XMLHttpRequest();
}
// if there wasn't a request object created before: try to create it for older ms browsers (ie6, ie5, etc.)
if (!xmlHttpObject) 
{
    try 
    {
        xmlHttpObject = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e) 
    {
        try 
        {
            xmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e) 
        {
            xmlHttpObject = null;
        }
    }
}

// ### Saves the actual rating for all stars into starStateSave
function saveRating (actImg) {
	textStateSave = actImg.parentNode.getElementsByTagName("p")[0].innerHTML;
	theImgs = actImg.parentNode.getElementsByTagName("img");
	for (i = 0; i < theImgs.length; i++) {
		starStateSave[i] = theImgs[i].getAttribute("src");
	}
}

// ### Restores the actual rating for all stars into starStateSave
function restoreRating (actImg) {
	actImg.parentNode.getElementsByTagName("p")[0].innerHTML = textStateSave;
	actImg.parentNode.getElementsByTagName("p")[0].setAttribute("class", "");
	theImgs = actImg.parentNode.getElementsByTagName("img");
	for (i = 0; i < theImgs.length; i++) {
		theImgs[i].setAttribute("src", starStateSave[i]);
	}
}

// ### Set all stars like if the user would rate exactly the actual star (actImg)
function showMousePos (actImg, z) {
	theImgs = actImg.parentNode.getElementsByTagName("img");
	switch (z) {
		case 1:
			// alert("z ist 1. Wert p: -" + actImg.parentNode.getElementsByTagName("p")[0].innerHTML + "-.");
			actImg.parentNode.getElementsByTagName("p")[0].innerHTML = 'schlecht';
			actImg.parentNode.getElementsByTagName("p")[0].setAttribute("class", "hovered");
			break;
		case 2:
			// alert("z ist 2");
			actImg.parentNode.getElementsByTagName("p")[0].innerHTML = 'ausreichend';
			actImg.parentNode.getElementsByTagName("p")[0].setAttribute("class", "hovered");
			break;
		case 3:
			// alert("z ist 3");
			actImg.parentNode.getElementsByTagName("p")[0].innerHTML = 'okay';
			actImg.parentNode.getElementsByTagName("p")[0].setAttribute("class", "hovered");
			break;
		case 4:
			// alert("z ist 4");
			actImg.parentNode.getElementsByTagName("p")[0].innerHTML = 'gut';
			actImg.parentNode.getElementsByTagName("p")[0].setAttribute("class", "hovered");
			break;
		case 5:
			// alert("z ist 5");
			actImg.parentNode.getElementsByTagName("p")[0].innerHTML = 'super';
			actImg.parentNode.getElementsByTagName("p")[0].setAttribute("class", "hovered");
			break;
		default:
			// alert("z ist hä?");
			actImg.parentNode.getElementsByTagName("p")[0].innerHTML = 'unentschieden';
			actImg.parentNode.getElementsByTagName("p")[0].setAttribute("class", "hovered");
			break;
	}
	for (i = 0; i < theImgs.length; i++) {
		if (i < z) {
			theImgs[i].setAttribute("src", "fileadmin/rating/stars/starFullHov.gif")
		} else {
			theImgs[i].setAttribute("src", "fileadmin/rating/stars/starEmpty.gif")
		}
	}
}

// ### Transfer the rating value to the server, get the new content for the actual div, Call saveRating
function rate (actImg, aId, starId) {
	actDiv = actImg.parentNode;
	xmlHttpObject.open("get", theurl + "?aId="+aId+"&starId="+starId);
	xmlHttpObject.onreadystatechange = handleResponse;
	xmlHttpObject.send(null);
}

function handleResponse() {
	if(xmlHttpObject.readyState == 4){
		theRes = xmlHttpObject.responseText.split("<<-->>");
		actDiv.innerHTML = theRes[0];
		window.setTimeout("updStars ()", 3000);
	}
}

function updStars () {
	actDiv.innerHTML = theRes[1];
}
