(function(){
	var pics = [
		["snow_img/s_flake2.gif",22,20],
		["snow_img/s_flake3.gif",21,21],
		["snow_img/s_flake4.gif",26,29],
		["snow_img/s_flake5.gif",18,22]
		];
	var global = this;
	var numberOfflakes = 10;
	var windowState,flakeObjs, flakeHTML;
	var posMod, sy, sx, clientHeight, clientWidth,topPosition;
	var globalName = 'snowField'; //used for string argument only setTimeout fall-back.
	var widths = new Array(numberOfflakes),heights = new Array(numberOfflakes);
	var runCount = 0, runFor = 1000000 //number of seconds to run befor shutting down.
	if((document.layers)&&(this.Layer)){
		flakeHTML = [
		'<layer id=\"outFlake','',
		'\" left=\"0\" top=\"0\" width=\"','',
		'\" height=\"','',
		'\" visibility=\"hide\"><layer id=\"midFlake','',
		'\" left=\"0\" top=\"0\" width=\"','',
		'\" height=\"','',
		'\" visibility=\"hide\"><layer id=\"inFlake','',
		'\" left=\"0\" top=\"0\" width=\"','',
		'\" height=\"','',
		'\"><img src=\"','',
		'\" width=\"','',
		'\" height=\"','',
		'\" visibility=\"hide\"><\/layer><\/layer><\/layer>'];
	}else{
		flakeHTML = [
		'<div id="outFlake','', //1
		'" style="position:absolute;width:','', //3
		'px;overflow:hidden;height:','', //5
		'px;font-size:1px;visibility:hidden;\"><div id="midFlake','', //7
		'" style="position:absolute;width:','', //9
		'px;overflow:hidden;height:','', //11
		'px;font-size:1px;visibility:hidden;\"><div id="inFlake','', //13
		'" style="position:relative;width:','', //15
		'px;overflow:hidden;height:','', //17
		'px;font-size:1px;visibility:hidden;\"><img src=\"','', //19
		'\" width=\"','', //21
		'\" height=\"','', //23
		'\"><\/div><\/div><\/div>'];
	}
	for(var c = numberOfflakes;c--;){
		var picAr = pics[((Math.random()*pics.length)|0)%pics.length];
		widths[c] = (flakeHTML[21] = (flakeHTML[3] = (flakeHTML[9] = (flakeHTML[15] =   picAr[1]))));
		heights[c] = (flakeHTML[23] = (flakeHTML[5] = (flakeHTML[11] = (flakeHTML[17] =   picAr[2]))));
		flakeHTML[19] = picAr[0];
		flakeHTML[13] = flakeHTML[7] = (flakeHTML[1] = c);
		document.write(flakeHTML.join(''));
	}
	function compatModeTest(obj){
		if((document.compatMode)&&(document.compatMode.indexOf('CSS') != -1)&&(document.documentElement)){
			return document.documentElement;
		}else if(document.body){
			return document.body;
		}else{
			return obj;
		}
	}
	function getWindowState(){
		var global = this;
		var readScroll = {scrollLeft:NaN,scrollTop:NaN};
		var readSizeC = {clientWidth:NaN,clientHeight:NaN};
		var readSizeI = {innerWidth:NaN,innerHeight:NaN};
		var readScrollX = 'scrollLeft';
		var readScrollY = 'scrollTop';
		function getWidthI(){return readSizeI.innerWidth;}
		function getWidthC(){return readSizeC.clientWidth|0;}
		function getHeightI(){return readSizeI.innerHeight;}
		function getHeightC(){return readSizeC.clientHeight|0;}
		function getHeightSmart(){return retSmaller(getHeightI(), getHeightC());}
		function getWidthSmart(){return retSmaller(getWidthI(), getWidthC());}
		function setInnerWH(){
			theOne.getWidth = getWidthI;
			theOne.getHeight = getHeightI;
		}
		function retSmaller(inr, other){
			if(other > inr){
				setInnerWH();
				return inr;
			}else{
				return other;
			}
		}
		var theOne = {
			getScrollX:function(){return readScroll[readScrollX]|0;},
			getScrollY:function(){return readScroll[readScrollY]|0;},
			getWidth:getWidthC,
			getHeight:getHeightC
		};
		function main(){return theOne;}
		function rankObj(testObj){
			var dv,dhN;
			if(testObj&&(typeof testObj.clientWidth == 'number')&&(typeof testObj.clientHeight == 'number')){
				if(((dv = global.innerHeight - testObj.clientHeight) >= 0)&&((dh = global.innerWidth - testObj.clientWidth) >= 0)){
					if(dh == dv){
						return 0;
					}else if((dh&&!dv)||(dv&&!dh)){
						return (dh+dv);
					}
				}
			}
			return NaN;
		}
		if((typeof global.innerHeight == 'number')&&(typeof global.innerWidth == 'number')){
			readSizeI = global;
			var bodyRank = rankObj(document.body);
			var rankDocEl = rankObj(document.documentElement);
			var selEl = null;
			if(!isNaN(bodyRank)){
				if(!isNaN(rankDocEl)){
					if(bodyRank < rankDocEl){
						selEl = document.body;
					}else if(bodyRank > rankDocEl){
						selEl = document.documentElement;
					}else{
						selEl = compatModeTest(document.body);
					}
				}else{
					selEl = document.body;
				}
			}else if(!isNaN(rankDocEl)){
				selEl = document.documentElement;
			}
			if(selEl){
				readSizeC = selEl
				theOne.getWidth = getWidthSmart;
				theOne.getHeight = getHeightSmart;
			}else{
				setInnerWH();
			}
		}else{
			readSizeC = compatModeTest(readSizeC);
		}
		if((typeof global.pageYOffset == 'number')&&(typeof global.pageXOffset == 'number')){
			readScroll = global;
			readScrollY = 'pageYOffset';
			readScrollX = 'pageXOffset';
		}else{
			readScroll = compatModeTest(readScroll);
		}
		return (getWindowState = main)();
	}
	function readWindow(){
		clientHeight = windowState.getHeight() - 2;
		clientWidth = windowState.getWidth() - 2;
		sy = windowState.getScrollY() + 1;
		sx = windowState.getScrollX() + 1;
	}
	function getStyleObj(id, outer){
		var obj = null;
		if(document.getElementById){
			obj = document.getElementById(id);
		}else if(document.all){
			obj = document.all[id];
		}else if(document.layers){
			obj = document.layers[id];
			if((!obj)&&(outer)&&
			  (outer.document)&&
			  (outer.document.layers)){
				obj = outer.document.layers[id];
			}
		}
		return (obj && obj.style) || obj;
	}
	var cosAr = [];
	var angleStep = Math.PI/90;
	for(var c = 0;c <= 180;c++){
		cosAr[c] = Math.cos(c*angleStep)
	}
	function FlakeObj(id, parent){
		var next;
		var outerDivClip, outerDiv = getStyleObj("outFlake"+id);
		var midDivClip, midDiv = getStyleObj("midFlake"+id, outerDiv);
		var innerDivClip, innerDiv = getStyleObj("inFlake"+id, midDiv);
		var y = 0,x = 0,cx,cy,s1,s2;
		var w = widths[id],h = heights[id],returnObj = this;
		topPosition = clientHeight;
		function setPos(){
			var temp, ouTop = 0,ouLeft = 0,ouW = 0,ouH = 0,inTop = 0,inLeft = 0;
			var xPlusW = x + w;
			var yPlusH = y + h;
			if((xPlusW >= 0)&&(yPlusH >= 0)&&(x < clientWidth)&&(y < clientHeight)){
				if(y >= 0){
					ouTop = (y < clientHeight)?y:0;
					ouH = ((yPlusH) >= clientHeight)?(clientHeight - y):h;
				}else{
					inTop = y;
					ouH = yPlusH;
				}
				if(x >= 0){
					ouLeft = (x < clientWidth)?x:0;
					ouW = ((xPlusW) >= clientWidth)?(clientWidth - x):w;
				}else{
					inLeft = x;
					ouW = xPlusW;
				}
				outerDiv.visibility = 'visible';
			}else{
				outerDiv.visibility = 'hidden';
			}
			outerDivClip.width = (outerDiv.pixelWidth = ouW)+posMod;

			//innerDiv.left = inLeft+posMod;
			midDiv.left = inLeft+posMod;

			outerDivClip.height = (outerDiv.pixelHeight = ouH)+posMod;

			//innerDiv.top = inTop+posMod;
			midDiv.top = inTop+posMod;

			midDivClip.width =  (midDiv.pixelWidth = ((-1*inLeft) + (temp = (ouW-inLeft))))+posMod;

			innerDivClip.width =  (innerDiv.pixelWidth = temp)+posMod;

			outerDiv.left = (sx+ouLeft)+posMod;

			midDivClip.height =  (midDiv.pixelHeight = ((-1*inTop) + (temp = (ouH-inTop))))+posMod;

			innerDivClip.height =  (innerDiv.pixelHeight = temp)+posMod;

			outerDiv.top = (sy+ouTop)+posMod;
		}
		if((outerDiv)&&(innerDiv)){
			(this.reset = function(){
				if(runCount > runFor)returnObj = next;
				y = -(((Math.random()*topPosition)+h)|0);
				x = w+((Math.random()*(clientWidth-(w<<1)))|0);
				cx = Math.random()*3;
				cy = (7 - cx)|0; //rate of fall
				cx = (cx + 3)|0; //sideways drift
				s1 = (Math.random()*180)|0;
				s2 = 1+((Math.random()*4)|0);
				return;
			})();
			outerDivClip =  (outerDiv && (typeof outerDiv.clip != 'string') && outerDiv.clip)||outerDiv;
			midDivClip =  (midDiv && (typeof midDiv.clip != 'string') && midDiv.clip)||midDiv;
			innerDivClip =  (innerDiv && (typeof innerDiv.clip != 'string') && innerDiv.clip)||innerDiv;
			this.position = function(){
				y += cy;
				x += (cx * cosAr[(s1 = ((s1 + s2)%180))])|0;
				if(y >= clientHeight){
					this.reset();
				}
				setPos();
				next = next.position();
				return returnObj;
			};
			setPos();
			innerDiv.visibility = (outerDiv.visibility = 'visible');
		}else{
			this.position = function(){return;};
		}
		this.toString = function(){
			return 'cd '+id+' x = '+x+' y = '+y+' cx = '+cx+' s2 = '+s2+' s1 = '+s1+'\n'+next.toString();
		}
		if(++id < numberOfflakes){
			next = new FlakeObj(id, parent)
		}else{
			topPosition = (clientHeight>>3);
			next = parent
		}
	}
	function init(){
		var stRef;
		if(!(stRef = getStyleObj("outFlake"+(numberOfflakes-1)))){
			setTimeout(init, 200);
		}else{
			windowState = getWindowState();
			posMod = (typeof stRef.top == 'string')?'px':0;
			readWindow();
			flakeObjs = new FlakeObj(0, init);
			runFor *= 1000;
			init.act();
		}
	};
	init.position = function(){return this;}
	init.act = function(){
		readWindow();
		flakeObjs = flakeObjs.position();
		if(flakeObjs != init)setTimeout(init.act,60);
		runCount += 60;
	};
	init.act.toString = function(){return globalName+'.act()';};
	init.toString = function(){
		while((global[globalName])&&(global[globalName] != this)){globalName += globalName};
		global[globalName] = this; //this - is the init function.
		return globalName+'()';
	};
	setTimeout(init, 200);
})();