function Rating (emptySrc, fullSrc, prefix, size, rating, text, total) {
	this._emptySrc = emptySrc;
	this._fullSrc = fullSrc;
	this._prefix = prefix;
	this._size = size;
	this._rating = rating;
	this._text = text;
    this._total = total;
}

Rating.prototype.getRating = function() {
	return this._rating;
}

Rating.prototype._emptySrc = "";
Rating.prototype._fullSrc = "";
Rating.prototype._prefix = ""
Rating.prototype._size = 0;
Rating.prototype._rating = 0;
Rating.prototype._text = null;
Rating.prototype._total = 0;
Rating.prototype._isOn = true;
Rating.prototype.onRate;

Rating.prototype.init = function() {
	for (var i = 0; i < this._size; i++) {
		var obj = this;
		document.getElementById(this._prefix + i).onmouseover = function() {obj.swap(this);}
		document.getElementById(this._prefix + i).onclick = function() {obj.updateRating(this);}
		document.getElementById(this._prefix + i).onmouseout = function() {obj.rollBack();}
	}

	if (this._rating > this._size) {
		alert('Rating ' + this._rating + ' cannot be more than size ' + this._size);
	} else {
		for (var i = 0; i < this._rating; i++) {
			document.getElementById(this._prefix + i).src = this._fullSrc;
		}
	}
    this.setText(this._total + ' rating(s)');
}

Rating.prototype.setText = function(txt) {
	document.getElementById(this._prefix + 'text').innerHTML = txt;
}

Rating.prototype.showText = function(pos) {
	document.getElementById(this._prefix + 'text').innerHTML = this._text[pos];
}

Rating.prototype.updateRating = function(elem) {
	var pos = parseInt(elem.id.replace(this._prefix, ""));
	var rate = 0;
	if (pos > this._size - 1) {
		alert('Position ' + pos + ' cannot be more than size ' + this._size);
	} else {
		for (var i = 0; i <= pos; i++) {
			document.getElementById(this._prefix + i).src = this._fullSrc;
			rate++;
		}
		for (var i = pos + 1; i < this._size; i++) {
			document.getElementById(this._prefix + i).src = this._emptySrc;
		}
	}
	this._rating = rate;
    this.off(false);
    this.onRate(rate, this._prefix);
    this.setText('Thanks for rating!');
}

Rating.prototype.callback = function(code) {
    this._code = code;
}

Rating.prototype.off = function(isDisable) {
	for (var i = 0; i < this._size; i++) {
        if (isDisable == true) {
            var obj = this;
            document.getElementById(this._prefix + i).onmouseover = function() {obj.setText('You have already rated.');}
            document.getElementById(this._prefix + i).onmouseout = function() {obj.setText(obj._total + ' rating(s)');}
        } else {
            document.getElementById(this._prefix + i).onmouseover = function() {}
            document.getElementById(this._prefix + i).onmouseout = function() {}
        }
		document.getElementById(this._prefix + i).onclick = function() {}
	}
    this._isOn = false;
}

Rating.prototype.rollBack = function() {
	var pos = this._rating;
	if (pos > this._size) {
		alert('Position ' + pos + ' cannot be more than size ' + this._size);
	} else {
		for (var i = 0; i < pos; i++) {
			document.getElementById(this._prefix + i).src = this._fullSrc;
		}
		for (var i = pos; i < this._size; i++) {
			document.getElementById(this._prefix + i).src = this._emptySrc;
		}
	}
    this.setText(this._total + ' rating(s)');
}

Rating.prototype.swap = function(elem) {
	var pos = parseInt(elem.id.replace(this._prefix, ""));
	if (pos > this._size - 1) {
		alert('Position ' + pos + ' cannot be more than size ' + this._size);
	} else {
		var i = 0;
		for (i = 0; i <= pos; i++) {
			document.getElementById(this._prefix + i).src = this._fullSrc;
		}
		this.showText(i - 1);
		for (i = pos + 1; i < this._size; i++) {
			document.getElementById(this._prefix + i).src = this._emptySrc;
		}
	}
}
