var http          = createRequestObject();
var currentId     = '';
var newRating     = new Array();
var imgPath       = _componentsPath+'/com_rating/images/';
var starLeft      = imgPath+'star-left.png';
var starRight     = imgPath+'star-right.png';
var starLeft_ovr  = imgPath+'star-left-o.png';
var starRight_ovr = imgPath+'star-right-o.png';
var starLeft_cur  = imgPath+'star-left-c.png';
var starRight_cur = imgPath+'star-right-c.png';


function createRequestObject () {
    var ro;
    var browser = navigator.appName;

    if (browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        ro = new XMLHttpRequest();
    }

    return ro;
} // createRequestObject


function updateRating (obj, rateId) {
	var rateId = rateId;
	var fullId = obj.id;
	var idName = fullId.substr(0, fullId.indexOf('_'));
	currentId  = idName;

    http.open('get', 'ajax.lnk.php?option=com_rating&task=update&id='+idName+'&rated='+rateId);
    http.onreadystatechange = handleResponse;
    http.send(null);
} // updateRating


function handleResponse () {
	if (http.readyState == 4) {
        var response = http.responseText;
        var index = response.indexOf(';');
        var rated = response.substr(0, index);
        var total = response.substr(index+1);

		if (rated == 'ERROR'){
        	if (total) {
            	var errMsg = total;
        	} else {
        		var errMsg = 'Sorry... Failed to update rating.';
        	}

			alert(errMsg);
		} else {
    	    document.getElementById(currentId+'_showrating').innerHTML = 'Rated '+rated+' by '+total+' votes';

        	rating = Math.ceil(rated);
            newRating[currentId] = rating;

	        var obj = document.getElementById(currentId+'_10');
    	    changeout(obj, rating, 10);

			alert('Your vote has submitted, thanks for your time.');
		}
    }
} // handleResponse


function changeover (obj, ovrId) {
	var ovrId  = ovrId;
	var fullId = obj.id;
	var idName = fullId.substr(0, fullId.indexOf('_'));

	for (i=0; i<ovrId; i++) {
		var num = i+1;
		
		if (i%2 == 0) {
			document.getElementById(idName+'_'+num).src = starLeft_ovr;
		} else {
			document.getElementById(idName+'_'+num).src = starRight_ovr;
		}
	}
} // changeover


function changeout (obj, rating, outId) {
	var outId  = outId;
	var fullId = obj.id;
	var idName = fullId.substr(0, fullId.indexOf('_'));

    rating = (newRating[idName]) ? newRating[idName] : rating;

	for (i=0; i<outId; i++) {
		var num = i + 1;

		if (i%2 == 0) {
			if (i < rating) {
				document.getElementById(idName+'_'+num).src = starLeft_cur;
			} else {
				document.getElementById(idName+'_'+num).src = starLeft;
			}
		} else {
			if (i < rating) {
				document.getElementById(idName+'_'+num).src = starRight_cur;
			} else {
				document.getElementById(idName+'_'+num).src = starRight;
			}
		}
	}
} // changeout


function displayStars (rating, idName, rated, totalVotes) {
    var t = ' out of 5';
    var showrating = 'Click and vote!';

	for (i=0; i<10; i++) {
		var num = i + 1;
		var n   = num / 2;

		if (i%2 == 0) {
			if (i < rating) {
				document.write('<img src="'+starLeft_cur+'" id="'+idName+'_'+num+'" title="'+n+t+'" onmouseout="changeout(this, '+rating+', '+num+')" onmouseover="changeover(this, '+num+')" onclick="updateRating(this, '+num+')" />');
			} else {
				document.write('<img src="'+starLeft+'" id="'+idName+'_'+num+'" title="'+n+t+'" onmouseout="changeout(this, '+rating+', '+num+')" onmouseover="changeover(this, '+num+')" onclick="updateRating(this, '+num+')" />');
			}
		} else {
			if (i < rating) {
				document.write('<img src="'+starRight_cur+'" id="'+idName+'_'+num+'" title="'+n+t+'" onmouseout="changeout(this, '+rating+', '+num+')" onmouseover="changeover(this, '+num+')" onclick="updateRating(this, '+num+')" />');
			} else {
				document.write('<img src="'+starRight+'" id="'+idName+'_'+num+'" title="'+n+t+'" onmouseout="changeout(this, '+rating+', '+num+')" onmouseover="changeover(this, '+num+')" onclick="updateRating(this, '+num+')" />');
			}
		}
	}

	if (rated != 0) {
		showrating = 'Rated '+rated+' by '+totalVotes+' votes';
	}

    document.write('<br /><div class="ratingText" id="'+idName+'_showrating">'+showrating+'</div>');
} // displayStars
