var ti, ltrs = new Array();

function loader() {
	var as = document.getElementById("links").getElementsByTagName("a");
	for(var i=0;i<as.length;i++) {
		if(as[i].className=="link") {
			as[i].onmouseover = function() { shutters(true,this); };
			as[i].onmouseout = function() { shutters(false,this); };
		}
	}
}

function shutters(dir,a) {
	window.clearInterval(ti);
	
	if(dir) {
		while (ltrs.length>0) {document.body.removeChild(ltrs[0]); ltrs.splice(0,1);}
		ti = window.setInterval(openShutters,50);	
	}
	else {
		ti = window.setInterval((ltrs.length>0)?flyAway:closeShutters,50);
	}

	var b = document.getElementById("dbody");
	var maxlength = (document.body.offsetWidth>1080)?1080:document.body.offsetWidth;	

	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.firstChild.nodeValue);
				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 texts = new Array();
	texts = txt.toUpperCase().split("");
	var l, d, d2;
	
	for (var i=0;i<texts.length;i++) {
		l = document.createTextNode(texts[i]);
		d = document.createElement("div");
		d.className="ctext";
		d.appendChild(l);		
		d2 = document.createElement("div");
		d2.className = "c";
		d2.style.backgroundImage = "url(img/pattern_"+ parseInt(Math.random()*8+1)+".jpg)";		
		d2.appendChild(d);
		ltrs.push(d2);		
	}
	for (var i=0;i<ltrs.length;i++) {
		ltrs[i].top = function() {  return parseInt(this.style.top);}
		ltrs[i].position = function(top, left) { this.style.top = parseInt(top)+"px";this.style.left = parseInt(left)+"px";}
		ltrs[i].move = function() { this.style.top = parseInt(this.style.top)+this.speed+"px";}		
		ltrs[i].speed = parseInt(15 + Math.random()*15);
		document.body.appendChild(ltrs[i]);
		ltrs[i].position(100,(document.body.offsetWidth-70)/2);
	}
}
function fly() {
	for(var i=0, finished = 0; i<ltrs.length;i++) {
		if(ltrs[i].top()<(100+i*70-ltrs[i].speed)) ltrs[i].move();
		else {
			ltrs[i].position((100+i*70),ltrs[i].style.left);
			finished++;
		}
	}
	if (finished==ltrs.length) window.clearInterval(ti);
}
function flyAway() {
	try {
		for(var i=0; i<ltrs.length;i++) {
			ltrs[i].move();
			if(ltrs[i].top()>600) {
				document.body.removeChild(ltrs[i]);
				ltrs.splice(i,1);
			}
		}
		if (ltrs.length==0) shutters(false,null);
	}
	catch(e) {
		return;
	}
}


