var isIE = (
	window.navigator.userAgent.indexOf("MSIE ") >= 0 &&
	window.navigator.userAgent.indexOf("MSIE 8") < 0
);

var nTotalBubbles = 0;

function Bubble(p, w, h, uri) {
	this.p = p;
	this.w = w;
	this.h = h;

	this.x = Math.random() * this.p.w;
	this.y = Math.random() * (this.p.h + this.h) - this.h;

	this.initMover = function() {
		this.dx = 10*(Math.random() - 0.5);
		this.dy = -4*Math.random() - 1;
		this.dn = Math.ceil(Math.random() * 7) + 3;
	};

	this.initMover();

	this.setMove = function() {
		var s = this.e.style;
		s.left = Math.round(this.x) + "px";
		s.top  = Math.round(this.y) + "px";
	}

	this.incMove = function() {
		this.x += this.dx;
		this.y += this.dy;

		if(this.x > this.p.w || this.x < -this.w || this.y < -this.h) {
			this.x = Math.random() * this.p.w;
			this.y = this.p.h - 1;
		}

		this.setMove();

		if(--this.dn <= 0)
			this.initMover();
	};

	this.e = document.createElement("img");
	this.e.setAttribute("src", uri);
	var s = this.e.style;
	s.width    = this.w + "px";
	s.height   = this.h + "px";
	s.border   = 0;
	s.position = "absolute";
	s.zIndex   = 0;
	this.p.e.appendChild(this.e);
}


function BubbleField(e) {
	this.e = e;

	this.aBubbles = new Array();

	this.addBubble = function(w, h, uri) {
		this.aBubbles.push(new Bubble(this, w, h, uri));
	};

	this.incMove = function() {
		var i;
		for(i = 0; i < this.aBubbles.length; ++i)
			this.aBubbles[i].incMove();
	};

	this.resize = function() {
		this.w = this.e.clientWidth;
		this.h = this.e.clientHeight;
	};

	this.resize();
}

function toFilteredOnIE() {
	var i;
	for(i = 0; i < document.images.length; ++i) {
		var img = document.images[i];
		if(img.src.search('\.png$') >= 0) {
			img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img.src + "')";
			img.src = pathBase + 'imgs/blank.gif';
		}
	}
}


var bf;

var idTimer = null;
function incMove() {
	bf.incMove();
	idTimer = setTimeout("incMove()", 100);
}

window.onload = function() {
	exponentJSinitialize();

	if(isIE)
		toFilteredOnIE();

	bf = new BubbleField(document.getElementById("bubble"));

	var paroline = new Array(
	    'arte',	'poesia',	'amicizia',	'musica',
	    'folklore',	'speranza',	'teatro',	'pittura',
	    'scultura',	'spettacolo'
	);

	var i;
	for(i = 0; i < paroline.length; ++i)
		bf.addBubble(108, 110, pathBase + "imgs/bb-" + paroline[i] + ".gif");

	incMove();
};

window.onresize = function() { bf.resize(); }

window.onunload = function() {
	if(idTimer != null)
		window.clearTimeout(idTimer);
}
