var resourceRated;
var prevRatingValue;
var prevRatingsCount;
function onMouseOverStars(event, outerBar, innerBarId, rated) {
	if (resourceRated == null) {
		resourceRated = rated;
	}
	if (resourceRated) {
		document.getElementById(innerBarId + "_hint").innerHTML = "&nbsp;Вы уже проголосовали";
	} else {
		//if (!event) event = window.event;
		var pos_x = getEventXCoord(event, outerBar);
		//alert(event.layerX);
		//alert(outerBar.offsetLeft);
		//var pos_y = event.offsetY?(event.offsetY):event.layerY-el.offsetTop;	
		//el.offsetHeight
		var width = parseInt(pos_x/outerBar.offsetWidth/12.5 * 100) * 12.5 + 12.5;	
		if (width > 100) {
			width = 100;
		}
		setStarsWidth(innerBarId, width);
		document.getElementById(innerBarId + "_hint").innerHTML = "&nbsp;" + ((width / 12.5) - 3);
	}
}

function onMouseOutStars(event, outerBar, prevValue, ratingsCount, innerBarId, rated) {
	if (resourceRated == null) {
		resourceRated = rated;
	}
	if (prevRatingValue == null) {
		prevRatingValue = prevValue;
	}
	if (prevRatingsCount == null) {
		prevRatingsCount = ratingsCount;
	}
	if (!resourceRated) {
		setStarsWidth(innerBarId, prevRatingValue);
	}
	document.getElementById(innerBarId + "_hint").innerHTML = "&nbsp;оценок " + prevRatingsCount;
}

function onStarsClick(event, outerBar, innerBarId, resourceType, resourceId, ratingsCount, contextPath) {
	if (resourceRated != null && resourceRated) {
		return;
	}
	var pos_x = getEventXCoord(event, outerBar);
	var mark = parseInt(pos_x/outerBar.offsetWidth/12.5 * 100) - 2;
	//var resourceId = document.getElementById('ownerId').value;
	
	var uri = 'my/ajax/rate/' + resourceType + '?rating=' + mark + '&id=' + resourceId; 
	
	Gallery.addOnSuccessHandler('performAction', function(action, itype, reqUri, response) {
		if (itype == 'ratingchange') {
			var averageRating = parseInt(response);
			var negativeMinValue = 2;
			var multiplier = 100;
			var numberOfStars = 8;
			
			var ratMax = numberOfStars * multiplier; 
			var width = parseInt(((averageRating + ((negativeMinValue + 1) * multiplier)) / parseFloat(ratMax)) * 100);
			if (width > 100) {
				width = 100;
			}
			setStarsWidth(innerBarId, width);
			
			if (prevRatingsCount == null) {
				prevRatingsCount = ratingsCount;
			}
			prevRatingsCount += 1;
			resourceRated = true;
			
			document.getElementById(innerBarId + "_hint").innerHTML = "&nbsp;Ваша оценка принята!";
			
			Gallery.setCallback('performActionOnSuccessHandler', null);
		}
	});
	Gallery.addOnErrorHandler('performAction', function(action, itype) {
		if (itype == 'ratingchange') {
			alert("Ошибка");
		}
	});
	Gallery.performAction('', 'ratingchange', uri, contextPath);
}

function setStarsWidth(innerBarId, width) {
	//var start = outerBar.innerHTML.indexOf('width');
	//var end = outerBar.innerHTML.indexOf('%');
	//var widthValue = outerBar.innerHTML.substring(start, end);
	//outerBar.innerHTML = outerBar.innerHTML.replace(widthValue, "width:" + width);
	var filledPart = "_red";
	var zeroPart = "_gold";
	if (width > 25) {
		filledPart = "_gold";
		zeroPart = "_red"; 
	}
	document.getElementById(innerBarId + zeroPart).style.width = '0%';
	document.getElementById(innerBarId + filledPart).style.width = width + '%'; 
}

function getEventXCoord(event, el) {
	if (window.ActiveXObject) {
		// ie
		return window.event.offsetX;
	} else {
		// ff
		var left = 0;
		while (el) {
			left = el.offsetLeft + left;
			el = el.offsetParent;
		}
		return event.pageX - left;
	}
	//return event.offsetX?(event.offsetX):event.layerX-el.offsetLeft;
}

function resetRatingBar() {
	resourceRated = null;
	prevRatingValue = null;
	prevRatingsCount = null;
}