
// global request and XML document objects
var isIE=false;
var ratingxmlhttp=false;
var ratecntxmlhttp=false;

var userRating = '-1';
var avgRating = '-1';
var count = '0';

if (window.ActiveXObject) {
 isIE = true;
}

function newRatingXMLHttpRequest() {
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
    try {
        ratingxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            ratingxmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            ratingxmlhttp = false;
        }
    }
@end @*/
    if (!ratingxmlhttp && typeof XMLHttpRequest!='undefined') {
        ratingxmlhttp = new XMLHttpRequest();
    }
    if (ratingxmlhttp) {
        ratingxmlhttp.onreadystatechange = processRateInitStateChange;
    }
}

function newRateContentXMLHttpRequest() {
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
    try {
        ratecntxmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            ratecntxmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            ratecntxmlhttp = false;
        }
    }
@end @*/
    if (!ratecntxmlhttp && typeof XMLHttpRequest!='undefined') {
        ratecntxmlhttp = new XMLHttpRequest();
    }
    if (ratecntxmlhttp) {
        ratecntxmlhttp.onreadystatechange = processRateContentStateChange;
    }
}

function processRateInitStateChange() {
    switch (ratingxmlhttp.readyState) {
        case 2:
            break;
        case 3:
            break;
        case 4:
            // only if "OK"
            if (ratingxmlhttp.status == 0 || ratingxmlhttp.status == 200) {
                var doc = ratingxmlhttp.responseXML;
                if (doc) {
                    var ratingElement = doc.getElementsByTagName("rating")[0];
		            userRating = ratingElement.getAttribute("user-score");
		            avgRating = ratingElement.getAttribute("avg-score");
		            count = ratingElement.getAttribute("count");

		            if (userRating == '-1') {
		    	        updateStars(avgRating, "star_on");
		    	        document.getElementById("ratingcount").innerHTML = "" + count + " ratings";
			        } else {
				        updateStars(userRating, "star_user");
				        document.getElementById("ratingcount").innerHTML = "your rating";
			        }
		        }
            }
            break;
    }
}

function processRateContentStateChange() {
    switch (ratecntxmlhttp.readyState) {
        case 2:
            break;
        case 3:
            break;
        case 4:
            // only if "OK"
            if (ratecntxmlhttp.status == 0 || ratecntxmlhttp.status == 200) {
	            var doc = ratecntxmlhttp.responseXML;
	            if (doc) {
	                var ratingElement = doc.getElementsByTagName("rating")[0];
	    		    userRating = ratingElement.getAttribute("user-score");
			        avgRating = ratingElement.getAttribute("avg-score");
			        count = ratingElement.getAttribute("count");

				    if (userRating == '-1') {
				        updateStars(avgRating, "star_on");
					    document.getElementById("ratingcount").innerHTML = "" + count + " ratings";
				    } else {
					    updateStars(userRating, "star_user");
					    document.getElementById("ratingcount").innerHTML = "your rating";
				    }
			    }
            }
            break;
    }
}

function initRating(contentID) {
    newRatingXMLHttpRequest();
    var path = "/webapps/rating.action?contentID=" + contentID;
    ratingxmlhttp.open("GET", path, true);
    if (isIE) {
        ratingxmlhttp.send();
    } else {
        ratingxmlhttp.send(null);
    }
}

function rateContent(contentID, score) {
	if (userRating == '-1') {
	    newRateContentXMLHttpRequest();
	    var path = "/webapps/rating-rate.action?contentID=" + contentID + "&rating=" + score;
	    ratecntxmlhttp.open("GET", path, true);
	    if (isIE) {
	        ratecntxmlhttp.send();
	    } else {
	        ratecntxmlhttp.send(null);
	    }
    }
}

function ratingHover( hovering, checkValue )
{
	if ( hovering ) {
		if (userRating == '-1') {
			updateStars(checkValue, "star_user");
		}
	} else {
		if (userRating == "-1") {
			updateStars(avgRating, "star_on");
		} else {
			updateStars(userRating, "star_user");
		}
	}
}

function ratingSwitch( hovering )
{
	if (userRating != '-1') {
		if ( hovering ) {
			updateStars(avgRating, "star_on");
			document.getElementById("ratingcount").innerHTML = "" + count + " ratings";
		} else {
			updateStars(userRating, "star_user");
			document.getElementById("ratingcount").innerHTML = "your rating";
		}
	}
}

function updateStars(checkValue, imgName)
{
	var starImgName1 = "star_off";
	var starImgName2 = "star_off";
	var starImgName3 = "star_off";
	var starImgName4 = "star_off";
	var starImgName5 = "star_off";

	switch (checkValue) {
		case '5':
			starImgName5 = imgName;
		case '4':
			starImgName4 = imgName;
		case '3':
			starImgName3 = imgName;
		case '2':
			starImgName2 = imgName;
		case '1':
			starImgName1 = imgName;
			break;
	}
	document.getElementById("cntrating1").src = "/img/ratings/" + starImgName1 + ".jpg";
	document.getElementById("cntrating2").src = "/img/ratings/" + starImgName2 + ".jpg";
	document.getElementById("cntrating3").src = "/img/ratings/" + starImgName3 + ".jpg";
	document.getElementById("cntrating4").src = "/img/ratings/" + starImgName4 + ".jpg";
	document.getElementById("cntrating5").src = "/img/ratings/" + starImgName5 + ".jpg";
}
