// Diese Routinen und Algorithmen sind urheberlich geschützt (c) von Reinhard Doberstein, Mannheim; info@doberstein.com !
// This funktions are copyright (c) by Reinhard Doberstein, info@doberstein.com !

anzahl_fliegen = 5;

abstand_oben = 1;
abstand_links = 1;

Maus_links = 0.0;
Maus_oben = 0.0;

Fliegen_werte = new Array(anzahl_fliegen * 2);

/*
	// passend für NS
faktor_freund = 0.01;
faktor_feind = 10.0;
faktor_maus = 0.01;
faktor_random = 5.0;
faktor_daempfung = 0.9;
*/


faktor_freund = 0.01;
faktor_feind = 10.0;
faktor_maus = 0.01;
faktor_random = 5.0;
faktor_daempfung = 0.9;

ns =0;
ie =0;

loop_anzahl = 0;

function NachZahl(wert){
  NachZahl_erg = "";
  NachZahl_zahlen = "0123456789";
  NachZahl_komma = ".,";
  NachZahl_le = wert.length;
  for (NachZahl_i=0; NachZahl_i < NachZahl_le; NachZahl_i++) {
    NachZahl_c = wert.charAt(NachZahl_i);
    if ( NachZahl_zahlen.indexOf(NachZahl_c) >= 0) {
      NachZahl_erg = NachZahl_erg + NachZahl_c;
    } else {
      if ( NachZahl_komma.indexOf(NachZahl_c) >= 0) {
        NachZahl_erg = NachZahl_erg + ".";
      }
    }
  }
  return NachZahl_erg;
}	// function nachZahl()

function get_Fliegen_parameter(nr, was)
{
// if ( (nr + was) == 0 ) { window.status = ("wo = "+ (nr*anzahl_fliegen  + was) + " was = "+ Fliegen_werte [nr*anzahl_fliegen  + was]); }
	return (Fliegen_werte [nr*anzahl_fliegen  + was]);
}	// function get_Fliegen_parameter(nr, was)

function set_Fliegen_parameter(nr, was,wert)
{
// if ( (nr + was) == 0 ) { window.status = ("Set wo = "+ (nr*anzahl_fliegen  + was) + " was = "+ wert ); }
	Fliegen_werte [nr*anzahl_fliegen  + was] = wert;
}	// function set_Fliegen_parameter(nr, was)

function Mauskontrolle_ie() {
	Maus_links = window.event.x;
	Maus_oben = window.event.y;
	txt = "tst_pos_IE: " + Maus_links +"/"+ Maus_oben;
	window.defaultStatus = txt;
	return true;
  }

function Mauskontrolle_ns(Ereignis) {
	Maus_links = Ereignis.layerX;
	Maus_oben = Ereignis.layerY;
	txt = "tst_pos_IE: " + Maus_links +"/"+ Maus_oben;
	window.defaultStatus = txt;
	return true;
  }

function tst_pos_FliegenNS(Ereignis)
{
	txt = "tst_pos_NS: " + Ereignis.layerX +"/"+ Ereignis.layerY;
	txt = "tst_pos_NS: " + Ereignis.PageX +"/"+ Ereignis.PageY;
	window.defaultStatus = txt;
	alert (txt);
	return true;
}

function Fliegen_ns()
{
alert ("Fliegen_ie() ,step = " + loop_anzahl);

	loop_anzahl = loop_anzahl +1;

	erg = "";
	erg_y = "";
	for (i=0; i < anzahl_fliegen; i++){
		freund = i -1;
		if (freund < 0) {
			freund = freund + anzahl_fliegen;
		}
		eval( "freund_x = document.FL" + (freund+1) + ".left" );
		eval( "freund_y = document.FL" + (freund+1) + ".top" );
		feind = i +1
		if (feind >= anzahl_fliegen) {
			feind = feind - anzahl_fliegen;
		}
		eval( "feind_x = document.FL" + (feind+1) + ".left" );
		eval( "feind_y = document.FL" + (feind+1) + ".top" );
alert ( "feind_x = " + feind_x + " feind_y = " + feind_y);

		eval( "ich_x = document.FL" + (i+1) + ".left" );
		eval( "ich_y = document.FL" + (i+1) + ".top" );

		speed_x = get_Fliegen_parameter(i, 0)
		speed_y = get_Fliegen_parameter(i, 1)
erg = "FL:"+ (i+1);
erg = erg + " s_x = " + Math.round(speed_x*100)/100;
erg_y = "FLy:"+ (i+1);
erg_y = erg_y + " s_y = " + Math.round(speed_y*100)/100;

		strecke_x = freund_x - ich_x;
		strecke_y = freund_y - ich_y;
		strecke = Math.sqrt(strecke_x*strecke_x + strecke_y*strecke_y);
		speed_x = speed_x + ((freund_x - ich_x) *faktor_freund);
		speed_y = speed_y + ((freund_y - ich_y) *faktor_freund);
erg = erg + " fr_x = " + Math.round( ((freund_x - ich_x) *faktor_freund) *100)/100;
erg = erg + " fr_px = " + (freund_x - ich_x);
erg_y = erg_y + " fr_y = " + Math.round( ((freund_y - ich_y) *faktor_freund) *100)/100;
erg_y = erg_y + " fr_py = " + (freund_y - ich_y);

		if ( (ich_x - feind_x) != 0) {
		  speed_x = speed_x - (1/ (feind_x - ich_x)) *faktor_feind;
erg = erg + " fe_x = " + Math.round( 1/ (feind_x - ich_x) *faktor_feind *100)/100;
erg = erg + " fe_px = " + (feind_x - ich_x);
		}
		if ( (ich_y - feind_y) != 0) {
		  speed_y = speed_y - (1/ (feind_y - ich_y)) *faktor_feind;
erg_y = erg_y + " fe_y = " + Math.round( 1/ (feind_y - ich_y) *faktor_feind *100)/100;
erg_y = erg_y + " fe_py = " + (feind_y - ich_y);
		}

		speed_x = speed_x + (Maus_links - ich_x) *faktor_maus;
		speed_y = speed_y + (Maus_oben - ich_y) *faktor_maus;
erg = erg + " ma_x = " + Math.round( (Maus_links - ich_x) *faktor_maus *100)/100;
erg = erg + " ma_px = " + (Maus_links - ich_x);
erg_y = erg_y + " ma_y = " + Math.round( (Maus_oben - ich_y) *faktor_maus *100)/100;
erg_y = erg_y + " ma_py = " + (Maus_oben - ich_y);

		speed_x = speed_x - (Math.random() -0.5) *faktor_random;
		speed_y = speed_y - (Math.random() -0.5) *faktor_random;

		speed_x = speed_x *faktor_daempfung;
		speed_y = speed_y *faktor_daempfung;

if ( i == 0 ) {
//  window.status =erg +" "+ erg_y;
//  window.status =erg;
}
if ( i == 0 ) {
//  window.status =erg_y;
}

		eval( "document.FL" + (i+1) + ".left = (ich_x + speed_x +0.0)" );
		eval( "document.FL" + (i+1) + ".top = (ich_y + speed_y +0.0)" );

		 set_Fliegen_parameter(i, 0,speed_x)
		 set_Fliegen_parameter(i, 1,speed_y)

	}	// for (i=0; i < anzahl_fliegen; i++)
	if ( loop_anzahl < 2 ) {
		Fliegen_ns();
	}

//	window.setTimeout('Fliegen_ns()',100)
}	// function Fliegen_ns()

function Fliegen()
{
/*
  Maus_links = 300;
  Maus_oben = 200;
*/
	loop_anzahl = loop_anzahl +1;
	for (i=0; i < anzahl_fliegen; i++){
		freund = i -1;
		speed_x = 0;
		speed_y = 0;
		if (freund < 0) {
			freund = freund + anzahl_fliegen;
		}
		feind = i +1
		if (feind >= anzahl_fliegen) {
			feind = feind - anzahl_fliegen;
		}
		if ( ns == 1) {
		  eval( "freund_x = document.FL" + (freund+1) + ".left" );
		  eval( "freund_y = document.FL" + (freund+1) + ".top" );
		  eval( "feind_x = document.FL" + (feind+1) + ".left" );
		  eval( "feind_y = document.FL" + (feind+1) + ".top" );
		  eval( "ich_x = document.FL" + (i+1) + ".left" );
		  eval( "ich_y = document.FL" + (i+1) + ".top" );
		}
		if ( ie == 1) {
		  eval( "freund_x = NachZahl(document.all.FL" + (freund+1) + ".style.left)" );
		  eval( "freund_y = NachZahl(document.all.FL" + (freund+1) + ".style.top)" );
		  eval( "feind_x = NachZahl(document.all.FL" + (feind+1) + ".style.left)" );
		  eval( "feind_y = NachZahl(document.all.FL" + (feind+1) + ".style.top)" );
		  eval( "ich_x = NachZahl(document.all.FL" + (i+1) + ".style.left)" );
		  eval( "ich_y = NachZahl(document.all.FL" + (i+1) + ".style.top)" );
		}

		speed_x = get_Fliegen_parameter(i, 0)
		speed_y = get_Fliegen_parameter(i, 1)
// if ( i == 0 && loop_anzahl < 3) { alert ( "a: speed: " + speed_x + " / " + speed_y )};
// if ( i == 0 && loop_anzahl < 4) { alert ( "wo: " + ich_x + " / " + ich_y + " :: " + speed_x + " / "+ speed_y)};

// if ( i == 0 && loop_anzahl < 3) { alert ( "a1: freund: " + freund_x + " / " + freund_y )};
// if ( i == 0 && loop_anzahl < 3) { alert ( "a2: ich: " + ich_x + " / " + ich_y )};
		strecke_x = freund_x - ich_x;
		strecke_y = freund_y - ich_y;
		strecke = Math.sqrt(strecke_x*strecke_x + strecke_y*strecke_y);
// if ( i == 0 && loop_anzahl < 3) { alert ( "b: strecke: "+strecke_x + " / " + strecke_y + " :: "+ strecke)};
		speed_x = speed_x + ((freund_x - ich_x) *faktor_freund);
		speed_y = speed_y + ((freund_y - ich_y) *faktor_freund);
// if ( i == 0 && loop_anzahl < 3) { alert ( "c: speed: "+speed_x + " / " + speed_y )};

		if ( (ich_x - feind_x) != 0) {
		  speed_x = speed_x - (1/ (feind_x - ich_x)) *faktor_feind;
		}
		if ( (ich_y - feind_y) != 0) {
		  speed_y = speed_y - (1/ (feind_y - ich_y)) *faktor_feind;
		}
// if ( i == 0 && loop_anzahl < 3) { alert ( "d: speed: "+speed_x + " / " + speed_y )};
// if ( i == 0 && loop_anzahl < 4) { alert ( "b: speed: "+speed_x + " / " + speed_y )};

		speed_x = speed_x + (Maus_links - ich_x) *faktor_maus;
		speed_y = speed_y + (Maus_oben - ich_y) *faktor_maus;
// if ( i == 0 && loop_anzahl < 3) { alert ( "maus: "+ Maus_links + " / " + Maus_oben )};
// if ( i == 0 && loop_anzahl < 3) { alert ( "e: speed: "+speed_x + " / " + speed_y )};
// if ( i == 0 && loop_anzahl < 4) { alert ( "c: speed: "+speed_x + " / " + speed_y )};

		speed_x = speed_x - (Math.random() -0.5) *faktor_random;
		speed_y = speed_y - (Math.random() -0.5) *faktor_random;
// if ( i == 0 && loop_anzahl < 3) { alert ( "f: speed: "+speed_x + " / " + speed_y )};

// if ( i == 0 ) { window.status = Math.random(); };

// if ( i == 0 && loop_anzahl < 4) { alert ( "b: wo: " + ich_x + " / " + ich_y + " :: " + speed_x + " / "+ speed_y)};
// ( i == 0 ) { window.status = ich_x + " / " + ich_y + " :: " + speed_x + " / "+ speed_y};
//		speed_x = speed_x *0.01;
//		speed_y = speed_y *0.01;
		speed_x = speed_x *faktor_daempfung;
		speed_y = speed_y *faktor_daempfung;
// if ( i == 0 && loop_anzahl < 3) { alert ( "g: speed: "+speed_x + " / " + speed_y )};

		ich = i+1;
		i1 = i+1;
		ziel_x = 0.0+ Math.round(ich_x) + Math.round(speed_x);
		ziel_y = 0.0 + Math.round(ich_y) + Math.round(speed_y);
// if ( i == 0 && loop_anzahl < 3) { alert ( "h: ich: "+ich_x + " / " + ich_y )};
// if ( i == 0 && loop_anzahl < 3) { alert ( "i: ziel: "+ ziel_x + " / " + ziel_y )};

		if ( ns == 1) {
		  str1 = "document.FL" +  i1 + ".left = " + ziel_x ;
		  str2 = "document.FL" +  i1 + ".top = " + ziel_y ;
		}
		if ( ie == 1) {
//		  str1 = "document.all.FL" +  i1 + ".style.left = '" + Math.round(ziel_x) +"px'";
		  str1 = "document.all.FL" +  i1 + ".style.left = " + Math.round(ziel_x) ;
		  str2 = "document.all.FL" +  i1 + ".style.top = " + Math.round(ziel_y);
		}

// if ( i == 0 && loop_anzahl < 3) { alert ( "j1: str1: '"+ str1 + "'" )};
		eval (str1);
// if ( i == 0 && loop_anzahl < 3) { alert ( "j2: str2: '"+ str2 + "'" )};
		eval (str2);
// if ( i == 0 && loop_anzahl < 4) { alert ( "wohin: " + ziel_x + " / " + ziel_y )};

		 set_Fliegen_parameter(i, 0,speed_x)
		 set_Fliegen_parameter(i, 1,speed_y)

	}	// for (i=0; i < anzahl_fliegen; i++)
//	if ( loop_anzahl < 20000) {
	  window.setTimeout('Fliegen()',100)
//	}
}	// function Fliegen()

function start_fliegen()
{

  for (i=0; i < anzahl_fliegen; i++){
	set_Fliegen_parameter(i, 0, 0.1)
	set_Fliegen_parameter(i, 1, 0.1)
  }	// for (i=0; i < anzahl_fliegen; i++)
  do_src = 0;
  ns = 0;
  ie = 0;
  Maus_links = 300;
  Maus_oben = 200;
  if ((navigator.appName == "Netscape") && ( parseFloat(navigator.appVersion) >= 3.0 )  ) {
	do_src = 1;
	ns = 1;
	window.captureEvents(Event.LOAD);
	window.load = tst_pos_FliegenNS;

	window.captureEvents(Event.MOUSEMOVE);
	window.onmousemove = Mauskontrolle_ns;

  }	// if ((navigator.appName == "Netscape") && ( parseFloat(navigator.appVersion) >= 3.0 ))
  if ( (navigator.appName == "Microsoft Internet Explorer") && ( parseFloat(navigator.appVersion) >= 4.0 )  ) {
	do_src = 1;
	ie = 1;

  }	// if ((navigator.appName == "Netscape") && ( parseFloat(navigator.appVersion) >= 3.0 ))
  Fliegen();
  if ( do_src == 1){
	for (i=0; i < anzahl_fliegen; i++){
		if ( ns == 1) {
		  eval( "document.FL" + (i+1) + ".visibility = 'show'" );
		}	// if ( ns == 1)
		if ( ie == 1) {
		  eval( "document.all.FL" + (i+1) + ".style.visibility = 'visible'" );
		}	// if ( ie == 1)
	}	// for (i=0; i < anzahl_fliegen; i++)
  }
}	// function start_fliegen()
