/*V8.4*/
// MAJOR CHANGE: Fixed for images inside another Layer (Netscape issue only)
// MAJOR CHANGE: New Grow Method implemented: right
// MAJOR CHANGE: An optional param has been added in Mouseover for the Site Design Tree.
//               The site tree pops up a box after the zoom.
// MAJOR CHANGE: Now, you can set specialAddLeft and specialAddTop to move the image 
// MAJOR CHANGE: Now, elements (div tags) are printed by the ASP.NET script.

/*
 Programmer  : Francois Simard
 File Name   : zoom.js
 Date        : 4/18/2001
 Description : Script that zoom in or out an image.
 Copyright   : DeepMetrix Corp.
*/
//  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//          GLOBAL VARIABLES
//  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
var GrowMethod = 'center';
var specialAddLeft = 0;
var specialAddTop  = 0;

var bw         = new checkBrowser(); // Browser Detection
var zoomObj    = new Array();        // all the zoom objects
var stepWidth  = 0;                  // values to add to zoom corectly (Float)
var stepHeight = 0;                  // values to add to zoom corectly (Float)
var arrLen     = 0;
var screenWidth, screenHeight;
var scLeft, scTop;
var siteTreeBox, lastX, lastY;

var zoomNeedsLoadCheck = false; // if true, zoom has to wait for the onload event to occur (loaded=true) before working
var zoomLoaded = false;

// Open a window with the link from the element
var currentHref = '';
function OpenLink()
{
  if(currentHref != '#')
    window.open(currentHref, parseInt(Math.random()*100000).toString());
};

function EnableZoom()
{
  zoomLoaded = true;
}

//  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//          MOUSEOVER
//  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
function MouseOver(hLink, zName, iSrc, hRef, endW, endH, ImgGrowMethod, siteTreeParams, nsLayerName)
{
  if ( ImgGrowMethod )
   GrowMethod = ImgGrowMethod; 

  if(!zoomNeedsLoadCheck || (zoomNeedsLoadCheck && zoomLoaded))
    {
      var firstLoop = false;
      if(!zoomObj[zName])
        {
          firstLoop = true;
          zoomObj[zName] = new ZoomObj(zName, iSrc, nsLayerName);
          if(GrowMethod.toLowerCase() == 'bottomright')
            {
              zoomObj[zName].imgTop  += zoomObj[zName].initHeight + parseInt(endH/2);
              zoomObj[zName].imgLeft += zoomObj[zName].initWidth + parseInt(endW/2);
            }
        }

      // siteTreeParams is an array containing all the params to pass to the site tree box function
      siteTreeBox = siteTreeParams;

      if(siteTreeBox)// Make sure it is hidden
        {
          SV("pageInfo", "hidden");
          HideTag();
        }

      currentHref = hRef; // OpenLink

      zoomObj[zName].endWidth = endW;
      zoomObj[zName].endHeight = endH;

      if(hLink != '')
        zoomObj[zName].currLink = hLink;

      //Zoom out all other image in case the mouse out did not occured
      for (ele in zoomObj)
       {
          if(zoomObj[ele].showing)
            {
              window.clearTimeout(zoomObj[ele].timeOut);
              if(bw.ie && !bw.ns6)
                ZoomIE(ele, '--', zoomObj[ele].initWidth, zoomObj[ele].initHeight);
              else
                ZoomNS(ele, '--', zoomObj[ele].initWidth, zoomObj[ele].initHeight);
            }
        }

      // Steps
      stepWidth  = (zoomObj[zName].endWidth  - zoomObj[zName].initWidth)  /20;
      stepHeight = (zoomObj[zName].endHeight - zoomObj[zName].initHeight) /20;

      if(zoomObj[zName].timeOut == null)
        {
          zoomObj[zName].x = zoomObj[zName].imgLeft;
          zoomObj[zName].y = zoomObj[zName].imgTop;

          if(bw.ie && !bw.ns6 && !bw.macie)
            {
              zoomObj[zName].SetWidth(zoomObj[zName].initWidth);
              zoomObj[zName].SetHeight(zoomObj[zName].initHeight);
            }
        }
      else
        {
          window.clearTimeout(zoomObj[zName].timeOut);
          zoomObj[zName].timeOut = null;
        }

      scLeft = (document.all) ? document.body.scrollLeft : window.pageXOffset;
      scTop  = (document.all) ? document.body.scrollTop : window.pageYOffset;

      screenWidth = (document.all) ? document.body.clientWidth : window.innerWidth;

      if(bw.ie && !bw.ns6 && !bw.macie)
        ZoomIE(zName, '++', zoomObj[zName].endWidth, zoomObj[zName].endHeight);
      else
        ZoomNS(zName, '++', zoomObj[zName].endWidth, zoomObj[zName].endHeight);
    }
}

//  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//          MOUSEOUT
//  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
function MouseOut(zName)
{
  if(siteTreeBox)
    {
      HideTag();
      SV("pageInfo", "hidden");
      siteTreeBox = null;
    }

  if(zoomObj[zName].timeOut)
    {
      window.clearTimeout(zoomObj[zName].timeOut);
      zoomObj[zName].timeOut = null;
    }
  // An image cannot be resized in NS, NS6 and IE FOR MAC so we are going to show a bigger image only
  if(bw.ie && !bw.ns6 && !bw.macie)
    ZoomIE(zName, '--', zoomObj[zName].initWidth, zoomObj[zName].initHeight);
  else
    ZoomNS(zName, '--', zoomObj[zName].endWidth, zoomObj[zName].endHeight);
}

//  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//          ZOOM IE (called on MouseOver and MouseOut) IE ONLY
//  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
// Set increment to ++ to zoom in and -- to zoom out IE ONLY
function ZoomIE(ind, increment, wV, hV)
{
  if(!zoomObj[ind].showing)
    zoomObj[ind].SetVis(1);

  var ok = true;

  // Detect if edge is where it's at
  if(increment == '++')
    {
      if(zoomObj[ind].layerWidth >= wV || zoomObj[ind].layerHeight >= hV)
        {
          ok = false;
          zoomObj[ind].SetWidth(wV);
          zoomObj[ind].SetHeight(hV);
          zoomObj[ind].timeOut = null;
          // Zoom is done, now for the site tree 
          // we pass the left and top value of this image
          if(siteTreeBox)
            {
              siteTreeBox[siteTreeBox.length] = lastX + 10;
              siteTreeBox[siteTreeBox.length] = lastY;
              ShowBox(siteTreeBox);
            }
        }
    }
  else
    {
      if(zoomObj[ind].layerWidth <= wV || zoomObj[ind].layerHeight <= hV)
        {
          ok = false;
          //Make it dissapear
          zoomObj[ind].SetVis(0);
          zoomObj[ind].timeOut = null;
        }
    }

  // Grow of shrink
  if(ok)
    {
      var newX = zoomObj[ind].x;
      var newY = zoomObj[ind].y;

      if(increment == '--')
        {
          zoomObj[ind].SetHeight(zoomObj[ind].layerHeight-stepHeight);
          zoomObj[ind].SetWidth(zoomObj[ind].layerWidth-stepWidth);
          newX += stepWidth/2;
          newY += stepHeight/2;
        }
      else
        {
          zoomObj[ind].SetHeight(zoomObj[ind].layerHeight+stepHeight);
          zoomObj[ind].SetWidth(zoomObj[ind].layerWidth+stepWidth);
          newX -= stepWidth/2;
          newY -= stepHeight/2;
        }

      if(newX < scLeft)
        newX = scLeft;

      if(newX + zoomObj[ind].layerWidth > (screenWidth + scLeft) - 5)
        newX = (screenWidth - zoomObj[ind].layerWidth) - 5;

      if(newY < scTop)
        newY = scTop;
      if(newY > screenHeight + scTop)
        newY = (screenHeight - scTop) - zoomObj[ind].endHeight;

      if(GrowMethod.toLowerCase() == 'right')
        {
          if(increment == "--")
            {
              newX = zoomObj[ind].x;
            }
          if(newX < zoomObj[ind].imgLeft)
            {
              newX = zoomObj[ind].imgLeft;
            }
        }
        else if(GrowMethod.toLowerCase() == 'bottomright')
        {
          newY = GetRealTop(ind)
          newX = GetRealLeft(ind);
        }
        else if(GrowMethod.toLowerCase() == 'bottomleft')
        {
          newY = GetRealTop(ind)
          newX = GetRealLeft(ind) - zoomObj[ind].layerWidth + zoomObj[ind].initWidth + 1;
        }

      // This is saved for the site tree later
      lastX = newX + parseInt(zoomObj[ind].endWidth);
      lastY = newY;

      zoomObj[ind].MoveLayer(newX, newY);
      zoomObj[ind].timeOut = window.setTimeout("ZoomIE('"+ind+"', '"+increment+"', "+wV+", "+hV+")", 5);
    }
}

//  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//          ZOOM NS (called on MouseOver and MouseOut) NS, NS6 and MAC
//  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
function ZoomNS(ind, increment, wV, hV)
{
  if(increment == '++')
    {
      var newX = zoomObj[ind].imgLeft - parseInt((wV  - zoomObj[ind].initWidth) /2);
      var newY = zoomObj[ind].imgTop  - parseInt((hV - zoomObj[ind].initHeight)/2);

      if(newX < scLeft)
        newX = scLeft;
      if(newX + parseInt(zoomObj[ind].endWidth) > (screenWidth + scLeft) - 5)
        newX = ((screenWidth - zoomObj[ind].endWidth) + scLeft) - 5;

      if(newY < scTop)
        newY = scTop;
      if(newY > screenHeight + scTop)
        newY = (screenHeight - scTop) - zoomObj[ind].endHeight;

        if(GrowMethod.toLowerCase() == 'bottomright')
        {
          newY = GetRealImgTop(ind) - 1
          newX = GetRealImgLeft(ind) - 1;
        }
        else if(GrowMethod.toLowerCase() == 'bottomleft')
        {
          newY = GetRealImgTop(ind);
          newX = GetRealImgLeft(ind) - wV + zoomObj[ind].initWidth;
          
          if ( ! bw.ns7 )
          {
            newY -= 1;
            newX -= 3;
          }
        }
        
        zoomObj[ind].SetWidth(wV);
        zoomObj[ind].SetHeight(hV);
        zoomObj[ind].MoveLayer(newX, newY);
        zoomObj[ind].SetVis(1);
      
      // Zoom is done, now for the site tree 
      // we pass the left and top value of this image
      if(siteTreeBox)
        {
          siteTreeBox[siteTreeBox.length] = newX + parseInt(zoomObj[ind].endWidth) + 10;
          siteTreeBox[siteTreeBox.length] = newY;
          ShowBox(siteTreeBox);
        }
    }
  else
    {
      zoomObj[ind].SetVis(0);
      //zoomObj[ind].MoveLayer(-10,-10);
         
    }
}

//  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//          OBJECT CREATION
//  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
// imgName, image path, final width and final height
function ZoomObj(zName, zPath, nsLayerName)
{
  //Props
  this.name = zName;
  this.src  = zPath;

  if(document.layers)
    //if(nsLayerName)
    //  this.css = document.layers[nsLayerName].document.layers['div'+zName];
    //else
      this.css = document.layers['div'+zName];
  else
    this.css = (bw.ns6) ? document.getElementById('div'+zName).style : eval('document.all.div'+zName+'.style');

  this.image         = (bw.ns) ? null                     : eval("document.images['new"+zName+"']");
  this.x             = (bw.ns) ? this.css.left            : this.css.pixelLeft;
	this.y             = (bw.ns) ? this.css.top             : this.css.pixelTop;

	this.layerWidth    = (bw.ns) ? this.css.clip.width      : this.css.pixelWidth;
	this.layerHeight   = (bw.ns) ? this.css.clip.height     : this.css.pixelHeight;
	
	var addX = 0;
	var addY = 0;

	if(document.layers)
	  {
	    //alert(zName.replace(/ZIMG/, 'el') + "el");
	    if(document.images[zName])
        this.imgObj = document.images[zName];
	    else
	      {
	        var Lobj = document.layers[nsLayerName];
	        addX = Lobj.x;
	        addY = Lobj.y;
	        this.imgObj = Lobj.document.images[zName];
	      }
	  }
  else
    {
	    this.imgObj = document.images[zName];
	  }

	this.initWidth     = this.imgObj.width;
  this.initHeight    = this.imgObj.height;

  var tempLeft = (bw.ns) ? this.imgObj.x + addX : getRealLeft(this.name);
  var tempTop  = (bw.ns) ? this.imgObj.y + addY : getRealTop(this.name);

	this.imgTop  = specialAddTop + tempTop;
	this.imgLeft = specialAddLeft  + tempLeft;
	
  this.showing       = (this.css.visibility == 'visible' || this.css.visibility == 'show') ? 1 : 0;
  this.endWidth      = 0;
  this.endHeight     = 0;
  this.currLink      = ''; // HLink for the image
  this.timeOut       = null; //When a timeout is set, assign the id to this property so it can be stopped

  //Methods
	this.SetVis      = SetVisible;
	this.SetWidth    = SetLayerWidth;
	this.SetHeight   = SetLayerHeight;
	this.MoveLayer   = moveLayer;
  return this;
}

//  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//          METHODS FOR OBJECTS
//  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
function SetLayerHeight(hVal)
{
  this.layerHeight   = hVal;
  if(bw.ie && !bw.ns6 && !bw.macie)
    this.image.height  = hVal;
}

function SetLayerWidth(wVal)
{
  this.layerWidth     = wVal;
  if(bw.ie && !bw.ns6 && !bw.macie)
    this.image.width    = wVal;
}

function moveLayer(xV, yV)
{
  this.x        = xV; 
	this.y        = yV;
	this.css.top  = this.y;
	this.css.left = this.x;
}

//If bVal then show the div
function SetVisible(bVal)
{
  if (bVal)
    {
      if(bw.ie)
        this.css.visibility = 'visible';
      else
        this.css.visibility = 'show';
      this.showing          = 1;
    }
  else
    {
      if(bw.ie)
        this.css.visibility = 'hidden';
      else
        this.css.visibility = 'hide';
      this.showing          = 0;
    }
}

// Get the image left value IE ONLY
function getRealLeft(zName)
{
	xPos   = eval('document.'+zName+'.offsetLeft');
	(bw.macie && !bw.mac5) ? tempEl = eval('document.'+zName+'.parentElement') : tempEl = eval('document.'+zName+'.offsetParent');
  	while (tempEl != null) 
    	{
    		xPos  += tempEl.offsetLeft;
    	  (bw.macie && !bw.mac5) ? tempEl = tempEl.parentElement : tempEl = tempEl.offsetParent;
    	}
   return xPos;
}

// Get the image top value IE ONLY
function getRealTop(zName)
{
  yPos   = eval('document.'+zName+'.offsetTop');
	(bw.macie && !bw.mac5) ? tempEl = eval('document.'+zName+'.parentElement') : tempEl = eval('document.'+zName+'.offsetParent');
	while (tempEl != null)
	  {
  		yPos  += tempEl.offsetTop;
  		(bw.macie && !bw.mac5) ? tempEl = tempEl.parentElement : tempEl = tempEl.offsetParent;
  	}
  if(bw.macie && !bw.mac5){yPos-=20;}
	return yPos;
}

//  *-*-*-*-*-*-*-**-*-*-*-*-*-*-**-*-*-*-*-*-*-**-*-*-*-*-*-*-*
//          BROWSER DETECTION
//  *-*-*-*-*-*-*-**-*-*-*-*-*-*-**-*-*-*-*-*-*-**-*-*-*-*-*-*-*
function checkBrowser()
{
  this.ver   = navigator.appVersion;
	this.ns    = (document.layers) ? 1 : 0;//new way, not restricted to versions
	this.ie    = (document.layers) ? 0 : 1;
	this.ns6   = 0;
	this.ns7   = 0;
	this.macie = 0;// This is for IE on iMac.
	this.mac5  = 0;// This is for IE 5 on iMac.
	if( this.ie )
	{
	  if( !document.all )
	    { 
	      this.ns6 = 1; 
	      if ( navigator.userAgent.indexOf('Netscape/7') != -1 )
	      {
	        this.ns7 = 1;
	      }
	    }
	  else if(navigator.userAgent.indexOf('Mac') != -1)
      {
        this.macie = 1;
        if( navigator.appVersion.indexOf('MSIE 5') != -1 )
          { this.mac5 = 1; }
      }
	}
  this.bw = (this.ie || this.ns );
	return this;
}
