//***********************************************
//* ADOSCROLLER 1.0
//***********************************************
//* Standard Scroller modified and optimized.
//*
//* Last modification: 2004-11-17
//*
//* Author: Boris Gregorcic
//*         BOC Information Systems GmbH
//*
//***********************************************

// CONFIGURATION SECTION


//---------------------------------------------
// the width and height of the scroller window
// should be the same as the width and height
// of the frame in which the AdoScroller.htm
// is loaded.

scroller_window_width = 0;
scroller_window_height = 0;

//---------------------------------------------
// The standard header space defines the space
// from the top of the model page to the actual
// upper border of the model graphic. Normally 
// the header contains the model name, however
// you should make sure, that it is always exactly 
// the height specified here.
// Therefore you should also put possible text
// emitted in the header inside a <NOBR></NOBR>
// to avoid line breaks and changes in the header 
// height when resizing the browser window

std_header_space = 0;
std_footer_space = 0;


//---------------------------------------------
// Scrollbar defines the width of the space
// allocated for a scrollbar. This is relevant
// if the model frame has scrolling enabled,
// which is of course true in most of the cases

scrollbar = 14;

//---------------------------------------------
// adoScrollerFrame names the frame where the
// AdoScroller.htm is been loaded. If you don't 
// use the default frame name 'ado_scroller',
// change this variable to parent.<framename>.

 q = 0; // don't ask ... was necessary in seperate window mode
       // set to 4 there 

/***********************************/

selIn = false;
newX = 0;
newY = 0;
selDrag = false;
var sortZoom = new Array;   // SMU

init_vars();

//---------------------------------------------
window.onload = function()
//---------------------------------------------
{
	document.onmousemove = GetAdoMouse;
	selection_x = 0;
	selection_y = 0;
	ATM_x = 0;
	ATM_y = 0;
	factor = 0;
}

//---------------------------------------------
function getSelectionX()
//---------------------------------------------
{
  return selection_x;
}

//---------------------------------------------
function GetAdoMouse(Event)
//---------------------------------------------
{
  oldX = newX;
  oldY = newY;
  
  if (Event)
  {
    oEvent = Event;
  }
  else
  {
    oEvent = event;
  }

  newX = oEvent.clientX;
  newY = oEvent.clientY;
  if (selDrag)
  {    
    drag(newX - oldX, newY - oldY);
  }
}


//---------------------------------------------
function drag(deltaX, deltaY)
//---------------------------------------------
{
  oObj = document.getElementById("Layer1"); 
  if (!oObj || !parent.overview)
  {
    window.setTimeout("drag(" + deltaX + "," + deltaY + ");", 100);
  }
  else
  {
    if (!parent.overview.factor)
    {
      window.setTimeout("drag(" + deltaX + "," + deltaY + ");", 100);
    }
    else
    {
      oX = oObj.style.left;
      oY = oObj.style.top;  
      oX = Number(oX.substring(0, oX.length - 2));
      oY = Number(oY.substring(0, oY.length - 2));

      if ((oX + deltaX >= 0) && (oX + deltaX + parent.overview.scrollselector_x <= scroller_window_width))
      {
        oObj.style.left = oX + deltaX;
      }
      if ((oY + deltaY >= 0) && (oY + deltaY + parent.overview.scrollselector_y <= scroller_window_height))
      {
        oObj.style.top = oY + deltaY;
      }
      myScrollTo(oX*parent.overview.factor, oY*parent.overview.factor);
    }
  }
}

//---------------------------------------------
function init_vars()
//---------------------------------------------
{
  if (!parent.overview)
  {
    window.setTimeout("init_vars();",100);
  }
  else
  {
    oAdoScroller = parent.overview.document.getElementById("scrollerbody");
    if (!oAdoScroller)
    {
      window.setTimeout("init_vars();",100);
    }
    else
    {
      scroller_window_width = oAdoScroller.offsetWidth;
      scroller_window_height = oAdoScroller.offsetHeight;
    }
  }
}

//---------------------------------------------
function resizeAS()
//---------------------------------------------
{
  if (!parent.model)
  {
    window.setTimeout("resizeAS();",100);      
  }
  else
  {
    initAS();
  }
}

//---------------------------------------------
function reloadAS()
//---------------------------------------------
{
  if (self != top)
  {
    if (!parent.overview)
    {
      window.setTimeout("reloadAS();",100);      
    }
    else
    {
      parent.overview.location.reload();
    }
  }
}

//---------------------------------------------
function initAS()
//---------------------------------------------
{
  init_vars();
  if (!parent.model)
  {
    window.setTimeout("initAS();", 100);
  }
  else
  {
  	elemImage = parent.model.document.getElementById("modelimage");
  	
  	if (elemImage)
  	{
			oMg = elemImage.firstChild;
			oMb = parent.model.document.getElementById("modelbody");
			oMp = parent.model.document.getElementById("normal");

			scrollImage = parent.model.overviewimage;
			if (scrollImage.src == "")
			{
				window.setTimeout("initAS();", 100);
			}
			else
			{
				x_overflow = false;
				y_overflow = false;
				
				modelimage_x = oMg.offsetWidth;
				modelimage_y = oMg.offsetHeight;
				
				if (parent.model.innerWidth) //Firefox
				{
					modelwindow_x = parent.model.innerWidth;
					modelwindow_y = parent.model.innerHeight;
					modelpage_x = Math.max(oMb.offsetWidth, modelimage_x);
					modelpage_y = Math.max(oMb.offsetHeight, modelimage_y);
				}
				else //IE
				{
					modelwindow_x = oMb.offsetWidth;
					modelwindow_y = oMb.offsetHeight;
					modelpage_x = oMp.offsetWidth;
					modelpage_y = oMp.offsetHeight;					
				}
				
				modelpage_x = (modelpage_y > modelwindow_y)?(modelpage_x + scrollbar):modelpage_x;
				modelpage_y = (modelpage_x > modelwindow_x)?(modelpage_y + scrollbar):modelpage_y;
								
				scrollimage_x = scrollImage.width;
				scrollimage_y = scrollImage.height;
				
				scrollwindow_x = scroller_window_width;
				scrollwindow_y = scroller_window_height;
				
				parent.overview.factor = Math.max(modelpage_x /scrollwindow_x, modelpage_y / scrollwindow_y);
				
				parent.overview.scrollselector_x = Math.floor(modelwindow_x / parent.overview.factor);
				parent.overview.scrollselector_y = Math.floor(modelwindow_y / parent.overview.factor);
								
				parent.overview.scrollpage_x = Math.floor(modelpage_x / parent.overview.factor);
				parent.overview.scrollpage_y = Math.floor(modelpage_y / parent.overview.factor);

				scrollselector_x = (scrollselector_x > scrollpage_x)?scrollpage_x:scrollselector_x;
				scrollselector_y = (scrollselector_y > scrollpage_y)?scrollpage_y:scrollselector_y;
				
				factor_image = Math.max(modelimage_x / scrollimage_x, modelimage_y / scrollimage_y);
				factor_scroll = (factor / factor_image);
				
				parent.overview.scrollimagefitted_x = Math.floor(scrollimage_x / factor_scroll);
				parent.overview.scrollimagefitted_y = Math.floor(scrollimage_y / factor_scroll);
				
				initAdoScroller();
	
				//alert("Breite Modellgrafik: " + modelimage_x + "\nHöhe Modellgrafik: " + modelimage_y + "\nBreite Modellfenster: " + modelwindow_x + "\nHöhe Modellfenster: " + modelwindow_y + "\nBreite Modellseite: " + modelpage_x + "\nHöhe Modellseite: " + modelpage_y + "\n\nBreite Scrollgrafik: " + scrollimage_x + "\nHöhe Scrollgrafik: " + scrollimage_y + "\nBreite Scrollfenster: " + scrollwindow_x + "\nHöhe Scrollfenster: " + scrollwindow_y + "\n\nSkalierungsfaktor Modellseite->Scrollfenster: " + factor_page + "\n\nBreite Selektor: " + scrollselector_x + "\nHöhe Selektor: " + scrollselector_y + "\n\nSkalierungsfaktor Modellgrafik->Scrollgrafik: " + factor_image + "\n\nSkalierungsfaktor Scrollgrafik->eingepasste Scrollgrafik: " + factor_scroll + "\n\nBreite eingepasste Scrollgrafik: " + scrollimagefitted_x + "\nHöhe eingepasste Scrollgrafik: " + scrollimagefitted_y);				
			}
		}
	}
}
/***********************************/

//---------------------------------------------
function AS_position()
//---------------------------------------------
{
  if (parent.overview.document.getElementById('Layer1') && (parent.model.isInScrollWindow == 0))
  {
    parent.overview.document.getElementById('Layer1').style.top = parseInt(parent.model.document.getElementById("modelbody").scrollTop/parent.overview.factor);
    parent.overview.document.getElementById('Layer1').style.left = parseInt(parent.model.document.getElementById("modelbody").scrollLeft/parent.overview.factor);
  }
}

//---------------------------------------------
function initAdoScroller()
//---------------------------------------------
{
  if (! parent.overview)
  {
    window.setTimeout("initAdoScroller();", 100);
  }
  else
  {
    if (! parent.overview.document)
    {
      window.setTimeout("initAdoScroller();", 100);
    }
    else
    {
      cont = parent.overview.document.getElementById("container");
      if (! cont)
      {
        window.setTimeout("initAdoScroller();", 100);
      }
      else
      {        
        if ((! parent.overview.scrollselector_x) || (! parent.overview.scrollselector_x))
        {
          window.setTimeout("initAdoScroller();", 100);
        }
        else
        {
          content = ("<table id=\"ASModelArea\" width=\"" + parent.overview.scrollpage_x + "\" height=\"" + parent.overview.scrollpage_y + "\"><tr><td><img src=\"" + scrollImage.src + "\" width=\"" + parent.overview.scrollimagefitted_x + "\" height=\"" + parent.overview.scrollimagefitted_y + "\" border=\"0\"></td></tr></table>");
          content += ("<div id=\"Layer1\" style=\"position:absolute; width:" + parent.overview.scrollselector_x + "px; height:" + parent.overview.scrollselector_y + "px; z-index:1; left: 0px; top: 0px\" ");
          content += ("onMouseOver=\"selectionIn();\" onMouseOut=\"selectionOut();\" ");
          content += ("onMouseDown=\"startDrag();\" onMouseUp=\"endDrag();\">");
          content += ("<table id=\"ASSelectionArea\" width=\"" + parent.overview.scrollselector_x + "\"  height=\"" + parent.overview.scrollselector_y + "\" >");
          content += ("  <tr border=\"0\">");
          content += ("    <td>&nbsp;</td>");
          content += ("  </tr>");
          content += ("</table>");
          content += ("</div>");
          cont.innerHTML = content;
        }
      }
    }
  }
}

//---------------------------------------------
function selectionIn()
//---------------------------------------------
{
  selIn = true;
}

//---------------------------------------------
function endDrag()
//---------------------------------------------
{
  parent.model.isInScrollWindow = 0;
  selDrag = false;
}

//---------------------------------------------
function startDrag()
//---------------------------------------------
{
  parent.model.isInScrollWindow = 1;
  selDrag = true;
}
//---------------------------------------------
function selectionOut()
//---------------------------------------------
{
  selIn = false;
  endDrag();
}

//---------------------------------------------
function myScrollTo(x,y)
//---------------------------------------------
{
  if (!parent.model)
  {
    window.setTimeout("myScrollTo(" + x + "," + y + ");", 100);
  }
  else
  {
    parent.model.scrollTo(Math.round(x),Math.round(y));
  }
}