var ti, ltrs = new Array(), current_link;

document.observe("dom:loaded", function() {

	$("links").observe("mouseover", function(event) { var a = event.findElement("A"); if((a&&current_link&&current_link==a)||!a.hasClassName("link")) return; current_link=a; shutters(true,a); });
	$("links").observe("mouseout", function(event) {
		var a = event.findElement("A"), e = {x:event.pointerX()-a.offsetLeft, y:event.pointerY()-a.offsetTop};
		if(!a||!current_link||(e.x>0&&e.x<a.clientWidth&&e.y>0&&e.y<a.clientHeight)) return;
		current_link = null;
		shutters(false,a);
	});
});

function shutters(dir,a) {
	window.clearInterval(ti);
	
	if(dir) {
		while (ltrs.length>0) ltrs.shift().remove();
		ti = window.setInterval(openShutters,50);	
	}
	else {
		ti = window.setInterval((ltrs.length>0)?flyAway:closeShutters,50);
	}

	var b = $("dbody");
	var maxlength = Math.min(document.body.offsetWidth,1080);	

	function openShutters() {
		if (b.offsetWidth+30<maxlength) b.style.width = (b.offsetWidth+30)+"px";
		else {
			b.style.width = maxlength + "px";	
			window.clearInterval(ti);
			if (maxlength>=960) {
				createText(a.innerHTML.unescapeHTML().replace("&nbsp;","~"));
				ti = window.setInterval(fly,25);
			}
		}
	}
	function closeShutters() {
		if (b.offsetWidth-30>800) b.style.width = (b.offsetWidth-30)+"px";
		else {
			b.style.width = "800px";	
			window.clearInterval(ti);
		}
	}
}



function createText(txt) {

	var words = $w(txt.toUpperCase()), mlen = words.max(function(w) { return w.length}), parts=[], part, len = (mlen>8)?14:8, unit=(len==8)?70:40, iadd=(len==8)?"":"_S";
	while(words.length>0) {
		part = words.shift();
		while(words.length>0&&part.length+1+words[0].length<=len) part+= " " + words.shift();
		parts.push(part);
	}

	var d, left;
	parts.each(function(part,i) {
		left = (document.body.offsetWidth - unit*parts.length)/2+i*unit;
		part.toArray().each(function(t,ii) {
			d = new Element("div").addClassName("c").setStyle({backgroundImage:"url(img/pattern_"+parseInt(Math.random()*8+1)+iadd+".jpg)"});						
			if(len==14) d.addClassName("s");
			d.unit = unit;
			d.inpart = ii;
			d.speed = parseInt(15 + Math.random()*15);			
			d.top = function() { return parseInt(this.style.top); }
			d.position = function(top, left) { this.style.top = parseInt(top)+"px"; if(typeof(left)=="number") this.style.left = parseInt(left)+"px";}
			d.move = function() { this.style.top = this.top()+this.speed+"px";}			
			ltrs.push(d.update("<div class='ctext"+((len==14)?" s":"")+"'>"+t.replace("~"," ")+"<"+"/div>"));
			document.body.insert(d);
			d.position(100,left);
		});
	});
}

function fly() {
	var finished = 0;
	ltrs.each(function(l) {
		if(l.top()<(100+l.inpart*l.unit-l.speed)) l.move();
		else {
			l.position((100+l.inpart*l.unit));
			finished++;
		}
	});

	if (finished==ltrs.length) window.clearInterval(ti);
}
function flyAway() {

	ltrs.each(function(l,i) {
		l.move();
		if(l.top()>$("divider").offsetTop-l.unit) {
			ltrs.splice(i,1)[0].remove();
		}
	});
	if (ltrs.length==0) shutters(false,null);
}

