/*########################################################################
#                                                                        #
#                            SnowFlakes                                  #
#                                                                        #
#                    Heislitz Creative Solutions						 #
#						 Autor: Matthias Will							 #
#                                                                        #
#                           (c) 2010                                     #
#                                                                        #
#                      Alle Rechte vorbehalten.                          #
#                                                                        #
########################################################################*/

var ROOT, SNOW_LAYER, WIN_WIDTH, WIN_HEIGHT, FLAKECOUNT, INTERVALTIME;
var MAX_STARTTIME, MELT_TIME, SIZES, OFFSET_BOTTOM, IS_SAFARI;
var flakes, startcounter, wind_direction;

function SnowFlake(sign){
	
	this.setPosition = function(){
		this.shape.style.top = this.y + 'px';
		this.shape.style.left = this.x + 'px';
	}
	
	//------------------------------------------------------------
	
	this.fall = function(){
		if(startcounter < this.start){
			return;
		}else{
			this.shape.style.display = 'block';
		}
		
		var direction, newx, newy;
		
		//oben
		if(this.y < (WIN_HEIGHT - this.height - OFFSET_BOTTOM)){
			this.y += 1;
			
			direction = Math.random();
		
			//links
			if(direction < 0.333 && this.x > 0){
				this.x -= 1;
			}
			
			//rechts
			if(direction > 0.666 && this.x < WIN_WIDTH - this.width){
				this.x += 1;
			}
			
			//wind addieren
			this.x += Math.round(wind_direction * ((this.z / 2) + 0.5));
		}else{
			if(this.melttime > 0){
				this.melttime -= 1;
				this.shape.style.opacity = this.z * (this.melttime/MELT_TIME);
			}else{
				this.melttime = MELT_TIME;
				if(IS_SAFARI){
					this.shape.style.opacity = this.z;
				}else{
					this.shape.style.opacity = 1;
				}
				this.y = this.height * -1;
				this.x = Math.round((WIN_WIDTH - this.width) * Math.random());
			}
		}
		
		if(this.x > WIN_WIDTH - this.width){
			this.x -= WIN_WIDTH - this.width;
		}else if(this.x <= 0){
			this.x += WIN_WIDTH - this.width;
		}
		
		this.setPosition();
	}
	
	//############################################################
	
	var sizeindex;
	
	this.z = Math.random();
	sizeindex = Math.round(this.z * (SIZES.length - 1));
	
	this.shape = document.createElement('div');
	this.shape.style.fontSize = SIZES[sizeindex] + 'px';
	this.shape.style.color = '#FFF';
	this.shape.style.position = 'absolute';
	this.shape.style.zIndex = sizeindex + 1;
	this.shape.style.cursor = 'default';
	if(IS_SAFARI){
		this.shape.style.opacity = this.z;
	}
	this.shape.innerHTML = sign;
	SNOW_LAYER.appendChild(this.shape);
	
	this.width = this.shape.offsetWidth;
	this.height = this.shape.offsetHeight;
	
	this.shape.style.display = 'none';
	
	this.x = Math.round((WIN_WIDTH - this.width) * Math.random());
	this.y = this.height * -1;
	
	this.start = Math.round((MAX_STARTTIME * 1000 / INTERVALTIME) * Math.random());
	this.melttime = MELT_TIME;
	
	this.setPosition();
}

//----------------------------------------------------------------

function changeWind(){
	var ret;
	
	ret = wind_direction + Math.round((Math.random() - 0.5) * 2);
	
	if(Math.abs(ret) < 5){
		return ret;
	}else{
		return wind_direction;
	}
}

//----------------------------------------------------------------

function animateSnow(){	
	if(startcounter < 2147483647){
		startcounter += 1;
	}else{
		startcounter = -1;
	}
	
	for(i=0; i < FLAKECOUNT; i += 1){
		flakes[i].fall();
	}
	
	wind_direction = changeWind();
}

//----------------------------------------------------------------

function initSnow(){
	//ROOT = document.getElementsByTagName('body')[0];
	ROOT = document.getElementById('snow');
	SNOW_LAYER = document.createElement('div');
	ROOT.appendChild(SNOW_LAYER);
	
	WIN_WIDTH = ROOT.clientWidth;
	WIN_HEIGHT = ROOT.clientHeight + 10;
	
	OFFSET_BOTTOM = 100;
	
	//FLAKECOUNT = Math.round(WIN_WIDTH * WIN_HEIGHT / 5000);
	FLAKECOUNT = 250;
	INTERVALTIME = 75;
	MAX_STARTTIME = Math.round(WIN_HEIGHT * INTERVALTIME / 1000);
	MELT_TIME = 20;
	
	flakes = new Array();
	startcounter = 0;
	
	wind_direction = 0;
	
	SIZES = new Array(12, 14, 16, 18);
	
	IS_SAFARI = navigator.userAgent.indexOf('Safari') > -1;
	
	for(i=0; i < FLAKECOUNT; i += 1){
		flakes[i] = new SnowFlake('&#149;'); // Stern: &#1645;
	}
	
	setInterval('animateSnow()', INTERVALTIME);
}

//----------------------------------------------------------------

window.onload = function(){
	initSnow();
}
