function objGet(x,d)	{d=d?d:document; return d.getElementById?d.getElementById(x):d.all?d.all[x]:d.layers?d.layers[x]:null;}
function creEl(x,d)	{d=d?d:document; return d.createElement(x);}
function addEl(x,d)	{d=d?d:document; d.appendChild(x);}

function classFlake(n,x,y)
{
this.n = n;
this.x0 = x;
this.y0 = y;
this.x = x;
this.y = y;
this.state = -1;
this.data = {traj:0, size:1};
this.obj_id = "f"+n;
}

function classDropped(n)
{
this.obj_id = "d"+n;
this.x = 0;
this.y = 0;
this.state = -1;
}

function rnd(n)
{
return Math.floor(Math.random()*n);
}

function classSnowGame(flakes,area)
{
var root = this;
this.area = [0,0,200,200];
this.traj = [];
this.nmax = 100;
this.n = 0;
this.nd = [];
this.ndmax = 1000;
this.flakes = [];
this.dropped = [];
this.items  = {};
this.counter = [];
this.flakeAdd = function (root,n,x,y)
	{
	root.flakes[n].x0 = x;
	root.flakes[n].y0 = y;
	root.flakeMove(root,n,x,y);
	root.flakeStateSet(root,n,0);
	++root.n;

	root.counter[0].value = n;
	root.counter[0].max = root.counter[0].max>n ? root.counter[0].max : n;
	root.counterWrite(root);
	};
this.droppedAdd = function (root,nd,n)
	{
	root.droppedSet(root, nd, n);
	root.droppedMove(root, nd, root.flakes[n].x, root.flakes[n].y);
	root.droppedStateSet(root, nd, 0);
	++root.nd;
	};
this.droppedSet = function (root,nd,n)
	{
	var obj = root.dropped[nd].obj_id;
	obj.style.width  = root.flakes[n].data.size + "px";
	obj.style.height = root.flakes[n].data.size + "px";
	};
this.counterWrite = function (root)
	{
	var i, li;
	li = root.counter.length
	for(i=0;i<li;++i)
		{
		root.counter[i].obj.value = root.counter[i].max;
		}
	}
this.counterInit = function (root)
	{
	root.counter[0] = {obj:objGet('numb'),value:0,max:0};
	root.counter[1] = {obj:objGet('time'),value:0,max:0,time:0};
	}
this.flakeAddAllTimer = function (root)
	{
	var i,li;
	if (root.n<root.nmax)
		{
		li = root.flakes.length;
		for (i=0;i<li;++i)
			{
			if (root.flakes[i].state === -1)
				{
				root.flakeAdd(root,i,rnd(root.area[2]), rnd((root.area[3]>>2)*1.5)-10);
				}
			}
		}
	};
this.flakeAddOneTimer = function (root)
	{
	var i,li;
	if (root.n<root.nmax)
		{
		li = root.flakes.length;
		for (i=0;i<li;++i)
			{
			if (root.flakes[i].state === -1)
				{
				root.flakeAdd(root,i,rnd(root.area[2]), 0);
				return;
				}
			}
		}
	};
this.droppedAddOneTimer = function (root, n)
	{
	var i,li;
	if (root.nd<root.ndmax)
		{
		li = root.dropped.length;
		for (i=0;i<li;++i)
			{
			if (root.dropped[i].state === -1)
				{
				root.droppedAdd(root,i,n);
				return;
				}
			}
		}
	};
this.flakeAddMouse = function (root)
	{
	var n;
	n = rnd(root.flakes.length);
	root.flakeRemove(root, n);
	root.flakeAdd(root, n);
	};
this.flakeRemove = function (root,n)
	{
	root.flakeStateSet(root,n,-1);
	--root.n;
	--root.counter[0].value;
	};
this.droppedRemove = function (root,n)
	{
	root.dropeStateSet(root,n,-1);
	--root.nd;
	};
this.flakeStateSet = function (root,n,state)
	{
	root.flakes[n].state = state;
	root.flakes[n].obj_id.style.display = state!==-1 ? "" : "none";
	};
this.droppedStateSet = function (root,n,state)
	{
	root.dropped[n].state = state;
	root.dropped[n].obj_id.style.display = state!==-1 ? "" : "none";
	};
this.flakeMove = function (root,n,x,y)
	{
//alert([x,y])
	root.flakes[n].x = x;
	root.flakes[n].y = y;
	root.flakes[n].obj_id.style.left = x + "px";
	root.flakes[n].obj_id.style.top  = y + "px";
	};
this.droppedMove = function (root,n,x,y)
	{
	root.dropped[n].x = x;
	root.dropped[n].y = y;
	root.dropped[n].obj_id.style.left = x + "px";
	root.dropped[n].obj_id.style.top  = y + "px";
	};
this.flakeMoveTraj = function (root,n)
	{
	var f,t,x,y;
	f = root.flakes[n];
	t = root.traj[f.data.traj];
	++f.state;
	if (f.state >= t.length)
		{
		f.state = 0;
		f.y0 = f.y;
		}
	x = f.x0 + t[f.state].x;
	y = f.y0 + t[f.state].y;
	root.flakes[n].state = f.state;
//alert([
//f.toSource(),
//	f.x0, t[f.state].x,
//	f.y0, t[f.state].y
//])
	if (y<root.area[3])
		{
		root.flakeMove(root, n, x, y);
		}
	else	{
		if (root.nd>root.ndmax)
			{
			root.droppedRemove(root,rnd(root.nd));
			}
		root.droppedAddOneTimer(root,n)
		root.flakeRemove(root,n);
		}
	};
this.flakeMoveTimer = function (root)
	{
	var i,li;
	li = root.flakes.length;
	for (i=0;i<li;++i)
		{
		if (root.flakes[i].state !== -1)
			{
			root.flakeMoveTraj(root,i);
			}
		}
	};
this.createTraj = function ()
	{
	var i,j,k,a,p,ma,mp, alpha, yy, my, pn;	//x,y,xx,
	ma = 10;
	mp = 5;
	my = 100;
	this.traj = [];
	for (i=0;i<20;++i)
		{
		a  = Math.random()*ma*1.1 + 2;			// amplitude
		p  = (Math.random()*mp + ma) * mp * 2 / (ma);	// pieces of alpha in 2 * PI
//p*=2;
p*=3;
		pn = 1 - Math.floor(Math.random()*2) * 2;	// pos / neg
		alpha = 2 * Math.PI / p;
		yy = my / p;				// y
		this.traj[i] = [];
		for (j=0;j<p;++j)
			{
			this.traj[i][j] = {
				x: Math.floor(pn * a * Math.sin(j * alpha)),
				y: Math.floor(yy * j)
				};
			}
//alert(this.traj[i].toSource())
/*
		j = 0;
		x = 0;
		xx = Math.floor(pn * a * Math.sin(j * alpha));
		yy = Math.floor(mh / p * 1.5);			// y
		this.traj[i][j] = {
			x: xx - x,
			y: yy
			};
		x += xx;
		for (j=1;j<p;++j)
			{
			xx = Math.floor(pn * a * Math.sin(j * alpha));
			this.traj[i][j] = {
				x: xx - x,
				y: yy
				};
			x += xx;
			}
*/
		}
	};
this.getTime = function()
	{
	return new Date().getTime() / 1000;
	}
this.init = function (root, flakes, area)
	{
	var i,li,lt,obj,obj_out, n;
	root.counterInit(root);
	root.createTraj();
	root.area = area   ? area   : root.area;
	root.nmax = flakes ? flakes : root.nmax;
	objGet('f').value = root.nmax;
	objGet('w').value = root.area[2];
	objGet('h').value = root.area[3];
	//root.counterWrite(root);
	// area init
	obj_out = objGet('snow_area');
	obj_out.style.position = "relative";
	obj_out.style.width    = root.area[2]+"px";
	obj_out.style.height   = root.area[3]+"px";
	obj_out.style.lineHeight = '0'; //IE
	obj_out.style.fontSize   = '0'; //IE
	obj_out.style.border   = "1px solid #dddddd";
	obj_out.style.display  = "block";
	obj_out.style.overflow = "hidden";
	// flakes destroy
	li = root.flakes.length;
	for(i=0;i<li;++i)
		{
		obj = root.flakes[i].obj_id;
		obj.style.display   = "none";
		obj_out.removeChild(obj);
		}
	// flakes init
	li = root.nmax;
	lt = root.traj.length;
	root.flakes = [];
	for(i=0;i<li;++i)
		{
		root.flakes[i] = new classFlake(i);
		root.flakes[i].data = {traj: rnd(lt), size: rnd(3) + 1};
		obj = creEl('DIV');
		obj.id = root.flakes[i].obj_id;
		obj.style.backgroundColor = "#000000";
		obj.style.display  = "none";
		obj.style.position = 'absolute';
		obj.style.width    = root.flakes[i].data.size + "px";
		obj.style.height   = root.flakes[i].data.size + "px";
		obj.style.lineHeight = '0'; //IE
		obj.style.fontSize   = '0'; //IE
		addEl(obj,obj_out);
		root.flakes[i].obj_id = objGet(root.flakes[i].obj_id);
		}
	// dropped destroy
	li = root.dropped.length;
	for(i=0;i<li;++i)
		{
		obj = root.dropped[i].obj_id;
		obj.style.display = "none";
		obj_out.removeChild(obj);
		}
	// dropped init
	li = root.ndmax;
	root.dropped = [];
	for(i=0;i<li;++i)
		{
		root.dropped[i] = new classDropped(i);
		obj = creEl('DIV');
		obj.id = root.dropped[i].obj_id;
		obj.style.backgroundColor = "#000000";
		obj.style.display  = "none";
		obj.style.position = 'absolute';
		obj.style.width    = 0 + "px";
		obj.style.height   = 0 + "px";
		obj.style.lineHeight = '0'; //IE
		obj.style.fontSize   = '0'; //IE
		addEl(obj,obj_out);
		root.dropped[i].obj_id = objGet(root.dropped[i].obj_id);
		}
	// items init
	li = root.area[2];
	root.items = [];
	for(i=0;i<li;++i)
		{
		root.items[i] = [];
		}


	root.timer_move = new classTimer2(SGTimerMove,150);
	root.timer_move.start();
	root.timer_add  = new classTimer2(SGTimerAdd,150);
	root.counter[1].time = root.getTime();
	root.timer_add.start();

/*
var k, j, lj, li,t,x,y,x0,y0,t;
x = 0;
y = 0;
li = root.traj.length;
for(i=0,k=0;i<li;++i)
	{
	t = root.traj[i];
	lj = t.length;
	x0 = i * 10;
	y0 = 0;
	for(j=0;j<lj;++j,++k)
		{
x = x0 + t[j].x;
y = y0 + t[j].y;
obj    = creEl('DIV');
obj.id = 'f'+k;
obj.style.position = 'absolute';
obj.style.width  = 2 + "px";
obj.style.height = 2 + "px";
obj.style.left   = x + "px";
obj.style.top    = y + "px";
obj.style.backgroundColor = "#000000";
addEl(obj,obj_out);
		}
	}
*/

	};
this.init(root,flakes,area);
}


function SGTimerMove()
	{
	var root = SG, n;
//	root.flakeAddAllTimer(root);
	root.flakeMoveTimer(root);
	n = root.getTime();
	root.counter[1].value = Math.floor((n - root.counter[1].time)*100);
	root.counter[1].time = n;
	root.counter[1].max = root.counter[1].max>root.counter[1].value ? root.counter[1].max : root.counter[1].value;
	root.timer_move.start();
	}
function SGTimerAdd()
	{
	var root = SG;
	root.flakeAddOneTimer(root);
	root.timer_add.start();
	}
function SGTimerStop()
	{
	var root = SG;
	root.timer_move.stop();
	root.timer_add.stop();
	}

function classTimer2(func,time)
{
this.id    = null;
this.state = 0;
this.start = function(func,time)
	{
	this.stop();
	this.init(func,time);	//,timernd
	this.id = setTimeout(this.func, this.time);
   	this.state = 1;
	}
this.stop = function()
	{
   	if (this.id!=null) {clearTimeout(this.id);}
   	this.state = 0;
	}
this.init = function(func,time)
	{
	this.func = typeof(func)!=='undefined' ? func : this.func;
	this.time = typeof(time)!=='undefined' ? time : this.time;
	}
this.init(func,time);
}

var SG;
function SGinit()
{
//SG = new classSnowGame(200);
SG = new classSnowGame();
}

/*
function flakePut(event)
{
var mouse_x, mouse_y;
mouse_x = event.pageX ? event.pageX : event.clientX;
mouse_y = event.pageY ? event.pageY : event.clientY;
flakes = 0;
flakes++;
flakeCreate(mouse_x,mouse_y);
}

obj.onmousedown = flakePut;

*/


/*


function pisNapis(vText)
{
vText = vText.split("");
var vPismenka =
	{
	"A":new Array(0.2 ,0.5 ,0.8 ,0.3),
	"h":new Array(1   ,0.5 ,0.75,0.4),
	"o":new Array(0.5 ,0.75,0.5),
	"j":new Array(0.75,0.9)
	};
var i,j,k,l,x,y,z,a,b, o,ox,fsize;
fsize = 20;
x = 100;
y = 150;
k = oBottomLine.length;
o = objGet("snowArea");
for (i in vText)
	{
	a=vText[i];
	ox=creEL(d,'DIV');
		ox.style.position   = "absolute";
		ox.style.left       = x+"px";
		ox.style.top        = y-fsize+"px";
		ox.style.fontSize   = fsize+"px";
		ox.style.lineHeight = fsize+"px";
		creaddTN(d,ox,a);
	appEL(o,ox); 
	for (j in vPismenka[a])
		{
		b = y-vPismenka[a][j]*fsize;
		for (l=0;l<3;l++)	//3x
			{oBottomLine[k] = new objText(x+j*3+l,b,y); k++;}
		}
	x+=fsize;
	}
}

//function pisObrazky()
//{
//var i,x,y,k,yy;
//x = 43;
//y = 37;
//yy= 0;
//k = oBottomLine.length; //pridam za lajnu
////for (i=0;i<70;i+=2)
//	{
//	yy = Math.floor(y-0.3*i);
//	k++;
//	oBottomLine[k] = new objText(x+i,yy,yy+5);
//	}
//for (i=70;i<140;i+=2)
////	{
////	yy = Math.floor(y-0.3*(140-i));
////	k++;
///	oBottomLine[k] = new objText(x+i,yy,yy+5);
//	}
//yy = 105;
//for (i=0;i<140;i+=2)
//	{
//	k++;
//	oBottomLine[k] = new objText(x+i,yy,yy+10);
//	}
//}

*/