
$(document).ready(function(){

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;
    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
          return from;
    }
    return -1;
  };
}


/* init */

$('#stage').data('shot_counter', 0);
$('#stage').data('held_gesundheit', 100);
$('#stage').data('killed', 0);
$('#stage').data('thislevel_killed', 0);
$('#stage').data('stop' , '0');
$('#levelnachricht').html('<a id="start">Start</a>'); 

var current_level=1;
var gegnerschusspower=2;

$('#start').click(function(){ next_level(); });
//$('#stop').click(function() { $('#stage').stopTime('controlled');  });


function next_level(){
	$('#levelnachricht_con').show('slow'); 
  $('#stage').data('stop' , '0');
	$('.alive').remove(); 

				$("#levelnachricht").oneTime(1000, function() {
					$(this).html("LEVEL "+ current_level);
				  }).oneTime(5000, "soon", function() {
					$(this).html('');
					$('#levelnachricht_con').hide('slow'); 
 START();
				});
}



function START(){

/* erzeugen der Gegner in Abhgk. vom Level    3500 - current_level * 100          */

	$(function(){ 
				$('#stage').everyTime(  3500 - current_level * 100  ,  'controlled'     , function(i){ var Obj =  new Gegner();  Obj.firstmove(); });
				});
}



function Gegner(){
var name = "gegner_"+Math.ceil(Math.random()*15000);
var r_name = 'r_'+name;

/* var speed=Math.ceil(Math.random()*6000);
this.speed= speed; */

switch(current_level){
 case 1: speed=2500; break;
 case 3: speed=2000; break;
 case 5: speed=1500; break;
 case 7: speed=1000; break;
 case 9: speed=500; break;

}
this.speed= speed;

this.m_silhouette = makeSilhouette();

this.sil_with= this.m_silhouette[0];
this.sil_width= this.m_silhouette[0];
this.sil_height= this.m_silhouette[1];
this.sil_image= this.m_silhouette[2];
this.sil_pos_x= this.m_silhouette[3];
this.sil_pos_y= this.m_silhouette[4];
this.silhouette= this.m_silhouette[5];

$('#gegner_umgebung').prepend('<div id="'+ r_name +'" class="alive '+ name +'" style="position:absolute; left:'+ this.sil_pos_x +'px; top:'+ this.sil_pos_y +'px;width:'+ this.sil_with +'px;height:'+ this.sil_height +'px;background:url(images/'+ this.sil_image +') no-repeat;"></div>'); 
$('#gegner_umgebung').prepend('<img id="'+ name +'" class="alive '+ name +'" style="position:absolute; left:'+ this.sil_pos_x +'px; top:'+ this.sil_pos_y +'px;width:'+ this.sil_with +'px;height:'+ this.sil_height +'px;z-index:109;"  src="images/blind.gif" width="'+ this.sil_with +'" />'); 

this.id = name; 
this.ai = name; 
this.air = r_name; 

this.firstmove = firstmove; 


//$('#gegner_kontrollkaesten').prepend('<div id="kk_'+ name +'" style="width:75px;height:75px;float:left;padding-left:5px;">'+name+' '+speed+'</div>'); 

// funktioniert nicht ausserhalb der Funktion Gegner - weil DOM aenderung
$('.alive').click(function() {    var mansID=$(this).attr('id');       $('.'+mansID).remove();  akill(); });
$('.hiddening').click(function() {   var mansID=$(this).attr('id');    $('.'+mansID).remove();  });

}



function firstmove(){   

/* $('#j_info').prepend('<p>Sil: '+this.silhouette+' Speed: '+this.speed+'</p>');  */

if(this.silhouette==4){
$('#'+this.ai).animate ({ top: (this.sil_pos_y-this.sil_height) }, 1500); // positionieren
$('#'+this.ai).animate ({opacity: "1" }, this.speed ); // zielen
$('#'+this.ai).animate ({opacity: "1" }, 2400, 'linear' ); 
$('#'+this.ai).animate ({ top: this.sil_pos_y }, 2500 ); //verstecken
$('#'+this.ai).animate ({"opacity" : 1}, 500, function(){  $('#'+this.id).removeClass("alive");  $('#'+this.id).addClass("hiddening");   });

$('#'+this.air).animate ({ top: (this.sil_pos_y-this.sil_height) }, 1500); // positionieren
$('#'+this.air).animate ({opacity: "1" }, this.speed ); // zielen
$('#'+this.air).animate ({opacity: "1" }, 2400, 'linear', ashot ); //schiessen              !! vom Zufall abh&auml;ngig machen, 50:50  
$('#'+this.air).animate ({ top: this.sil_pos_y }, 2500 ); //verstecken
$('#'+this.air).animate ({"opacity" : 1}, 500, function(){  $('#'+this.id).removeClass("alive");  $('#'+this.id).addClass("hiddening");   });
}

if(this.silhouette==1){
$('#'+this.ai).animate ({ left: (this.sil_pos_x+this.sil_width) }, 1500); // positionieren
$('#'+this.ai).animate ({opacity: "1" }, this.speed ); // zielen
$('#'+this.ai).animate ({opacity: "1" }, 2400, 'linear'); //schiessen              !! vom Zufall abh&auml;ngig machen, 50:50  
$('#'+this.ai).animate ({ left: this.sil_pos_x }, 2500 ); //verstecken
$('#'+this.ai).animate({"opacity" : 1}, 500, function(){ $('#'+this.id).removeClass("alive");  $('#'+this.id).addClass("hiddening");   });

$('#'+this.air).animate ({ left: (this.sil_pos_x+this.sil_width) }, 1500); // positionieren
$('#'+this.air).animate ({opacity: "1" }, this.speed ); // zielen
$('#'+this.air).animate ({opacity: "1" }, 2400, 'linear', ashot ); //schiessen              !! vom Zufall abh&auml;ngig machen, 50:50  
$('#'+this.air).animate ({ left: this.sil_pos_x }, 2500 ); //verstecken
$('#'+this.air).animate({"opacity" : 1}, 500, function(){ $('#'+this.id).removeClass("alive");  $('#'+this.id).addClass("hiddening");   });
}

if(this.silhouette==2){
$('#'+this.ai).animate ({ left: (this.sil_pos_x-this.sil_width) }, 1500); // positionieren
$('#'+this.ai).animate ({opacity: "1" }, this.speed ); // zielen
$('#'+this.ai).animate ({opacity: "1" }, 2400, 'linear' ); //schiessen              !! vom Zufall abh&auml;ngig machen, 50:50  
$('#'+this.ai).animate ({ left: this.sil_pos_x }, 2500 ); //verstecken
$('#'+this.ai).animate({"opacity" : 1}, 500, function(){  $('#'+this.id).removeClass("alive");  $('#'+this.id).addClass("hiddening");   });

$('#'+this.air).animate ({ left: (this.sil_pos_x-this.sil_width) }, 1500); // positionieren
$('#'+this.air).animate ({opacity: "1" }, this.speed ); // zielen
$('#'+this.air).animate ({opacity: "1" }, 2400, 'linear', ashot ); //schiessen              !! vom Zufall abh&auml;ngig machen, 50:50  
$('#'+this.air).animate ({ left: this.sil_pos_x }, 2500 ); //verstecken
$('#'+this.air).animate ({"opacity" : 1}, 500, function(){  $('#'+this.id).removeClass("alive");  $('#'+this.id).addClass("hiddening");   });
}

if(this.silhouette==5){
$('#'+this.ai).animate ({ top: (this.sil_pos_y-this.sil_height) }, 1500); // positionieren
$('#'+this.ai).animate ({opacity: "1" }, this.speed ); // zielen
$('#'+this.ai).animate ({opacity: "1" }, 2400, 'linear' ); 
$('#'+this.ai).animate ({ top: this.sil_pos_y }, 2500 ); //verstecken
$('#'+this.ai).animate ({"opacity" : 1}, 500, function(){  $('#'+this.id).removeClass("alive");  $('#'+this.id).addClass("hiddening");   });

$('#'+this.air).animate ({ top: (this.sil_pos_y-this.sil_height) }, 1500); // positionieren
$('#'+this.air).animate ({opacity: "1" }, this.speed ); // zielen
$('#'+this.air).animate ({opacity: "1" }, 2400, 'linear', ashot ); //schiessen              !! vom Zufall abh&auml;ngig machen, 50:50  
$('#'+this.air).animate ({ top: this.sil_pos_y }, 2500 ); //verstecken
$('#'+this.air).animate ({"opacity" : 1}, 500, function(){  $('#'+this.id).removeClass("alive");  $('#'+this.id).addClass("hiddening");   });
}

/* der callback kann this nicht wieder aufrufen, kA warum. W&auml;re schon nicht schlecht, wenn Gegner, an der selben Stelle wieder auftaucht, wenn ernicht erschossen ist  */
}

var kleine_fenster_anr=new Array();
kleine_fenster_anr=[7,8,9,23,24,25,26,27,28,36,37,38];


function makeSilhouette(){
var roter_mann=zufallszahl(0,9); // ab Level 5  Wahrscheinlichkeit des Erscheinens 10%
if(current_level >= 5 && roter_mann == 4 ){silbynumbers=zufallszahl(1,4);}
else{silbynumbers=zufallszahl(1,3);}

// roterMann umgehen - var ueberschreiben
silbynumbers=zufallszahl(1,3);


switch(silbynumbers){

// hlb Silhouette von links
case 1: 
this.silhouette=1;
this.sil_width=8; 
this.sil_height=33; 
this.sil_image='gegner_li.gif';   

do{var zi=zufallszahl(0,45);}
while(kleine_fenster_anr.indexOf(zi)!=-1)

this.sil_pos_x=alle_fenster[zi][0]-this.sil_width;
this.sil_pos_y=alle_fenster[zi][3]-this.sil_height+2;
break; 


// hlb Silhouette von rechts
case 2: 
this.silhouette=2;
this.sil_width=8; 
this.sil_height=33; 
this.sil_image='gegner_re.gif';   


do{var zi=zufallszahl(0,45);}
while(kleine_fenster_anr.indexOf(zi)!=-1)

this.sil_pos_x=alle_fenster[zi][2];
this.sil_pos_y=alle_fenster[zi][3]-this.sil_height+2;
break; 


// Silhouette Kopf
case 3: 
this.silhouette=4;
this.sil_width=20; 
this.sil_height=16; 
this.sil_image='gegner_kopf.gif';

var zi=zufallszahl(0,45);

this.sil_pos_x=alle_fenster[zi][0];
this.sil_pos_y=alle_fenster[zi][3];
//this.sil_pos_y=alle_fenster[zi][3]+(this.sil_height/2);

break; 


// Silhouette Kopf
case 4: 
this.silhouette=5;
this.sil_width=20; 
this.sil_height=16; 
this.sil_image='gegner_kopf_rot.gif';

var zi=zufallszahl(0,45);


this.sil_pos_x=alle_fenster[zi][0];
this.sil_pos_y=alle_fenster[zi][3];
//this.sil_pos_y=alle_fenster[zi][3]+(this.sil_height/2);

break; 
}


//$('#j_info').prepend('<p>Sil: '+this.silhouette+' Zahl: '+zi+'  Level: '+current_level+'</p>'); 

return [sil_width, sil_height, sil_image, sil_pos_x, sil_pos_y, silhouette];
}



function zufallszahl(min,max){
var num=Math.floor(min+(max-min+1)*(Math.random()));
return num;
}
   

function ashot(){ 
if($('#stage').data('stop')=="1"){return false;}

var shot_count = parseInt($('#stage').data('shot_counter'));
shot_count++;
$('#stage').data('shot_counter', shot_count);
$('#schuesse').html(shot_count);

//$('#stage').effect("bounce", { times:1 }, 300);
$('#blood').slideToggle(200);
$('#blood').slideToggle(200);

switch(current_level){
 case 3: gegnerschusspower=4; break;
 case 5: gegnerschusspower=6; break;
 case 7: gegnerschusspower=8; break;
 case 9: gegnerschusspower=10; break;
}

var held_gesundheit =parseInt($('#stage').data('held_gesundheit'));
//held_gesundheit = held_gesundheit-(gegnerschusspower/2);
held_gesundheit = held_gesundheit-gegnerschusspower;
$('#stage').data('held_gesundheit', held_gesundheit); 
$('#gesundheit').width(held_gesundheit);  

if(held_gesundheit<=0){game_over();}
}


function game_over(){
$('#stage').data('stop' , '1');
$('#stage').stopTime('controlled');
$('#gesundheit').width(0); 
$('#gegner_umgebung').html(''); 
$('#levelnachricht_con').show();
$('#levelnachricht').html('Game Over<br><a style="color:#cc0000;text-decoration:none;font-size:14px;" href="index.php">replay?</a>'); 
}


function akill(){  // - > Prueffunktion  genug gekilled Levelsoll erfuellt?
var kill_count = parseInt($('#stage').data('killed')); 
var levelkill_count = parseInt($('#stage').data('thislevel_killed')); 
var levelsoll = 3 * current_level;

kill_count++;
levelkill_count++;

$('#stage').data('killed', kill_count);
$('#stage').data('thislevel_killed', levelkill_count);
$('#killed').html(kill_count);

if(levelkill_count == levelsoll){ 
$('#stage').data('stop' , '1');
$('#stage').data('thislevel_killed', 0);
$('#stage').stopTime('controlled');

current_level++;
next_level(); 
}

}


});//endready
