String.prototype.encase = function(pattern) {
	if (typeof pattern != "string")
		return this;
	var a = "a", _a = "A", z = "z", _z = "Z", str = "", pro = String.prototype, i, j, pt, ch;
	pro.__c = pro.toString;
	for (i = 0, j = Math.min(this.length, pattern.length); i < j; i++) {
		ch = this.charAt(i);
		pt = pattern.charAt(i);
		pro.__c = pt >= _a && pt <= _z ? pro.toUpperCase : pt >= a && pt <= z ? pro.toLowerCase : pro.toString;
		str += ch.__c();
	}
	str += this.substring(i).__c();
	delete pro.__c;
	return str;
}

function note(str, title) {
	if (title == null) title = "Nota";
	if (title == "")
		return overlib(str, HAUTO, VAUTO, BORDER, 1, BGCOLOR, '303030', FGCOLOR, 'B8C4F0', FGBACKGROUND, 'images/steel2.jpg', CAPCOLOR, 'C9C1A9');
	else
		return overlib(str, HAUTO, VAUTO, BORDER, 1, BGCOLOR, '303030', FGCOLOR, 'B8C4F0', FGBACKGROUND, 'images/steel2.jpg', CAPCOLOR, 'C9C1A9', CAPTION, title);
}

var voteNote = "Se questo haiku++ ti piace particolarmente, votalo. Salir&agrave; di posizione nella classifica dei pi&ugrave; ascoltati.";
var verses = new Array("primo", "secondo", "terzo");
var tricks = new Array("", "crasi", "consocrasi", "anasinalefe", "episinalefe", "compensazione", "bilocazione", "ecosillaba");
var accent = new Array("", " tronco", " sdrucciolo", " bisdrucciolo", " trisdrucciolo");

function metNote(str) {
	var r = '';
	var thisRow;
	var a = str.split(/\,/g);
	
	for (var i = 0; i < a.length && a[i].length > 0; i++) {
		thisRow = '';
		var ana = parseInt(a[i]);
		var epi = Math.round((a[i] - ana) * 10);
		if (isNaN(Number(a[i])))
			epi = a[i];
		if (ana + epi == 0) continue;
		if (ana > 0) {
			ana += '';
			for (var j = 0; j < ana.length; j++) {
				thisRow += tricks[ana.charAt(j)];
				if (ana.length > j + 2) thisRow += ', '; else thisRow += ' e ';
			}
			thisRow = thisRow.substring(0, thisRow.length - 2);
		}
		if (thisRow.length > 0)
			thisRow += "nel ";
		thisRow += verses[i] + " verso";
		if (typeof(epi) == "string")
			thisRow += " " + epi;
		else thisRow += accent[epi];
		r += thisRow.encase('A.') + ".<br>";
	}
	
	return r.length > 0 && note(r, 'Metrica');
}

String.prototype._nf = function(c, trim) {
	return this.length < c ? ("0" + this)._nf(c, trim) : trim ? this.substring(this.length - c) : this;
}

Number.prototype.format = function(c, trim) {
	return ("" + this)._nf(c, Number(trim) > 0);
}

var _nvx = 0;
var _nv = new Array("visible", "hidden");
function toggleVisibility(element, className) {
	var d = document.getElementsByTagName(element);
	for (var i = 0; i < d.length; i++)
		if (d[i].className == className)
			d[i].style.visibility = _nv[_nvx % _nv.length];
	_nvx++;
	return false;
}

var _ndx = 0;
var _nd = new Array("block", "none");
function toggleDisplay(element, className) {
	var d = document.getElementsByTagName(element);
	for (var i = 0; i < d.length; i++)
		if (d[i].className == className)
			d[i].style.display = _nd[_ndx % _nd.length];
	_ndx++;
	return false;
}

var rollovered;
var activated;
var activatedAnimation;

function activate(id) {
	if (activated) deactivate();
	activated = document.getElementById(id);
	activatedAnimation = document.getElementById('playing' + id);
	activated.style.backgroundAttachment = "scroll";
	activated.style.borderColor = "#A0A0A0";
	activated.style.borderStyle = "solid";
	activatedAnimation.style.visibility = "visible";
	bgsound.location = "bgsound.php?seed=" + Math.random() + "&src=" + id;
	return false;
}

function soundLoaded() {
	activatedAnimation.style.visibility = "hidden";
}

function deactivate() {
	if (!activated) return;
	activated.style.backgroundAttachment = "fixed";
	activated.style.borderColor = "#383838";
	activated.style.borderStyle = "dashed";
	activatedAnimation.style.visibility = "hidden";
	activated = activatedAnimation = null;
}

function prepare(obj) {
	rollovered = obj;
	if (obj != activated)
		obj.style.borderColor = "#5467B9";
}

function unprepare(obj) {
	rollovered = null;
	if (obj != activated)
		obj.style.borderColor = "#383838";
}

