function Level( lev ){
	this.bubbleMax = 0;
	this.bubbleCount = 0;
    this.paused = 1;
    this.someoneJumping = 0;
    this.guys = new Array();
	this.machine = new Machine();
    this.guyNum = 0;//
	this.jumpChance = 0;
    this.floorNum = 0;//= 5;
    this.floorHeight = 70;
    this.floorOff = 0;//8;
    this.leftWallX = 10;
    this.rightWallX = 310;
    this.sickChances = 1;
    this.gameTime = 0;//60000;
    this.floorChart = new Array();
    this.switchLevel = switchLevel;
    this.setPaused = setPaused;
    this.initLevel = initLevel;
	this.handleBumpage = handleBumpage;
	this.executeOneFrame = executeOneFrame;
	this.updateInputEffects = updateInputEffects;
	this.getGuyOnLevSoCloseToX = getGuyOnLevSoCloseToX;
	this.passSickness = passSickness;
                  this.insertGuy = insertGuy;
                  this.deleteGuy = deleteGuy;
                  this.getSomeoneJumping = getSomeoneJumping;
                  this.setSomeoneJumping = setSomeoneJumping;
                  this.getGuyUnderTarget = getGuyUnderTarget;
                  this.updateTarget = updateTarget;
				  this.scoreLevel = scoreLevel;
    this.initLevel( lev ); 
}

function getSomeoneJumping(){
      return this.someoneJumping;
}

function setSomeoneJumping( b ){
      this.someoneJumping = b;
}

function setPaused( p ){
     if ( this.paused && !p ) repeatLevelFrame();
     this.paused = p;
}

function initLevel( lev ){
	randCount = lev;
	document.getElementById( "machine").style.display = "block";
    this.switchLevel( lev );
	for ( var a = 0; a < this.guyNum; a++ ){
       this.guys.push( new Guy(a) );
       this.guys[a].floorLev = Math.floor(a / ( this.guyNum / this.floorNum ));
       for ( var b = 0; b < 5; b++ ){
          if ( getRand() < this.sickChances[b] ) this.guys[a].sick = b;
       }
       this.floorChart[this.guys[a].floorLev].push( a );
    }
	setGuyConstants( {jumpChance:this.jumpChance, floorNum:this.floorNum, floorOff:this.floorOff, floorHeight:this.floorHeight, leftWallX:this.leftWallX, rightWallX:this.rightWallX} );
    for (  var a = 0; a < this.floorNum; a++ ){
        var c = this.floorChart[a].length;
        var d = (this.rightWallX - this.leftWallX) / c;
        for ( var b = 0; b < c; b++ ){
              this.guys[this.floorChart[a][b]].xco =  Math.floor(d*b + getRand() * .5 * d ) + 15;
        }
    }
   
    var elem = document.getElementById( "creatures" );       
    elem.innerHTML = "";
    for ( var a = 0; a < this.guyNum; a++ ){
         elem.innerHTML += "<div id=\"guy" + a + "\" class=\"guy\" ><div id=\"bubble" + a + "\" class=\"bubble\" ></div></div>";
    }
    elem = document.getElementById( "xCreatures" );       
    elem.innerHTML = "";
    for ( var a = 0; a < this.guyNum; a++ ){
      elem.innerHTML += "<div id=\"xGuy" + a + "\" class=\"xGuy\" ><div id=\"xBubble" + a + "\" class=\"xBubble\" ></div><div id=\"heart" + a + "\" class=\"heart\" ></div></div>";
    }
	document.getElementById("countDown").innerHTML = "";
	this.executeOneFrame();
	
}

function switchLevel( lev ){
     switch( lev ){
          case 1:
               this.sickChances = [ 1, .85, 0, 0, 0, 0 ];
               this.guyNum = 10;
               this.floorNum = 4;
               this.floorOff = 38;
               this.gameTime = 40000;
			   this.jumpChance = 0;
			   this.bubbleMax = 0;
               break;
			case 2:
               this.sickChances = [ 1, .3, .75, 0, 0, 0 ];
               this.guyNum = 10;
               this.floorNum = 4;
               this.floorOff = 38;
			   this.gameTime = 55000;
               this.jumpChance = 0;
			   this.bubbleMax = 0;
			   break;   
			case 3:
               this.sickChances = [ 1, 1, .85, 0, 0, 0 ];
               this.guyNum = 10;
               this.floorNum = 4;
               this.floorOff = 38;
			   this.gameTime = 65000;
               this.jumpChance = 1;
			   this.bubbleMax = 0;
			   break; 
			case 4:
               this.sickChances = [ 1, 1, .8, 0, 0, 0 ];
               this.guyNum = 15;
               this.floorNum = 5;
               this.floorOff = 8;
			   this.gameTime = 75000;
               this.jumpChance = 1;
			   this.bubbleMax = 0;
			   break;    
	case 5:
               this.sickChances = [ 1, .5, .6, .85, 0, 0 ];
               this.guyNum = 10;
               this.floorNum = 5;
               this.floorOff = 8;
			   this.gameTime = 90000;
               this.jumpChance = 1;
			   this.bubbleMax = 0;
			   break;    
	case 6:
               this.sickChances = [ 1, .5, .6, .85, 0, 0 ];
               this.guyNum = 10;
               this.floorNum = 5;
               this.floorOff = 8;
			   this.gameTime = 85000;
               this.jumpChance = 1;
			   this.bubbleMax = 2;
			   break;    
     case 7:
               this.sickChances = [ 1, .5, .6, .85, 0, 0 ];
               this.guyNum = 10;
               this.floorNum = 5;
               this.floorOff = 8;
			   this.gameTime = 80000;
               this.jumpChance = 1;
			   this.bubbleMax = 3;
			   break;    
		case 8:
               this.sickChances = [ 1, .5, .6, .85, 0, 0 ];
               this.guyNum = 15;
               this.floorNum = 5;
               this.floorOff = 8;
			   this.gameTime = 120000;
               this.jumpChance = 1;
			   this.bubbleMax = 4;
			   break;
		 case 9:
               this.sickChances = [ 1, .5, .6, .85, 0, 0 ];
               this.guyNum = 15;
               this.floorNum = 5;
               this.floorOff = 8;
			   this.gameTime = 110000;
               this.jumpChance = 1;
			   this.bubbleMax = 4;
			   break;   
		 case 10:
               this.sickChances = [ 1, .5, .6, .85, 0, 0 ];
               this.guyNum = 15;
               this.floorNum = 5;
               this.floorOff = 8;
			   this.gameTime = 100000;
               this.jumpChance = 1;
			   this.bubbleMax = 4;
			   break;   
     }
     this.gameCount = this.gameTime;
     for ( var q = 0; q < this.floorNum; q++ ){
         this.floorChart[q] = new Array();
    }
}

function executeOneFrame(){
    if ( !this.paused ) repeatLevelFrame();
	this.updateInputEffects();
    for ( var a = 0; a < this.guyNum; a++ ){
		this.guys[a].updateGuy();
	}
	this.handleBumpage();
	for ( var a = 0; a < this.guyNum; a++ ){
		this.guys[a].renderGuy();
	}
	this.machine.renderMachine();
    this.updateTarget();
	this.gameCount -= 100;
    document.getElementById("bar2").style.width = (320 - Math.ceil( 320 * this.gameCount / this.gameTime )) + "px";
    if ( this.gameCount < 9000 ){
		document.getElementById("countDown").innerHTML = "" + Math.ceil( this.gameCount / 1000 );
	}
	if ( this.gameCount <= 0 ){
         this.setPaused( 1 );
         document.getElementById( "machine").style.display = "none";
		 document.getElementById( "xRealm" ).style.clip = "rect( " + 0  + "px " +  320  + "px " + 356 + "px " + 0 + "px)";
         
		 if ( this.gameCount == 0 ) this.scoreLevel();
    }
	
}

function scoreLevel(){
	document.getElementById( "machine").style.display = "none";
	document.getElementById( "xRealm" ).style.clip = "rect( " + 0  + "px " +  320  + "px " + 356 + "px " + 0 + "px)";
	var won = 1;
	for ( var a = 0; a < this.guyNum; a++ ){
		if ( this.guys[a].sick > 0 ){
			won = 0;
			break;
		}
	}
	if ( won ){
		levelCount++;
		setTimeout( "screenManager.showScreen( 'success', 'fade', 'in', introduceLevel, 1 );", 10 );
	}else{
		setTimeout( "screenManager.showScreen( 'failure', 'fade', 'in', introduceLevel, 1 );", 10 );
	}
}

function handleBumpage(){ 
    for ( var a = 0; a < this.floorNum; a++ ){
        var f = this.floorChart[a];
        var zz = f.length;
        if ( zz == 0 ) continue;
        var t = this.guys[f[0]];
		var tt = 0;
        if ( t.act == 0 && t.xco < this.leftWallX + t.wid ) t.act=3;
        for ( var b = 1; b < zz; b++ ){
           t = this.guys[f[b]];
           tt = this.guys[f[b-1]];
           if ( t.xco < tt.xco + t.wid + tt.wid ){
                var ps = 0;
                if ( t.act == 0 && t.dir == -1 ){
                    t.act = 3;
                    ps = 1;
                }
                if ( tt.act == 0 && tt.dir == 1){
                    tt.act = 3;
                    if ( ps ) this.passSickness( t, tt );                       
                }
            }
        }
        t = this.guys[f[zz-1]];
        if ( t.act == 0 && t.xco > this.rightWallX - t.wid ) t.act = 3;
    }
}
 
function passSickness( t, tt ){
     if ( t.inBubble || tt.inBubble ) return;
     if ( t.sick > tt.sick ){
           var ttt = t;
           t = tt;
           tt = ttt;
      }
      if ( t.sick < tt.sick - 1 ){
           t.sick++;
           t.redrawHeart = 1;
      } 
}
 
function updateInputEffects(){
	if ( touchStart == 2 ){
		touchStart = 3;
		level.setPaused( 1 );
		screenManager.showScreen( 'pause', 'horiz', 'in' );
	}else{
	   if ( touchStart < 1 ) this.machine.handleMachineDrag();
	   if ( touchStart == 3 ) touchStart = 0;
	}
    if ( touchStart == 1 ){
        touchStart = 0;
        if ( !this.machine.handleMachineTouch() ){        
           var lev = Math.floor(( touchY - this.floorOff ) / this.floorHeight );
           var y = touchY - ( lev * this.floorHeight + this.floorOff );
           if ( y < 60 ){
			   var g = this.getGuyOnLevSoCloseToX( lev, 10, touchX, 1 );
			   if ( g > -1 ){
				   if ( this.guys[g].inBubble ){
				      this.guys[g].toggleBubble();
					  this.bubbleCount--;
				   }else if( this.bubbleCount < this.bubbleMax ){
					  this.guys[g].toggleBubble();
					  this.bubbleCount++;
				   }
			   }
		   }
        }
    }else if( touchStart == - 1 ){
		this.machine.dragging = 0;
		touchStart = 0;
		
	}
}

function getGuyOnLevSoCloseToX( lev, fuzz, x, w){
   
	lev = this.floorChart[lev];
	var t = lev.length;
    if ( t == 0 ) return -1;
    for ( var b = 0; b < t; b++ ){
          if ( this.guys[lev[b]].xco  >= x ) break;
    } 
    if ( b > 0 ){ 
         var c = lev[b-1];
		 var ww = w * this.guys[c].wid;
         if ( Math.abs( x - this.guys[c].xco) < fuzz + ww ) return c;
	}
    if ( b < t  ){
         var c = lev[b]; 
		 var ww = w * this.guys[c].wid;
         if ( Math.abs( x - this.guys[c].xco) < fuzz + ww ) return c;
    }
    return -1;
}

 function insertGuy( a, lev ){
        var f = this.floorChart[lev];
        var b;
        var t = f.length;
        for ( b = 0; b < t; b++ ){
            if ( this.guys[f[b]].xco >= this.guys[a].xco ) break;
        }    
        if ( b== t ) f.push(a);
        else f.splice( b, 0, a );
}

function deleteGuy( a, lev ){
         var f = this.floorChart[lev];
         var b;
         var t = f.length;
         for ( b = 0; b < t;  b++ ){
              if ( f[b] == a ){
                   f.splice(b, 1); 
                   break;
              }   
         }                     
    }

function getGuyUnderTarget(){
           var lev = Math.floor(( this.machine.yco - this.floorOff ) / this.floorHeight );
           y = this.machine.yco - ( lev * this.floorHeight + this.floorOff );
           if ( Math.abs( y - 23) < 20 ){
                  return this.getGuyOnLevSoCloseToX( lev, 20, this.machine.xco, 0);
           } 
           return -1;
    }

    function updateTarget(){
         var m = this.machine;
         m.targGuy = this.getGuyUnderTarget();
         if ( m.targGuy != m.oldTargGuy ){
            if ( m.targCount > 0 ){
                    m.targPose = 2;
                    m.targCount = 0;
             }else{
                    m.targPose = 0;
             }
            m.oldTargGuy = m.targGuy;
                                  
         }else{
             if ( m.targGuy > -1 && !this.guys[m.targGuy].inBubble ){
             if ( this.guys[m.targGuy].sick > 0 ){
                 m.targCount++;
                 if ( m.targCount == 5 * this.guys[m.targGuy].sick + 5 ){
                     this.guys[m.targGuy].sick--;
                     this.guys[m.targGuy].redrawHeart = 1;
                     m.targCount = 0;
                 }else{
                      m.targPose = 1;
                      if ( m.targCount % 4 >1 ) m.targPose = 4; 
                 }
            }else {
                 m.targPose = 0;
                 m.targCount = 0;
            }      
            }else m.targPose = 0;
        }
    }
