// Javascript functions for layers

left_no = new Image ( 28 , 28 ) ;
left_no.src = "../Images/left_0.gif" ; 
left_normal = new Image ( 28 , 28 ) ;
left_normal.src = "../Images/left_n.gif" ; 
left_over = new Image ( 28 , 28 ) ;
left_over.src = "../Images/left_o.gif" ;
left_down = new Image ( 28 , 28 ) ;
left_down.src = "../Images/left_d.gif" ;

right_no = new Image ( 28 , 28 ) ;
right_no.src = "../Images/right_0.gif" ; 
right_normal = new Image ( 28 , 28 ) ;
right_normal.src = "../Images/right_n.gif" ; 
right_over = new Image ( 28 , 28 ) ;
right_over.src = "../Images/right_o.gif" ;
right_down = new Image ( 28 , 28 ) ;
right_down.src = "../Images/right_d.gif" ;

up_normal = new Image ( 28 , 28 ) ;
up_normal.src = "../Images/up_n.gif" ; 
up_over = new Image ( 28 , 28 ) ;
up_over.src = "../Images/up_o.gif" ;
up_down = new Image ( 28 , 28 ) ;
up_down.src = "../Images/up_d.gif" ;

film = new Array () ;
filmFrames = new Array () ;

// show/hide layer

function show ( layer_name )
{
  object = new getObj ( layer_name ) ;
  object.style.visibility = "visible" ;
}

function hide ( layer_name )
{
  object = new getObj ( layer_name ) ;
  object.style.visibility = "hidden" ;
}

// show/hide menu

var pos_old , pos_new , pos_counter , pull_handle , layer_name ;

function pull ()
{
  object = new getObj ( layer_name ) ;

  if ( document.all )
    object.style.pixelLeft = pos_old + ( pos_new - pos_old ) * pos_counter / 10 ;
  else
    object.style.left = pos_old + ( pos_new - pos_old ) * pos_counter / 10 ;
    
  pos_counter++ ;
  if ( pos_counter <= 10 )
    setTimeout ( "pull();", 50 );
}

function moveh ( name , pos )
{
  object = new getObj ( name ) ;

  if ( document.all )
    pos_old = object.style.pixelLeft ;
  else
    pos_old = parseInt(object.style.left) ;

  layer_name = name ;
  pos_new = pos ;
  pos_counter = 0 ;
  pull();
}

function layerbutton ( name , image )
{
  object = new getObj ( name ) ;
  if (document.layers)
    object.obj.document[name+"-img"].src = image.src ;
  else
    document.images[name+"-img"].src = image.src ;

//  object.obj.document.images[0].src = image.src ;
}

function filmbutton ( name , image )
{
  if ( document[name] )
    document[name].src = image.src ;
  else
  {
    if ( document.layers )
    {
      for ( i = 0 ; i < document.layers.length ; i++ )
      {
        if ( document.layers[i].document[name] )
          document.layers[i].document[name].src = image.src ;
      }
    }
    else
    {
      document.all[name].src = image.src ;
    }
  }
}

function movein ( name )
{
  object = new getObj ( name ) ;

  if ( document.all )
  {
    object.style.pixelTop = document.body.scrollTop+60;
    object.style.pixelLeft = -1000;
    object.style.visibility = "visible";
  }
  else
  {
    object.style.top = pageYOffset+60;
    object.style.left = -1000;
    object.style.visibility = "visible";
  }
  
  moveh ( name , 0 ) ;
}

function moveout ( name )
{
  object = new getObj ( name ) ;
  if ( document.all )
    w = object.obj.offsetWidth ; 
  else if ( document.layers )
    w = object.obj.clip.width ;
  else
    w = 1000;

  moveh ( name , -w ) ;
}


// Functions for films

function elementProperties ( element , x , y , visibility )
{
  this.element = element ;
  this.x = x ;
  this.y = y ;
  this.visibility = visibility ;
}

function showFrame ( filmname , filmnameFrames , number )
{
  var i ;

  for ( i = 0 ; i < filmnameFrames[number].length ; i++ )
  {
    // For visual reasons the order of the commands differs in
    // the following two cases
    
    if ( ( filmnameFrames[number][i].visibility == "show" ) 
      || ( filmnameFrames[number][i].visibility == "visible" ) )
    {
      setElementPosition ( filmname[1] , 
                           filmnameFrames[number][i].element ,
                           filmnameFrames[number][i].x , 
                           filmnameFrames[number][i].y ) ;
      showElement ( filmnameFrames[number][i].element ) ;
    }
    else
    {
      hideElement ( filmnameFrames[number][i].element ) ;
      setElementPosition ( filmname[1] , 
                           filmnameFrames[number][i].element ,
                           filmnameFrames[number][i].x , 
                           filmnameFrames[number][i].y ) ;
    }
  }
  filmname[0] = number ;
}

function prevFrame ( filmname , filmnameFrames )
{
  if ( clearedFlag == true )
  {
    actualFrame = filmname[0] ;
    if ( actualFrame > 3 )
    {
      actualFrame-- ;

      showFrame ( filmname , filmnameFrames , actualFrame ) ;
    }
    if ( top.draftflag == 1 )
      if (filmEditorWindow && !filmEditorWindow.closed)
        showFilmscript () ;
  }
}

var determineFrames = false;

function randomFrame ( filmname , filmnameFrames , number )
{
  if ( clearedFlag == true )
  {
    actualFrame = filmname[0] ;

    if (!filmnameFrames[number])
    {
      determineFrames = true;
      filmname[0] = 2;
      for (counttemp=3;counttemp<filmname.length;++counttemp)
      {
        nextFrame ( filmname , filmnameFrames ) ;
      }
      filmname[0] = actualFrame;
      determineFrames = false;
    }

    if ( number == actualFrame+1 )
      nextFrame ( filmname , filmnameFrames ) ;
    else if ( number > 2 )
    {
      if ( filmnameFrames[number] )
         showFrame ( filmname , filmnameFrames , number ) ;
      else
        alert ( "Sie müssen erst die ganze Animation\neinmal durchlaufen haben, bevor Sie\nBilder einzeln aufrufen können!" ) ;
    }
    if ( top.draftflag == 1 )
      if (filmEditorWindow && !filmEditorWindow.closed)
        showFilmscript () ;
  }
}

var actualFrame , frame , filmnameFrames , commandLoopHandle , filmname , clearedFlag = true ;

function commandLoop ()
{
  var pointer ;

  if ( moveFlag == false )
    if ( frame != "" )
    {
      pointer = frame.indexOf ( ";" ) ;
      if ( pointer == -1 ) 
      { 
        command = frame ;
        frame = "" ;
      }
      else
      {
        command = frame.slice ( 0 , pointer ) ;
        frame = frame.slice ( pointer+1 ) ;
      }
      pointer = command.indexOf ( ":" ) ;
      func = command.slice ( 0 , pointer ) ;
      param = command.slice ( pointer+1 ) ;
      while ( func.charAt(0) == ' ' ) func = func.slice ( 1 ) ;
      pointer = param.indexOf ( "," ) ;
      if ( pointer == -1 )  
      {
        element = param ;
        param = "" ;
      }
      else
      {
        element = param.slice ( 0 , pointer ) ;
        param = param.slice ( pointer+1 ) ;
      }
      while ( element.charAt(0) == ' ' ) element = element.slice ( 1 ) ;
      
      if (!getObj(element))
      {
        alert(element+" is undefined");
        frame="";
        return;
      }

      // command execution

      if ( func == "SEP" ) // setElementPosition
      {
        pointer = param.indexOf ( "," ) ;
        x = parseInt ( param.slice ( 0 , pointer ) ) ;
        y = parseInt ( param.slice ( pointer+1 ) ) ;
        setElementPosition ( filmname[1] , element , x , y ) ;
      }
      if ( func == "HE" ) // hideElement
      {
        hideElement ( element ) ;
      }
      if ( func == "SE" ) // showElement
      {
          showElement ( element ) ;
      }
      if ( func == "ME" ) // moveElement
      {
        pointer = param.indexOf ( "," ) ;
        x = parseInt ( param.slice ( 0 , pointer ) ) ;
        y = parseInt ( param.slice ( pointer+1 ) ) ;
        moveElement ( filmname[1] , element , x , y ) ;
      }

      for ( i = 0 ; i < filmnameFrames[actualFrame].length ; i++ ) 
       if ( filmnameFrames[actualFrame][i].element == element ) break ;

      if ( i == filmnameFrames[actualFrame].length ) 
        filmnameFrames[actualFrame][i] = new elementProperties ( element , 0 , 0 , "" ) ;   
    }
    else
    {
      if ( document.all )
      {
        for ( i = 0 ; i < filmnameFrames[actualFrame].length ; i++ )
        {
          element = filmnameFrames[actualFrame][i].element ;
          elementobject = new getObj ( element ) ;
          offsetobject = new getOffset ( filmname[1] , element ) ;
          filmnameFrames[actualFrame][i].x = elementobject.style.pixelLeft - offsetobject.x ;
          filmnameFrames[actualFrame][i].y = elementobject.style.pixelTop - offsetobject.y ;
          filmnameFrames[actualFrame][i].visibility = elementobject.style.visibility  ;
        }
      }  
      else
      {
        filmnameobject = new getObj ( filmname[1] ) ;
        for ( i = 0 ; i < filmnameFrames[actualFrame].length ; i++ )
        {
          element = filmnameFrames[actualFrame][i].element ;
          elementobject = new getObj ( element ) ;
          offsetobject = new getOffset ( filmname[1] , element ) ;

          filmnameFrames[actualFrame][i].x = parseInt(elementobject.style.left) - offsetobject.x ;
          filmnameFrames[actualFrame][i].y = parseInt(elementobject.style.top) - offsetobject.y ;
          filmnameFrames[actualFrame][i].visibility = elementobject.style.visibility  ;
        }
      }
      if (!determineFrames)
        clearInterval ( commandLoopHandle ) ;
      clearedFlag = true ;
    }
}

function updateFrame ( film , frames )
{
  filmnameFrames = frames ;
  filmname = film ;

  actualFrame = filmname[0] ;
  filmnameFrames[actualFrame] = new Array () ;
  if ( actualFrame > 3 )
    for ( i = 0 ; i < filmnameFrames[actualFrame-1].length ; i++ )
      filmnameFrames[actualFrame][i] = new elementProperties ( filmnameFrames[actualFrame-1][i].element , 0 , 0 , "" ) ;

  frame = filmname[actualFrame] ;
      
  clearedFlag = false ;
  if (!determineFrames)
    commandLoopHandle = setInterval ( "commandLoop()" , 10 ) ;
  else
  {
    while (!clearedFlag) {commandLoop();}
  }
}

function verified_nextFrame(film, frames)
{
  film_verified=film;
  frames_verified=frames;
  if (!clearedFlag)
  {
    setTimeout("verified_nextFrame(film_verified, frames_verified);", 10);
  }
  else
    nextFrame(film_verified, frames_verified);
}

function nextFrame ( film , frames )
{
  if ( clearedFlag == true )
  {
    filmnameFrames = frames ;
    filmname = film ;

    if ( filmname[0] < filmname.length - 1 )
    {
      filmname[0]++ ;

      updateFrame ( film , frames ) ;

      if ( top.draftflag == 1 )
        if (filmEditorWindow && !filmEditorWindow.closed)
          setTimeout("showFilmscript();", 50) ;
    }
  }
}


function resetFilm ( filmname )
{
  if ( clearedFlag == true )
  {
    filmname[0] = 2 ;

    for ( actualFrame = 3 ; actualFrame < filmname.length ; actualFrame++ )
    {
      frame = filmname[actualFrame] ;
      while ( frame != "" )
      {
        pointer = frame.indexOf ( ";" ) ;
        if ( pointer == -1 ) 
        { 
          command = frame ;
          frame = "" ;
        }
        else
        {
          command = frame.slice ( 0 , pointer ) ;
          frame = frame.slice ( pointer+1 ) ;
        }
        pointer = command.indexOf ( ":" ) ;
        func = command.slice ( 0 , pointer ) ;
        param = command.slice ( pointer+1 ) ;
        while ( func.charAt(0) == ' ' ) func = func.slice ( 1 ) ;
        pointer = param.indexOf ( "," ) ;
        if ( pointer == -1 )  
        {
          element = param ;
          param = "" ;
        }
        else
        {
          element = param.slice ( 0 , pointer ) ;
          param = param.slice ( pointer+1 ) ;
        }
        while ( element.charAt(0) == ' ' ) element = element.slice ( 1 ) ;

        if (getObj(element))
          hideElement ( element ) ;
      }
    }  
    if ( top.draftflag == 1 )
      if (filmEditorWindow && !filmEditorWindow.closed)
        showFilmscript () ; 
  }
}

var filmEditorWindow ;

function openFilmEditor ()
{
  width = 400 ;
  height = 600 ;

  if ( !filmEditorWindow )
    filmEditorWindow = open("../Navi/editor.htm","Filmeditor","width="+width+",height="+height+",left=0,top=0,scrollbars=no"); 

}

function showFilmscript ()
{
  writeStatus ( "OK" ) ;

  scriptname = filmEditorWindow.frames[0].document.forms['Scripteditor'].Scriptname.value ;

  for ( i = 1 ; i <= top.numberOfFilms ; i++ )
    if ( film[i] && ( film[i][2] == scriptname ) ) break ; 

  if ( i > top.numberOfFilms )
  {
    filmEditorWindow.frames[1].document.open(); 
    with (filmEditorWindow.frames[1].document) {
      writeln("<html><head><title>Filmeditor</title>"); 
      writeln("</head>"); 
      writeln("<body bgcolor=\"#FFFFFF\">");  
      writeln("</body>");
      writeln("</html>");
      close(); 
    }
    filmEditorWindow.frames[2].document.forms['Frame'].Contents.value = "" ;

    writeStatus ( "On the page there is no filmscript with this name!" ) ;
    return ;
  }

  // i now contains the number of the filmscript

  showFrameInEditor ( i ) ;
}

function showFrameInEditor ( filmnumber )
{
  filmEditorWindow.frames[1].document.open(); 

  with (filmEditorWindow.frames[1].document) 
  {
    writeln("<html><head><title>Filmeditor</title>"); 
    writeln("</head>"); 
    writeln("<body bgcolor=\"#FFFFFF\">");  
    writeln("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" bgcolor=\"#C0C0C0\" width=\"100%\"><tr><td>" ) ;
    writeln("<table bgcolor=\"#C0C0C0\" cellpadding=\"3\" cellspacing=\"1\" width=\"100%\" border=\"0\">");

    currentframe = film[filmnumber][0] - 2 ;
    framecounter = 1 ;
    while ( film[filmnumber][framecounter+2] != null )
    {
      writeln("<tr>");    
  
      if ( framecounter == currentframe )
        writeln ( "<td align=right bgcolor=\"#F4B673\"><a name=\"currentframe\">" + framecounter + "</a></td>" ) ;
      else 
        writeln ( "<td align=right bgcolor=\"#FFFFE0\">" + framecounter + "</td>" ) ;
     
      commands = film[filmnumber][framecounter+2] ;
      pointer = commands.indexOf ( ";" ) ;
      command = commands.slice ( 0 , pointer ) ;
      commands = commands.slice ( pointer+1 ) ;
      pointer = command.indexOf ( ":" ) ;
      writeln ( "<td bgcolor=\"#E4EEFF\">" + command.slice ( 0 , pointer ) + "</td>" ) ;
      command = command.slice ( pointer+1 ) ;
      while ( command != "" )
      {
        pointer = command.indexOf ( "," ) ; 
        if ( pointer == -1 ) pointer = command.length ;
        writeln ( "<td bgcolor=\"#E4EEFF\">" + command.slice ( 0 , pointer ) + "</td>" ) ;
        command = command.slice ( pointer+1 ) ;    
      }
      writeln("</tr>");

      while ( commands != "" )
      {
        pointer = commands.indexOf ( ";" ) ;
        command = commands.slice ( 0 , pointer ) ;
        commands = commands.slice ( pointer+1 ) ;
        pointer = command.indexOf ( ":" ) ;
        writeln("<tr>");
        writeln("<td bgcolor=\"#FFFFE0\">&nbsp;</td>");
        writeln ( "<td bgcolor=\"#E4EEFF\">" + command.slice ( 0 , pointer ) + "</td>" ) ;
        command = command.slice ( pointer+1 ) ;
        while ( command != "" )
        {
          pointer = command.indexOf ( "," ) ; 
          if ( pointer == -1 ) pointer = command.length ;
          writeln ( "<td bgcolor=\"#E4EEFF\">" + command.slice ( 0 , pointer ) + "</td>" ) ;
          command = command.slice ( pointer+1 ) ;    
        }
        writeln("</tr>");   
      }
      framecounter++ ;
    }   

    writeln("</table>");
    writeln("</td></tr></table>");
    writeln("</body>");
    writeln("</html>");
    close(); 
  }

  if ( currentframe > 0 )
    if ( document.all )
      filmEditorWindow.frames[1].scrollTo ( 0 , filmEditorWindow.frames[1].document.all.currentframe.y ) ;  
    else
      filmEditorWindow.frames[1].scrollTo ( 0 , filmEditorWindow.frames[1].document.anchors[0].y ) ;

  if ( currentframe > 0 ) 
  {
     commands = film[filmnumber][currentframe+2] ;
     contents = "" ;
     while ( commands != "" )
     {
       pointer = commands.indexOf ( ";" ) ;
       contents += commands.slice ( 0 , pointer ) + "\n" ;
       commands = commands.slice ( pointer+1 ) ;
     }
  }
  else
   contents = "Currently no frame is displayed" ;

  filmEditorWindow.frames[2].document.forms['Frame'].Contents.value = contents ;
  writeStatus ( "OK" ) ;

}

function updateFilmscript ()
{
  if (!filmEditorWindow)
    return;

  scriptname = filmEditorWindow.frames[0].document.forms['Scripteditor'].Scriptname.value ;

  for ( filmnumber = 1 ; filmnumber <= top.numberOfFilms ; filmnumber++ )
    if ( film[filmnumber] && ( film[filmnumber][2] == scriptname ) ) break ; 

  if ( filmnumber > top.numberOfFilms ) 
  {
    writeStatus ( "On the page there is no film with this name!" ) ;
    return ;
  }

  currentframe = film[filmnumber][0] - 2 ;

  contents = filmEditorWindow.frames[2].document.forms['Frame'].Contents.value ;
  
  if (contents == "Currently no frame is displayed")
  {
    alert("Currently no frame is displayed!");
    return;
  }

  // eliminate spaces

  while ( true )
  {
    pointer = contents.indexOf ( " " ) ;
    if ( pointer < 0 ) break ; 

    contents = contents.slice ( 0 , pointer ) + contents.slice ( pointer+1 ) ;
  }

  // replace \n\n by \n 

  while ( true )
  {
    pointer = contents.indexOf ( "\n\n" ) ;
    if ( pointer < 0 ) break ; 

    contents = contents.slice ( 0 , pointer ) + "\n" + contents.slice ( pointer+2 ) ;
  }

  // replace \n by ;

  while ( true )
  {
    pointer = contents.indexOf ( "\n" ) ;
    if ( pointer < 0 ) break ; 

    contents = contents.slice ( 0 , pointer ) + ";" + contents.slice ( pointer+1 ) ;
  }

  // eliminate \r

  while ( true )
  {
    pointer = contents.indexOf ( "\r" ) ;
    if ( pointer < 0 ) break ; 

    contents = contents.slice ( 0 , pointer ) + "" + contents.slice ( pointer+1 ) ;
  }

  if ( contents.charAt(contents.length-1) != ";" ) contents += ";" ;

  film[filmnumber][currentframe+2] = contents ;

  if ( film[filmnumber][0] > 3 ) 
    prevFrame ( film[filmnumber] , filmFrames[filmnumber] ) ;
  else 
    film[filmnumber][0]-- ;

  nextFrame ( film[filmnumber] , filmFrames[filmnumber] ) ;

  writeStatus("Filmscript updated");
}

function writeStatus ( text )
{
  if ( document.getElementById )
  {
    x = filmEditorWindow.frames[2].document.getElementById ( 'Statusline' ) ;
    x.innerHTML = text ;
  }
  else if ( document.all )
  {
    x = filmEditorWindow.frames[2].document.all['Statusline'] ;
    x.innerHTML = text ;
  }
  else if ( document.layers )
  {
     x = filmEditorWindow.frames[2].document.layers['Statusline'] ;
     text2 = '<P CLASS="testclass">' + text + '</P>' ;
     x.document.open() ;
     x.document.write ( text2 ) ;
     x.document.close() ;
  }
}

function exportFilmscript ()
{
  if (!filmEditorWindow)
    return;

  scriptname = filmEditorWindow.frames[0].document.forms['Scripteditor'].Scriptname.value ;

  for ( filmnumber = 1 ; filmnumber <= top.numberOfFilms ; filmnumber++ )
    if ( film[filmnumber] && ( film[filmnumber][2] == scriptname ) ) break ; 

  if ( filmnumber > top.numberOfFilms ) 
  {
    writeStatus ( "On the page there is no film with this name!" ) ;
    return ;
  }

  width = 400 ;
  height = 500 ;

  exportWindow = open("","Export","width="+width+",height="+height+",left=100,top=100,scrollbars=yes,menubar=yes"); 
  exportWindow.document.open(); 
  with (exportWindow.document) 
  {
    framecounter = 1 ;
    writeln ( "<html><body>" ) ;
    writeln ( "\\openFilmscript{" + scriptname + "}<br>" ) ;
  
    while ( film[filmnumber][framecounter+2] != null )
    {
      commands = film[filmnumber][framecounter+2] ;

      if ( framecounter > 1 )
        writeln ( "\\nextframe<br>" ) ;

      while ( commands != "" )
      {
        pointer = commands.indexOf ( ";" ) ;
        if ( pointer == -1 ) 
        { 
          command = commands ;
          commands = "" ;
        }
        else
        {
          command = commands.slice ( 0 , pointer ) ;
          commands = commands.slice ( pointer+1 ) ;
        }
        pointer = command.indexOf ( ":" ) ;
        func = command.slice ( 0 , pointer ) ;
        param = command.slice ( pointer+1 ) ;
        while ( func.charAt(0) == ' ' ) func = func.slice ( 1 ) ;
        pointer = param.indexOf ( "," ) ;
        if ( pointer == -1 )  
        {
          element = param ;
          param = "" ;
        }
        else
        {
          element = param.slice ( 0 , pointer ) ;
          param = param.slice ( pointer+1 ) ;
        }
        while ( element.charAt(0) == ' ' ) element = element.slice ( 1 ) ;
  
        // command execution

        if ( func == "SEP" ) // setElementPosition
        {
          pointer = param.indexOf ( "," ) ;
          x = parseInt ( param.slice ( 0 , pointer ) ) ;
          y = parseInt ( param.slice ( pointer+1 ) ) ;
          writeln ( "\\setElementPosition{" + element + "}{" + x + "}{" + y + "}<br>" ) ;
        }
        if ( func == "HE" ) // hideElement
        {
          writeln ( "\\hideElement{" + element + "}<br>" ) ;
        }
        if ( func == "SE" ) // showElement
        {
          writeln ( "\\showElement{" + element  + "}<br>" ) ;
        }
        if ( func == "ME" ) // moveElement
        {
          pointer = param.indexOf ( "," ) ;
          x = parseInt ( param.slice ( 0 , pointer ) ) ;
          y = parseInt ( param.slice ( pointer+1 ) ) ;
          writeln ( "\\moveElement{" + element + "}{" + x + "}{" + y + "}<br>" ) ;
        }  
      }
      framecounter++ ;    
    }

    writeln ( "\\closeFilmscript" ) ;
    writeln ( "</body></html>" ) ;
    close(); 
  } 
}

function hideElement ( elementname )
{
  elementobject = new getObj ( elementname ) ;
  elementobject.style.visibility = "hidden" ;

  if ( top.draftflag == 1 )
  {
    elementobject = new getObj ( elementname + "_label" ) ;
    elementobject.style.visibility = "hidden" ;
  }
}

function showElement ( elementname )
{
  elementobject = new getObj ( elementname ) ;
  elementobject.style.visibility = "visible" ;

  if ( top.draftflag == 1 )
  {
    elementobject = new getObj ( elementname + "_label" ) ;
    elementobject.style.visibility = "visible" ;
  }
}

function getOffset ( fn , en )
{
  filmobject = new getObj ( fn ) ;
  elementobject = new getObj ( en ) ;

  if ( document.layers )
  {
    if ( elementobject.obj.parentLayer.clip ) // film is in a layer
    {
      this.x = filmobject.obj.pageX - elementobject.obj.parentLayer.pageX ; 
      this.y = filmobject.obj.pageY - elementobject.obj.parentLayer.pageY ;
    }
    else 
    {
      this.x = filmobject.obj.pageX ; 
      this.y = filmobject.obj.pageY ;
    }
  }
  else
  {
      this.x = filmobject.obj.offsetLeft ; 
      this.y = filmobject.obj.offsetTop ;
  }  
}

function setElementPosition ( filmname , elementname , x , y )
{
  filmobject = new getObj ( filmname ) ;
  elementobject = new getObj ( elementname ) ;
  offsetobject = new getOffset ( filmname , elementname ) ;
  elementlabelobject = new getObj ( elementname + "_label" ) ;

  elementobject.style.left = x + offsetobject.x ; 
  elementobject.style.top = y + offsetobject.y ;

  if ( top.draftflag == 1 )
    if ( !document.layers )
    {
      text = '<font color"#FF0000">' + elementname + ':&nbsp;' +  x + ',&nbsp;' + y + '</font>' ;
      elementlabelobject.obj.innerHTML = text ;
      elementlabelobject.style.left = x + offsetobject.x ;
      elementlabelobject.style.top = y + offsetobject.y - 10 ;
    }
    else
    {
      elementlabelobject.style.left = x + offsetobject.x ;
      elementlabelobject.style.top = y + offsetobject.y - 10 ;
      text = '<div><font color="#FF0000">' + elementname + ':&nbsp;' +  x + ',&nbsp;' + y + '</font></div>' ;    
      elementlabelobject.obj.document.open() ;
      elementlabelobject.obj.document.write ( text ) ;
      elementlabelobject.obj.document.close() ;
    }
}

var posx_old , posy_old , posx_new , posy_new , moveFlag = false , offset ;

function wait ( time )
{
  date = new Date () ;
  start = date.getTime () ;
  while ( true ) 
  {
    now = new Date () ;
    if ( now.getTime () > start + time ) break ;
  }
}

function element_pull ()
{
  object = new getObj ( layer_name ) ;
  objectlabel = new getObj ( layer_name + "_label" ) ;
  if ( !document.layers )
  {
    object.style.left = 
      posx_old + ( posx_new - posx_old ) * pos_counter / 10 ;
    object.style.top = 
      posy_old + ( posy_new - posy_old ) * pos_counter / 10 ;

    if ( top.draftflag == 1 )
    {
      text = '<font color="#FF0000">' + layer_name + ':&nbsp;' + ( parseInt(object.style.left) - parseInt(offset.x) ) + ',&nbsp;' + ( parseInt(object.style.top) - parseInt(offset.y) ) + '</font>' ;
      objectlabel.obj.innerHTML = text ;
      objectlabel.style.left = object.style.left ;
      objectlabel.style.top = parseInt(object.style.top) - 10 ;
    }
  }
  else
  {
    object.style.left = 
      posx_old + ( posx_new - posx_old ) * pos_counter / 10 ;
    object.style.top = 
      posy_old + ( posy_new - posy_old ) * pos_counter / 10 ;

    if ( top.draftflag == 1 )
    {
      objectlabel.style.left = object.style.left ;
      objectlabel.style.top = object.style.top - 10 ;
      text = '<div><font color="#FF0000">' + layer_name + ':&nbsp;' + ( object.style.left - offset.x ) + ',&nbsp;' + ( object.style.top - offset.y ) + '</font></div>' ;
      objectlabel.obj.document.open() ;
      objectlabel.obj.document.write ( text ) ;
      objectlabel.obj.document.close() ;
    }
  }

  pos_counter++ ;
  if ( pos_counter > 10 )
  {
    if (!determineFrames)
      clearInterval ( pull_handle ) ;
    moveFlag = false ;
  }
}

function moveElement ( filmname , elementname , x , y )
{
  elementnameobject = new getObj ( elementname ) ;
  filmnameobject = new getObj ( filmname ) ;

  if ( document.all )
  {
    posx_old = elementnameobject.style.pixelLeft ;
    posy_old = elementnameobject.style.pixelTop ;
  }
  else
  {
    posx_old = parseInt(elementnameobject.style.left) ;
    posy_old = parseInt(elementnameobject.style.top) ;
  }

  layer_name = elementname ;

  offset = new getOffset ( filmname , elementname ) ;

  posx_new = offset.x + x ;
  posy_new = offset.y + y ;

  pos_counter = 0 ;

  moveFlag = true ;
  if (!determineFrames)
  {
    pull_handle = setInterval ( "element_pull()" , 50 ) ;
  }
  else
  {
    while (moveFlag) {element_pull();}
  }
}


// Functions for accessing nested layers in all browser types
// Source: www.xs4all.nl/~ppk/js/index.html

function getObj(name)
{
  if (document.getElementById)
  {
    if (!document.getElementById(name))
      return false;
    this.obj = document.getElementById(name);
    this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
    if (!document.all[name])
      return false;
    this.obj = document.all[name];
    this.style = document.all[name].style;
  }
  else if (document.layers)
  {
    if (!getObjNN4(document,name))
      return false;
    this.obj = getObjNN4(document,name);
    this.style = this.obj;
  }
  return true;
}

function getObjNN4(obj,name)
{
  var x = obj.layers;
  var thereturn;
  
  for (var i=0;i<x.length;i++)
  {
    if (x[i].id == name)
      thereturn = x[i];
    else if (x[i].layers.length)
      var tmp = getObjNN4(x[i],name);

    if (tmp) thereturn = tmp;
  }
  return thereturn;
}

function DisplayMagnify ( name )
{
  width = document.applets[name].getDimension().width ;
  height = document.applets[name].getDimension().height ;
  if ( width < 100 ) width = 100 ;
  if ( height < 100 ) height = 100 ;

//  x = screen.availWidth - width - 10 ;
//  y = 0 ;

  fenster = open("","Lupe","width="+width+",height="+height+",left=100,top=100"); 
  fenster.document.open(); 
  with (fenster.document) {
    writeln("<html><head><title>Lupe</title>"); 
    writeln("<style type='text/css'>");
    writeln("#topleft {");
    writeln("	position: absolute;");
    writeln("	top:	  0px;");
    writeln("	left:	  0px;");
    writeln("   margin-left: 0px;");
    writeln("   margin-top:  0px;");
    writeln("}");
    writeln("</style>");
    writeln("</head>"); 
    writeln("<body id=topleft return true;\">");  
    writeln("<applet name=\""+name+"\" code=\"formula.class\" codebase=\"../Eqn\" width="+this.width+" height="+this.height+">"); 
    writeln("<param name=\"formula\" value=\""+document.applets[name].getFormula()+"\">");
    writeln("<param name='fgcolor' value='000000'>");
    writeln("<param name='bgcolor' value='ffffff'>");
    writeln("<param name='halign' value='center'>"); 
    writeln("<param name='editable' value='false'>");
    writeln("<param name='valign' value='middle'>");
    writeln("</applet>");
    writeln("</body>");
    writeln("</html>");
    close(); 
  }
}

function DisplaySetFunction ( name , formula )
{
  document.applets[name].setFormula ( formula ) ;
}

function InputGetValue ( name )
{
  return document.forms[name].Text.value ;
}

var plotterWindow = null;

function plotterOpen ( parameters )
{
  if (plotterWindow && !plotterWindow.closed)
  {
    plotterWindow.focus();
    return;
  }

  // Applet dimensions
  appletWidth = 500 ;
  appletHeight = 400 ;

  x = 0 ;  // screen.availWidth - appletWidth - 10 ;
  y = 0 ; //screen.availHeight - appletHeight - 50 ;
  width = appletWidth + 10 ;
  height = appletHeight + 230 ;
  plotterWindow = open("","fen","width="+width+",height="+height+",left="+x+",top="+y);
  plotterWindow.document.open(); 
  with (plotterWindow.document) {
    writeln("<html><head><title>Plotter</title>"); 
    writeln("</head>"); 
    writeln("<body bgcolor='#FFFFFF'>" ) ;
//    writeln(" style='position:absolute;top:0px;left:0px;margin-top:0px;margin-left:0px'>");  
    writeln("<applet name=\"kurven\" code=\"kurven.class\" codebase=\"../Plotter\" width="+appletWidth+" height="+appletHeight+">");

    fct_string = "" ;
    
    if ( parameters != null )
    {
      pars = parameters.substr ( 0 , parameters.length ) ;
      while ( pars.length > 0 )
      {
        ind = pars.indexOf ( ':' ) ;
        if ( ind == -1 ) break ;
        para_name = pars.substring ( 0 , ind ) ;
        pars = pars.substring ( ind+1 , pars.length ) ;
        ind = pars.indexOf ( ';' ) ;
        if ( ind == -1 ) break ;
        para_value = pars.substring ( 0 , ind ) ;
        pars = pars.substring ( ind+1 , pars.length ) ;

        writeln ( "<param name=\""+para_name+"\" value=\""+para_value+"\">" ) ;
        
        if ( para_name == "setfunction" )
          fct_string = para_value ;
      }
    }

    writeln("</applet><br>"); 
    writeln("<table>");
    writeln("<tr>");
    writeln("<td><font color=\"red\">Funktion:</font></td>");
    writeln("<td><applet name=\"fct\" code=\"formula.class\" codebase=\"../Eqn\" width=350 height=50 align=\"middle\" hspace=\"0\" vspace=\"0\">"); 
    writeln("<param name='formula' value='"+fct_string+"'>");
    writeln("<param name='fgcolor' value='ff0000'>");
    writeln("<param name='bgcolor' value='ffffff'>");
    writeln("<param name='halign' value='left'>"); 
    writeln("<param name='editable' value='false'>");
    writeln("<param name='valign' value='middle'>");
    writeln("</applet></td>");
    writeln("<td><a href=\"Javascript:top.opener.plotterShowFormula('fct');\"");
    writeln("onMouseOver=\"document.mag0.src='../Eqn/mag_over.gif'; return true;\"");
    writeln("onMouseOut=\"document.mag0.src='../Eqn/mag.gif'; return true;\"><img name=mag0 src=\"../Eqn/mag.gif\" width=22 height=22 border=0></a>");
    writeln("</tr>");
    writeln("<tr>");
    writeln("<td><font color=\"blue\">Erste Ableitung:</font></td>");
    writeln("<td><applet name=\"deriv1\" code=\"formula.class\" codebase=\"../Eqn\" width=350 height=50 align=\"middle\" hspace=\"0\" vspace=\"0\">"); 
    writeln("<param name='formula' value='diff("+fct_string+",x,1)'>");
    writeln("<param name='fgcolor' value='0000ff'>");
    writeln("<param name='bgcolor' value='ffffff'>");
    writeln("<param name='halign' value='left'>"); 
    writeln("<param name='editable' value='false'>");
    writeln("<param name='valign' value='middle'>");
    writeln("</applet></td>");
    writeln("<td><a href=\"Javascript:top.opener.plotterShowFormula('deriv1');\"");
    writeln("onMouseOver=\"document.mag1.src='../Eqn/mag_over.gif'; return true;\"");
    writeln("onMouseOut=\"document.mag1.src='../Eqn/mag.gif'; return true;\"><img name=mag1 src=\"../Eqn/mag.gif\" width=22 height=22 border=0></a>");
    writeln("</tr>");
    writeln("</tr>");
    writeln("<tr>");
    writeln("<td><font color=\"lightgreen\">Zweite Ableitung:</green></td>");
    writeln("<td><applet name=\"deriv2\" code=\"formula.class\" codebase=\"../Eqn\" width=350 height=50 align=\"middle\" hspace=\"0\" vspace=\"0\">"); 
    writeln("<param name='formula' value='diff("+fct_string+",x,2)'>");
    writeln("<param name='fgcolor' value='00ff00'>");
    writeln("<param name='bgcolor' value='ffffff'>");
    writeln("<param name='halign' value='left'>"); 
    writeln("<param name='editable' value='false'>");
    writeln("<param name='valign' value='middle'>");
    writeln("</applet></td>");
    writeln("<td><a href=\"Javascript:top.opener.plotterShowFormula('deriv2');\"");
    writeln("onMouseOver=\"document.mag2.src='../Eqn/mag_over.gif'; return true;\"");
    writeln("onMouseOut=\"document.mag2.src='../Eqn/mag.gif'; return true;\"><img name=mag2 src=\"../Eqn/mag.gif\" width=22 height=22 border=0></a>");
    writeln("</tr>");
    writeln("</tr>");
    writeln("</table>");
    writeln("<form name='plotter' action ='' onSubmit='return false;'>");
    writeln("<input type=button value='Hilfe' onclick='top.opener.plotterShowHelp();'>");
    writeln("</form>");
    writeln("</body>");
    writeln("</html>");
    close(); 
  }
}



function plotterShowFunction ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.showFunction() ;
}

function plotterHideFunction ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.hideFunction() ;
}

function plotterShowLabel ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.showLabel () ;
}

function plotterHideLabel ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.hideLabel () ;
}

function plotterSetLabel ( name )
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.setLabel ( name ) ;
}

function plotterShowDeriv1 ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.showDeriv1() ;
}

function plotterHideDeriv1 ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.hideDeriv1() ;
}

function plotterShowDeriv1Ch ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.showDeriv1Ch() ;
}

function plotterHideDeriv1Ch ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.hideDeriv1Ch() ;
}

function plotterShowDeriv2 ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.showDeriv2() ;
}

function plotterHideDeriv2 ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.hideDeriv2() ;
}

function plotterShowDeriv2Ch ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.showDeriv2Ch() ;
}

function plotterHideDeriv2Ch ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.hideDeriv2Ch() ;
}

function plotterShowTangent ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.showTangent() ;
}

function plotterHideTangent ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.hideTangent() ;
}

function plotterShowSecant ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.showSecant() ;
}

function plotterHideSecant ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.hideSecant() ;
}

function plotterShowSlope ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.showSlope() ;
}

function plotterHideSlope ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.hideSlope() ;
}

function plotterShowNormal ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.showNormal() ;
}

function plotterHideNormal ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.hideNormal() ;
}

function plotterShowCircCurv ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.showCircCurv() ;
}

function plotterHideCircCurv ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.hideCircCurv() ;
}

function plotterShowEvolute ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.showEvolute() ;
}
 
function plotterHideEvolute ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.hideEvolute() ;
}

function plotterSetFunction ( expr )
{ 
  if (plotterWindow && !plotterWindow.closed)
  {
    plotterWindow.document.kurven.setFunction ( expr ) ;
    plotterWindow.document.applets['fct'].setFormula ( expr ) ;
    plotterWindow.document.applets['deriv1'].setFormula ( "diff("+expr+",x,1)" ) ;
    plotterWindow.document.applets['deriv2'].setFormula ( "diff("+expr+",x,2)" ) ;
  }
}

function plotterStartInt ( intDegree )
{
  if (plotterWindow && !plotterWindow.closed)
  {
    plotterWindow.focus();
    plotterWindow.document.kurven.startInt ( intDegree ) ;
  }
}

function plotterSetIntPol ()
{
  if (!plotterWindow || plotterWindow.closed)
    return;
  plotterWindow.focus();
  plotterWindow.document.kurven.setIntPol () ;
  var fct_string ;
  fct_string = plotterWindow.document.kurven.getFunction () ;
  fct_string = "" + fct_string ;   // This is necessary so that netscape 
                                   // interpretes fct_string as a string.
                                   // Otherwise it interpretes it as the
                                   // function getFunction(). A "Hurray!"
                                   // to netscape bugs!

  deriv1_string = diffPoly ( fct_string ) ;
  deriv2_string = diffPoly ( deriv1_string ) ;

  plotterWindow.document.applets['fct'].setFormula ( fct_string ) ;
  plotterWindow.document.applets['deriv1'].setFormula ( deriv1_string ) ;
  plotterWindow.document.applets['deriv2'].setFormula ( deriv2_string ) ;
}

function toTex ( expr )
{
  var ret , pointer ;

  pointer = 0 ;
  ret = expr ;

  while ( true )
  {
    pointer = ret.indexOf ( "*" ) ;
    if ( pointer < 0 ) break ;
   
    ret = ret.substring ( 0 , pointer ) + "\\cdot " + ret.substring ( pointer + 1 , ret.length ) ;  
  
  }
 
  return ret ;
}

function diffPoly ( expr )
{
  // Expects the polynomial to be in infix notation and the monomials
  // ordered by decreasing exponents. The monomial of degree 1 should be
  // a*x, not a*x^1. The monomial of degree 0 should be a not a*x^0.

  var temp , ret , pointer , pointer2 , pointer2 , exponent ;

  ret = "" ;
  temp = expr ;
  
  while ( true )
  {
    if ( temp == "" ) break ;
    
    pointer = temp.indexOf ( "x" ) ;
    if ( pointer < 0 ) break ;

    coeff = parseFloat ( temp ) ;
    if ( isNaN ( coeff ) ) coeff = 1.0 ;

    if ( ( pointer == temp.length-1 ) || ( temp.substring ( pointer+1 , pointer+2 ) != "^" ) )
    {
      coeff_deriv = coeff ;
      
      if ( coeff != 0.0 )
        ret += normalize ( coeff_deriv ) ;

      temp = temp.substring ( pointer+1 , temp.length ) ;
    }  
    else
    {
      temp = temp.substring ( pointer+2 , temp.length ) ;

      exponent = parseInt ( temp ) - 1 ;

      coeff_deriv = coeff * ( exponent + 1 ) ;
      coeff_deriv_string = normalize ( coeff_deriv ) ;

      if ( coeff != 0.0 )
        if ( exponent == 1 )
          ret += coeff_deriv_string + "*x" ;
        else 
          ret += coeff_deriv_string + "*x^" + exponent ;

      pointer2 = temp.indexOf ( "+" ) ;
      pointer3 = temp.indexOf ( "-" ) ;

      if ( pointer2 < 0 )
        if ( pointer3 < 0 ) break ;
        else temp = temp.substring ( pointer3 , temp.length ) ;
      else
        if ( pointer3 < 0 ) temp = temp.substring ( pointer2 , temp.length ) ;
        else temp = temp.substring ( Math.min ( pointer2 , pointer3 ) , temp.length ) ; 
    }

  }

  if ( ret.charAt ( 0 ) == "+" ) ret = ret.substring ( 1 , ret.length ) ;

  return ret ;
}

function normalize ( number )
{
  var temp , pointer ;
  
  temp = "0.0" ;

  if ( number > 0.0 )
  { 
    if ( number < 1.0 ) 
    {
      temp = "" + number ;
      pointer = temp.indexOf ( "." ) ;
      temp = temp.substring ( pointer+1 , temp.length ) ;
      temp = "+0." + temp ;
    }
    else 
      temp = "+" + number ;
  }
  else if ( number < 0.0 )
  { 
    if ( number > -1.0 ) 
    {
      temp = "" + number ;
      pointer = temp.indexOf ( "." ) ;
      temp = temp.substring ( pointer+1 , temp.length ) ;
      temp = "-0." + temp ;
    }
    else
      temp = "" + number
  }

  // cut off all decimal digits but the first two after the point

  pointer = temp.indexOf ( "." ) ;
  if ( pointer < temp.length - 3 ) temp = temp.substring ( 0 , pointer+3 ) ;

  return temp ;
}

function plotterShowTaylor ( taylorDegree )
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.showTaylor ( taylorDegree ) ; 
}

function plotterHideTaylor ()
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.hideTaylor () ; 
}

function plotterSetLimits ( xmin , xmax , ymin , ymax )
{
  if (plotterWindow && !plotterWindow.closed)
    plotterWindow.document.kurven.setLimits ( parseFloat(xmin) , parseFloat(xmax) , parseFloat(ymin) , parseFloat(ymax) ) ;
}

function plotterShowFormula ( name )
{
  if (!plotterWindow || plotterWindow.closed)
    return;
  fct_string = plotterWindow.document.applets[name].getTexFormula() ;
  fct_string = fct_string + "" ;

  width = plotterWindow.document.applets[name].getDimension().width ;
  height = plotterWindow.document.applets[name].getDimension().height ;

  if ( width < 100 ) width = 100 ;
  if ( height < 100 ) height = 100 ;

//  x = screen.availWidth - width - 10 ;
//  y = 0 ;

  fenster = open("","Lupe","width="+width+",height="+height+",left=100,top=100"); 
  fenster.document.open(); 
  with (fenster.document) {
    writeln("<html><head><title>Lupe</title>"); 
    writeln("<style type='text/css'>");
    writeln("#topleft {");
    writeln("	position: absolute;");
    writeln("	top:	  0px;");
    writeln("	left:	  0px;");
    writeln("   margin-left: 0px;");
    writeln("   margin-top:  0px;");
    writeln("}");
    writeln("</style>");
    writeln("</head>"); 
    writeln("<body id=topleft return true;\">");  
    writeln("<applet name=\""+name+"\" code=\"formula.class\" codebase=\"../Eqn\" width="+this.width+" height="+this.height+">"); 
    writeln("<param name=\"texformula\" value=\""+fct_string+"\">");
    writeln("<param name='fgcolor' value='000000'>");
    writeln("<param name='bgcolor' value='ffffff'>");
    writeln("<param name='halign' value='center'>"); 
    writeln("<param name='editable' value='false'>");
    writeln("<param name='valign' value='middle'>");
    writeln("</applet>");
    writeln("</body>");
    writeln("</html>");
    close(); 
  }
}

var plotterHelpWindow ;

function plotterShowHelp ( name )
{
  plotterHelpWindow = open("../Plotter/help.htm","Hilfe","width=600,height=450,left=100,top=100,scrollbars=yes"); 
}


// Function for user initiated window resizing

function setLocation( x , y , w , h )
{
  newWidth = w ;
  newHeight = h ;
  
  if ( document.layers )
  {
    tmp1 = parent.outerWidth - parent.innerWidth ;
    tmp2 = parent.outerHeight - parent.innerHeight ;
    newWidth -= tmp1 ;
    newHeight -= tmp2 ;
  }

  parent.window.resizeTo ( newWidth , newHeight ) ;
  parent.window.moveTo ( x , y ) ;
}

