/******************************************
* Snow Effect Script- By Altan d.o.o. (snow@altan.hr, http://www.altan.hr/snow/index.html)
* Visit Dynamic Drive (http://www.dynamicdrive.com/) for full source code
* Modified Dec 31st, 02' by DD. This notice must stay intact for use
******************************************/

// Modified Oct 2005 by Martin G C Davies to make interaction with other JavaScript unlikely.
// Modified Feb 2006 by Martin G C Davies to allow images to go up as well as down.

function PSofSnowFall(snowFallName,imagePath,image,numberOfImages,speed,direction) {
	var this_name = snowFallName;
	var this_imagePath = imagePath;
	var this_imageSource = this_imagePath + "Snow1.gif";
	var this_numberOfImages = 10;
	var this_speed = 20; // smaller number moves the snow faster
	var this_direction = 1;
	var this_ns4up = (document.layers) ? 1 : 0;  // browser sniffer
	var this_ie4up = (document.all) ? 1 : 0;
	var this_ns6up = (document.getElementById&&!document.all) ? 1 : 0;

	var this_dx;	// coordinate and position variables
	var this_xp;	// coordinate and position variables
	var this_yp;	// coordinate and position variables
	var this_am;	// amplitude and step variables
	var this_stx;	// amplitude and step variables
	var this_sty;	// amplitude and step variables
	var this_i;
	var this_doc_width = 800;
	var this_doc_height = 600;
	
	if (image != "") {
		this_imageSource = this_imagePath + image;
	}
	
	if (numberOfImages != 0) {
		this_numberOfImages = numberOfImages;
	}
	
	if (speed != 0) {
		this_speed = speed;
	}

	if (direction == "up") {
		this_direction = -1;
	}

	if (this_ie4up) {
		this_doc_width = document.body.clientWidth;
		this_doc_height = document.body.clientHeight;
	}
	else if (this_ns4up||this_ns6up) {
		this_doc_width = self.innerWidth;
		this_doc_height = self.innerHeight;
	}

	this_dx = new Array();
	this_xp = new Array();
	this_yp = new Array();
	this_am = new Array();
	this_stx = new Array();
	this_sty = new Array();

	for (this_i = 0; this_i < this_numberOfImages; ++ this_i) {  
		this_dx[this_i] = 0;                        // set coordinate variables
		this_xp[this_i] = Math.random()*(this_doc_width-50);  // set position variables
		this_yp[this_i] = Math.random()*this_doc_height;
		this_am[this_i] = Math.random()*30;         // set amplitude variables
		this_stx[this_i] = 0.02 + Math.random()/10; // set step variables
		this_sty[this_i] = 0.7 + Math.random();     // set step variables
		
		if (this_direction == -1) {
			this_sty[this_i] = - this_sty[this_i];
		}
		
		if (this_ns4up) {
			document.write("<layer name=\""+ this_name + this_i +"\" left=\"15\" top=\"15\" visibility=\"show\"><img src='"+this_imageSource+"' border=\"0\"><\/layer>");
		}
		else if (this_ie4up||this_ns6up) {
			document.write("<div id=\"" + this_name + this_i +"\" style=\"POSITION: absolute; Z-INDEX: "+ this_i +"; VISIBILITY: visible; TOP: 15px; LEFT: 15px;\"><img src='"+this_imageSource+"' border=\"0\"><\/div>");
		}
	}
	
 
	// Just functions and methods below here.
	
	function restart(this_i)
	{
		this_xp[this_i] = Math.random()*(this_doc_width-this_am[this_i]-30);
		this_yp[this_i] = 0;
		this_stx[this_i] = 0.02 + Math.random()/10;
		this_sty[this_i] = 0.7 + Math.random();
		
		if (this_direction == -1) {
			this_yp[this_i] = this_doc_height - 50;
			this_sty[this_i] = - this_sty[this_i];
		}
		
		if (this_ie4up) {
			this_doc_width = document.body.clientWidth;
			this_doc_height = document.body.clientHeight;
		}
		else if (this_ns4up||this_ns6up) {
			this_doc_width = self.innerWidth;
			this_doc_height = self.innerHeight;
		}
	}
	
	this.snowIE = function() {
		for (this_i = 0; this_i < this_numberOfImages; ++ this_i) {
			this_yp[this_i] += this_sty[this_i];

			if (this_direction == 1) {
				if (this_yp[this_i] > this_doc_height-50) {
					restart(this_i);
				}
			}
			else {
				if (this_yp[this_i] < 0) {
					restart(this_i);
				}
			}
	  	
			this_dx[this_i] += this_stx[this_i];
			document.all[this_name+this_i].style.pixelTop = this_yp[this_i];
			document.all[this_name+this_i].style.pixelLeft = this_xp[this_i] + this_am[this_i]*Math.sin(this_dx[this_i]);
		}

		setTimeout(this_name + ".snowIE()", this_speed);		
	}
  
	this.snowNS = function() {
		for (this_i = 0; this_i < this_numberOfImages; ++ this_i) {
			this_yp[this_i] += this_sty[this_i];
      
			if (this_direction == 1) {
				if (this_yp[this_i] > this_doc_height-50) {
					restart(this_i);
				}
			}
			else {
				if (this_yp[this_i] < 50) {
					restart(this_i);
				}
			}
      
			this_dx[this_i] += this_stx[this_i];
			document.layers[this_name+this_i].top = this_yp[this_i];
			document.layers[this_name+this_i].left = this_xp[this_i] + this_am[this_i]*Math.sin(this_dx[this_i]);
		}
    
 		setTimeout(this_name + ".snowNS()", this_speed);		
	}

	this.snowNS6 = function() {
		for (this_i = 0; this_i < this_numberOfImages; ++ this_i) {
	  		this_yp[this_i] += this_sty[this_i];
	  	
			if (this_direction == 1) {
				if (this_yp[this_i] > this_doc_height-50) {
					restart(this_i);
				}
			}
			else {
				if (this_yp[this_i] < 50) {
					restart(this_i);
				}
			}
	  	
	  		this_dx[this_i] += this_stx[this_i];
			document.getElementById(this_name+this_i).style.top=this_yp[this_i];
			document.getElementById(this_name+this_i).style.left=this_xp[this_i] + this_am[this_i]*Math.sin(this_dx[this_i]); 
		}

		setTimeout(this_name + ".snowNS6()", this_speed);		
	}
  
	this.play = function() {
  		if (this_ie4up) {
			this.snowIE();
  		}
  		else if (this_ns4up) {
			this.snowNS();
  		}
  		else if (this_ns6up) {
			this.snowNS6();
  		}
	}
}