/*
    _______________________            ______________________
    Snow script                        Browsers support:
             Version 0.1.               -> Internet Explorer
                                        -> Mozilla
    _____________________________       -> Opera
    Features:                           -> Firefox
     -> Server Side Independency        -> Konqueror
     -> Cross Browser Support           -> Safari
     -> Dynamic Loading
     -> Easy Customization
     -> Different snowflakes





                              ______________________________
                                  Serghei Egoricev (c) 2005
                                    egoricev [at] gmail.com
*/ 
Snow = function ()
{}

// snowflakes count
Snow.count = 20;
// images for snowflakes
Snow.flakesImg = new Array("/img/snow/snw1.png", "/img/snow/snw2.png");
// max fall speed
Snow.maxFallSpeed = 10;
// max wind force (abs value)
Snow.maxWind = 10;
// max img size (to avoid scroll bars)
Snow.imgHeight = 25;
Snow.imgWidth = 25;
// for internal use variables
Snow.dx = 0; // current wind force
Snow.flakes = new Array; // array of flakes
Snow.fallTimeout = 50; // snowflakes fall calculation (milliseconds)
Snow.windChangeTimeout = 3000; // wind force change timeout (milliseconds)
Snow.Enabled = true;

Snow.CreateFlake = function ()
{
	var d = document.createElement("DIV");
	d.x = Math.random()*(document.body.clientWidth-Snow.imgWidth);
	d.y = Math.random()*(document.body.clientHeight-Snow.imgHeight);
	d.dy = Math.random()*Snow.maxFallSpeed+1;

	d.style.position = "absolute";
	d.style.zIndex = 1000;
	d.style.left = Math.floor(d.x) + "px";
	d.style.top = Math.floor(d.y) + "px";
	var i = document.createElement("IMG");
	i.src = Snow.flakesImg[Math.floor(Math.random()*2)];
	d.appendChild(i);
	Snow.checkForPng(i);
	Snow.flakes.push(d);
	document.body.appendChild(d);
	return i;
}

Snow.Start = function ()
{
	if (Snow.Enabled)
	{
		for (i = 0; i<Snow.count; i++)
			Snow.CreateFlake();
		window.setInterval("Snow.Fall();", Snow.fallTimeout);
		window.setInterval("Snow.ChangeWind();", Snow.windChangeTimeout);
	} // if
}

Snow.ChangeWind = function ()
{
	var dx = (Math.random()*3) - 2;
	var ddx = Snow.dx + dx;
	if (Math.abs(ddx) > Snow.maxWind)
		ddx = Snow.dx - dx;
	Snow.dx = ddx;
}

Snow.Fall = function ()
{
	for (i = 0; i < Snow.count; i++)
	{
		var d = Snow.flakes[i];
		d.y += d.dy;
		if (d.y > document.body.clientHeight - Snow.imgHeight)
			d.y = 0;
		d.x += Snow.dx - (d.y % 100 - 50) % 2;
		if (d.x > document.body.clientWidth - Snow.imgWidth)
			d.x = 0;
		else
			if (d.x < 0)
				d.x = document.body.clientWidth - Snow.imgWidth;
		d.style.top = document.body.scrollTop + Math.floor(d.y) + "px";
		d.style.left = Math.floor(d.x) + "px";
	} // for
}

Snow.checkForPng = function (img)
{
	if (/MSIE/.test(navigator.userAgent) && /\.png$/.test(img.src))
	{
		var div = document.createElement("DIV");
		div.src = img.src;
		div.style.width = 25 + "px";
		div.style.height = 25 + "px";
		div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img.src+"')"
		img.parentNode.replaceChild(div, img);
	} // if
}

function SetCookie(sName, sValue)
{
  var date = new Date();
	date.setMonth(date.getMonth()+4);
  document.cookie = sName + "=" + escape(sValue) + "; expires=" + date.toGMTString();
}

function GetCookie(sName)
{
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }
  return null;
}
Snow.Enabled = GetCookie("SnowDisabled") == null;

Snow.Off = function ()
{
	Snow.Enabled = false;
	SetCookie("SnowDisabled", "true");
	for (i = 0; i < Snow.count; i++)
		document.body.removeChild(Snow.flakes[i]);
  Snow.count = 0;
}

Snow.Start();

if (Snow.Enabled)
{
	document.write("<div style=\"position:absolute;left:97%;top:135px;\">");
	document.write("<a style=\"text-decoration:none;color:#cccccc;font-size:8px;\" href=\"javascript:Snow.Off();\" title=\"(eng) Turn snow off\r\n(pyc) \u0423\u0431pa\u0442\u044C c\u043De\u0433\r\n(rom) Stinge ninge\">[X]</a></div>");
} // if
