//Autumn leaves- by Kurt Grigg (kurt.grigg@virgin.net)
//Modified by Dynamic Drive for NS6 functionality
//visit http://www.dynamicdrive.com for this script
var animazioneOn = false;
//Pre-load your image below!
imgEffetto = new Image();
imgEffetto.src = img = "";
elementToAnimate=8; //Smoothness depends on image file size, the smaller the size the more you can use!
Ypos=new Array();
Xpos=new Array();
Speed=new Array();
Step=new Array();
Cstep=new Array();
ns=(document.layers)?1:0;
ns6=(document.getElementById&&!document.all)?1:0;
for (i = 0; i < elementToAnimate; i++){
	if(ns){
		document.write("<layer name='animazione"+i+"' left='0' top='0' visibility='hidden'><img src="+img+"></layer>");
	}else{
		document.write('<div style="position:absolute;top:0px;left:0px;visibility: hidden;"><div style="position:relative">');
		document.write('<img id="animazione'+i+'" src="'+img+'" style="position:absolute;top:0px;left:0px">');
		document.write('</div></div>');
	}
}
var WinHeight=(ns||ns6)?window.innerHeight-40:window.document.body.clientHeight-40;
var WinWidth=(ns||ns6)?window.innerWidth-70:window.document.body.clientWidth-70;

for (i=0; i < elementToAnimate; i++){ 
	Ypos[i] = Math.round(Math.random()*WinHeight);
	Xpos[i] = Math.round(Math.random()*WinWidth);
	Speed[i]= Math.random()*5+3;
	Cstep[i]=0;
	Step[i]=Math.random()*0.1+0.05;
}

function animaOggetto(){
	var WinHeight=(ns||ns6)?window.innerHeight-40:window.document.body.clientHeight-40;
	var WinWidth=(ns||ns6)?window.innerWidth-70:window.document.body.clientWidth-70;
	var hscrll=(ns||ns6)?window.pageYOffset:document.body.scrollTop;
	var wscrll=(ns||ns6)?window.pageXOffset:document.body.scrollLeft;
	for (i=0; i < elementToAnimate; i++){
		sy = Speed[i]*Math.sin(90*Math.PI/180);
		sx = Speed[i]*Math.cos(Cstep[i]);
		Ypos[i]+=sy;
		Xpos[i]+=sx; 
		if (Ypos[i] > WinHeight){
			Ypos[i]=-60;
			Xpos[i]=Math.round(Math.random()*WinWidth);
			Speed[i]=Math.random()*5+3;
		}
		if (ns){
			document.layers['animazione'+i].visibility = 'visible';
			document.layers['animazione'+i].left=Xpos[i];
			document.layers['animazione'+i].top=Ypos[i]+hscrll;
		}
		else {
			document.getElementById("animazione"+i).style.visibility = 'visible';
			document.getElementById("animazione"+i).style.left=Math.min(WinWidth,Xpos[i]);
			document.getElementById("animazione"+i).style.top=Ypos[i]+hscrll;
		}
		Cstep[i]+=Step[i];
	}
	intervalEffetto = setTimeout('animaOggetto()',50);
	animazioneOn = true;
}

function initEffetto(name){
	if(name != "" && name != undefined && name !="noeffetto"){
		for (var z = 0; z< elementToAnimate; z++){
			var elementAnimated = document.getElementById("animazione"+z);
			var img = 'http://cartoline.auguri.it/elementi_cartoline/effetto/'+name+'.gif';
			elementAnimated.setAttribute('src',img);
		}
		if(animazioneOn == false){
			animaOggetto();
		}
	}else{
		stopEffetto();
	}
}

function stopEffetto(){
	for (var j = 0; j< elementToAnimate; j++){
		if(ns){
			document.layers['animazione'+j].visibility = 'hidden';
		}else{
			var elementAnimated = document.getElementById("animazione"+j);
			 elementAnimated.style.visibility = 'hidden';
			elementAnimated.setAttribute('src','');
		}
	}
	clearTimeout(intervalEffetto);
	animazioneOn = false;
}