function setGuyConstants( ob ){
   guyConst = new Object();
   for ( q in ob ){
	   guyConst[q] = ob[q];
   }
}


function Guy(g){
	 this.id = g;     
     this.xco = 0;
     this.redrawHeart = 1;
     this.floorLev = 0;
     this.act = 0;
     this.sick = 0;
     this.actCount = g % 24;
	 this.pose = this.actCount % 8;
     this.jumpCount = 0;
     this.air = 0;
     this.inBubble = 0;
     this.wid = 14;
     this.redrawBubble = 0;
     this.dir = 1;
     if ( getRand() < .5 ) this.dir = -1;
	 this.updateGuy = updateGuy;
	 this.renderGuy = renderGuy;
	 this.handleWalk = handleWalk;
	 this.handleTurn = handleTurn;
                   this.handleJumpUp = handleJumpUp;
                   this.handleJumpDown = handleJumpDown;
	 this.toggleBubble = toggleBubble;
	 //this.setGuyContants = setGuyConstants;
}

function toggleBubble(){
	this.redrawBubble = 1;
    this.inBubble = !this.inBubble;
	if ( this.inBubble ) this.wid = 30;
	else this.wid = 14;
}

function updateGuy(){
	 this.actCount++;
     if ( this.actCount == 24 ) this.actCount = 0;
     switch( this.act ){
        case 0: this.handleWalk(); break;  
        case 1: this.handleJumpUp(); break; 
        case 2: this.handleJumpDown(); break;  
        case 3: this.handleTurn(); break;
        case 4: this.handleWalk(); this.act = 0; break;
     }
}

 function handleWalk(){
    this.pose = this.actCount % 8;
    if ( this.actCount == 0 ){
    if ( getRand() < guyConst.jumpChance ){
          if ( ( !this.inBubble) && level.getSomeoneJumping() == 0 ){

                   if ( getRand() < .5 ){
                            if ( this.floorLev < guyConst.floorNum - 1 && level.getGuyOnLevSoCloseToX( this.floorLev + 1, 50, this.xco, 1) == -1 ) {
                                  this.act = 2;
                                  level.setSomeoneJumping( 1 );
                             } 
                    }else{
                            if ( this.floorLev > 0 && level.getGuyOnLevSoCloseToX( this.floorLev - 1, 50, this.xco, 1) == -1 ){
                                   this.act = 1;
                                   level.setSomeoneJumping( 1 );
                            }
                    }
          }
    }else{
            if ( getRand() < .5 ) this.pose = 8;
    }
    }
    this.xco += this.dir * 3;
}

 function handleJumpUp(){
            this.jumpCount++;
            var j = this.jumpCount;
            if ( j < 4 ) this.pose = j + 8;
            else if ( j < 6 ) this.pose = 15 - j;
            else if ( j == 18 ){
                  this.act = 0;
                  this.floorLev--;
                  this.air = 0;
                  this.jumpCount = 0;
                  this.pose = 11;
                  level.setSomeoneJumping( 0 ); 
            }else{
                if ( j == 6 ) level.insertGuy( this.id, this.floorLev-1 );
                else if ( j == 8 ) level.deleteGuy( this.id, this.floorLev );
                this.pose = 0;
                this.air =  (14 -j) * (14-j) -77; 
            }
    }

   function handleJumpDown(){
            this.jumpCount++;
            var j = this.jumpCount;
            if ( j < 4 ) this.pose = j + 8;
            else if ( j < 6 ) this.pose = 15 - j;
            else if ( j == 13 ){
                  this.act = 0;
                  this.floorLev++;
                  this.air = 0;
                  this.jumpCount = 0;
                  this.pose = 11;
                  level.setSomeoneJumping( 0 ); 
            }else{
                if ( j == 8 ){
                     level.insertGuy( this.id, this.floorLev+1 );
                     level.deleteGuy( this.id, this.floorLev );
                }
                this.pose = 0;
                this.air =  (4 -j)*(4-j) ; 
            }
    }


function handleTurn(){
   this.pose = this.actCount % 8;
   if ( this.actCount % 5 == 0 ){
       this.dir *= -1;
       if ( this.xco < guyConst.leftWallX + this.wid  ) this.xco = guyConst.leftWallX + this.wid;
       else if ( this.xco > guyConst.rightWallX - this.wid ) this.xco = guyConst.rightWallX - this.wid;
       this.act = 4; 
    }
}
 
function renderGuy(){
    var elem = document.getElementById( "guy" + this.id );
    var elem2 = document.getElementById( "xGuy" + this.id );
	var val = (this.xco - 15) + "px";
	elem.style.left = val;
	elem2.style.left = val;
    val = (this.floorLev * guyConst.floorHeight + guyConst.floorOff + this.air) + "px";
    elem.style.top = val;
	elem2.style.top = val;
    if ( this.dir == -1 ) val = -Math.floor( 31.833333 * this.pose ) + "px 0px";
    else val = Math.floor( 31.833333 * ( this.pose + 1 ) -382 ) + "px -60px";
    elem.style.backgroundPosition = val;
	elem2.style.backgroundPosition = val;
    if ( this.redrawBubble ){
       this.redrawBubble = 0;
       if ( this.inBubble == 1 ) val = "block";
	   else val = "none";
	   document.getElementById( "bubble" + this.id ).style.display = val;
	   document.getElementById( "xBubble" + this.id ).style.display = val;
	}
    if ( this.redrawHeart ){
       this.redrawHeart = 0;
       elem = document.getElementById( "heart" + this.id );
       elem.style.top = "";
       elem.style.backgroundPosition = (this.sick * -10) + "px 0px";
	}
    if ( this.pose == 11 ){
        document.getElementById( "heart" + this.id ).style.top = "24px";
        this.redrawHeart = 1;
	}
}