nx=0;ny=0;cx=0;cy=0;
na=0;ca=0;
var position;
spin=new Array();
stepX=new Array();
stepY=new Array();
count = 0;
stepSize = 100; // must be multiple of 4
pointCount = 4; // Set to number of points to walk too
point = 1;
intervalId = 0;

function walk() 
{
    // Only 1 foot moves at a time. Left is 0, right is 1
    count++;
    if (count > stepSize)
        count = 0;
    sizeMultiplier = 0;
        
    if (count > stepSize/2) // Right foot
    {
        i = 1;
        if (count < (stepSize/4)*3) // Lift
        {
            cx = stepX[count-(stepSize/4)];
            cy = stepY[count-(stepSize/4)];
            sizeMultiplier = (count/2)/stepSize;   
        }
        else // Place
        {    
            step();
            stepX[count-(3*stepSize/4)] = cx;
            stepY[count-(3*stepSize/4)] = cy;
            sizeMultiplier = (stepSize-count)/stepSize; 
        }       
    }
    else // Left foot
    {
        i = 0;
        if (count < stepSize/4) // Lift
        {
            if(stepX[count]) cx = stepX[count];
            if(stepY[count]) cy = stepY[count];
            sizeMultiplier = count/stepSize;
        }
        else //Place
        {
            step();
            stepX[count] = cx;
            stepY[count] = cy;
            sizeMultiplier = (stepSize-2*count)/stepSize;
        }
    }
    sizeMultiplier = sizeMultiplier*1.2 - 0.10;
    if(sizeMultiplier < 0) sizeMultiplier = 0;
    if(sizeMultiplier > 0.25) sizeMultiplier = 0.25;
    
    spin[i].style.width=Math.round(35*(1+sizeMultiplier));
    spin[i].style.height=Math.round(42*(1+sizeMultiplier));
    
    spin[i].style.top =cy - Math.sin(ca)*2 + i*0;
    spin[i].style.left=cx - Math.cos(ca)*3 + i*0;
    spin[i].rotation=ca;
} 

function startUp()
{ 
    picHold.innerHTML="";
    next();
    db=document.body;
    position=db.all.tags("IMG");
    for(i=0;i<2;i++)
    { 
        picNet=document.createElement("v:image");
        spin[i]=picHold.appendChild(picNet);
        spin[i].imagedata.src=position[i+1].src;
        spin[i].style.top=5000;
        spin[i].style.position="absolute";
    }
    intervalId = setInterval("walk()",15);
    clear.outerHTML="";
} 

function step()
{
	// move closer to target
	var nHit = 0;
	var fMove = function(cn,tn, amount, hitCount, degrees) 
	{
		var dn = tn - cn;
		if ( Math.abs(dn) < 5 )
		{
			if (hitCount) nHit++;
			return tn;
		}
		else
		{
		    if (degrees)
		    {
		        dc = 1;
		        if(Math.abs(dn) <= 180)
		        {
		            if (cn < tn) dc = cn + amount;
		            else dc = cn - amount;
		        }
                else
                {
                    if (cn < tn) dc = cn - amount;
                    dc = cn + amount;	
                }
                if (dc > 360) return 360-dc;
                if (dc < 0) return 0-dc;
                return dc;	        
		    }
		    else
		    {
    		    if (cn < tn) return cn + amount;
    		    else return cn - amount;
		    }
		}
	}
	
	dx = Math.abs(cx-nx);
	dy = Math.abs(cy-ny);
	
	if (dx > dy) 
	{
	    xAmount = 2;
	    yAmount = 2*dy/dx;
	}
	else 
	{
	    xAmount = 2*dx/dy;
	    yAmount = 2;
	}
		
	cx = fMove(cx, nx, xAmount, true, false);
	cy = fMove(cy, ny, yAmount, true, false);
    na = Math.atan2(ny-cy,nx-cx)*180 / Math.PI;
    if (na < 0) na = 360 + na;
	ca = fMove(ca, na, 4, false, true);
	
	// See if we are close enough to target to set a new target
	if (nHit == 2)
	{
    	if (point > pointCount)
    	{
            spin[0].style.visibility="hidden";
            spin[1].style.visibility="hidden";    	
            clearInterval(intervalId);
            return;    
    	}
	    nextCount = 0;
	    do
	    {	        
    	    this.next(false);
    	    nextCount++;
    	} while (Math.abs(ca - na) > 140 && nextCount < 20)
    	if (point++ == pointCount) this.next(true);
    	
	}
}

function next(endWalk)
{
	// calculate screen dimensions
	var frameWidth = 0;
	var frameHeight = 0;
	if (self.innerWidth)
    {
    	frameWidth = self.innerWidth;
    	frameHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientWidth)
    {
    	frameWidth = document.documentElement.clientWidth;
    	frameHeight = document.documentElement.clientHeight;
    }
    else if (document.body)
    {
    	frameWidth = document.body.clientWidth;
    	frameHeight = document.body.clientHeight;
    }
    
    // Create a point on the visible screen to walk too
    if (endWalk)
    {
        nx = frameWidth + 10;
        ny = frameHeight + 10;
    }
    else
    {
        nx = Math.ceil(Math.random()*frameWidth);
        ny = Math.ceil(Math.random()*frameHeight);
    }
    
    // Account for a scrolled screen
	nx = nx + document.body.scrollLeft + document.documentElement.scrollLeft;
	ny = ny + document.body.scrollTop + document.documentElement.scrollTop;
		
    na = Math.atan2(ny-cy,nx-cx)*180 / Math.PI;
    if (na < 0) na = 360 + na; 
    
//    alert('next x' + nx + ' y ' + ny + ' next a ' + na + ' current a ' + ca);
}

