/********************************************************************************************/
// Navigation
/********************************************************************************************/

var mArrOID   = new Array (1);
var mArrLevel = new Array (1);
var mArrName  = new Array (1);
var mArrLang  = new Array (1);
var mArrLink  = new Array (1);
var mArrOpen  = new Array (1);
var mArrImg   = new Array (1);

var ind         = 0;
var strLevelUp  = "";
var indAkt      = 0;
var strLanguage = "de";
var strLangEng  = "en";
var strLangGer  = "de";
var mName       = "dummy";
var strSep      = ",";

/********************************************************************************************/
//TitleParts: Overview over the titles of a theme
/********************************************************************************************/

var arrTitlePartsOID  = new Array();
var nTitleParts       = 0;
nTitlePartsX          = 430;
nTitlePartsY          = 380;
var schrittweite      = 9;
var schrittweiteDiv   = 0.25;
var timer;
var pos1= 0;


function start()
{
 if(nTitleParts > 0)
   timer = setInterval("bewegen()", 30);
}

function bewegen()
{
 pos1 -= schrittweite;
 schrittweite = schrittweite - 0.25;
 
 if(schrittweite<1)
 {
   schrittweite = 1;
   window.clearInterval(timer);
}

 for(var tp = 0; tp < nTitleParts; tp++)
 {
   bewegeTeil(arrTitlePartsOID[tp], tp, arrTitlePartsOID.length);
 }
}

function bewegeTeil(strTeil, teilnummer, teilanzahl)
{
 document.getElementById(strTeil).style.left = nTitlePartsX - pos1 * Math.sin(-Math.PI/4 + 2 * Math.PI /teilanzahl * teilnummer);
 document.getElementById(strTeil).style.top  = nTitlePartsY + pos1 * Math.cos(-Math.PI/4 + 2 * Math.PI /teilanzahl * teilnummer);
}

function cleanDivTitleParts ()
{
 arrTitlePartsOID = new Array();
 nTitleParts      = 0;
}

function addDivTitleParts(strOID, strName, strHref, strImg)
{
 strOID = "t"+strOID;
 document.write("<style type='text/css'>\n");
 document.write("#" + strOID + " {position:absolute;left: " + nTitlePartsX +" ;top: " + nTitlePartsY + ";}\n");
 document.write("</style>\n");

 document.write("<div ID='" + strOID + "'>\n");
 document.write("<a href='" + strHref + "'>");
 document.write("<img src='" + strImg + "' style='filter:Alpha(opacity=100,finishopacity=0, style=2' width='80'>");
 document.write("");
 document.write("<p>"+ strName + "</p></a>\n");
 document.write("</div>\n");

 arrTitlePartsOID[nTitleParts] = strOID;
 nTitleParts++;
}

function themeIntroXY(oid, x, y)
{
	nTitlePartsX = x;
	nTitlePartsY = y;
	themeIntro(oid);
}

function themeIntro(oid)
{
 //getInfoForAktMenu (oid);//Sollte eigentlich gelaufen sein!
 cleanDivTitleParts ();
 var ind = indAkt;
 var level = mArrLevel[indAkt];
 var strImg;
	
 ind++;level++;
	while (ind < mArrOID.length)
	{
		if(mArrLevel[ind] == level)
		{
			strImg = "img/logo_sw.jpg";
			if(mArrImg[ind]!=null && mArrImg[ind]!="")
					strImg = mArrImg[ind];

			//get language and decide to show or not
			nLang=mArrLang[ind].indexOf(strLanguage);
			if(nLang>=0) {
				//get language and generate menuname
				var nfrom = 0;
				var lenName = 0;
				mName = mArrName[ind];
				nLang = nLang/3;
				for ( var n=0; n < nLang; n++){
					//nfrom   = mName.indexOf(strLanguage);
					nfrom   = mName.indexOf(strSep);
		
					lenName = mName.length;
					if(nfrom > 0) {
						mName = mName.substring(nfrom+1, lenName);
					}
				}
				nfrom = mName.indexOf(strSep);
				if(nfrom > 0) {
					mName = mName.substring(0, nfrom);
				}

				addDivTitleParts(mArrOID[ind], mName, strLevelUp + mArrLink[ind] + "_" + strLanguage + ".html", strLevelUp + strImg);
			}
		}
		//Beim ersten Level, der kleiner ist, wird abgebrochen
		if(mArrLevel[ind] < level)
			break;
		ind++;
	}
}

function themeIntroTable(oid)
{
 //getInfoForAktMenu (oid);//Sollte eigentlich gelaufen sein!
 cleanDivTitleParts ();
 var ind = indAkt;
 var level = mArrLevel[indAkt];
 var strImg;
	
 ind++;level++;
	document.write("<TABLE cellSpacing=0 width='80%' border=0><TBODY>");
	while (ind < mArrOID.length)
	{
		if(mArrLevel[ind] == level)
		{
			strImg = "img/logo_sw.jpg";
			if(mArrImg[ind]!=null && mArrImg[ind]!="")
					strImg = mArrImg[ind];

			//get language and decide to show or not
			nLang=mArrLang[ind].indexOf(strLanguage);
			if(nLang>=0) {
				//get language and generate menuname
				var nfrom = 0;
				var lenName = 0;
				mName = mArrName[ind];
				nLang = nLang/3;
				for ( var n=0; n < nLang; n++){
					//nfrom   = mName.indexOf(strLanguage);
					nfrom   = mName.indexOf(strSep);
		
					lenName = mName.length;
					if(nfrom > 0) {
						mName = mName.substring(nfrom+1, lenName);
					}
				}
				nfrom = mName.indexOf(strSep);
				if(nfrom > 0) {
					mName = mName.substring(0, nfrom);
				}

				addDivTitleParts(mArrOID[ind], mName, strLevelUp + mArrLink[ind] + "_" + strLanguage + ".html", strLevelUp + strImg);


				document.write("<TR vAlign=center>");
				document.write(" <TD align=middle width='25%'");
				if(ind%2 == 0){
					document.write(" style='background-image: url(" + strLevelUp + "img/riffel.gif)'");
				}
				document.write(">");
				document.write("  <A href='" + strLevelUp + mArrLink[ind] + "_" + strLanguage + ".html" + "' target=_blank>");
				document.write("  <img hspace=10 vspace=10 alt='" + mName + "' src='" + strLevelUp + strImg + "' width='75' border=1>");
				document.write("  <BR>" + mName + "</A> </TD>");
				document.write(" <TD width=1 bgColor=#ff0000><IMG height=1 src='img/spacer.gif' width=1></TD>");
				document.write(" <TD class=normal");
				if(ind%2 == 0){
					document.write(" style='background-image: url(" + strLevelUp + "img/riffel.gif)'");
				}
				document.write(">");
				document.write("  <A href='" + strLevelUp + mArrLink[ind] + "_" + strLanguage + ".html" + "' target=_blank>" + mName + "</A>");
				//if(mArrDescription[ind] != null){
				//	document.write("<p>" + mArrDescription[ind] + "</p>");
				//}
				document.write("</TD>");
				document.write("</TR>");
			}


		}
		//Beim ersten Level, der kleiner ist, wird abgebrochen
		if(mArrLevel[ind] < level)
			break;
		ind++;
	}
	document.write("</TBODY></TABLE>");
}

/********************************************************************************************/
//Paint a table containing a name, abreviation and an image, having a link
/********************************************************************************************/

function paintTable(arrAbk, arrName, arrHRef, arrImg, arrImgW, arrImgH, arrText)
{
	document.write("<TABLE cellSpacing=0 width='80%' border=0><TBODY>");
	for(var i=0; i<arrName.length; i++)
	{
		document.write("<TR vAlign=center>");
		document.write(" <TD align=middle width='25%'");
		if(i%2 == 0){
			document.write(" style='background-image: url(" + strLevelUp + "img/riffel.gif)'");
		}
		document.write(">");
		document.write("  <A href='" + arrHRef[i] + "' target=_blank>");
		document.write("  <img hspace=10 vspace=10 alt='" + arrAbk[i] + "' src='" + arrImg[i] + "' width='" + arrImgW[i] + "' height='" + arrImgH[i] + "' border=0>");
		document.write("  <BR>" + arrAbk[i] + "</A> </TD>");
		document.write(" <TD width=1 bgColor=#ff0000><IMG height=1 src='img/spacer.gif' width=1></TD>");
		document.write(" <TD class=normal");
		if(i%2 == 0){
			document.write(" style='background-image: url(" + strLevelUp + "img/riffel.gif)'");
		}
		document.write(">" + arrName[i]);
		if(arrText != null){
			document.write("<p>" + arrText[i] + "</p>");
		}
		document.write("</TD>");
		document.write("</TR>");
	}
	document.write("</TBODY></TABLE>");
}

/********************************************************************************************/
//BrowserDetector
/********************************************************************************************/

function BrowserDetector(ua) {

// Defaults
  this.browser = "Unknown";
  this.platform = "Unknown";
  this.version = "";
  this.majorver = "";
  this.minorver = "";

  uaLen = ua.length;

// ##### Split into stuff before parens and stuff in parens
  var preparens = "";
  var parenthesized = "";

  i = ua.indexOf("(");
  if (i >= 0) {
    preparens = Trim(ua.substring(0,i));
        parenthesized = ua.substring(i+1, uaLen);
        j = parenthesized.indexOf(")");
        if (j >= 0) {
          parenthesized = parenthesized.substring(0, j);
        }
  }
  else {
    preparens = ua;
  }

// ##### First assume browser and version are in preparens
// ##### override later if we find them in the parenthesized stuff
  var browVer = preparens;

  var tokens = parenthesized.split(";");
  var token = "";
// # Now go through parenthesized tokens
  for (var i=0; i < tokens.length; i++) {
    token = Trim(tokens[i]);
        //## compatible - might want to reset from Netscape
        if (token == "compatible") {
          //## One might want to reset browVer to a null string
          //## here, but instead, we'll assume that if we don't
          //## find out otherwise, then it really is Mozilla
          //## (or whatever showed up before the parens).
        //## browser - try for Opera or IE
    }
        else if (token.indexOf("MSIE") >= 0) {
      browVer = token;
    }
    else if (token.indexOf("Opera") >= 0) {
      browVer = token;
    }
        //'## platform - try for X11, SunOS, Win, Mac, PPC
    else if ((token.indexOf("X11") >= 0) || (token.indexOf("SunOS") >= 0) ||
(token.indexOf("Linux") >= 0)) {
      this.platform = "Unix";
        }
    else if (token.indexOf("Win") >= 0) {
      this.platform = token;
        }
    else if ((token.indexOf("Mac") >= 0) || (token.indexOf("PPC") >= 0)) {
      this.platform = token;
        }
  }

  var msieIndex = browVer.indexOf("MSIE");
  if (msieIndex >= 0) {
    browVer = browVer.substring(msieIndex, browVer.length);
  }

  var leftover = "";
  if (browVer.substring(0, "Mozilla".length) == "Mozilla") {
    this.browser = "Netscape";
        leftover = browVer.substring("Mozilla".length+1, browVer.length);
  }
  else if (browVer.substring(0, "Lynx".length) == "Lynx") {
    this.browser = "Lynx";
        leftover = browVer.substring("Lynx".length+1, browVer.length);
  }
  else if (browVer.substring(0, "MSIE".length) == "MSIE") {
    this.browser = "IE";
    leftover = browVer.substring("MSIE".length+1, browVer.length);
  }
  else if (browVer.substring(0, "Microsoft Internet Explorer".length) ==
"Microsoft Internet Explorer") {
    this.browser = "IE"
        leftover = browVer.substring("Microsoft Internet Explorer".length+1,
browVer.length);
  }
  else if (browVer.substring(0, "Opera".length) == "Opera") {
    this.browser = "Opera"
    leftover = browVer.substring("Opera".length+1, browVer.length);
  }

  leftover = Trim(leftover);

  // # Try to get version info out of leftover stuff
  i = leftover.indexOf(" ");
  if (i >= 0) {
    this.version = leftover.substring(0, i);
  }
  else
  {
    this.version = leftover;
  }
  j = this.version.indexOf(".");
  if (j >= 0) {
    this.majorver = this.version.substring(0,j);
    this.minorver = this.version.substring(j+1, this.version.length);
  }
  else {
    this.majorver = this.version;
  }


} // function BrowserCap

// Utility function to trim spaces from both ends of a string
function Trim(inString) {
  var retVal = "";
  var start = 0;
  while ((start < inString.length) && (inString.charAt(start) == ' ')) {
    ++start;
  }
  var end = inString.length;
  while ((end > 0) && (inString.charAt(end - 1) == ' ')) {
    --end;
  }
  retVal = inString.substring(start, end);
  return retVal;
}

/********************************************************************************************/
//Sail boat
/********************************************************************************************/

var indSail;
var schrittweiteSail = 9;
var boat1x           = 300;
var boat1y           = 300;
var boat1direction   = 0;
var sail1direction   = -1;
var boat1velocity    = 1;
var boat1distance    = 0;
var boat1speeddir    = 1;
var boat1angle       = 0;
var boat1name        = "img/boot";
var arrBoatImg = new Array(1);
var arrSailImg = new Array(1);

function startSail()
{
 indSail = 0;
 sailgetNext();
 
 if(arrSailCommand.length > 0)
   timerSail = setInterval("sail()", 60);
}

function sail()
{
 boat1x = boat1x + boat1velocity * Math.sin(boat1direction/180*Math.PI) * boat1speeddir;
 boat1y = boat1y - boat1velocity * Math.cos(boat1direction/180*Math.PI) * boat1speeddir;
 
 //calculate the direction of the boat
 var gradBoat = (boat1direction - (boat1direction%10));
 //here the direction of the sail ist especial
 if(sail1direction!=-1)
 	gradSail = sail1direction;
 //normaly, the direction of the sail is optimaly
 else
 	gradSail = gradBoat;
 //alert(gradSail);
  
 document.getElementById('boat1').style.left = boat1x;
 document.getElementById('boat1').style.top  = boat1y;
 if(document.imgBoat1.src != arrBoatImg[gradBoat].src)
 	document.imgBoat1.src=arrBoatImg[gradBoat].src;
 if(document.imgSail1.src != arrSailImg[gradSail].src)
 	document.imgSail1.src=arrSailImg[gradSail].src;
 
 //Teilziel erreicht?
 boat1distance = boat1distance - boat1velocity;
 if(boat1velocity==0){
 	boat1distance--;
 }
 if(boat1distance<0)
 	sailgetNext();
}

//get next sailing command
function sailgetNext()
{
 if(indSail >= arrSailCommand.length)
   indSail        = 0;

 var arrCommand;
 while(boat1distance<=0 && boat1angle<=0)
 {
	arrCommand = arrSailCommand[indSail].split(";");
	if(arrCommand.length > 0)
	{
		///////////////
		//set Position
		if(arrCommand[0]==0)
		{
			boat1x = arrCommand[1];boat1x++;boat1x--;
			boat1y = arrCommand[2];boat1y++;boat1y--;
		}
		///////////////
		//set direction
		if(arrCommand[0]==1)
		{
			boat1direction = arrCommand[1];boat1direction++;boat1direction--;
			if(arrCommand.length >= 3)
				sail1direction = arrCommand[2];
			else
				sail1direction = -1;
		}
		///////////////
		//set direction
		if(arrCommand[0]==2)
			boat1velocity = arrCommand[1];boat1velocity++;boat1velocity--;
		///////////////
		//set distance of line
		if(arrCommand[0]==3){
			var v = arrCommand[1].substring(0,1);
			if(v=="-"){
				boat1speeddir = -1;
				arrCommand[1] = arrCommand[1].substring(1);
			}else{
				boat1speeddir = 1;
			}
			boat1distance = arrCommand[1];boat1distance++;boat1distance--;
		}
		///////////////
		//set bow
		if(arrCommand[0]==4)
		{
			boat1radius = arrCommand[1];boat1radius++;boat1radius--;
			boat1angle = arrCommand[2];boat1angle++;boat1angle--;
		}
		///////////////
		//set text
		if(arrCommand[0]==5)
			document.getElementById("boattext1o").innerText  = arrCommand[1];
		///////////////
		//set text
		if(arrCommand[0]==6)
			document.getElementById("boattext1u").innerText  = arrCommand[1];
		///////////////
		//set Buoy
		if(arrCommand[0]==7)
		{
			if(arrCommand[1]==0){
				document.imgBuoy.src = strLevelUp + "img/blank.gif";
			}
			if(arrCommand[1]==1){
				document.getElementById('buoy').style.left = boat1x;
				document.getElementById('buoy').style.top = boat1y;
				document.imgBuoy.src = strLevelUp + "img/boats/buoy.gif";
			}
		}
		///////////////
		//clear timer
		if(arrCommand[0]==-1)
			window.clearInterval(timerSail);
	}
	else
	{
		indSail = 0;
		break;
	}
	indSail++;
 }
}

function preLoadImgBoats()
{
	// preload images:
	if (document.images) 
	{
		for(var i=0; i<360; i=i+10)
		{
			arrBoatImg[i]  = new Image(); arrBoatImg[i].src  = strLevelUp + "img/boats/boot" + i + ".gif";
			arrSailImg[i]  = new Image(); arrSailImg[i].src  = strLevelUp + "img/sails/sail" + i + ".gif";
		}
		for(var i=171; i<190; i++)
		{
			arrSailImg[i]  = new Image(); arrSailImg[i].src  = strLevelUp + "img/sails/sail" + i + ".gif";
		}
	}
}

