function getCookieVal(offset) {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1) endstr = document.cookie.length;
   return unescape (document.cookie.substring(offset, endstr));
}

// Get Cookie function
function GetCookie(name) {
   var arg = name+"=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var k = 0;
   while (k < clen) {
      var j = k + alen;
      if (document.cookie.substring(k, j) == arg) return getCookieVal(j);
      k = document.cookie.indexOf(" ", k) + 1;
      if (k == 0) break;
   }
   return null;
}

function SetCookie (name, value) {
	 var argv = SetCookie.arguments;
	 var argc = SetCookie.arguments.length;
	 var expires = (argc > 2) ? argv[2] : null;
	 var path = (argc > 3) ? argv[3] : null;
	 var domain = (argc > 4) ? argv[4] : null;
	 var secure = (argc > 5) ? argv[5] : false;
	 document.cookie = name + "=" + escape (value) +
	 ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	 ((path == null) ? "" : ("; path=" + path)) +
	 ((domain == null) ? "" : ("; domain=" + domain)) +
	 ((secure == true) ? "; secure" : "");
}



  
  var snowsrc="js/snow2.gif"
  var no = 22; // pocet vlocek

  var timer;
 // var snowing = false; definovano primo v dokumentu
 snowing = false;
  var arrOfFlocs = new Array(); // pole indexovane x-ovou souradnici po napr. 20 bodech - obsahuje dalsi pole s umistenyma vlockama
  var heightOnX = new Array(); // pole vysek na x-ove souradnici - 0, kdyz tam nic neni
  var maxPrepared = 140; // pocet pripravenych spadanych vlocek
  var indexForPrepared = 1;
  var mf = 10; // mezery mezi vlockama dole

  var ns4up = (document.layers) ? 1 : 0;  // browser sniffer
  var ie4up = (document.all) ? 1 : 0;
  var ns6up = (document.getElementById && !document.all) ? 1 : 0;
  var Opera = window.opera ? true : false;


  var dx, xp, yp;    // coordinate and position variables
  var am, stx, sty;  // amplitude and step variables
  var coordx;
  var i, doc_width = 800, doc_height = 600;
  
 /* if (ns4up||ns6up) {
	doc_width = self.innerWidth;
	doc_height = self.innerHeight;
  } else if (ie4up) {
	doc_width = document.body.clientWidth;
	doc_height = document.body.clientHeight;
  } */

//alert(document.body.offsetHeight + "," + self.innerHeight + "," + document.documentElement.clientHeight + ", " + document.body.clientHeight + ", " + window.pageYOffset);

	function nastavRozmery() {
		if (Opera) 
		{
			doc_width = document.documentElement.clientWidth;
			doc_height = document.documentElement.clientHeight;
		}
		else if (document.body && (document.body.offsetHeight > 0))
		{
			doc_width = document.body.offsetWidth;
			doc_height = document.body.offsetHeight;
		}
		else if (self.innerWidth)
		{
			doc_width = self.innerWidth;
			doc_height = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientWidth)
		{
			doc_width = document.documentElement.clientWidth;
			doc_height = document.documentElement.clientHeight;
		}
		else if (document.body)
		{
			doc_width = document.body.clientWidth;
			doc_height = document.body.clientHeight;
		}
		else
		{
			doc_width = 800;
			doc_height = 600;
		}
	}
	nastavRozmery();
	

  dx = new Array();
  xp = new Array();
  yp = new Array();
  am = new Array();
  stx = new Array();
  sty = new Array();
  coordx = new Array();
  
  function indexOfFloc(x) {
    return Math.round(x / mf);
  }

  for (i = 0; i < Math.round(doc_width / mf); i++) {
    heightOnX[i] = 0;
  }

  for (i = 1; i <= maxPrepared; i++) {
	if (ns4up) {                     
	  document.write("<layer name=\"prep"+ i +"\" left=\"-25\" top=\"-25\" visibility=\"hide\"><img src='"+snowsrc+"' border=\"0\"><\/layer>");
	} else if (ie4up||ns6up) {
	  document.write("<div id=\"prep"+ i +"\" style=\"POSITION: absolute; Z-INDEX: "+ i +"; VISIBILITY: hidden; TOP: -25px; LEFT: -25px;\"><img src='"+snowsrc+"' border=\"0\"><\/div>");
	}
  }

  for (i = 0; i < no; ++ i) {  
	dx[i] = 0;                        // set coordinate variables
	xp[i] = Math.random()*(doc_width-50);  // set position variables
	yp[i] = Math.random()*doc_height;
	am[i] = Math.random()*20;         // set amplitude variables
	stx[i] = 0.02 + Math.random()/10; // set step variables
	sty[i] = 0.7 + Math.random();     // set step variables
	coordx[i] = -1;
	if (ns4up) {                      // set layers
		document.write("<layer name=\"dot"+ i +"\" left=\"-25\" top=\"-25\" visibility=\"hide\"><img src='"+snowsrc+"' border=\"0\"><\/layer>");
	} else if (ie4up||ns6up) {
		document.write("<div id=\"dot"+ i +"\" name=\"dot"+ i +"\" style=\"POSITION: absolute; Z-INDEX: "+ i +"; VISIBILITY: hidden; TOP: -25px; LEFT: -25px;\"><img src='"+snowsrc+"' border=\"0\"><\/div>");
	}
  }

  function initPrep() {
	var j = 0;
    for (j = 0; j < Math.round(doc_width / mf); j++) {
      heightOnX[j] = 0;
    }
	for (j = 1; j <= maxPrepared; j++) {
	  if (ns4up) {                   
		document.layers["prep" + j].style.visibility='hide';
	  } else if (ie4up) {
		document.all["prep" + j].style.visibility='hidden';
	  } else if (ns6up) {
		document.getElementById("prep" + j).style.visibility='hidden';
	  }
	} 
    indexForPrepared = 1;
  }
 

  function snowNS() {  // Netscape
    for (i = 0; i < no; ++ i) {
      yp[i] += sty[i];
      if (yp[i] > doc_height-50) {
        xp[i] = Math.random()*(doc_width-am[i]-30);
        yp[i] = 0;
        stx[i] = 0.02 + Math.random()/10;
        sty[i] = 0.7 + Math.random();
        doc_width = self.innerWidth;
        doc_height = self.innerHeight;
      }
      dx[i] += stx[i];
      document.layers["dot"+i].top = yp[i];
      document.layers["dot"+i].left = xp[i] + am[i]*Math.sin(dx[i]);
    }
    timer = setTimeout("snowNS()", 15);
  }

  function snowIE_NS6() {  // IE, NS6 animace
//    doc_width  = ns6up ? window.innerWidth  : document.body.clientWidth;
 //   doc_height = ns6up ? window.innerHeight : document.body.clientHeight;
	nastavRozmery();

    for (i = 0; i < no; ++ i) {
      yp[i] += sty[i];
	//  if (i == 0 && yp[i]>800) {
//		  alert ("xp: " + xp[i] + ", indexOfFloc: " + indexOfFloc(xp[i]) + ", heightOnX: " + heightOnX[indexOfFloc(xp[i])] + ", doc_height: " + doc_height + ", sty: " + sty[i]);
//	  }
      if (yp[i] > doc_height-heightOnX[indexOfFloc(coordx[i])]-42) { // spadla dolu
	  //  alert("spadla " + i);
		heightOnX[indexOfFloc(coordx[i])] += 12;
        if (ie4up) {
          document.all["prep" + indexForPrepared].style.pixelTop = yp[i];
          document.all["prep" + indexForPrepared].style.pixelLeft = document.all["dot"+i].style.pixelLeft;
          document.all["prep" + indexForPrepared].style.visibility='visible';
        } else if (ns6up) {
          document.getElementById("prep" + indexForPrepared).style.top=yp[i];
          document.getElementById("prep" + indexForPrepared).style.left=document.getElementById("dot"+i).style.left;
          document.getElementById("prep" + indexForPrepared).style.visibility='visible';
        } 
		indexForPrepared++;
		if (indexForPrepared > maxPrepared)
			initPrep();

        xp[i] = Math.random()*(doc_width-am[i]-30);
        yp[i] = 0;
        stx[i] = 0.02 + Math.random()/10;
        sty[i] = 0.9 + Math.random();
      } 
      dx[i] += stx[i];
      coordx[i] = xp[i] + am[i]*Math.sin(dx[i]);
      if (ie4up){
        document.all["dot"+i].style.pixelTop = Math.round(yp[i]);
        document.all["dot"+i].style.pixelLeft = Math.round(coordx[i]);
      } else if (ns6up){
		//  if (i==1) {
		 //   alert(document.getElementById("dot"+i).style.pixelTop + ", " + document.getElementById("dot"+i).style.top + ", " + document.getElementById("dot"+i).style + ", " + Math.round(yp[i]));
		 //}
		//document.getElementById("dot"+i).setAttribute("style.top", "100", 0); 
        document.getElementById("dot"+i).style.top = Math.round(yp[i]);
		//document.getElementById("dot"+i).style.pixelTop = Math.round(yp[i]);
        document.getElementById("dot"+i).style.left = Math.round(coordx[i]);
       // document.getElementById("dot"+i).style.pixelLeft = Math.round(coordx[i]);
      }   
    }
    timer = setTimeout("snowIE_NS6()", 20);
  }

  function startSnow() {
	SetCookie("snowing", 1);
	if (snowing)
		return;
	snowing = true;
 // alert("ns4up " + ns4up + ", ns6up " +  ns6up + ", ie4up " + ie4up);
  //alert("sirka " + doc_width + ", vyska " + doc_height);

    for (i = 0; i < no; ++ i) {  // projdeme vlocky
		if (ns4up) {    
			document.layers["dot"+i].style.visibility='show';
		} else if (ie4up) {
			document.all["dot"+i].style.visibility='visible';
		} else if (ns6up) {
			document.getElementById("dot"+i).style.visibility='visible';
		}
	}
	if (ns4up) {
	  snowNS();
    } else if (ie4up||ns6up) {
      snowIE_NS6();
    }
  }

  function stopSnow() {
	SetCookie("snowing", 0);
	if (!snowing)
		return;
	snowing = false;
    window.clearTimeout(timer);
    for (i = 0; i < no; ++ i) {  // projdeme vlocky
		if (ns4up) {    
			document.layers["dot"+i].style.visibility='hide';
		} else if (ie4up) {
			document.all["dot"+i].style.visibility='hidden';
		} else if (ns6up) {
			document.getElementById("dot"+i).style.visibility='hidden';
		}
	}
  }

  if (GetCookie("snowing") == "1") {
	startSnow();
  }