
// ---- Configure Parameters ---
FlakesPath = "http://angubin.narod.ru/bab";
FlakesType = ".gif";
MaxFlakePics = 4; 
MaxFlakes =10;
XPos = 0;
YPos = 0;
MaxXPos =934; // Or main layer width
MaxYPos = 768; // Or main layer width
TimeInterval = 80;

YSpeed_Const = 3;
YSpeed_Max = 4;

XSpeed_Const = 0;

Omega_Const = 10;
Omega_Max = 30;
OmegaOnSpeed_Const = 4;
OmegaOnSpeed_Max = 6;
Omega_Norm = (Omega_Const + Omega_Max)*3.14/180/1;

Radius_Const = 20;
Radius_Max = 50;
RadiusOnSpeed_Const = 4;
RadiusOnSpeed_Max = 10;
Radius_Norm = ((Radius_Max + Radius_Const)/1);

YRadius_Max = 1;
YRadius_Smallest = 4;

LongPause_Const = 5;
LongPause_Max = 70;

ShotPause_Const = 25;
ShotPauseOnDelta_Const = 400;
ShotPause_Max = 800;

// Chois brouser
var ns4up = (document.layers) ? 1 : 0;
var ie4up = (document.all) ? 1 : 0;


//--- Load Flakes Image ----
var FlakesPics;
	FlakesPics = new Array();
	for( i = 0; i < MaxFlakePics; i++)
	{
		FlakesPics[i] = new Image();
		FlakesPics[i].src = (FlakesPath + i + FlakesType);
	} 
//--- Create Netscape Layer ---
function FlakeLayerNS( num )
{
	document.write( '<layer name="fl' + num + '">' );
	document.write( "<IMG SRC='" + FlakesPath + Math.floor(Math.random()*MaxFlakePics) + FlakesType + "'>" );
	document.write( "</layer>\n" );	
};
function FlakeLayerIE( num )
{
	document.write( "<IMG SRC='/flake0.gif' ID='fi" + num + "' STYLE='position:absolute;'>" ); // Path not nessesary
};
//--- Create Flakes Laer ---
	for ( i = 0; i < MaxFlakes; i++ )
	{
		if ( ie4up )
			FlakeLayerIE( i );
		else
			FlakeLayerNS( i );		
	}
var Flakes = new Array();
//---------------------
//--- Flake Object ----
//---------------------

function CreateFlake(Number)
{
// Constructor
	this.Number = Number;
};

function GetNewOmega( Flake, speed )
{
// Craete owega speed
   if ( Flake.limit == 0 )
       t_omega = (Omega_Const + Math.random()*Omega_Max);
   else
       t_omega = (speed*OmegaOnSpeed_Const + Math.random()*speed*OmegaOnSpeed_Max);

    t_omega = t_omega * 3.14 / 180 / 6; // transform in radian
    return t_omega;
};

function GetNewRadius( Flake, speed )
{
   if ( Flake.limit == 1 )
    t_radius = Radius_Const + Math.random(Radius_Max);
   else
    t_radius = speed*RadiusOnSpeed_Const + Math.random()*RadiusOnSpeed_Max;
    if ( t_radius < 4 )
      t_radius = 0;
    return t_radius;
}

function GetLongPause( speed )
{
    return (LongPause_Const + Math.random()*LongPause_Max);
}


function GetShotPause(speed, delta)
{
   return ( ShotPause_Const+ delta*ShotPauseOnDelta_Const + Math.random() *delta*ShotPause_Max);
}


function FlakeInit( Flake )
{
// Init new Flakes
        if ( ie4up )
		document.images[("fi"+Flake.Number)].src = FlakesPics[Math.floor(Math.random() * MaxFlakePics)].src; //Change image
 
	Flake.y = MaxYPos+90;
	Flake.x = XPos + Math.random()*MaxXPos;

	Flake.x_speed = XSpeed_Const;
	Flake.y_speed = (YSpeed_Const + Math.random()*YSpeed_Max);

	Flake.limit = Math.floor(Math.random() * 2); // Owega Or Radius Limit

	Flake.omega = GetNewOmega(Flake, Flake.y_speed);
	Flake.omega_v = 0;
	Flake.o_pause = 50;
	Flake.o_count = 0;

    	Flake.radius = GetNewRadius(Flake, Flake.y_speed);
    	Flake.radius_v = 0;
    	Flake.r_pause = 0;
    	Flake.r_count = 100;


    	Flake.y_rad = Math.random()*YRadius_Max*Flake.radius/YRadius_Smallest;

	Flake.fi = Math.random()*360;
	Flake.fi = Flake.fi * 3.14 / 180;


	Flake.y = Flake.y - Flake.radius;
	
}
function Next( Flake )
{
//--- Control Traectory ---

    Flake.x -= Flake.x_speed;
    Flake.y -= Flake.y_speed;

    Flake.fi -= Flake.omega;
    Flake.radius -= Flake.radius_v;
    Flake.omega -= Flake.omega_v;

    Flake.r_count++;
    if ( Flake.r_count > Flake.r_pause )
    {
      Flake.r_count = 0;
      if ( Flake.radius_v != 0 )
      {
         Flake.radius_v = 0;
         Flake.r_pause = GetLongPause( Flake.y_speed );
      }
      else
      {
         a = GetNewRadius(Flake, Flake.y_speed);
         Flake.r_pause = GetShotPause( Flake.y_speed, Math.abs(a - Flake.radius)/Radius_Norm );
         Flake.radius_v = ( a - Flake.radius)/Flake.r_pause;
      };
    };

    Flake.o_count++;
    if ( Flake.o_count > Flake.o_pause )
    {
      Flake.o_count = 0;
      if ( Flake.omega_v != 0 )
      {
         Flake.omega_v = 0;
         Flake.o_pause = GetLongPause( Flake.y_speed );
      }
      else
      {
         a = GetNewOmega(Flake, Flake.y_speed);
         Flake.o_pause = GetShotPause( Flake.y_speed, Math.abs(a - Flake.omega)/Omega_Norm );
         Flake.omega_v = ( a - Flake.omega)/Flake.o_pause;
      };
    };
}


function FlakeGetX( Flake )
{
	a = Flake.x - Flake.radius*Math.cos(Flake.fi);
	return (a);
}

function FlakeGetY( Flake )
{
	a = Flake.y  + Flake.y_rad *Math.sin(Flake.fi);
	return (a);
}
//--- End Flake Object ----
//-------------------------

//--- SetPosition ---
function PositionFlake( Flake )
{
	iname = ("fi" + Flake.Number);
	if ( ie4up )
	{
		document.images[iname].style.top = FlakeGetY( Flake );
		document.images[iname].style.left = FlakeGetX( Flake );
	}
	else
	{
		document.layers[("fl"+Flake.Number)].moveTo(FlakeGetX( Flake ), FlakeGetY( Flake ) ); 
	}
};


function OnTimer()
{
	if ( ns4up )
		// Event OnScroll does not work in Netscape, check scrolling (IZVRAT no rabotaet)
		SetNewValNS();

	for ( i = 0; i < MaxFlakes; i++ )
	{
		Flake = Flakes[i];
		if ( FlakeGetY( Flake )<-90  )
			FlakeInit( Flake );

		Next(Flake);
		PositionFlake(Flake);
	}
//	window.status = Math.random();
	setTimeout("OnTimer();", TimeInterval );	
};

//--- Chenge size-position on scroll on resize
function SetNewValIE ()
{
	YPos = document.body.scrollTop;
	XPos = document.body.scrollLeft;
	MaxXPos = document.body.scrollLeft + document.body.clientWidth- 100;
	MaxYPos = document.body.scrollTop + document.body.clientHeight-50;
};
function SetNewValNS ()
{
	XPos = self.pageXOffset;
	YPos = self.pageYOffset;
	MaxXPos = self.pageXOffset + self.innerWidth;
	MaxYPos = self.pageYOffset + self.innerHeight;
};
function SetNewVal()
{
	if ( ie4up )
		SetNewValIE();
	else
		// Event OnScroll does not work in Netscape, If needed add this code in function OnTimer (IZVRAT no rabotaet)
		SetNewValNS();
};
//--- Set position and event ---
//--- if not needed coment this block ----
	window.onresize = SetNewVal;
	window.onscroll = SetNewVal;
	SetNewVal();

//--- Crate Flake and timer---
	for ( i = 0; i < MaxFlakes; i++ )
	{
		Flakes[i] = new CreateFlake(i);
		FlakeInit(Flakes[i]);
		Flakes[i].y = Math.random() * MaxYPos;
	}

	OnTimer();

