/**
 * (c) Firma JAKUBIAK 2007
 * Antoni Jakubiak
 * http://www.jakubiak.biz/
 */



/**
 * Głosowanie
 * 
 * @dependencies advAJAX
 */
function Vote(voteKeyId) {
	/**
	 * Identyfikator pliku
	 */
	this._voteKeyId = voteKeyId;
	/**
	 * Liczba gwiazdek ktore obslugujemy
	 */
	this._numOfStars = 5;
	/**
	 * Szerokosc gwiazdi w pikselach
	 */
	this._starWidth = 32;
	/**
	 * Aktualna ocena w procentach 0 - 100
	 * 0 najgorsza - 100 najlepsza
	 */
	this._rating     = 0; // 0 - 100;
}
/**
 * Inicjowanie, metoda szablonowa
 */
Vote.prototype.init = function() {
	this._repaintRating();
}
/**
 * Włącz system oceniania
 */
Vote.prototype.enableRating = function() {
	var oThis = this;
	for ( var i = 1; i <= this._numOfStars; i++ ) {
		var star = this._getStar(i);
		star.onmouseover = this._createOnMouseOverHandler(i);
		star.onmouseout  = this._createOnMouseOutHandler(i);
		star.onclick     = this._createOnClickHandler(i);
	}
}
/**
 * Wyłącz system oceniania
 */
Vote.prototype.disableRating = function() {
	this._repaintRating();
	for ( var i = 1; i <= this._numOfStars; i++ ) {
		star.onmouseover = function() {};
		star.onmouseout  = function() {};
		star.onclick     = function() {};
	}
}
/**
 * Utworzenie zdarzenia odpowiedzialnego za najechanie myszka
 */
Vote.prototype._createOnMouseOverHandler = function(starNumber) {
	var oThis = this;
	return function() { return oThis._onMouseOverHandler(starNumber); }
}
Vote.prototype._createOnMouseOutHandler = function(starNumber) {
	var oThis = this;
	return function() { return oThis._onMouseOutHandler(starNumber); }
}
Vote.prototype._createOnClickHandler = function(starNumber) {
	var oThis = this;
	return function() { return oThis._onClickHandler(starNumber); }
}
/**
 * Myszka nad gwiazdka
 * kolorowanie gwiazdek
 */
Vote.prototype._onMouseOverHandler = function(starNumber) {
	for ( var i = 1; i <= this._numOfStars; i++ ) {
		var star = this._getStar(i);
		star.className = i <= starNumber ? "starOver" : "star";
	}
}
/**
 * Myszka zjechala z gwiazdki
 * kolorowanie normalne
 */
Vote.prototype._onMouseOutHandler = function(starNumber) {
	this._repaintRating();
}
/**
 * Ocena
 */
Vote.prototype._onClickHandler = function(starNumber) {
	var votenote = parseInt(starNumber);
	var oThis = this;
	advAJAX.post({
		url: "vote-saveXml",
		parameters : {
			"votekeyid"   : this._voteKeyId,
			"votenote"    : votenote
		},
		onSuccess : function(result) {
			if( ! result.responseXML ) {
				return;
			}
			var avg = result.responseXML.getElementsByTagName("avgPercent").item(0).firstChild.nodeValue;
			oThis.setRating(avg);
		}
	});
}



/**
 * Pobranie gwiazdki
 * @access private
 * @param starNumber 1 - this._numOfStars
 */
Vote.prototype._getStar = function(starNumber) {
	var star = document.getElementById("star"+starNumber);
	return star;
}

/**
 * Ustawienie oceny zapamietanej 
 * prawdopodobnie ocena ta jest obliczona 
 * na podstawie wpisow w bazie danych
 * 
 */
Vote.prototype.setRating = function(r) {
	this._rating = r;
	this._repaintRating();
}

/**
 * Odrysowanie oceny
 * Jezeli ocena wynosi na przyklad 70%
 * i mamy 5 gwiazdek to każda gwiazdka ma po 20% 
 * 20%      20%      20%      20%      20%
 * malujemy malujemy malujemy malujemy zostawiamy
 * przy czym 4 gwiazdke malujemy tylko w polowie
 * 
 * @access privat
 */
Vote.prototype._repaintRating = function() {
	var starRating = 100 / this._numOfStars;
	
	for ( var i = 1; i <= this._numOfStars; i++ ) {
		var star = this._getStar(i);
		
		var max = i * starRating;
		var min = max - starRating;
		
		if ( this._rating > max ) {
			// cała gwiazdka jest zaznaczona
			star.className = "starSelected";
			star.style.backgroundPosition = "0px 0px";
		} else if ( this._rating > min ) {
			// gwiazdka jest zaznaczona po części
			star.className = "starSelected";
			// sprawdzamy jaka czesc gwiazdki ma byc wypelniona
			var add = ( this._rating - min ) / starRating;
			// i przeliczamy to na piksele
			var px = this._starWidth - Math.ceil( this._starWidth * add );
			star.style.backgroundPosition = "-" + px + "px 0px";
		} else {
			// gwiazdka nie jest zaznaczona
			star.className = "star";
		}
	}
}











