// ----------------------------------------------------------------
//
// %W% %G% %U%
//
// movingLayer.js
// --------------
// Author: Walter Rothlin
//
// Abstract: Contains JavaScript functions to moving layers depending
//           on the browser's scroll bar position and function to handle
//           static layers
//
// History:
// 08/10/99    V1.0    Initial Version
// 08/31/99    V1.1    Add multiLayer moving
// 09/05/99    V1.2    Add Static layer handling
// 09/14/99    V1.3    Disable scrolling in one direction
// 09/15/99    V1.4    Methods for animation on a Line, Circle
// 09/16/99    V1.5    Animated moving layers
// 09/21/99    V1.6    New animateLayer*_1 function
//
// ----------------------------------------------------------------

var relocateTimer= null;


function relocateLayer() {
  if (IE4) {
     x_offset= document.body.scrollLeft;
     y_offset= document.body.scrollTop;
  } else {
     x_offset= pageXOffset;
     y_offset= pageYOffset;
  }
  moveLayer("MovingLayer",x_offset, y_offset);
  document.relocateTimer= setTimeout( "relocateLayer()", 100 );
}

function getScrollBarPositions() {
  if (IE4) {
     x_offset = document.body.scrollLeft;
     y_offset = document.body.scrollTop;
  } else {
     x_offset = pageXOffset;
     y_offset = pageYOffset;
  }
  retPos = new Point(x_offset,y_offset);
  return retPos;
}

scrollPositionOld = new Point(0,0);

function relocateLayers(countOfLayers) {
  scrollPosition = getScrollBarPositions();

  if (!(pointsAreEqual(scrollPosition,scrollPositionOld))) {
    for (var i=0; i<countOfLayers; i++) {
      var layerNames = movingLayer_Names[i];
      var left       = movingLayer_Left[i];
      var top        = movingLayer_Top[i];
      if (movingLayer_horizontal[i] == null) {
        movingLayer_horizontal[i] = true;
	  }
	  if (movingLayer_horizontal[i]) {
	    left = scrollPosition.x + movingLayer_Left[i];
	  }
      if (movingLayer_vertical[i] == null) {
        movingLayer_vertical[i] = true;
	  }
	  if (movingLayer_vertical[i]) {
        top  = scrollPosition.y + movingLayer_Top[i];
	  }
      moveLayer(layerNames,left,top);
    }
	scrollPositionOld = scrollPosition;
	// showOnStatus('New Scrollbars');
  }
  document.relocateTimer= setTimeout( "relocateLayers("+countOfLayers+")", 100 );
}


function loadScrollLayer() {
  if( !document.relocateTimer ) {
    relocateLayer();
  }
}

function loadScrollLayers(countOfLayers) {
  if( !document.relocateTimer ) {
    relocateLayers(countOfLayers);
  }
}

function unloadScrollLayer() {
  if( document.relocateTimer ) {
    clearTimeout( document.relocateTimer );
    document.relocateTimer= null;
  }
}

function focusScrollLayer() {
//Keine Funktion
}

function blurScrollLayer() {
//Keine Funktion
}


function createStaticLayer_Start(layerIndex) {
  var layerName   = staticLayer_Names     [layerIndex];
  var left        = staticLayer_Left      [layerIndex];
  var top         = staticLayer_Top       [layerIndex];
  var width       = staticLayer_Width     [layerIndex];
  var heigh       = staticLayer_Heigh     [layerIndex];
  var bgColor     = staticLayer_bgColor   [layerIndex];
  var bgImage     = staticLayer_bgImage   [layerIndex];
  var aligment    = staticLayer_aligment  [layerIndex];
  var withTable   = (!((bgColor == "") && (bgImage == "")));
  createHTMLLayer_Start(layerName,left,top,width,heigh,withTable,bgColor,bgImage,aligment);
}


function createLayer_Start(layerIndex) {
  var layerName   = movingLayer_Names     [layerIndex];
  var left        = movingLayer_Left      [layerIndex];
  var top         = movingLayer_Top       [layerIndex];
  var width       = movingLayer_Width     [layerIndex];
  var heigh       = movingLayer_Heigh     [layerIndex];
  var bgColor     = movingLayer_bgColor   [layerIndex];
  var bgImage     = movingLayer_bgImage   [layerIndex];
  var aligment    = movingLayer_aligment  [layerIndex];
  var withTable   = (!((bgColor == "") && (bgImage == "")));
  createHTMLLayer_Start(layerName,left,top,width,heigh,withTable,bgColor,bgImage,aligment);
}

function createHTMLLayer_Start(layerName,left,top,width,heigh,withTable,bgColor,bgImage,aligment) {
  if (IE4) {
     document.write("<DIV ID='" + layerName + "' STYLE='position:absolute; left:"+left+"; top:"+top+"; width:"+width+"; height:"+heigh+"; visibility:visible;'>\n");
  } else {
     document.write("<LAYER ID='" + layerName + "' LEFT="+left+" TOP="+top+" WIDTH="+width+" HEIGHT="+heigh+" VISIBILITY=SHOW>\n");
  }
  if (withTable) {
     document.write("<TABLE background='"+bgImage+"' bgcolor='"+bgColor+"' border='0' cellpadding='0' cellspacing='0'>\n");
     document.write("  <TR><TD valign='"+aligment+"' height='"+heigh+"' width='"+width+"'>\n");
  }
}


function writeLayerHTML_Start(width,heigh,withTable,bgColor,bgImage,aligment) {
  createHTMLLayer_Start("MovingLayer",0,0,width,heigh,withTable,bgColor,bgImage,aligment)
}

function createLayer_End(document,layerIndex) {
  var bgColor     = movingLayer_bgColor   [layerIndex];
  var bgImage     = movingLayer_bgImage   [layerIndex];
  createHTMLLayer_End(document,bgColor,bgImage);
}

function createStaticLayer_End(document,layerIndex) {
  var bgColor     = staticLayer_bgColor   [layerIndex];
  var bgImage     = staticLayer_bgImage   [layerIndex];
  createHTMLLayer_End(document,bgColor,bgImage);
}

function createHTMLLayer_End(document,bgColor,bgImage) {
  var withTable   = (!((bgColor == "") && (bgImage == "")));
  if (withTable) {
     document.write("</TD></TR></TABLE>\n");
  }
  if (IE4) {
     document.write("</DIV>\n");
  } else {
     document.write("</LAYER>\n");
  }
}

// Functions for Points, Lines, ..
function Point(x, y) {
   this.x  = x;
   this.y  = y;
   this.toString = displayPoint;
}

function pointsAreEqual(pointA,pointB) {
   return ((pointA.x == pointB.x) && (pointA.y == pointB.y));
}

function displayPoint() {
  return '(' + this.x + '/' + this.y + ')';
}

function getLineCoordinates(startPoint,endPoint,step,firstStep,lastStep) {
 with (Math) {
	if (step > lastStep)  { step = lastStep; }
	if (step < firstStep) { step = firstStep; }
	var x1 = startPoint.x;
	var y1 = startPoint.y;
	var x2 = endPoint.x;
	var y2 = endPoint.y;
	var x  = x1 + ((x2-x1) / (lastStep-firstStep) * step);
	var a  = ((y1-y2)/(x1-x2));
	var c  = y1 - (a*x1);
    var y  = (a*x) + c;
	retPoint = new Point(round(x),round(y));
	// showOnStatus('x:' + x + ' x1:' + x1 + ' x2:' + x2  + ' Step: ' + step + ' x:' + round(x) + ' y:' + round(y));
	return retPoint;
 }
}

function getCircleMRCoordinates(startPoint,endPoint,centerPoint,radius,rotationClockWise,step,firstStep,lastStep) {
 with (Math) {
	if (step > lastStep)  { step = lastStep; }
	if (step < firstStep) { step = firstStep; }

	if (rotationClockWise ==  null) {
	   rotationClockWise = true;
	}
	if (rotationClockWise) {
       rotationDirection = -1;
	} else {
	   rotationDirection = 1;
	}

	var startWinkel  = atan2((startPoint.x - centerPoint.x),(startPoint.y - centerPoint.y));
	var endWinkel    = atan2((endPoint.x   - centerPoint.x),(endPoint.y   - centerPoint.y));
	var x    = startWinkel + ((endWinkel-startWinkel) / (lastStep-firstStep) * step);
	var xNew = round(centerPoint.x + (-cos(1.586 + x)*radius));
	var yNew = round(centerPoint.y + (rotationDirection * sin(1.586 + x)*radius));

	retPoint = new Point(xNew,yNew);

	// showOnStatus('startPoint(' + startPoint.x + '/' + startPoint.y + ') endPoint(' + endPoint.x + '/' + endPoint.y +') centerPoint(' + centerPoint.x + '/' + centerPoint.y +')');
	// showOnStatus('(' + startPoint.y + '-' + centerPoint.y + ')/(' + centerPoint.x + '-' + startPoint.x + ')=' +quot1);
	// showOnStatus('startWinkel:' + startWinkel + ' endWinkel:' + endWinkel + ' x:' + x  + ' Step: ' + step + ' x:' + xNew + ' y:' + yNew);
	// showOnStatus('x:' + x  + ' Step: ' + step + ' x:' + xNew + ' y:' + yNew);
	return retPoint;
 }
}

function getCircle3PCoordinates(startPoint,intermediatePoint,endPoint,rotationClockWise,step,firstStep,lastStep) {
	var centerPoint  = calcMittelPunkt(startPoint,intermediatePoint,endPoint);
	var radius       = calcRadius(centerPoint,intermediatePoint);
	return getCircleMRCoordinates(startPoint,endPoint,centerPoint,radius,rotationClockWise,step,firstStep,lastStep);
}

function calcRadius(centerPoint,anyPoint) {
   // showOnStatus('Center:' + centerPoint.toString() + ' Any:' + anyPoint.toString());
   var x = centerPoint.x - anyPoint.x;
   var y = centerPoint.y - anyPoint.y;
   return Math.sqrt((x*x) + (y*y));
}

function calcMittelPunkt(startPoint,intermediatePoint,endPoint) {
   retPoint = new Point(150,300);  // TBS TBS not implemented yet
   return retPoint;
}

// Animation functions
function animationDisplayLayer(layerType,layerNo) {
  showOnStatus('');
}

function animationMoveLayer(layerType,layerNo,newPoint) {
  step[layerNo]++;
  // showOnStatus('Step (layerNo): ' + step[layerNo] + ' x:' + newPoint.x + ' y:' + newPoint.y);
  if (layerType == 'static') {
     staticLayer_Left[aniLayerNo[layerNo]] = newPoint.x;
     staticLayer_Top[aniLayerNo[layerNo]]  = newPoint.y;
     showLayer(staticLayer_Names[aniLayerNo[layerNo]],true);
     moveLayer(staticLayer_Names[aniLayerNo[layerNo]],staticLayer_Left[aniLayerNo[layerNo]],staticLayer_Top[aniLayerNo[layerNo]]);
  } else {
     scrollPosition = getScrollBarPositions();
     movingLayer_Left[aniLayerNo[layerNo]] = newPoint.x;
	 var leftPos =  movingLayer_Left[aniLayerNo[layerNo]] + scrollPosition.x;
     movingLayer_Top[aniLayerNo[layerNo]]  = newPoint.y;
	 var topPos =  movingLayer_Top[aniLayerNo[layerNo]] + scrollPosition.y;
     showLayer(movingLayer_Names[aniLayerNo[layerNo]],true);
     moveLayer(movingLayer_Names[aniLayerNo[layerNo]],leftPos,topPos);
  }
}

function animateLayerOnLine(layerNo,delay,aniHandlerNo) {      // obsolet! use animateLayerOnLine_1
  animationDisplayLayer(layerType[layerNo],layerNo);
  var newPoint = getLineCoordinates(startPoint[layerNo],endPoint[layerNo],step[layerNo],firstStep[layerNo],lastStep[layerNo]);
  animationMoveLayer(layerType[layerNo],layerNo,newPoint);
  if (!(pointsAreEqual(newPoint,endPoint[layerNo]))) {
     submit(layerNo,delay,aniHandlerNo);
  }
}

// returns false as soon as end point is reached
function animateLayerOnLine_1(layerNo,delay,aniHandlerNo) {
  animationDisplayLayer(layerType[layerNo],layerNo);
  var newPoint = getLineCoordinates(startPoint[layerNo],endPoint[layerNo],step[layerNo],firstStep[layerNo],lastStep[layerNo]);
  animationMoveLayer(layerType[layerNo],layerNo,newPoint);
  if (pointsAreEqual(newPoint,endPoint[layerNo])) {
     return false;
  } else {
     return true;  
  }
}


function animateLayerOnCircle(layerNo,delay,aniHandlerNo) {      // obsolet! use animateLayerOnCircle_1
  animationDisplayLayer(layerType[layerNo],layerNo);
  var newPoint = getCircleMRCoordinates(startPoint[layerNo],endPoint[layerNo],centerPoint[layerNo],radius[layerNo],rotationClockWise[layerNo],step[layerNo],firstStep[layerNo],lastStep[layerNo])
  animationMoveLayer(layerType[layerNo],layerNo,newPoint);
  if (!(pointsAreEqual(newPoint,endPoint[layerNo]))) {
     submit(layerNo,delay,aniHandlerNo);
  }
}

// returns false as soon as end point is reached
function animateLayerOnCircle_1(layerNo,delay,aniHandlerNo) {
  animationDisplayLayer(layerType[layerNo],layerNo);
  var newPoint = getCircleMRCoordinates(startPoint[layerNo],endPoint[layerNo],centerPoint[layerNo],radius[layerNo],rotationClockWise[layerNo],step[layerNo],firstStep[layerNo],lastStep[layerNo])
  animationMoveLayer(layerType[layerNo],layerNo,newPoint);
  if (pointsAreEqual(newPoint,endPoint[layerNo])) {
     return false;
  } else {
     return true;  
  }
}

function animateLayerOnCircle3P(layerNo,delay,aniHandlerNo) {      // obsolet! use animateLayerOnCircle3P_1
  animationDisplayLayer(layerType[layerNo],layerNo);
  var newPoint = getCircle3PCoordinates(startPoint[layerNo],interPoint[layerNo],endPoint[layerNo],rotationClockWise[layerNo],step[layerNo],firstStep[layerNo],lastStep[layerNo]);
  animationMoveLayer(layerType[layerNo],layerNo,newPoint);
  if (!(pointsAreEqual(newPoint,endPoint[layerNo]))) {
     submit(layerNo,delay,aniHandlerNo);
  }
}

// returns false as soon as end point is reached
function animateLayerOnCircle3P_1(layerNo,delay,aniHandlerNo) {
  animationDisplayLayer(layerType[layerNo],layerNo);
  var newPoint = getCircle3PCoordinates(startPoint[layerNo],interPoint[layerNo],endPoint[layerNo],rotationClockWise[layerNo],step[layerNo],firstStep[layerNo],lastStep[layerNo]);
  animationMoveLayer(layerType[layerNo],layerNo,newPoint);
  if (pointsAreEqual(newPoint,endPoint[layerNo])) {
     return false;
  } else {
     return true;  
  }
}
