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

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

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

function classSnowGame(flakes,area)
{
var root = this;
this.area = area ? area : [0,0,200,200];
this.traj = [];
this.nmax = flakes;
this.n = 0;
this.flakes = [];
this.items  = {};
this.flakeAdd = function (root,n,x,y)
	{
	root.flakeMove(root,n,x,y);
	root.flakeStateSet(root,n,0);
	++root.n;
	};
this.flakeAddTimer = 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) {break;}}
		if (i<li)
			{
			root.flakeAdd(root,i,rnd(root.area[2]), 0);
			}
		}
	};
this.flakeAddMouse = function (root)
	{
	var n;
	n = rnd(root.flakes.length);
	root.flakeRemove(root, n);
	root.flakeAdd(root, n);
	};
this.flakeRemove = function ()
	{
	root.flakeStateSet(-1);
	--root.n;
	};
this.flakeStateSet = function (root,n,state)
	{
	root.flakes[n].state = state;
	root.flakes[n].obj_id.style.display = state>0 ? "" : "none";
	};
this.flakeMove = function (root,n,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.flakeMoveTraj = function (root,n)
	{
	var f;
	++root.flakes[n].state;
	if (root.flakes[n].state > root.traj.length)
		{
		root.flakes[n].state = 0;
		}
//alert(root.flakes[n].data.toSource())
//alert(root.traj[2].toSource())
	f = root.flakes[n];
	root.flakeMove(
		root, n,
		f.x + root.traj[f.data.traj][f.state].x,
		f.y + root.traj[f.data.traj][f.state].y);
	};
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,a,p,ma,mp, alpha, h , mh, pn;
	ma = 10;
	mp = 5;
	mh = 100;
	this.traj = [];
	for (i=0;i<20;++i)
		{
		a  = Math.random()*ma*1.5 + 2;			// amplitude
		p  = (Math.random()*mp + ma) * mp * 2 / (ma);	// pieces of alpha in 2 * PI
		h  = mh / p;					// y
		pn = 1 - Math.floor(Math.random()*2) * 2;	// pos / neg
		alpha = 2 * Math.PI / p;
		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(h * j),
				};
			}
		}
	};
this.init = function (root)
	{
	var i,j,li,lj,k,obj,obj_out;
	root.createTraj();
	obj_out = objGet('snow_area');
	obj_out.style.position = "relative";
	obj_out.style.width    = "400px";
	obj_out.style.height   = "400px";
	obj_out.style.border   = "1px solid #dddddd";
	obj_out.style.display  = "block";
	li = root.nmax;
	for(i=0;i<li;++i)
		{
		root.flakes[i] = new classFlake(i);
		root.flakes[i].data = {traj: rnd(root.traj.length), size: rnd(3) + 1};
		obj = creEl('DIV');
		obj.id = root.flakes[i].obj_id;
		obj.style.backgroundColor = "#000000";
		obj.style.diplay   = "none";
		obj.style.position = 'absolute';
		obj.style.width    = root.flakes[i].data.size + "px";
		obj.style.height   = root.flakes[i].data.size + "px";
		addEl(obj,obj_out);
		root.flakes[i].obj_id = objGet(root.flakes[i].obj_id);
		}
/*
	for(i=0,k=0;i<li;++i)
		{
		lj = this.traj[i].length;
		for(j=0;j<lj;++j,++k)
			{
obj    = creEl('DIV');
obj.id = 'f'+k;
obj.style.position = 'absolute';
obj.style.width    = this.traj[i][j].s + "px";
obj.style.height   = this.traj[i][j].s + "px";
obj.style.left     = this.traj[i][j].x + i*30 + "px";
obj.style.top      = this.traj[i][j].y + "px";
obj.style.backgroundColor = "#000000";
addEl(obj,obj_out);

			}
		}
*/
//alert(k);
//alert(root.toSource())
	root.timer = new classTimer2(SGTimer,100);
	root.timer.start();
	};
this.init(root);
}

function SGTimer()
	{
	var root = SG;
	root.flakeAddTimer(root);
	root.flakeMoveTimer(root);
	}

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 init()
{
SG = new classSnowGame(50);
//timer = setInterval(SG.move,100);
//	'hello' : function() {root.timer.start(root.talk.hello,500);},
}

/*
function hodiny()
{
window.clearTimeout(timerID)

*/

/*
function objVlocka()
{
var that = this;
this.x = Math.round(Math.random() * maxx);
this.y = Math.round(Math.random() * maxy);
this.size  = Math.floor(Math.random() * size) + 1;
this.speed = Math.round(Math.random() * speed + 0.98) + 1;
this.objID = "s"+i;
this.domove = function()
	{
	var s = objGet(that.objID).style;
	s.left = that.x+"px";
	s.top  = that.y+"px";
	}
}

function setStyles(obj,styles)
{
var i;
for (i in styles)
	{
	obj.style[i] = styles[i];
	}
}

function flakeMoveTo(obj,x,y)
{
obj.left = x+"px";
obj.top  = y+"px";
}

function flakeCreate(x,y)
{
alert([x,y]);
var obj,n;
obj    = creEl('DIV');
obj.id = 'flake'+flakes;
setStyles(obj,flake_sty);
flakeMoveTo(obj,x,y);
appEl(obj,objGet('snowArea'));
}

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);
}

var html, body;
var cfg, area_sty, flake_sty;
cfg = {
  area_id: 'snowArea',
  area_w : 200,
  area_h : 200
  };
area_sty  = {width: cfg.area_w+"px", height: cfg.area_h+"px", position: "absolute", background: "#888", overflow: "hidden", display: "block", border: "1px solid #88b"};
flake_sty = {width: "3px", height: "3px", left: "0px", top: "0px", position: "absolute", background: "#fff", border: "1px solid #cde", borderWidth: "0 1px 1px 0", mozBorderRadius : "6px", fontSize: 0, lineHeight: 0};


function init()
{
var obj;

obj   = objGet(cfg.area_id);
obj.innerHTML = "";
obj.onmousedown = flakePut;
setStyles(obj,area_sty);
}

*/


/*


var a,i,j,o,ox;
sDroppedPos = 0
html = d.getElementsByTagName('html')[0];
body = d.getElementsByTagName('body')[0];
//if (document.all) {body.style.height="100%";}
//html.style.overflow="hidden";
o = objGet("snowArea");
	o.innerHTML = "";
//	o.style.position   = "relative";
	o.style.position   = "absolute";
	o.style.top        = snowAreaTop    + "px";
	o.style.left       = snowAreaLeft   + "px";
	o.style.width      = snowAreaWidth  + "px";
	o.style.height     = snowAreaHeight + "px";
//	o.style.background = "#8ce";
//	o.style.border     = "1px solid #88b";
	o.style.overflow   = "hidden";
	o.style.display    = "block";
ox=creEL(d,'DIV');
	ox.id="snowFlakes";
	ox.style.position = "absolute";
	ox.style.top      = 0;
appEL(o,ox); 
ox=creEL(d,'DIV');
	ox.id="bottomLine";
	ox.style.position = "absolute";
	ox.style.top      = 0;
appEL(o,ox); 
max_x = o.offsetWidth - sFlakewh;
max_y = o.offsetHeight;
sBottomLine   = new Array(max_x+sFlakewh<<1);
for (i=-sFlakewh;i<max_x+sFlakewh;i++) {sBottomLine[i] = max_y;}
objBottomLine = objGet("bottomLine");

o = objGet("snowFlakes");
for (i=0;i<snowFlakes;i++)
	{
	v[i]  = new objSnowFlake(i);
	a     = new objFlakeStyle(v[i]);
	ox    = creEL(d,'DIV');
	ox.id = v[i].objID;
	for (j in a)
		{ox.style[j] = a[j];}
	appEL(o,ox); 
	}
flakeStyleParam = new objFlakeStyle(v[0]);
oBottomLine     = new Array();
//pisNapis('Ahoj');
//pisObrazky();
fce_circle(84,80,46);
}




//	Original: FREE 2006 Peter Mlich http://www.volny.cz/peter.mlich/
// upraveno 2010
// --- Snih + zapadavani ---
var snowAreaLeft   = 200;
var snowAreaTop    = 30;
var snowAreaLeft   = 0;
var snowAreaTop    = 30;
var snowAreaWidth  = 160;
var snowAreaHeight = 130;
var snowFlakes     = 30;	// >0
var sFlakewh       = 2;		// 3x3
var sFlakespeedmax = 2;
var sFlakespeedmin = 1; 	// >0
var sDroppedMax    = 1500;	// max to reset
var sDrop          = true;	// true/false
var sDropChance    = 3;		// 1-10
var flakeStyleParam= new Array();
var objBottomLine  = null;
var timerID    = null;
var timerSpeed = 120;
var stopT      = 0;
var html,body,max_x,max_y,sBottomLine,sDroppedPos,oBottomLine;
var v = new Array(snowFlakes);
var d = document;

function objGet(x)	{return d.getElementById?d.getElementById(x):d.all?d.all[x]:d.layers?d.layers[x]:null;}
function creEL(d,x)	{return d.createElement(x);}
function appEL(d,x)	{d.appendChild(x);}
function creaddTN(d,o,x){var t=d.createTextNode(x);o.appendChild(t);}

function objText(x,y1,y2)
{
this.x =x;
this.y1=y1;
this.y2=y2;
}

function objFlakeStyle(f)
{
this.position   = "absolute";
this.fontSize   = 0;
this.lineHeight = 0;
this.background = "#fff";
this.border     = "1px solid #cde";
this.borderWidth= "0 1px 1px 0";
this.mozBorderRadius = 6+"px";
this.height     = f.size+"px";
this.width      = f.size+"px";
this.left       = f.x+"px";
this.top        = f.y+"px";
}

function objSnowFlake(i)
{
this.x = Math.round(Math.random() * max_x);
this.y = Math.round(Math.random() * max_y);
this.speed = Math.round(Math.random() * sFlakespeedmax) + sFlakespeedmin;
this.size  = Math.floor(Math.random() * sFlakewh) + 1;
this.objID = "s"+i;
this.showPos = function()
	{
	var z  = objGet(this.objID).style;
	z.left = this.x+"px";
	z.top  = this.y+"px";
	}
}

function initSnow()
{var a,i,j,o,ox;
sDroppedPos = 0
html = d.getElementsByTagName('html')[0];
body = d.getElementsByTagName('body')[0];
//if (document.all) {body.style.height="100%";}
//html.style.overflow="hidden";
o = objGet("snowArea");
	o.innerHTML = "";
//	o.style.position   = "relative";
	o.style.position   = "absolute";
	o.style.top        = snowAreaTop    + "px";
	o.style.left       = snowAreaLeft   + "px";
	o.style.width      = snowAreaWidth  + "px";
	o.style.height     = snowAreaHeight + "px";
//	o.style.background = "#8ce";
//	o.style.border     = "1px solid #88b";
	o.style.overflow   = "hidden";
	o.style.display    = "block";
ox=creEL(d,'DIV');
	ox.id="snowFlakes";
	ox.style.position = "absolute";
	ox.style.top      = 0;
appEL(o,ox); 
ox=creEL(d,'DIV');
	ox.id="bottomLine";
	ox.style.position = "absolute";
	ox.style.top      = 0;
appEL(o,ox); 
max_x = o.offsetWidth - sFlakewh;
max_y = o.offsetHeight;
sBottomLine   = new Array(max_x+sFlakewh<<1);
for (i=-sFlakewh;i<max_x+sFlakewh;i++) {sBottomLine[i] = max_y;}
objBottomLine = objGet("bottomLine");

o = objGet("snowFlakes");
for (i=0;i<snowFlakes;i++)
	{
	v[i]  = new objSnowFlake(i);
	a     = new objFlakeStyle(v[i]);
	ox    = creEL(d,'DIV');
	ox.id = v[i].objID;
	for (j in a)
		{ox.style[j] = a[j];}
	appEL(o,ox); 
	}
flakeStyleParam = new objFlakeStyle(v[0]);
oBottomLine     = new Array();
//pisNapis('Ahoj');
//pisObrazky();
fce_circle(84,80,46);
}

function hodiny()
{
window.clearTimeout(timerID)

var i,j,k,ox,presun,s,a,b,c,m,n;
for (i=0;i<snowFlakes;i++)
	{
	presun = 0;
	if (sDrop)
	 {
	//y>max_y collision
	s = new Array();
	b = 0;
	c = v[i].y+v[i].size+v[i].speed;
	for (j=0;j<v[i].size;j++)	//bottom? pocet kolizi
		{s[j]=(c>=sBottomLine[v[i].x+j])?1:0; b+=s[j];}
	if (b>0)
		{
		presun = 1;
//if (v[i].size>0 || (v[i].size==1)
			//edge?
			{
			a = (v[i].size)*2;
			b = 0; for(k=0;k<a;k++)		{b+=s[k];}
			c = 0; for(k=a;k<v[i].size;k++) {c+=s[k];}
			if (b<a && c<a && Math.random()*2>1) {b=a;} else {c=a;}
			      if (b<a) {v[i].x-=a; presun=0;
					for (j=0;j<v[i].size;j++) {if (c>=sBottomLine[v[i].x+j]) {presun=1;break;}}}
			else {if (c<a) {v[i].x+=a; presun=0;
					for (j=0;j<v[i].size;j++) {if (c>=sBottomLine[v[i].x+j]) {presun=1;break;}}}}
			}
		//change Bottomline
		if (presun==1)
			{
			a = v[i].size;
			for (j=0;j<a;j++)
				{sBottomLine[v[i].x+j]--;}	//alg3
//				{sBottomLine[v[i].x+j]-=a;}	//alg1
//				{sBottomLine[v[i].x+j]=v[i].y;}	//alg2
			}
		}
	//obj simple collision
        if (presun==0)
	  {
	b = v[i].x-(v[i].size>>1);
	c = v[i].y+v[i].size+v[i].speed;
	for (j in oBottomLine)
		{
		if (oBottomLine[j].x==b && c>oBottomLine[j].y1 && c<oBottomLine[j].y2 && Math.random()*sDropChance<1)
			{presun=2; oBottomLine[j].y1--; break;}
//-=v[i].size
		}
	  }
	 }
	if (presun>0)
		{
		//move to Dropped
		a = objGet(v[i].objID);
		ox= creEL(d,'DIV');
		for (j in flakeStyleParam)
			{ox.style[j] = a.style[j];}
		appEL(objBottomLine,ox); 
		sDroppedPos++;
		if (sDroppedPos>sDroppedMax || sBottomLine[v[i].x]<5) {stopT=1; initSnow(); hodiny(); stopT=0; break;}

		//new Flake
		v[i]   = new objSnowFlake(i);
		v[i].y = 0;
		}
	else	{
		//change xy
		v[i].x+= 1 - Math.floor(Math.random() * 3);
		v[i].y+= v[i].speed;
		}
	v[i].showPos();
	}

if (stopT==0) {timerID=window.setTimeout("hodiny()",timerSpeed);}
}

function fce_circle(cx,cy,radius)
{
  var error = -radius;
  var x = radius;
  var y = 0;
  while (x >= y)
  {
    fce_circleplot8points(cx, cy, x, y);
    error += y;
    ++y;
    error += y;
    if (error >= 0)
    {
      --x;
      error -= x;
      error -= x;
    }
  }
}
 
function fce_circleplot8points(cx, cy, x, y)
{
  fce_circleplot4points(cx, cy, x, y);
  if (x != y) fce_circleplot4points(cx, cy, y, x);
}
 
function fce_circleplot4points(cx, cy, x, y)
{
  setPixel(cx + x, cy + y);
  if (x != 0) setPixel(cx - x, cy + y);
  if (y != 0) setPixel(cx + x, cy - y);
  if (x != 0 && y != 0) setPixel(cx - x, cy - y);
}

function setPixel(x,y)
{
oBottomLine[oBottomLine.length] = new objText(x,y,y+5);
showPixel(x,y);
}

function showPixel(x,y)
{
d  = document;
o  = objGet("snowArea");
ox = creEL(d,'DIV');
	ox.style.position = "absolute";
	ox.style.left     = x +'px';
	ox.style.top      = y +'px';
	ox.style.width    = '1px';
	ox.style.height   = '1px';
	ox.style.background = '#f00';
appEL(o,ox); 
}




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);
//	}
//}

*/