// ----------------------------------------------------------------------------------------------------------------------
// door2zoom / AjaxZoom - Script
// $LastChangedDate: 2009-12-08 22:16:16 +0100 (Di, 08 Dez 2009) $
// $LastChangedRevision: 5278 $
// $LastChangedBy: robert $
// (C)2008 door2solution software gmbH - All rights reserved
// www.door2solution.com
// ----------------------------------------------------------------------------------------------------------------------
 

/*******************************************************************************
 * HELICON JAVASCRIPT UTIlS
 ******************************************************************************/
 
var hcjsu_isIE = document.all ? true : false;

function hcjsu_checkImagesComplete(aNode,id)
{
  var result = true; 
 
  // Is this a Image Node?
  if (aNode.nodeType == 1)
  {
    //element of type html-object/tag
    if (aNode.tagName.toLowerCase() =="img")
    {
      result = aNode.complete;

      if ((! result) && (aNode.src.indexOf("templates.zoom") > 0))
         result = true;
    }
  }

  if (result)
  {
    for (var i = 0; ((i < aNode.childNodes.length) && (result)); i++)
    {
      if (! hcjsu_checkImagesComplete(aNode.childNodes[i],id))
        result = false;
    }
  }
  return result;
}

function hcjsu_getElementHeight(aElement) 
{
  result = 0; 
  if (typeof aElement == 'string')
    aElement = d2z_getElementByIdOrName(aElement);
  if (aElement)  
	  result = aElement.offsetHeight;
  return result;
}

function hcjsu_getElementWidth(aElement) 
{
  result = 0; 
  if (typeof aElement == 'string')
    aElement = d2z_getElementByIdOrName(aElement);
  if (aElement)  
	  result = aElement.offsetWidth;
  return result;
}
 

function hcjsu_getPageOffsetLeft(aElement)
{
  res = 0; 
  if (typeof aElement == 'string')
    aElement = d2z_getElementByIdOrName(aElement) 
  while (aElement)
  {
    res += aElement.offsetLeft;
    aElement = aElement.offsetParent;
  }

  return res;
}

function hcjsu_getPageOffsetTop(aElement)
{
  res = 0; 
  if (typeof aElement == 'string')
    aElement = d2z_getElementByIdOrName(aElement) 
  while (aElement)
  {
    res += aElement.offsetTop;
    aElement = aElement.offsetParent;
  }

  return res;
}


function hcjsu_mouseEventClass (domEvent)
{
  this._domEvent = domEvent; // contains the original event
  this.target = this._domEvent.target || this._domEvent.srcElement;
  
	if (this._domEvent.pageX || this._domEvent.pageY) 	
  {
		this.pageX = this._domEvent.pageX;
		this.pageY = this._domEvent.pageY;
	}
	else 
    if (this._domEvent.clientX || this._domEvent.clientY) 	
    {
	 	  this.pageX = this._domEvent.clientX + document.body.scrollLeft
	  		+ document.documentElement.scrollLeft;
	  	this.pageY = this._domEvent.clientY + document.body.scrollTop
	  		+ document.documentElement.scrollTop;
	  }
	  
	// Calculate position relative to target
	  
  this.offsetX = this.pageX - hcjsu_getPageOffsetLeft(this.target);
  this.offsetY = this.pageY - hcjsu_getPageOffsetTop(this.target);
	  
	// posx and posy contain the mouse position relative to the document
	// Do something with this information
  
  if (hcjsu_isIE)
  { if (this._domEvent.button == 1) // left in IE
      this.button = 0;  
    if (this._domEvent.button == 2) // right in IE
      this.button = 2;
    if (this._domEvent.button == 4) // right in IE
      this.button = 1;      
  }
  else
  { 
    this.button = this._domEvent.button;
  }
  this.remapMouseEventTo = hcjsu_mouseEventClass_remapMouseEventTo;
}

function hcjsu_mouseEventClass_remapMouseEventTo (anotherElement)
{
  if (typeof anotherElement == 'string')
    anotherElement = d2z_getElementByIdOrName(anotherElement);
  if (anotherElement)
  {
	  var pagePosX =  hcjsu_getPageOffsetLeft(this.target) + this.offsetX;
	  var pagePosY =  hcjsu_getPageOffsetTop(this.target) + this.offsetY;
	  this.offsetX = pagePosX - hcjsu_getPageOffsetLeft(anotherElement);
	  this.offsetY = pagePosY - hcjsu_getPageOffsetTop(anotherElement);
	  this.target = anotherElement; 
  } 
}

function hcjsu_setMouseCursor(aElement, aCursor)
{
  result = 0; 
  if (typeof aElement == 'string')
    aElement = d2z_getElementByIdOrName(aElement);
  if (aElement)  
	  aElement.style.cursor = aCursor;
}


function hcjsu_removeElement(aElement)
{
  if (typeof aElement == 'string')
    aElement = d2z_getElementByIdOrName(aElement);
  if (aElement)
  {
    if (aElement.parentNode)
      aElement.parentNode.removeChild(aElement);
  } 
}

function hcjsu_avoidDuplicateIds(aElement)
{
  if (aElement.id)
  {
    aElement.id = aElement.id+'_AVOID_DUPLICATE_IDS'; 
  }
  for (var i = 0; ((i < aElement.childNodes.length) && (result)); i++)
  {
    if (hcjsu_avoidDuplicateIds(aElement.childNodes[i]));
  }
}

var _hcjsu_XMLHTTPReqQueue = new Array();
var _hcjsu_XMLHTTPReqQueue_reqRunning = false;
var _hcjsu_XMLTHTTPReqObject = null; 

function hcjsu_XMLHTTPReqQueueObject ()
{
  this.open = _hcjsu_XMLHTTPReqQueueObject_open;
  this.setRequestHeader = _hcjsu_XMLHTTPReqQueueObject_setRequestHeader;
  this.send = _hcjsu_XMLHTTPReqQueueObject_send;
  this.queue = _hcjsu_XMLHTTPReqQueueObject_queue;
  this._perform = _hcjsu_XMLHTTPReqQueueObject_perform;
  this.setUserData = _hcjsu_XMLHTTPReqQueueObject_setUserData; 
  this.getUserData = _hcjsu_XMLHTTPReqQueueObject_getUserData; 
  this.onreadystatechange = null;   
}

function _hcjsu_XMLHTTPReqQueueObject_setUserData(userData)
{
  this.fUserData = userData 
}

function _hcjsu_XMLHTTPReqQueueObject_getUserData()
{
  return this.fUserData;
} 


function _hcjsu_XMLHTTPReqQueueObject_perform()
{
  result = false;
  if (!_hcjsu_XMLHTTPReqQueue_reqRunning)
  {
    _hcjsu_XMLHTTPReqQueue_reqRunning = true;
    _hcjsu_XMLTHTTPReqObject = e4s_initXmlHttpReq();
    this.request = _hcjsu_XMLTHTTPReqObject; 
    if (this.fUser)
      this.request.open(this.fMethod, this.fUrl, this.fAsync, this.fUser, this.fPassword);
    else
      this.request.open(this.fMethod, this.fUrl, this.fAsync);
    this.request.onreadystatechange = __hcjsu_XMLHTTPReqQueueObject_handleReadyStateChange;
    this.request.setRequestHeader(this.fHeader, this.fValue);
    this.request.send(this.fData);
    result = true;
  }
  return result;
}

function _hcjsu_XMLHTTPReqQueueObject_queue()
{
  _hcjsu_XMLHTTPReqQueue.push(this);
  setTimeout('__hcjsu_XMLHTTPReqQueueObject_checkQueue()', 10);
}

function _hcjsu_XMLHTTPReqQueueObject_open(method, url, async, user, password)
{
  this.fMethod = method;
  this.fUrl = url;
  if (typeof async == 'undefined')
    async = true;
  this.fAsync = async;
  if (typeof user == 'undefined')
    user = null;
  this.fUser = user;
  if (typeof password == 'undefined')
    password = null;
  this.fPassword = password;
}

function _hcjsu_XMLHTTPReqQueueObject_setRequestHeader(header, value)
{
  this.fHeader = header;
  this.fValue = value;
}

function _hcjsu_XMLHTTPReqQueueObject_send(data)
{
  if (typeof data == 'undefined')
    data = null;
  this.fData = data;
}

function __hcjsu_XMLHTTPReqQueueObject_checkQueue()
{
  if (!_hcjsu_XMLHTTPReqQueue_reqRunning)
  {
    if (_hcjsu_XMLHTTPReqQueue.length > 0)
    {
      if (!_hcjsu_XMLHTTPReqQueue[0]._perform())
      {
        setTimeout('__hcjsu_XMLHTTPReqQueueObject_checkQueue()', 100);
      }
    }
  }
  else
  {
    setTimeout('__hcjsu_XMLHTTPReqQueueObject_checkQueue()', 100);
  }
}

function __hcjsu_XMLHTTPReqQueueObject_handleReadyStateChange()
{
  if (typeof window[_hcjsu_XMLHTTPReqQueue[0].onreadystatechange] == 'function')
    window[_hcjsu_XMLHTTPReqQueue[0].onreadystatechange](_hcjsu_XMLHTTPReqQueue[0]);
  if (_hcjsu_XMLHTTPReqQueue[0].request.readyState == 4)
  {
    _hcjsu_XMLHTTPReqQueue.shift();
    _hcjsu_XMLHTTPReqQueue_reqRunning = false;
    setTimeout('__hcjsu_XMLHTTPReqQueueObject_checkQueue()', 100);
  }
}



/*******************************************************************************
 * DOOR2ZOOM
 ******************************************************************************/ 


var d2z_global_server = "http://www.door2zoom.com/d2z/d2z";
var d2z_global_client = "TEST";
var d2z_global_replace_mode = 1000;
var d2z_global_debug_options = 0;

var NAV_POSITION_TOP = "T";
var NAV_POSITION_BOTTOM = "B";
var NAV_POSITION_LEFT = "L";
var NAV_POSITION_RIGHT = "R";
var NAV_POSITION_NONE = "N";
var D2Z_ZOOM_MODE_RECT = "RECT";
var D2Z_ZOOM_MODE_MOVE = "MOVE";

var D2Z_LEVEL_RESET = 0x0100;
var D2Z_LEVEL_BACK = 0x0200;
var D2Z_LEVEL_PAN = 0x0400;
var D2Z_LEVEL_ABS = 0x0800;
var D2Z_LEVEL_FREEPAN = 0x0500;
var D2Z_UNDEFINED_POS = -99999;

var D2Z_CORNERTYPE_LEFTTOP = 'lt';
var D2Z_CORNERTYPE_RIGHTTOP = 'rt'; 
var D2Z_CORNERTYPE_LEFTBOTTOM = 'lb';
var D2Z_CORNERTYPE_RIGHTBOTTOM = 'rb';

var D2Z_DEBUG_NONE = 0x00000000;
var D2Z_DEBUG_XY   = 0x00000001;
var D2Z_DEBUG_ALL  = 0xFFFFFFFF; 

// var REPLACE_MODE_ANIMATED = 1;
// var REPLACE_MODE_QUICK = 2;
// var QUICK_REPLACE = false;

var m_mouse_x = 0;
var m_mouse_y = 0;
var m_in_zoom = false;

document.onselectstart = function()
{
   return false;
}

function d2z_setGlobals()
{
   d2z_global_replace_mode = 2000;
   // d2z_global_replace_mode = REPLACE_MODE_ANIMATED;
   d2z_global_navigation_position = NAV_POSITION_BOTTOM;
   d2z_global_navigation_color = "#A0A0A0";
   d2z_global_navigation_size = "52";
   d2z_global_navigation_img = "";
   d2z_global_border_color = "";
   d2z_global_width = 0;
   d2z_global_height = 0;
   d2z_global_template = "";
   d2z_global_rotating_timer = -1;
   d2z_global_current_pan_id = "";
   d2z_global_movemode_magfactor = 4;
   d2z_global_pan_repositioning = false;
   //d2z_global_debug_options = 0xFFFFFFFF;
   d2z_global_cursor = '../images/lupe.cur';
}

/**
Sets the URL of the door2zoom server
@function {public static void} ?
@param url of the server
@author AP
*/

function d2z_setServer(server)
{
  d2z_global_server = server;
}

function d2z_setCursor(cursor, div_id)
{
   if (div_id)
   {
      var ajaxobj = d2z_getAjaxObj(div_id);
      ajaxobj.m_cursor = cursor;
   } else
   {
      d2z_global_cursor = cursor;
   }
}

global_ajaxzoom_objects = new Array();
d2z_setGlobals();

var IE = document.all ? true : false;
// var STEPS = 30;
var TIMEOUT = 50;


/**
Converts a CSS dimension specification into an integer.
@function {public static int} ?
@param CSS dimension string value
@returns Returns the dimension as integer.
This function converts a dimension string into an integer by removing "px" from
the string and converting it into int. For example, if "10px" is passed, this
function returns 10 
*/

function d2z_cssDimensionToInt(value)
{
  value = value+''; // make sure the value is really a string
  return parseInt(value.replace(/px/i, ""));
}

/**
Converts an integer into a CSS dimension string
@function {public static string} ?
@param pixel-dimension as integer
@returns Returns the dimension as string.
For example, this function converts 10 into "10px" 
*/

function d2z_intToCssDimension(value)
{
  if (value === NaN)
    return '0px';
  value = value+''; // convert to string
  if (value.indexOf('px') >= 0)
    return value
  else
    return value+'px'; 
}

/**
Searches for an element based on id or name
@function {public static obj} ?
@param ID or name of the lement
@returns Reference to the element or NULL if no element exists.
@author AP
This function first looks using d2z_getElementByIdOrName. If the document does not
contain an element with an id which matches the given name, the function tries
document.getElementsByName.
*/

function d2z_getElementByIdOrName(aName)
{
  var result = document.getElementById(aName);
  if (!result)
  {
    var elems = document.getElementsByName(aName); 
    result = elems[0]; 
  }
  return result;
}

function d2z_doStartZoom(ajaxzoom, replaceMode)
{
  if (replaceMode === undefined)
    ajaxzoom.m_replace_mode = d2z_global_replace_mode;
  else
    ajaxzoom.m_replace_mode = replaceMode;
  return ajaxzoom.startZoom(replaceMode);
  /*  
  try
  {
    ajaxzoom.m_animating = true; // AP: Set Animation Flag 
    var div = d2z_getElementByIdOrName(ajaxzoom.m_div_id + "_IMG");
  
    var mainImage = document.images[ajaxzoom.m_div_id + "_IMAGE"];
  
    ajaxzoom.createNewDiv();
    ajaxzoom.newImg.src = mainImage.src;
    
    // Calculate temporary zoom variables

    ajaxzoom.m_step = 0;
    
    if (replaceMode === undefined)
      ajaxzoom.m_replace_mode = d2z_global_replace_mode;
    else
      ajaxzoom.m_replace_mode = replaceMode;

    ajaxzoom.m_tmp_zoom_final_bw = ajaxzoom.getImageCanvasWidth();
    ajaxzoom.m_tmp_zoom_final_bh = ajaxzoom.getImageCanvasHeight();  
    
    var hzf = ajaxzoom.m_tmp_zoom_final_bw / (ajaxzoom.m_zr_x2 - ajaxzoom.m_zr_x1);  
    var vzf = ajaxzoom.m_tmp_zoom_final_bh / (ajaxzoom.m_zr_y2 - ajaxzoom.m_zr_y1);
    
    ajaxzoom.m_tmp_zoom_final_l = (ajaxzoom.m_zr_x1 * hzf) * -1;
    ajaxzoom.m_tmp_zoom_final_t = (ajaxzoom.m_zr_y1 * vzf) * -1;
    ajaxzoom.m_tmp_zoom_final_aw =
       (ajaxzoom.m_tmp_zoom_final_bw * hzf) - ajaxzoom.m_tmp_zoom_final_bw;  
    ajaxzoom.m_tmp_zoom_final_ah =
       (ajaxzoom.m_tmp_zoom_final_bh * vzf) - ajaxzoom.m_tmp_zoom_final_bh;  

    d2z_handleZoomNextEvent(ajaxzoom.m_div_id); // Do animation
  }
  catch (e)
  {
    ajaxzoom.m_animating = false; // Error occured - stop animation
  }
  */
}

function d2z_doEndAni(id)
{
  ajaxzoom = d2z_getAjaxObj(id);
  ajaxzoom.m_animating = false; // AP: Unset animation flag
  d2z_handleUpdateContentEvent(id); // AP: populate DOM using the ajax result
}

function d2z_handleZoomNextEvent(id)
{
   ajaxzoom = d2z_getAjaxObj(id);

   var steps = (ajaxzoom.m_replace_mode == 0) ? 1 : Math.round(ajaxzoom.m_replace_mode / TIMEOUT);

   if (ajaxzoom.m_step < steps)
   {
      ajaxzoom.m_step++;

      var ratio = ajaxzoom.m_step / steps;

      var curl = ajaxzoom.m_tmp_zoom_final_l * ratio;
      var curt = ajaxzoom.m_tmp_zoom_final_t * ratio;
      var curw = ajaxzoom.m_tmp_zoom_final_bw + (ajaxzoom.m_tmp_zoom_final_aw * ratio);  
      var curh = ajaxzoom.m_tmp_zoom_final_bh + (ajaxzoom.m_tmp_zoom_final_ah * ratio);  

      ajaxzoom.newDiv.style.visibility = "visible";
      
      ajaxzoom.newImg.style.width = d2z_intToCssDimension(Math.round(curw));
      ajaxzoom.newImg.style.height = d2z_intToCssDimension(Math.round(curh));
      ajaxzoom.newImg.style.left = d2z_intToCssDimension(Math.round(curl));
      ajaxzoom.newImg.style.top = d2z_intToCssDimension(Math.round(curt));

      setTimeout("d2z_handleZoomNextEvent('" + ajaxzoom.m_div_id + "')", TIMEOUT);
   } 
   else
   {
      setTimeout("d2z_doEndAni('" + ajaxzoom.m_div_id + "')", 100);
   }
}

var global_zoom_area_color_coords = ''; // '#FFFFFF';
var global_zoom_area_fill_opacy = 30;
var global_zoom_color = '#0000A0';

/**
Sets the debug level
@function {public static}
@param debug level. 0 = No debug, 
*/

function d2z_setDebugOptions(debugLevel)
{
  d2z_global_debug_options = debugLevel;
}

function d2z_checkDebugOption(aDebugOption)
{
  return (d2z_global_debug_options & aDebugOption);
}



/**
Sets the client
@function {public static}
@param Client
*/

function d2z_setClient(client)
{
  d2z_global_client = client;
}

/**
Sets the zoom mode of an d2z element
@function {public static obj}
@param Mode
@param ID of the d2z element
*/

function d2z_setZoomMode(mode, id)
{
   var ajaxzoom = d2z_getAjaxObj(id);
   ajaxzoom.m_zoom_mode = mode;
}


/**
Sets the template
@function {public static obj}
@param template
@param ID of the d2z element. If no ID is specified the template is set globally
*/

function d2z_setTemplate(template, div_id)
{
   if (div_id)
   {
      var ajaxobj = d2z_getAjaxObj(div_id);
      ajaxobj.m_template = template;
   } else
   {
      d2z_global_template = template;
   }
}

function d2z_setIdx(idx, div_id)
{
   if (div_id)
   {
      var ajaxobj = d2z_getAjaxObj(div_id);
      ajaxobj.m_idx = idx;
   }
}


/**
Sets the zoom factor
@function {public static obj}
@param Navigation size
@param ID of the d2z element. If no ID is specified the zoom factor size is set globally
*/

function d2z_setZoomMagnification(magnification, div_id)
{
   d2z_movemode_setMagnificationFactor(magnification);
}

/**
Sets the navigation position.
@function {public static obj}
@param Navigation position
@param ID of the d2z element. If no ID is specified the position size is set globally
*/

function d2z_setNavigationPosition(pos, div_id)
{
   if (div_id)
   {
      var ajaxobj = d2z_getAjaxObj(div_id);
      ajaxobj.m_navigation_position = pos;
   } else
   {
      d2z_global_navigation_position = pos;
   }
}

/**
Sets the color of the navigator
@function {public static obj}
@param Color
@param ID of the d2z element. If no ID is specified the color is set globally
*/

function d2z_setNavigationColor(color, div_id)
{
   if (div_id)
   {
      var ajaxobj = d2z_getAjaxObj(div_id);
      ajaxobj.m_navigation_color = color;
   } else
   {
      d2z_global_navigation_color = color;
   }
}

/**
Sets the navigation image
@function {public static obj}
@param Image
@param ID of the d2z element. If no ID is specified the color is set globally
*/

function d2z_setNavigationImage(img, div_id)
{
   if (div_id)
   {
      var ajaxobj = d2z_getAjaxObj(div_id);
      ajaxobj.m_navigation_img = img;
   } else
   {
      d2z_global_navigation_img = img;
   }
}

/**
Sets the navigation size.
@function {public static obj}
@param Navigation size
@param Id of the d2z element. If no ID is specified the navigation size is set globally
*/

function d2z_setNavigationSize(sz, div_id)
{
   if (div_id)
   {
      var ajaxobj = d2z_getAjaxObj(div_id);
      ajaxobj.m_navigation_size = sz;
   } 
   else
   {
      d2z_global_navigation_size = sz;
   }
}

/**
Sets the width
@function {public static}
@param Width
@param Id of the d2z element. If no ID is specified the navigation size is set globally
*/


function d2z_setWidth(width, div_id)
{
   if (div_id)
   {
      var ajaxobj = d2z_getAjaxObj(div_id);
      ajaxobj.m_width = width;
   } else
   {
      d2z_global_width = width;
   }
}

/**
Sets the height
@function {public static}
@param Height
@param Id of the d2z element. If no ID is specified the navigation size is set globally
*/


function d2z_setHeight(height, div_id)
{
   if (div_id)
   {
      var ajaxobj = d2z_getAjaxObj(div_id);
      ajaxobj.m_height = height;
   } else
   {
      d2z_global_height = height;
   }
}

function d2z_setReplaceSpeed(speed)
{
   d2z_global_replace_mode = speed;
}


/**
Sets the zoom factor
@function {public static void}
@param Color
@param ID of the d2z element. If no ID is specified the color is set globally
*/

function d2z_setBorderColor(color, div_id)
{
   if (div_id)
   {
      var ajaxobj = d2z_getAjaxObj(div_id);
      ajaxobj.m_border_color = color;
   } else
   {
      d2z_global_border_color = color;
   }
}


/**
Constructor of the AjaxZoomObject
@function {public static void}
@param Id of the assigned d2z element
*/

function AjaxZoomObject(div_id)
{
  this.m_div_id = div_id;
  
  this.m_request = null;
  this.m_width = -1;
  this.m_height = -1;
  this.m_navigation_position = '';
  this.m_navigation_color = '';
  this.m_template = '';
  this.m_navigation_img = '';
  this.m_navigation_size = -1;
  this.m_border_color = '';
  this.global_zoom_color = '';
  this.m_filename = '';
  this.m_idx = -1;
  this.m_zoom_mode = D2Z_ZOOM_MODE_RECT;
  this.m_loading = false; // an ajax request is currently running
  
  this.m_popup = null;
  this.m_popup_dest_width = 0;
  this.m_popup_dest_height = 0;
  this.m_popup_curr_width = 0;
  this.m_popup_curr_height = 0;
  this.m_popup_pos = 'C';
  this.m_popup_request = null;
  
  this.move_initdone = false;
  this.move_toplevel = true;
  
  this.m_animating = false; // Animation Flag. true if script is currently animating
  this.m_last_request_result = false; // result of last ajax request. Use d2z_populateResult
  this.m_last_request_result_tmpdiv = null; // pointer to temporary div element
  this.m_af_overlay_div = null; // Anti Flicker Overlay Div
  
  // this IgnoredMouseEventElements
  
  this.m_ignoreMouseInElments = new Array (
    'IMG_'+div_id+'_IMG_PAN_N',
    'IMG_'+div_id+'_IMG_PAN_NE',
    'IMG_'+div_id+'_IMG_PAN_E',
    'IMG_'+div_id+'_IMG_PAN_SE',
    'IMG_'+div_id+'_IMG_PAN_S',
    'IMG_'+div_id+'_IMG_PAN_SW',
    'IMG_'+div_id+'_IMG_PAN_W',
    'IMG_'+div_id+'_IMG_PAN_NW',
    'IMG_'+div_id+'_IMG_PAN_RELOC');
  
  
//new Array("Anita", "Bettina", "Christa", "Doris");
    
  
  // Selection Rect Drawing Flags
  
  this.m_draw_selrect = true;  // Set to false when select is hidden
  
  // User Selection
  
  this.m_usersel_x1 = D2Z_UNDEFINED_POS;
  this.m_usersel_y1 = D2Z_UNDEFINED_POS;
  this.m_usersel_x2 = D2Z_UNDEFINED_POS;
  this.m_usersel_y2 = D2Z_UNDEFINED_POS;
  
  // Fixed Up User Selection. These fields get filled by fixupUserSel.
  // Never set these fields manually.
  
  // User Selection
  
  this.m_fu_usersel_x1 = D2Z_UNDEFINED_POS;  
  this.m_fu_usersel_y1 = D2Z_UNDEFINED_POS;  
  this.m_fu_usersel_x2 = D2Z_UNDEFINED_POS;  
  this.m_fu_usersel_y2 = D2Z_UNDEFINED_POS;
  
  // Outer user selection
  
  this.m_fu_outerusersel_x1 = D2Z_UNDEFINED_POS;  
  this.m_fu_outerusersel_y1 = D2Z_UNDEFINED_POS;  
  this.m_fu_outerusersel_x2 = D2Z_UNDEFINED_POS;  
  this.m_fu_outerusersel_y2 = D2Z_UNDEFINED_POS;
  
  // Zoom Helper variables
  
  this.m_tmp_zoom_final_l = 0; // Final left position of newdiv-img (usually left outside)
  this.m_tmp_zoom_final_t = 0; // Final left position of newdiv-img (usually left outside)
  this.m_tmp_zoom_final_aw = 0; // Final additional width
  this.m_tmp_zoom_final_ah = 0; // Final additional height
  this.m_step = 0; // Current step of animation
  
  // New Div fields
  
  this.newDiv = null;
  
  // result caching fields 
  
  this.m_cache_getImageCanvasHeight = null; 
  this.m_cache_getImageCanvasWidth = null; 
  this.m_cache_getImageCanvasAspectRatio = null;
  this.m_cache_getImageCanvasAbsLeft = null;
  this.m_cache_getImageCanvasAbsTop = null;
  this.m_cache_getImageWidth = null;
  this.m_cache_getImageHeight = null;
    
  // Methods
  
  this.setZoomRect =  AjaxZoomObject_setZoomRect;
  this.fixupUserSel =  AjaxZoomObject_fixupUserSel;
  this.createNewDiv = AjaxZoomObject_createNewDiv;
  this.distroyNewDiv = AjaxZoomObject_distroyNewDiv;
  this.getImageCanvasDiv = AjaxZoomObject_getImageCanvasDiv;
  this.getImageCanvasImage = AjaxZoomObject_getImageCanvasImage;
  this.getImageCanvasWidth = AjaxZoomObject_getImageCanvasWidth; 
  this.getImageCanvasHeight = AjaxZoomObject_getImageCanvasHeight;
  this.getImageCanvasAspectRatio = AjaxZoomObject_getImageCanvasAspectRatio;
  this.getImageWidth = AjaxZoomObject_getImageWidth;
  this.getImageHeight = AjaxZoomObject_getImageHeight;
  this.updateCachedValues = AjaxZoomObject_updateCachedValues;
  this.clearUserSel = AjaxZoomObject_clearUserSel;
  this.setUserSel = AjaxZoomObject_setUserSel;
  this.setZoomRect = AjaxZoomObject_setZoomRect;
  this.getImageCanvasAbsLeft = AjaxZoomObject_getImageCanvasAbsLeft;    
  this.getImageCanvasAbsTop = AjaxZoomObject_getImageCanvasAbsTop;
  this.drawSelectionRect = AjaxZoomObject_drawSelectionRect;
  this.hideSelectionRect = AjaxZoomObject_hideSelectionRect; 
  this.drawSelCorner = AjaxZoomObject_drawSelCorner;
  this.drawFillRect = AjaxZoomObject_drawFillRect;
  this.getZoomColor = AjaxZoomObject_getZoomColor;
  this.isValidMouseHandlerElement = AjaxZoomObject_isValidMouseHandlerElement;
  this.showStatus = AjaxZoomObject_showStatus;
  this.getFullWidth = AjaxZoomObject_getFullWidth;
  this.getFullHeight = AjaxZoomObject_getFullHeight;
  this.runZoom = AjaxZoomObject_runZoom;
  this.startZoom = AjaxZoomObject_startZoom;
  this.partialZoom = AjaxZoomObject_partialZoom;
  this.updateDisplay = AjaxZoomObject_updateDisplay;
  this.moveFreePan = AjaxZoomObject_moveFreePan;
  this.antiFlickerStartOverlay = AjaxZoomObject_antiFlickerStartOverlay;
  this.antiFlickerEndOverlay = AjaxZoomObject_antiFlickerEndOverlay;
  this.setMouseCursor = AjaxZoomObject_setMouseCursor;
  this.showImage = AjaxZoomObject_showImage;
  this.setLoadingState = AjaxZoomObject_setLoadingState;   
  this.zoomAbsolute = AjaxZoomObject_zoomAbsolute;
}

function AjaxZoomObject_setLoadingState(isLoading)
{
  this.m_loading = isLoading;
  if (this.m_loading)
  {
    this.setMouseCursor('wait');
  } 
}

function AjaxZoomObject_antiFlickerStartOverlay()
{
  /*
  zd = d2z_getElementByIdOrName(this.m_div_id);
  if (zd)
  {
    ol = hcjsu_getPageOffsetLeft(zd);
    ot = hcjsu_getPageOffsetTop(zd);
    ow = hcjsu_getElementWidth(zd);
    oh = hcjsu_getElementWidth(zd);
    var afdiv_id = this.m_div_id+'_TMP_AFDIV';
    
    this.m_af_overlay_div = d2z_getElementByIdOrName(afdiv_id);
    if (this.m_af_overlay_div)
    {
      hcjsu_removeElement(this.m_af_overlay_div);
      this.m_af_overlay_div = null;
    }
    this.m_af_overlay_div = document.createElement('div');
    this.m_af_overlay_div.style.position = 'absolute';
    this.m_af_overlay_div.id = afdiv_id;
    this.m_af_overlay_div.style.left = d2z_intToCssDimension(ol);
    this.m_af_overlay_div.style.top = d2z_intToCssDimension(ot);
    this.m_af_overlay_div.style.width = d2z_intToCssDimension(ow);
    this.m_af_overlay_div.style.height = d2z_intToCssDimension(oh);
    this.m_af_overlay_div.innerHTML = zd.innerHTML;
    hcjsu_avoidDuplicateIds(this.m_af_overlay_div);
    document.getElementsByTagName("body")[0].appendChild(this.m_af_overlay_div);

    setTimeout("d2z_handleHideAntiFlickerEndOverlay('" + this.m_div_id + "')", 250);
  }
  */
}

function AjaxZoomObject_showImage(showImage)
{
  img = this.getImageCanvasImage();
  if (img)
  {
    if (showImage)
     img.style.visibility = "visible"
    else
     img.style.visibility = "hidden";
  }
}

function AjaxZoomObject_antiFlickerEndOverlay()         
{
  /*
  if (this.m_af_overlay_div)
  {
    hcjsu_removeElement(this.m_af_overlay_div);
    this.m_af_overlay_div = null;
  }
  */
}

function AjaxZoomObject_updateDisplay()
{
  var needNewDiv = false;
  
  if (this.m_animating || this.m_loading)
  {
    this.setMouseCursor('wait');
  }
  else
  {
    this.setMouseCursor('crosshair');
  }

  if (!this.m_animating && this.move_dragging)
  {
    this.setMouseCursor('move');
    // fillRect(diffx, diffy, Math.round(this.getWidth()+diffx),
    // Math.round(this.getHeight()+diffy));
    diffx = (this.m_usersel_x2 - this.m_usersel_x1); 
    diffy = (this.m_usersel_y2 - this.m_usersel_y1);
    this.m_move_left = diffx;
    this.m_move_top = diffy;
    this.lastdiffx = diffx;
    this.lastdiffy = diffy;
    this.createNewDiv();
    
    this.newDiv.style.visibility = "visible";
    this.newImg.style.left = d2z_intToCssDimension(this.m_move_left);
    this.newImg.style.top = d2z_intToCssDimension(this.m_move_top);
    this.newImg.style.visibility = "visible";
    
    needNewDiv = true;
  }
  
  if (this.m_draw_selrect)
  {
    this.drawSelectionRect();
  }
  
}

function AjaxZoomObject_setMouseCursor(aCursor)
{
  hcjsu_setMouseCursor(this.m_div_id+'_IMAGE', aCursor);
  hcjsu_setMouseCursor(this.m_div_id+'_TMPDIV', aCursor);
  hcjsu_setMouseCursor(this.m_div_id+'_NEW', aCursor);
  hcjsu_setMouseCursor(this.m_div_id+'_NEW_IMG', aCursor);
}

function AjaxZoomObject_moveFreePan(offx, offy)
{
  this.setMouseCursor('wait');
  var params = '&LEVEL=' + (D2Z_LEVEL_FREEPAN);
  params += "&OFFSETX=" + Math.round(offx);
  params += "&OFFSETY=" + Math.round(offy);
  d2z_global_current_pan_id = this.m_div_id;
  d2z_updateSendReqScript(this.m_div_id, params);
} 
 

function AjaxZoomObject_runZoom()
{
  this.setMouseCursor('wait');
  this.fixupUserSel(); // Make sure coordinates are ok
  this.setZoomRect(this.m_fu_outerusersel_x1,
    this.m_fu_outerusersel_y1,
    this.m_fu_outerusersel_x2,
    this.m_fu_outerusersel_y2);
  this.startZoom();
  d2z_sendZoomUpdateRequest(this.m_div_id,
    this.m_zr_x1,
    this.m_zr_y1,
    this.m_zr_x2,
    this.m_zr_y2);  
}


function AjaxZoomObject_getFullWidth()
{
  return (this.m_width == -1) ? d2z_global_width : this.m_width;
}

function AjaxZoomObject_getFullHeight()
{
   return (this.m_height == -1) ? d2z_global_height : this.m_height;
}

function AjaxZoomObject_partialZoom(x, y)
{
   this.move_toplevel = false;

   var div = d2z_getElementByIdOrName(this.m_div_id + "_IMG");
   this.move_wholeImage = div.firstChild.getAttribute("background");

   // ---

   var scaleFactor = 1 / d2z_global_movemode_magfactor;

   var x1 = x - this.getImageCanvasWidth() * scaleFactor;
   var y1 = y - this.getImageCanvasHeight() * scaleFactor;
   var x2 = x + this.getImageCanvasWidth() * scaleFactor;
   var y2 = y + this.getImageCanvasHeight() * scaleFactor;
   
   this.setUserSel(x1, y1, x2, y2);
   this.setZoomRect(x1, y1, x2, y2)

   this.drawSelectionRect();

   this.runZoom();
}


function d2z_zoomAbsolute(div_id,x1,y1,x2,y2)
{
   if (div_id)
   {
      var ajaxobj = d2z_getAjaxObj(div_id);
      ajaxobj.zoomAbsolute(x1,y1,x2,y2);
   } 
}

function AjaxZoomObject_zoomAbsolute(x1,y1,x2,y2)
{
   this.setMouseCursor('wait');
   var params = '&LEVEL=' + (D2Z_LEVEL_ABS);
   params += "&X1=" + Math.round(x1);
   params += "&Y1=" + Math.round(y1);
   params += "&X2=" + Math.round(x2);
   params += "&Y2=" + Math.round(y2);
 
   d2z_updateSendReqScript(this.m_div_id, params);
} 

function AjaxZoomObject_startZoom(replaceMode)
{
  try
  {
    this.m_animating = true; // AP: Set Animation Flag 
    var div = d2z_getElementByIdOrName(this.m_div_id + "_IMG");
    mainImage = this.getImageCanvasImage(this.m_div_id + "_IMAGE");
  
    this.createNewDiv();
    this.newImg.src = mainImage.src;
    
    // Calculate temporary zoom variables

    this.m_step = 0;
    
    if (replaceMode === undefined)
      this.m_replace_mode = d2z_global_replace_mode;
    else
      this.m_replace_mode = replaceMode;

    this.m_tmp_zoom_final_bw = this.getImageCanvasWidth();
    this.m_tmp_zoom_final_bh = this.getImageCanvasHeight();  
    
    var hzf = this.m_tmp_zoom_final_bw / (this.m_zr_x2 - this.m_zr_x1);  
    var vzf = this.m_tmp_zoom_final_bh / (this.m_zr_y2 - this.m_zr_y1);
    
    this.m_tmp_zoom_final_l = (this.m_zr_x1 * hzf) * -1;
    this.m_tmp_zoom_final_t = (this.m_zr_y1 * vzf) * -1;
    this.m_tmp_zoom_final_aw =
       (this.m_tmp_zoom_final_bw * hzf) - this.m_tmp_zoom_final_bw;  
    this.m_tmp_zoom_final_ah =
       (this.m_tmp_zoom_final_bh * vzf) - this.m_tmp_zoom_final_bh;
       
    this.setMouseCursor('wait');

    d2z_handleZoomNextEvent(this.m_div_id); // Do animation
  }
  catch (e)
  {
    this.m_animating = false; // Error occured - stop animation
  }
}


function AjaxZoomObject_showStatus(x, y)
{
  if (d2z_checkDebugOption(D2Z_DEBUG_XY))
  {
    var elmName = this.m_div_id + "_D2Z_SELRECT_STATUS";
    var d = d2z_getElementByIdOrName(elmName);
    if (! d)
    {
       d = document.createElement("div");
       d.id = elmName;
       d.style.position = "absolute";
       //d.style.backgroundColor = 'transparent';
       d.style.backgroundColor = '#FFFFFF';
       d.style.overflow = "hidden";
       d.style.zIndex = 10;  
       d.style.opacity = 0.5;
       d.style.filter = 'alpha(opacity=' + parseInt(50) + ')';
       this.getImageCanvasDiv().appendChild(d);
    }

    d.style.left = d2z_intToCssDimension(x + 4);
    d.style.top = d2z_intToCssDimension(y + 4);
    d.style.visibility = 'visible';

    // Set Text
    s = '<font face="Verdana" size="-2">' + x + '|' + y;
    if (this.m_usersel_x1 != D2Z_UNDEFINED_POS)
      s += '<br>'+this.m_usersel_x1+'|'+this.m_usersel_y1+'-'+this.m_usersel_x2+'|'+this.m_usersel_y2;
    s += '</font>';

    d.innerHTML = s;
  }
}

function AjaxZoomObject_isValidMouseHandlerElement(aElement)
{
  result = true;
  if (typeof aElement == 'string')
  {
    aElement = d2z_getElementByIdOrName(aElement);
  }
  if (aElement)
  {
    var elmName = aElement.id;
    if (!elmName)
      elmName = aElement.name;
    for (var n = 0; n < this.m_ignoreMouseInElments.length; n++)
    {
      if (this.m_ignoreMouseInElments[n] == elmName)
        result = false;  
    }
    /*
    if (elmName)
    {
      if ((elmName == 'IMG_'+this.m_div_id+'_PANEL') ||
          (elmName == this.m_div_id+'_IMG') ||
          (elmName == this.m_div_id+'_IMAGE') ||
          (elmName == this.m_div_id+'_NEW') ||
          (elmName == this.m_div_id+'_NEW_IMG') ||
          (elmName.indexOf(this.m_div_id+'_D2Z_SELRECT') === 0))
        result = true;
    }
    */
  }
  return result;
}

function AjaxZoomObject_getZoomColor()
{
  var clr = (this.global_zoom_color == '') ? global_zoom_color : this.global_zoom_color;
  return clr;
} 
 


/* Draw Selection rect using jsgr

function AjaxZoomObject_drawSelectionRect()
{
  if (this.fixupUserSel()) 
  {
    var jsgr = this.getJsgr(true);  
    if (jsgr)
    {
      var jsgr = this.getJsgr();  
      var corrX = this.getImageCanvasAbsLeft();
      var corrY = this.getImageCanvasAbsTop();
      
      var abs_us_x1 = this.m_fu_usersel_x1 + corrX; 
      var abs_us_y1 = this.m_fu_usersel_y1 + corrY; 
      var abs_us_x2 = this.m_fu_usersel_x2 + corrX; 
      var abs_us_y2 = this.m_fu_usersel_y2 + corrY; 
      var abs_os_x1 = this.m_fu_outerusersel_x1 + corrX; 
      var abs_os_y1 = this.m_fu_outerusersel_y1 + corrY; 
      var abs_os_x2 = this.m_fu_outerusersel_x2 + corrX; 
      var abs_os_y2 = this.m_fu_outerusersel_y2 + corrY; 
    
      jsgr.fillRect(this,
        abs_us_x1,
        abs_us_y1, 
        abs_us_x2-abs_us_x1, 
        abs_us_y2-abs_us_y1);
        
      jsgr.drawSelCorner(this, 'OUTERLEFTTOP', D2Z_CORNERTYPE_LEFTTOP, abs_os_x1, abs_os_y1);
      jsgr.drawSelCorner(this, 'OUTERRIGHTTOP', D2Z_CORNERTYPE_RIGHTTOP, abs_os_x2, abs_os_y1);
      jsgr.drawSelCorner(this, 'OUTERLEFTBOTTOM', D2Z_CORNERTYPE_LEFTBOTTOM, abs_os_x1, abs_os_y2);
      jsgr.drawSelCorner(this, 'OUTERRIGHBOTTOM', D2Z_CORNERTYPE_RIGHTBOTTOM, abs_os_x2, abs_os_y2);
    }
  }
}

*/


function AjaxZoomObject_drawSelectionRect()
{
  if (this.fixupUserSel()) 
  {
  
    this.m_draw_selrect = true;  
  
    var corrX = 0;
    var corrY = 0;
    
    var abs_us_x1 = this.m_fu_usersel_x1 + corrX; 
    var abs_us_y1 = this.m_fu_usersel_y1 + corrY; 
    var abs_us_x2 = this.m_fu_usersel_x2 + corrX; 
    var abs_us_y2 = this.m_fu_usersel_y2 + corrY; 
    var abs_os_x1 = this.m_fu_outerusersel_x1 + corrX; 
    var abs_os_y1 = this.m_fu_outerusersel_y1 + corrY; 
    var abs_os_x2 = this.m_fu_outerusersel_x2 + corrX; 
    var abs_os_y2 = this.m_fu_outerusersel_y2 + corrY; 
  
    this.drawFillRect(abs_us_x1,
      abs_us_y1, 
      abs_us_x2-abs_us_x1, 
      abs_us_y2-abs_us_y1);
      
    this.drawSelCorner('OUTERLEFTTOP', D2Z_CORNERTYPE_LEFTTOP, abs_os_x1, abs_os_y1);
    this.drawSelCorner('OUTERRIGHTTOP', D2Z_CORNERTYPE_RIGHTTOP, abs_os_x2, abs_os_y1);
    this.drawSelCorner('OUTERLEFTBOTTOM', D2Z_CORNERTYPE_LEFTBOTTOM, abs_os_x1, abs_os_y2);
    this.drawSelCorner('OUTERRIGHBOTTOM', D2Z_CORNERTYPE_RIGHTBOTTOM, abs_os_x2, abs_os_y2);
  }
}

function AjaxZoomObject_hideSelectionRect()
{
  this.m_draw_selrect = false;  
  var canvasDiv = this.getImageCanvasDiv();
  if (canvasDiv)
  {
    this.m_draw_selrect = false;  
    for (var i = canvasDiv.childNodes.length-1; (i >= 0); i--)
    {
      elmID = canvasDiv.childNodes[i].id;
      if (elmID)
      {
        if (elmID.indexOf(this.m_div_id + '_D2Z_SELRECT') === 0)
          canvasDiv.childNodes[i].style.visibility = 'hidden';
      }
    }
  }
}

function AjaxZoomObject_drawFillRect(x, y, dx, dy)
{
   var elmID = this.m_div_id + "_D2Z_SELRECT_AREA"; 
   if ((dx > 0) && (dy > 0))
   {
      var d = d2z_getElementByIdOrName(elmID);
      var isInternetExplorer = (navigator.appName.indexOf("Microsoft") != -1);

      if (!d)
      {
         d = document.createElement("div");
         d.id = elmID;
         d.style.position = "absolute";
         d.style.backgroundColor = this.getZoomColor();
         d.style.overflow = "hidden";
         d.style.zIndex = 10;

         if (isInternetExplorer)
         {
            d.style.opacity = global_zoom_area_fill_opacy;
            d.style.filter = 'alpha(opacity=' + global_zoom_area_fill_opacy + ')';
         } else
         {
            d.style.opacity = global_zoom_area_fill_opacy / 100;
            d.style.filter = 'alpha(opacity=' + (global_zoom_area_fill_opacy / 100) + ')';
         }

         this.getImageCanvasDiv().appendChild(d);
      }

      d.style.left = d2z_intToCssDimension(x);
      d.style.top = d2z_intToCssDimension(y);
      d.style.width = d2z_intToCssDimension(dx);
      d.style.height = d2z_intToCssDimension(dy);
      d.style.visibility = 'visible';

      if (global_zoom_area_color_coords && (global_zoom_area_color_coords != ''))
      {
         var dcoords = d2z_getElementByIdOrName(elmID+'_C');

         if (!dcoords)
         {
            dcoords = document.createElement("div");
            dcoords.id = elmID+'_C';
            dcoords.style.position = "absolute";
            dcoords.style.backgroundColor = this.getZoomColor();
            dcoords.style.overflow = "hidden";
            dcoords.zIndex = 10;

            this.getImageCanvasDiv().appendChild(dcoords);
         }

         var div_dx = this.getLeftPos();
         var div_dy = this.getTopPos();
         var s = '<FONT FACE="VERDANA" COLOR="' + global_zoom_area_color_coords + '" SIZE="-3">';
         s += (x - div_dx) + "/" + (y - div_dy) + " x " + (x + dx - div_dx) + "/" + (y + dy - div_dy);
         s += "</FONT>";
         dcoords.innerHTML = s;

         dcoords.style.left = d2z_intToCssDimension(x);
         dcoords.style.top = d2z_intToCssDimension(y);
         // dcoords.style.clip = "rect(0," + dx + "px," + dy + "px,0)";
      }
   }
}

function AjaxZoomObject_drawSelCorner(cornerId, cornerType, x, y)
{
  if (!cornerId)
    cornerId = cornerType;
  var elmName = this.m_div_id + "_D2Z_SELRECT_" + cornerId 
  var d = d2z_getElementByIdOrName(elmName);

  if (!d)
  {
     d = document.createElement("div");
     d.id = elmName;
     d.style.position = "absolute";
     d.style.backgroundColor = 'transparent';
     d.style.overflow = "hidden";
     d.style.zIndex = 10;

     this.getImageCanvasDiv().appendChild(d);
  }
  
  d.style.width = d2z_intToCssDimension(5); 
  d.style.height = d2z_intToCssDimension(5); 
  d.style.visibility = 'visible';

  if (cornerType == D2Z_CORNERTYPE_LEFTTOP)
  {
    d.style.left = d2z_intToCssDimension(x); 
    d.style.top = d2z_intToCssDimension(y); 
    d.style.borderLeft = '1px solid '+this.getZoomColor();
    d.style.borderTop = '1px solid '+this.getZoomColor();
    d.style.borderRight = 'none';
    d.style.borderBottom = 'none';
  }
  else
    if (cornerType == D2Z_CORNERTYPE_RIGHTTOP)
    {
      d.style.left = d2z_intToCssDimension(x-6); 
      d.style.top = d2z_intToCssDimension(y); 
      d.style.borderLeft = 'none';
      d.style.borderTop = '1px solid '+this.getZoomColor();
      d.style.borderRight = '1px solid '+this.getZoomColor();
      d.style.borderBottom = 'none';
    }
    else
      if (cornerType == D2Z_CORNERTYPE_LEFTBOTTOM)
      {
        d.style.left = d2z_intToCssDimension(x); 
        d.style.top = d2z_intToCssDimension(y-6); 
        d.style.borderLeft = '1px solid '+this.getZoomColor();
        d.style.borderTop = 'none';
        d.style.borderRight = 'none';
        d.style.borderBottom = '1px solid '+this.getZoomColor();
      }
      else
        if (cornerType == D2Z_CORNERTYPE_RIGHTBOTTOM)
        {
          d.style.left = d2z_intToCssDimension(x-6); 
          d.style.top = d2z_intToCssDimension(y-6); 
          d.style.borderLeft = 'none';
          d.style.borderTop = 'none';
          d.style.borderRight = '1px solid '+this.getZoomColor();
          d.style.borderBottom = '1px solid '+this.getZoomColor();
        }
}




function AjaxZoomObject_getImageCanvasAbsLeft()
{
  if (!this.m_cache_getImageCanvasAbsLeft)
    this.updateCachedValues();
  return this.m_cache_getImageCanvasAbsLeft;
}
    
function AjaxZoomObject_getImageCanvasAbsTop()
{
  if (!this.m_cache_getImageCanvasAbsLeft)
    this.updateCachedValues();
  return this.m_cache_getImageCanvasAbsTop;
}    

function AjaxZoomObject_setUserSel(x1, y1, x2, y2)
{
  this.m_usersel_x1 = x1;
  this.m_usersel_y1 = y1;
  this.m_usersel_x2 = x2;
  this.m_usersel_y2 = y2;
  this.fixupUserSel();
}

function AjaxZoomObject_setZoomRect(x1, y1, x2, y2)
{
  if ((x1 != x2) &&
    (y1 != y2))
  {
    this.m_zr_x1 = x1;
    this.m_zr_y1 = y1;
    this.m_zr_x2 = x2;
    this.m_zr_y2 = y2;
  }
  else
  {
    this.m_zr_x1 = this.m_fu_outerusersel_x1;
    this.m_zr_y1 = this.m_fu_outerusersel_y1;
    this.m_zr_x2 = this.m_fu_outerusersel_x2;
    this.m_zr_y2 = this.m_fu_outerusersel_y2;
  }
}  

function AjaxZoomObject_clearUserSel()
{
  this.m_usersel_x1 = D2Z_UNDEFINED_POS;
  this.m_usersel_y1 = D2Z_UNDEFINED_POS;
  this.m_usersel_x2 = D2Z_UNDEFINED_POS;
  this.m_usersel_y2 = D2Z_UNDEFINED_POS;
  this.fixupUserSel();
}

function AjaxZoomObject_getImageCanvasWidth()
{
  if ((!this.m_cache_getImageCanvasHeight) || 
      (!this.m_cache_getImageCanvasWidth))
    this.updateCachedValues();
  return this.m_cache_getImageCanvasWidth; 
}
 
function AjaxZoomObject_getImageCanvasHeight()
{
  if ((!this.m_cache_getImageCanvasHeight) || 
      (!this.m_cache_getImageCanvasWidth))
    this.updateCachedValues();
  return this.m_cache_getImageCanvasHeight; 
}

function AjaxZoomObject_getImageWidth()
{
  if ((!this.m_cache_getImageHeight) || 
      (!this.m_cache_getImageWidth))
    this.updateCachedValues();
  return this.m_cache_getImageWidth; 
}

function AjaxZoomObject_getImageHeight()
{
  if ((!this.m_cache_getImageHeight) || 
      (!this.m_cache_getImageWidth))
    this.updateCachedValues();
  return this.m_cache_getImageHeight; 
}


function AjaxZoomObject_getImageCanvasAspectRatio()
{
  if (!this.m_cache_getImageCanvasAspectRatio)
    this.updateCachedValues();
  return this.m_cache_getImageCanvasAspectRatio;
}


function AjaxZoomObject_updateCachedValues()
{
  var imgcanvasdiv = this.getImageCanvasDiv();
  if (imgcanvasdiv)
  {
    this.m_cache_getImageCanvasHeight =  d2z_cssDimensionToInt(imgcanvasdiv.style.height);
    this.m_cache_getImageCanvasWidth = d2z_cssDimensionToInt(imgcanvasdiv.style.width);
    this.m_cache_getImageCanvasAspectRatio = (this.m_cache_getImageCanvasWidth / this.m_cache_getImageCanvasHeight); 
    this.m_cache_getImageCanvasAbsLeft = hcjsu_getPageOffsetLeft(imgcanvasdiv);
    this.m_cache_getImageCanvasAbsTop = hcjsu_getPageOffsetTop(imgcanvasdiv);
  }
  
  var imgcanvasimg = this.getImageCanvasImage();
  if (imgcanvasimg)
  {
    this.m_cache_getImageHeight = imgcanvasimg.offsetHeight;
    this.m_cache_getImageWidth = imgcanvasimg.offsetWidth;
  }
}  

function AjaxZoomObject_getImageCanvasDiv()
{
  return d2z_getElementByIdOrName(this.m_div_id + "_IMG");
}

function AjaxZoomObject_getImageCanvasImage()
{
  return d2z_getElementByIdOrName(this.m_div_id + "_IMAGE");
} 

function AjaxZoomObject_fixupUserSel()
{
  // Check if there is a current selection

  if ((this.m_usersel_x1 == D2Z_UNDEFINED_POS) ||
     (this.m_usersel_y1 == D2Z_UNDEFINED_POS) ||
     (this.m_usersel_x2 == D2Z_UNDEFINED_POS) ||
     (this.m_usersel_y2 == D2Z_UNDEFINED_POS))
  { // No current selection 
    this.m_fu_usersel_x1 = D2Z_UNDEFINED_POS;
    this.m_fu_usersel_y1 = D2Z_UNDEFINED_POS;
    this.m_fu_usersel_x2 = D2Z_UNDEFINED_POS;
    this.m_fu_usersel_y2 = D2Z_UNDEFINED_POS;
    this.m_fu_outerusersel_x1 = D2Z_UNDEFINED_POS;
    this.m_fu_outerusersel_y1 = D2Z_UNDEFINED_POS;
    this.m_fu_outerusersel_x2 = D2Z_UNDEFINED_POS;
    this.m_fu_outerusersel_y2 = D2Z_UNDEFINED_POS;
    return false; // no valid selection
  }
  
  // User selection - Calculate outer rect
  // Make sure x1 and y contain the left top corner
  
  this.m_fu_usersel_x1 = Math.min(this.m_usersel_x1, this.m_usersel_x2);
  this.m_fu_usersel_y1 = Math.min(this.m_usersel_y1, this.m_usersel_y2);
  this.m_fu_usersel_x2 = Math.max(this.m_usersel_x1, this.m_usersel_x2);
  this.m_fu_usersel_y2 = Math.max(this.m_usersel_y1, this.m_usersel_y2);
  
  if (this.m_fu_usersel_x1 < 0)
    this.m_fu_usersel_x1 = 0;
  if (this.m_fu_usersel_y1 < 0)
    this.m_fu_usersel_y1 = 0;
  if (this.m_fu_usersel_x2 > this.getImageWidth())
    this.m_fu_usersel_x2 = this.getImageWidth();
  if (this.m_fu_usersel_y2 > this.getImageHeight())
    this.m_fu_usersel_y2 = this.getImageHeight();
  
  //
  // Calculate the outer rect based on aspect ratio
  //

  // Calculate the aspect ratio of the canvas and the selection  
  
  var car = this.getImageCanvasAspectRatio(); // Canvas apsect ratio
  var iar = (this.m_fu_usersel_x2-this.m_fu_usersel_x1) / (this.m_fu_usersel_y2-this.m_fu_usersel_y1) // Image Aspect ratio
  
  // determine the needed heigth and width of the outer selection box based on the aspect ratio
  
  if (car > iar)
  {
    var osh = this.m_fu_usersel_y2-this.m_fu_usersel_y1;   
    var osw = osh * car;
  }
  else
  {
    var osw = this.m_fu_usersel_x2-this.m_fu_usersel_x1;
    var osh = osw / car;   
  }
  
  // calculate the outer selection box
  
  var usmx = this.m_fu_usersel_x1 + ((this.m_fu_usersel_x2-this.m_fu_usersel_x1) / 2); // user selection middle X  
  var usmy = this.m_fu_usersel_y1 + ((this.m_fu_usersel_y2-this.m_fu_usersel_y1) / 2); // user selection middle Y  
  
  this.m_fu_outerusersel_x1 = Math.round(usmx - osw / 2);
  this.m_fu_outerusersel_y1 = Math.round(usmy - osh / 2); 
  this.m_fu_outerusersel_x2 = Math.round(this.m_fu_outerusersel_x1 + osw); 
  this.m_fu_outerusersel_y2 = Math.round(this.m_fu_outerusersel_y1 + osh);
  
  // Make sure x2 and y2 don't exceed the image width and heigt
  
  if (this.m_fu_outerusersel_x2 > this.getImageWidth())
  {
    var ow = Math.abs(this.m_fu_outerusersel_x2-this.m_fu_outerusersel_x1);
    this.m_fu_outerusersel_x2 = this.getImageWidth();
    this.m_fu_outerusersel_x1 = this.m_fu_outerusersel_x2 - ow;
  }
  
  if (this.m_fu_outerusersel_y2 > this.getImageHeight())
  {
    var cw = Math.abs(this.m_fu_outerusersel_y2-this.m_fu_outerusersel_y1);
    this.m_fu_outerusersel_y2 = this.getImageHeight();
    this.m_fu_outerusersel_y1 = this.m_fu_outerusersel_y2 - cw;
  }
  
  // make sure x1 and y1 of the outer selection box is never < 0;
  
  if (this.m_fu_outerusersel_x1 < 0)
  {
    var cw = Math.abs(this.m_fu_outerusersel_x1);
    this.m_fu_outerusersel_x1 = this.m_fu_outerusersel_x1 + cw;
    this.m_fu_outerusersel_x2 = this.m_fu_outerusersel_x2 + cw;
  }
  
  if (this.m_fu_outerusersel_y1 < 0)
  {
    var cw = Math.abs(this.m_fu_outerusersel_y1);
    this.m_fu_outerusersel_y1 = this.m_fu_outerusersel_y1 + cw;
    this.m_fu_outerusersel_y2 = this.m_fu_outerusersel_y2 + cw;
  }
  
  return true;
}

function AjaxZoomObject_createNewDiv()
{
  this.newDiv = d2z_getElementByIdOrName(this.m_div_id + "_NEW");
  if (!this.newDiv)
  { 
    try
    {
      var imgCanvas = this.getImageCanvasDiv();
      var imgCanvasImg = this.getImageCanvasImage();
    
      this.divExists = false;
      this.newDiv = document.createElement("div");
      this.newDiv.setAttribute("id", this.m_div_id + "_NEW");
    
      this.newDiv.style.position = "absolute";
      this.newDiv.style.left = '0px';
      this.newDiv.style.top = '0px';
      this.newDiv.style.width = imgCanvas.style.width;
      this.newDiv.style.height = imgCanvas.style.height;
      this.newDiv.style.overflow = "hidden"; // make sure IE does not get incontinent
    
      this.newDiv.style.visibility = "hidden";
      if (d2z_global_replace_mode <= 0)
         this.newDiv.style.visibility = "hidden";
      this.newDiv.style.zIndex = 25;
      
      imgCanvas.appendChild(this.newDiv);
      
      this.newImg = new Image();
      this.newImg.id = this.m_div_id + '_NEW_IMG';
      this.newImg.src = imgCanvasImg.src;
  /*    this.newImg.style.border = '1px solid #FF0000'; */
      this.newImg.style.position = "absolute";
      this.newImg.style.left = '0px';
      this.newImg.style.top = '0px';
      this.newImg.style.width = imgCanvasImg.style.width;
      this.newImg.style.height = imgCanvasImg.style.height;
      
      this.newDiv.appendChild(this.newImg);
      
    } 
    catch (e)
    {
      alert("Error in \"AjaxZoomObject_createNewDiv()\" "+ e );
    }
  }
  
  return this.newDiv; 
}

function AjaxZoomObject_distroyNewDiv()
{
  this.newDiv = d2z_getElementByIdOrName(this.m_div_id + "_NEW");
  if (this.newDiv)
  {
     try
     {
       this.newDiv.parentNode.removeChild(this.newDiv);
       // d2z_getElementByIdOrName(id).style.visibility = "hidden";
     } 
     catch (e)
     {
        alert(e);
     }
     this.newImg = null;
     this.newDiv = null;
  }
}


function d2z_displayImage(div_id, filename_relative)
{
   if (d2z_global_rotating_timer != -1)
   {
      // kill rotation
      window.clearTimeout(d2z_global_rotating_timer);
      d2z_global_rotating_timer = -1;
   }

   var ajaxobj = d2z_getAjaxObj(div_id);

   ajaxobj.m_filename = filename_relative;
   ajaxobj.hideSelectionRect();

   var width = ajaxobj.getFullWidth();
   var height = ajaxobj.getFullHeight();
   var navigation_position = (ajaxobj.m_navigation_position == '') ? d2z_global_navigation_position : ajaxobj.m_navigation_position;
   var navigation_color = (ajaxobj.m_navigation_color == '') ? d2z_global_navigation_color : ajaxobj.m_navigation_color;
   var navigation_img = (ajaxobj.m_navigation_img == '') ? d2z_global_navigation_img : ajaxobj.m_navigation_img;
   var navigation_size = (ajaxobj.m_navigation_size == -1) ? d2z_global_navigation_size : ajaxobj.m_navigation_size;
   var template = (ajaxobj.m_template == '') ? d2z_global_template : ajaxobj.m_template;
   var border_color = (ajaxobj.m_border_color == '') ? d2z_global_border_color : ajaxobj.m_border_color;
   var idx = ajaxobj.m_idx;

   var params = '';
   params += '&WIDTH=' + width;
   params += '&HEIGHT=' + height;
   params += '&FILENAME=' + filename_relative;
   params += '&TEMPLATE=' + template;
   params += '&NAV_POS=' + navigation_position;
   params += '&NAV_COL=' + navigation_color;
   params += '&NAV_SIZ=' + navigation_size;
   params += '&NAV_IMG=' + navigation_img;
   params += '&BORDER_COL=' + border_color;
   params += '&REPLACE=' + d2z_global_replace_mode;
   d2z_updateSendReqScript(div_id, params);
}

function d2z_sendZoomUpdateRequest(div_id, x1, y1, x2, y2)
{
   var ajaxobj = d2z_getAjaxObj(div_id);
   var params = '';
   params += "&X1=" + Math.round(x1);
   params += "&Y1=" + Math.round(y1);
   params += "&X2=" + Math.round(x2);
   params += "&Y2=" + Math.round(y2);

   // d2z_global_current_pan_id = div_id;

   // alert(params);
   d2z_updateSendReqScript(div_id, params);
}

function d2z_runZoom(div_id)
{
  var ajaxobj = d2z_getAjaxObj(div_id);
  ajaxobj.runZoom()
}


function d2z_runDummyZoom(div_id)
{
  var ajaxobj = d2z_getAjaxObj(div_id);
  if (ajaxobj)
  {
    ajaxobj.setZoomRect(0, 0, ajaxobj.getImageCanvasWidth(), ajaxobj.getImageCanvasHeight()); 
    d2z_doStartZoom(ajaxobj, 100);
  }
}

function d2z_moveFreePan(div_id, offx, offy)
{
  var ajaxobj = d2z_getAjaxObj(div_id);
  return ajaxobj.moveFreePan(offx, offy);
} 

function d2z_zoomFreePan(div_id, offx, offy)
{
  return d2z_moveFreePan(div_id, offx, offy);
}

function d2z_changePanLocation(div_id, location)
{
  var ajaxobj = d2z_getAjaxObj(div_id);
  //d2z_runDummyZoom(div_id);
  var params = '&PAN_LOCATION=' + location;
  d2z_updateSendReqScript(div_id, params);
}

function d2z_zoomPan(div_id, direction)
{
  var ajaxobj = d2z_getAjaxObj(div_id);
  offx = 0;
  offy = 0;
  movex = Math.round(ajaxobj.getImageCanvasWidth() / 6);
  movey = Math.round(ajaxobj.getImageCanvasHeight() / 6);
  moveRight = movex; 
  moveLeft = movex*-1; 
  moveUp = movey*-1; 
  moveDown = movey; 

  if (direction == 1) // UP
  {
    offy = moveUp;
  }
  else if (direction == 2) // DOWN
  {
    offy = moveDown;
  }
  else if (direction == 4) // RIGHT
  {
    offx = moveRight;
  }
  else if (direction == 5) // RIGHT-UP
  {
    offx = moveRight;
    offy = moveUp;
  }
  else if (direction == 6) // RIGHT-DOWN
  {
    offx = moveRight;
    offy = moveDown;
  }
  else if (direction == 8) // LEFT
  {
    offx = moveLeft;
  }
  else if (direction == 9)  // LEFT-UP
  {
    offx = moveLeft;
    offy = moveUp;
  }
  else if (direction == 10) // LEFT-DOWN
  {
    offx = moveLeft;
    offy = moveDown;
  }

  d2z_moveFreePan(div_id, offx, offy);
}

function d2z_zoomReset(div_id)
{
   var ajaxzoom = d2z_getAjaxObj(div_id);
   if (ajaxzoom.m_zoom_mode == D2Z_ZOOM_MODE_MOVE)
      window.location.reload();
   else
   {
      var params = '&LEVEL=' + D2Z_LEVEL_RESET;
      d2z_updateSendReqScript(div_id, params);
   }
}

function d2z_zoomBack(div_id)
{
   var params = '&LEVEL=' + D2Z_LEVEL_BACK;

   var ajaxzoom = d2z_getAjaxObj(div_id);
   ajaxzoom.move_toplevel = true;
   d2z_updateSendReqScript(div_id, params);
}

function d2z_branchMultipleIdx(div_id, direction)
{
   d2z_runDummyZoom(div_id);
   var params = '&MULTIDX=' + direction;
   d2z_updateSendReqScript(div_id, params);

   if (d2z_global_rotating_timer != -1)
   {
      // kill rotation
      window.clearTimeout(d2z_global_rotating_timer);
      d2z_global_rotating_timer = -1;
   }
}

function d2z_branchRotate(div_id, direction)
{
   if ((direction == "<") || (direction == ">"))
   {
//alert("d2z_branchMultipleIdx('" + div_id + "','" + direction + "');");
      d2z_branchMultipleIdx(div_id,direction);
      d2z_global_rotating_timer = window.setTimeout("d2z_branchRotate('" + div_id + "','" + direction + "');", 333);
   } else if (d2z_global_rotating_timer != -1)
   {
      // kill rotation
      window.clearTimeout(d2z_global_rotating_timer);
      d2z_global_rotating_timer = -1;
   }
}


function d2z_getAjaxObjByElementID(aElement)
{
  ret = null;
  if (typeof aElement == 'string')
    e = d2z_getElementByIdOrName(aElement)
  else
    e = aElement; 
  while (e && ret == null)
  {
    if (global_ajaxzoom_objects[e.id] && e.id != '')
    {
      return d2z_getAjaxObj(e.id); 
    }
    else
    {
      e = e.parentNode;
    } 
  }
  return null;
}


/**
Assignes internal mouse-handlers to the div-element and returns an 
instance of the AjaxZoomObject. 
@function {public static obj} ?
@param Name of the div
@returns Instance of the ajax object
@author AP
*/

function d2z_getAjaxObj(div_id)
{
  var ajaxobj = global_ajaxzoom_objects[div_id];

  if (ajaxobj == null)
  {
     ajaxobj = new AjaxZoomObject(div_id);
     global_ajaxzoom_objects[div_id] = ajaxobj;
  }
   
  // Assign Mouse Event handlers
  
  var imageCanvas = d2z_getElementByIdOrName(div_id + "_IMG");
  if (imageCanvas)
  {
    imageCanvas.onmousemove = d2z_handleMouseMoveEvent;
    imageCanvas.onmouseup = d2z_handleMouseUpEvent;
    imageCanvas.onmousedown = d2z_handleMouseDownEvent;
  }

  return ajaxobj;
}

function d2z_updateSendReqScript(div_id, params)
{

   var ajaxobj = d2z_getAjaxObj(div_id);

   ajaxobj.m_request = new hcjsu_XMLHTTPReqQueueObject();

   if (ajaxobj.m_request)
   {
      /*
      global_current_div_id = div_id;
      */

      now = new Date();
      url = d2z_global_server + '?ALIAS=30001';
      url += '&TIM=' + now.getTime();
      url += '&DATA=' + div_id;
      url += '&CLIENT=' + d2z_global_client;
      url += '&ZMODE=' + ajaxobj.m_zoom_mode;

      ajaxobj.m_request.onreadystatechange = 'd2z_handleOnReadyStateChangeEvent';
      ajaxobj.m_request.open('POST', url, true);
      ajaxobj.m_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      ajaxobj.m_request.send(params);
      ajaxobj.m_request.setUserData(div_id);
      ajaxobj.m_request.queue();
      ajaxobj.setLoadingState(true);
   }
}

function setPanRepositioning(doRepositioning)
{
   if (doRepositioning == "true")
      d2z_global_pan_repositioning = true;
   else
      d2z_global_pan_repositioning = false;
}

function d2z_handleHideAntiFlickerEndOverlay(id)
{
  var ajaxobj = d2z_getAjaxObj(id);
  if (ajaxobj)
    ajaxobj.antiFlickerEndOverlay();
}

function d2z_handleUpdateContentEvent(id)
{
   var ajaxobj = d2z_getAjaxObj(id);

   // Check if there is something to do
   if (ajaxobj.m_last_request_result) 
   {

     var postponeUpdate = false; 
     // Check if animation is still running
     if (ajaxobj.m_animating)
       postponeUpdate = true;

     // Check if all images are loaded
     if (! hcjsu_checkImagesComplete(ajaxobj.m_last_request_result_tmpdiv,id))
       postponeUpdate = true;
 
     if (! postponeUpdate)
     {
       ajaxobj.setLoadingState(false);

       // Update DOM 
       
       var output = d2z_getElementByIdOrName(ajaxobj.m_div_id);
       
       ajaxobj.antiFlickerStartOverlay();       
  
       if (output)
       {
         if (ajaxobj.m_last_request_result_tmpdiv)
         {
           // Remove all existing child nodes
           while (output.hasChildNodes()) 
           {
             output.removeChild(output.firstChild )
           };
           // Append Child nodes from div
           for (var i = 0; i < ajaxobj.m_last_request_result_tmpdiv.childNodes.length; i++)
           {
             output.appendChild(ajaxobj.m_last_request_result_tmpdiv.childNodes[i]);
           }
         }
         else
         {
           output.innerHTML = ajaxobj.m_last_request_result;
         }

         var ajaxobj = d2z_getAjaxObj(id); // makes sure that mouse handler is set
         
         ajaxobj.clearUserSel();
         ajaxobj.m_draw_selrect = true;  

         // Clear last ajax result
         ajaxobj.m_last_request_result = false;
         if (ajaxobj.m_last_request_result_tmpdiv)
         {
           hcjsu_removeElement(ajaxobj.m_last_request_result_tmpdiv);
           ajaxobj.m_last_request_result_tmpdiv = null;
         }
         
         ajaxobj.updateDisplay();
         
         return true; 
       }
     }
     else
     {
       window.setTimeout("d2z_handleUpdateContentEvent('" + id + "')", 100);   
       return false; 
     }
   }
}

function d2z_handleOnReadyStateChangeEvent(aReq)
{
   /*
   if (!global_current_div_id)
   {
      alert("Invalid calling sequence - global_current_div_id is empty");
      return;
   }
   */

   var div_id = aReq.fUserData;
   var ajaxobj = global_ajaxzoom_objects[div_id];

   var ready = aReq.request.readyState;
   if (ready == 4)
   {
      var data = aReq.request.responseText;
      ajaxobj.m_last_request_result = data; // Remember result
      // Generate tmporary div from conent - this might improve performance
      ajaxobj.m_last_request_result_tmpdiv = document.createElement('div');
      ajaxobj.m_last_request_result_tmpdiv.style.position = 'absolute';
      ajaxobj.m_last_request_result_tmpdiv.id = ajaxobj.m_div_id+'_TMPDIV';
      ajaxobj.m_last_request_result_tmpdiv.style.left = '-9999px';
      ajaxobj.m_last_request_result_tmpdiv.style.width = '1px';
      ajaxobj.m_last_request_result_tmpdiv.innerHTML = ajaxobj.m_last_request_result;
      document.getElementsByTagName("body")[0].appendChild(ajaxobj.m_last_request_result_tmpdiv);

      // Set result immediately if no animation is running
      // If a animation is running, 
      if (! ajaxobj.m_animating)
      {
        d2z_handleUpdateContentEvent(div_id);
      }
   }
}

function e4s_initXmlHttpReq()
{
   var xRequest = null;

   if (window.XMLHttpRequest)
   {
      xRequest = new XMLHttpRequest();
   } else if (window.ActiveXObject)
   {
      xRequest = new ActiveXObject("Microsoft.XMLHTTP");
   }

   return xRequest;
}

function mouseOverButton(div_id, button_id, alt)
{
   d2z_changeButton(div_id, button_id, 2, alt);
}

function mouseOutsideButton(div_id, button_id, alt)
{
   d2z_changeButton(div_id, button_id, 1, alt);
}

function d2z_setCanvasImagePos(div_id, x, y)
{
  
}

function d2z_changeButton(div_id, button_id, state, alt)
{
   try
   {
      id = "I_" + div_id + "_" + button_id;
      var buttonImg = d2z_getElementByIdOrName(id);
      if (buttonImg)
      {
         var src = buttonImg.src;
         if (src)
           var i = src.lastIndexOf('.')
         else
           i = -1;
         if (i > 0)
         {
            src = src.substring(0, i - 1) + state + src.substring(i);
         }
         buttonImg.src = src;
         buttonImg.setAttribute("alt", alt);
      } 
   } 
   catch (e)
   {
      alert("Error in mouseOverButton(" + id + ") - " + e);
   }
   /*
   try
   {
      id = "I_" + div_id + "_" + button_id;

      if (document.images[id])
      {
         var src = document.images[id].src;
         if (src)
           var i = src.lastIndexOf('.')
         else
           i = -1;
         if (i > 0)
         {
            src = src.substring(0, i - 1) + state + src.substring(i);
            document.images[id].src = src;
         }

         document.images[id].setAttribute("alt", alt);
      }
   } 
   catch (e)
   {
      alert("Error in mouseOverButton(" + id + ") - " + e);
   }
   */
}

// ----------------------


/**
@function {public static bool} ?
@returns false
@author AP
Handles Mouse Move within the Door2Zoom Picture
*/

function d2z_handleMouseMoveEvent(evt)
{
   if (IE)
   {
     mouseEvent = new hcjsu_mouseEventClass(event);
   }
   else
   {
     mouseEvent = new hcjsu_mouseEventClass(evt);
   }
  
   
   var ajaxzoom = d2z_getAjaxObjByElementID(mouseEvent.target)

   /*
   var ajaxzoom = d2z_getAjax(xm, ym);
   */

   if (ajaxzoom)
   {
       
   
     if ((ajaxzoom.m_loading) ||
       (!ajaxzoom.isValidMouseHandlerElement(mouseEvent.target)))
       return false; // Do not handle mouse events of this sub element
     // Make Sure the mouse event maps to the image
     mouseEvent.remapMouseEventTo(ajaxzoom.m_div_id+'_IMG');
     
     xm = mouseEvent.offsetX;
     ym = mouseEvent.offsetY;
  
     m_mouse_x = xm;
     m_mouse_y = ym;
     
     ajaxzoom.showStatus(xm, ym);
   
     if (ajaxzoom.m_usersel_x1 != D2Z_UNDEFINED_POS)
     {
       ajaxzoom.m_usersel_x2 = xm;
       ajaxzoom.m_usersel_y2 = ym;
     }
     
      if (ajaxzoom.m_zoom_mode == D2Z_ZOOM_MODE_MOVE) 
      {
         if (ajaxzoom.m_usersel_x1 != D2Z_UNDEFINED_POS)
         { 
           if (!ajaxzoom.move_dragging &&
              ((Math.abs(ajaxzoom.m_usersel_x2 - ajaxzoom.m_usersel_x1) > 0) ||
               (Math.abs(ajaxzoom.m_usersel_y2 - ajaxzoom.m_usersel_y1) > 0)))
           {
             // Show panning div in new div and start dragging
             ajaxzoom.hideSelectionRect();
             ajaxzoom.createNewDiv();
             ajaxzoom.showImage(false);
             ajaxzoom.move_dragging = true;
           }
           if (ajaxzoom.move_dragging)
           {
             ajaxzoom.updateDisplay();
           }
         }
      } 
      else 
        if (ajaxzoom.m_zoom_mode == D2Z_ZOOM_MODE_RECT)
        {

          if ((ajaxzoom.m_usersel_x1 != D2Z_UNDEFINED_POS) && (Math.abs(xm - ajaxzoom.m_usersel_x1) >= 8) && (Math.abs(ym - ajaxzoom.m_usersel_y1) >= 8) && ((ajaxzoom.m_usersel_x1 != xm) || (ajaxzoom.m_usersel_y1 != ym)))
          {
            ajaxzoom.drawSelectionRect();
          }
        } 
   }

   return false;
}

function d2z_movemode_hideFreepan(id)
{
  var ajaxzoom = d2z_getAjaxObj(id);
  if (ajaxzoom.newDiv)
     ajaxzoom.newDiv.style.visibility = "hidden";
}

function d2z_movemode_setMagnificationFactor(factor)
{
   d2z_global_movemode_magfactor = factor;
}

function d2z_doMovemodePartialZoom(id, x, y)
{
   // alert("PARTIALZOOM");
   var ajaxzoom = d2z_getAjaxObj(id);
   ajaxzoom.partialZoom(x, y);
   /*
   ajaxzoom.move_toplevel = false;

   var div = d2z_getElementByIdOrName(ajaxzoom.m_div_id + "_IMG");
   ajaxzoom.move_wholeImage = div.firstChild.getAttribute("background");

   // ---

   var scaleFactor = 1 / d2z_global_movemode_magfactor;

   var x1 = x - ajaxzoom.getImageCanvasWidth() * scaleFactor;
   var y1 = y - ajaxzoom.getImageCanvasHeight() * scaleFactor;
   var x2 = x + ajaxzoom.getImageCanvasWidth() * scaleFactor;
   var y2 = y + ajaxzoom.getImageCanvasHeight() * scaleFactor;
   
   ajaxzoom.setUserSel(x1, y1, x2, y2);
   ajaxzoom.setZoomRect(x1, y1, x2, y2)

   ajaxzoom.drawSelectionRect();

   d2z_runZoom(id);
   */
}


/**
@function {public static bool} ?
@returns false
@author AP
Handles onmousedownn-events within Door2Zoom-Images.
*/

function d2z_handleMouseDownEvent(evt)
{
   if (IE)
   {
     mouseEvent = new hcjsu_mouseEventClass(event);
   }
   else
   {
     mouseEvent = new hcjsu_mouseEventClass(evt);
   }
   
   if (mouseEvent.button != 0)
    return;

   /*
   var ajaxzoom = d2z_getAjaxObjXY(xm, ym);
   */
   var ajaxzoom = d2z_getAjaxObjByElementID(mouseEvent.target);

   if (ajaxzoom)
   {
     if ((ajaxzoom.m_loading) ||
        (!ajaxzoom.isValidMouseHandlerElement(mouseEvent.target)))
       return false; // Do not handle mouse events of this sub element
       // Make Sure the mouse event maps to the image
       mouseEvent.remapMouseEventTo(ajaxzoom.m_div_id+'_IMG');
       
     xm = mouseEvent.offsetX;
     ym = mouseEvent.offsetY;
  
     // window.status = xm + "/" + ym;
  
     m_mouse_x = xm;
     m_mouse_y = ym;
     
     ajaxzoom.showStatus(xm, ym);
   
     ajaxzoom.m_usersel_x1 = xm;
     ajaxzoom.m_usersel_y1 = ym;
   
     // window.status = ajaxzoom.m_zoom_mode;
     if (ajaxzoom.m_zoom_mode == D2Z_ZOOM_MODE_RECT)
     {
     } 
     else 
       if (ajaxzoom.m_zoom_mode == D2Z_ZOOM_MODE_MOVE)
       {
         ajaxzoom.m_move_downx = xm;
         ajaxzoom.m_move_downy = ym;
         ajaxzoom.m_move_mousedown = true;
         ajaxzoom.move_dragging = false;
  
         m_in_zoom = true;
       }
   }

   return false;
}

function d2z_doRectmodeMouseUpAction(ajaxzoom)
{
   if (ajaxzoom.m_usersel_x1 != D2Z_UNDEFINED_POS)
   {
      ajaxzoom.fixupUserSel();
      // AP: Check if new rect is large enough.
      if (((ajaxzoom.m_fu_usersel_x2 - ajaxzoom.m_fu_usersel_x1) >= 10) && 
          ((ajaxzoom.m_fu_usersel_y2 - ajaxzoom.m_fu_usersel_y1) >= 10))
      { // Area is large enough. 
         try
         {
           ajaxzoom.runZoom();
         } 
           catch (e)
         {
            alert(e);
         }
      }
      else
      { // Area is too small. Perform a partial zoom
        ajaxzoom.partialZoom(ajaxzoom.m_fu_usersel_x2, ajaxzoom.m_fu_usersel_y2);
      }
   }
}

/**
Event handler for onmouseup events
@function {public static bool} ?
*/

function d2z_handleMouseUpEvent(evt)
{
   if (IE)
   {
     mouseEvent = new hcjsu_mouseEventClass(event);
   }
   else
   {
     mouseEvent = new hcjsu_mouseEventClass(evt);
   }
   

   /*
   var ajaxzoom = d2z_getAjaxObjXY(xm, ym);
   */
   
   var ajaxzoom = d2z_getAjaxObjByElementID(mouseEvent.target);
   

   if (ajaxzoom)
   {
   
     if ((ajaxzoom.m_loading) ||
        (!ajaxzoom.isValidMouseHandlerElement(mouseEvent.target)))
       return false; // Do not handle mouse events of this sub element
     // Make Sure the mouse event maps to the image
     mouseEvent.remapMouseEventTo(ajaxzoom.m_div_id+'_IMG');
  
     xm = mouseEvent.offsetX;
     ym = mouseEvent.offsetY;
     
     ajaxzoom.showStatus(xm, ym);
   
     if (ajaxzoom.m_usersel_x1 != D2Z_UNDEFINED_POS)
     {
       ajaxzoom.m_usersel_x2 = xm;
       ajaxzoom.m_usersel_y2 = ym;
     }
   
      setTimeout("m_in_zoom=false;", 10000);

      if (ajaxzoom.m_zoom_mode == D2Z_ZOOM_MODE_RECT)
      {
        d2z_doRectmodeMouseUpAction(ajaxzoom);
      } 
      else
        if (ajaxzoom.m_zoom_mode == D2Z_ZOOM_MODE_MOVE)
        {
          if (ajaxzoom.move_dragging)
          {
            ajaxzoom.m_move_mousedown = false;
            ajaxzoom.move_dragging = false;
            ajaxzoom.move_dragging = false;
          
            // d2z_doEndAni(id);
            ajaxzoom.moveFreePan(ajaxzoom.m_usersel_x1 - ajaxzoom.m_usersel_x2,
              ajaxzoom.m_usersel_y1 - ajaxzoom.m_usersel_y2);
          }
          else
          {
            ajaxzoom.m_move_mousedown = false;
            ajaxzoom.move_dragging = false;
            ajaxzoom.distroyNewDiv();
            ajaxzoom.partialZoom(xm, ym);
          }
        }  
   }

   return false;
}


function TRACE(s)
{
   var debug = d2z_getElementByIdOrName('DEBUG');
   if (debug)
      debug.innerHTML = debug.innerHTML + '<BR>' + s;
}

// ---------------------------------------------------------------------------------------------------------
// Popup

function d2z_setPopup(width, height, pos, div_id)
{
   if (div_id)
   {
      var ajaxobj = d2z_getAjaxObj(div_id);

      ajaxobj.m_popup_dest_width = width;
      ajaxobj.m_popup_dest_height = height;
      ajaxobj.m_popup_curr_width = 0;
      ajaxobj.m_popup_curr_height = 0;
      ajaxobj.m_popup = null;
      ajaxobj.m_popup_pos = pos;
      ajaxobj.m_popup_offset_x = 0;
      ajaxobj.m_popup_offset_y = 0;
   }
}

function d2z_setPopupOffset(dx, dy, div_id)
{
   if (div_id)
   {
      var ajaxobj = d2z_getAjaxObj(div_id);

      ajaxobj.m_popup_offset_x = dx;
      ajaxobj.m_popup_offset_y = dy;
   }
}

function setPopupOffset(dx, dy, div_id)
{
  d2z_setPopupOffset(dx, dy, div_id)
}


function d2z_resizePopup(div_id)
{
   if (div_id)
   {
      var ajaxobj = d2z_getAjaxObj(div_id);

      if (ajaxobj.m_popup_curr_width < (ajaxobj.m_popup_dest_width - ajaxobj.m_popup_dest_width / 20))
      {
         ajaxobj.m_popup_curr_width += ajaxobj.m_popup_dest_width / 20;
         ajaxobj.m_popup_curr_height += ajaxobj.m_popup_dest_height / 20;

         ajaxobj.m_popup.style.visibility = "visible";
         if (ajaxobj.m_popup_pos == 'L')
         {
            ajaxobj.m_popup.style.left = d2z_intToCssDimension(ajaxobj.m_popup_offset_x);
            ajaxobj.m_popup.style.top = d2z_intToCssDimension(ajaxobj.m_popup_offset_y);
         } else if (ajaxobj.m_popup_pos == 'C')
         {
            ajaxobj.m_popup.style.left = d2z_intToCssDimension(ajaxobj.m_popup_offset_x + ((screen.width - ajaxobj.m_popup_curr_width) / 2));
            ajaxobj.m_popup.style.top = d2z_intToCssDimension(ajaxobj.m_popup_offset_y + ((screen.height - ajaxobj.m_popup_curr_height) / 2));
         }
         ajaxobj.m_popup.style.width = d2z_intToCssDimension(ajaxobj.m_popup_curr_width);
         ajaxobj.m_popup.style.height = d2z_intToCssDimension(ajaxobj.m_popup_curr_height);

         setTimeout("resizePopup('" + div_id + "');", 50);
      } else
      {
         if (ajaxobj.m_popup_pos == 'L')
         {
            ajaxobj.m_popup.style.left = d2z_intToCssDimension(ajaxobj.m_popup_offset_x);
            ajaxobj.m_popup.style.top = d2z_intToCssDimension(ajaxobj.m_popup_offset_y);
         } else if (ajaxobj.m_popup_pos == 'C')
         {
            ajaxobj.m_popup.style.left = d2z_intToCssDimension(ajaxobj.m_popup_offset_x + ((screen.width - ajaxobj.m_popup_dest_width) / 2));
            ajaxobj.m_popup.style.top = d2z_intToCssDimension(ajaxobj.m_popup_offset_y + ((screen.height - ajaxobj.m_popup_dest_height) / 2));
         }
         ajaxobj.m_popup.style.width = d2z_intToCssDimension(ajaxobj.m_popup_dest_width);
         ajaxobj.m_popup.style.height = d2z_intToCssDimension(ajaxobj.m_popup_dest_height);
      }
   }
}

function resizePopup(div_id)
{
  d2z_resizePopup(div_id);
}


function d2z_openPopup(div_id)
{
   if (div_id)
   {
      var ajaxobj = d2z_getAjaxObj(div_id);

      if (ajaxobj.m_popup_curr_width > 0)
      {
         closePopup(div_id);
         return;
      }

      if (ajaxobj.m_popup == null)
      {
         var existing_div = d2z_getElementByIdOrName(div_id + "_POPUP");
         if (existing_div != null)
            document.getElementsByTagName("body")[0].removeChild(existing_div);

         ajaxobj.m_popup = document.createElement("div");
         ajaxobj.m_popup.id = div_id + "_POPUP";
         ajaxobj.m_popup.style.position = "absolute";
         ajaxobj.m_popup.style.backgroundColor = "#FFFFFF";
         ajaxobj.m_popup.style.overflow = "hidden";
         ajaxobj.m_popup.style.zIndex = 99;
         ajaxobj.m_popup.style.borderWidth = "1px";
         ajaxobj.m_popup.style.borderStyle = "solid";
         ajaxobj.m_popup.style.borderColor = "#000000";
         ajaxobj.m_popup.style.visible = "none";
         ajaxobj.m_popup.style.width = "0px";
         ajaxobj.m_popup.style.height = "0px";

         document.getElementsByTagName("body")[0].appendChild(ajaxobj.m_popup);
      }

      ajaxobj.m_popup_curr_width = 0;
      ajaxobj.m_popup_curr_height = 0;

      ajaxobj.m_popup_request = new hcjsu_XMLHTTPReqQueueObject();

      if (ajaxobj.m_popup_request)
      {

         now = new Date();
         url = d2z_global_server + '?ALIAS=30002';
         url += '&TIM=' + now.getTime();
         url += '&CLIENT=' + d2z_global_client;
         url += '&FILENAME=' + ajaxobj.m_filename;
         url += '&WIDTH=' + ajaxobj.m_popup_dest_width;
         url += '&HEIGHT=' + ajaxobj.m_popup_dest_height;
         url += '&DATA=' + div_id;

         ajaxobj.m_popup_request.onreadystatechange = 'd2z_handlePopupReadyStateChange';
         ajaxobj.m_popup_request.open('POST', url, true);
         ajaxobj.m_popup_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
         ajaxobj.m_popup_request.setUserData(div_id);
         ajaxobj.m_popup_request.send('');
         ajaxobj.m_popup_request.queue();
         ajaxobj.setLoadingState(true);
      }

      resizePopup(div_id);
   }
}

function d2z_handlePopupReadyStateChange(aReq)
{
   div_id = aReq.fUserData;
   
   var ajaxobj = global_ajaxzoom_objects[div_id];

   var ready = aReq.request.readyState;
   if (ready == 4)
   {
      ajaxobj.setLoadingState(false);

      var data = aReq.request.responseText;

      var output = d2z_getElementByIdOrName(div_id + "_POPUP");

      if (output)
      {
         output.style.visibility = 'visible';
         output.innerHTML = data;
      }
   }
}


function d2z_closePopup(div_id)
{
   if (div_id)
   {
      var ajaxobj = d2z_getAjaxObj(div_id);

      if (ajaxobj.m_popup)
      {
         ajaxobj.m_popup_curr_width = 0;
         ajaxobj.m_popup_curr_height = 0;
         ajaxobj.m_popup.style.visibility = "hidden";
      }
   }
}



/*******************************************************************************
 * D2Z OLD VERSION COMPATIBILITY LAYER
 ******************************************************************************/

var LEVEL_RESET = D2Z_LEVEL_RESET;
var LEVEL_BACK = D2Z_LEVEL_BACK;
var LEVEL_PAN = D2Z_LEVEL_PAN;
var LEVEL_FREEPAN = D2Z_LEVEL_FREEPAN;
var ZOOM_MODE_RECT = D2Z_ZOOM_MODE_RECT;
var ZOOM_MODE_MOVE = D2Z_ZOOM_MODE_MOVE;

/**
This function is depreciated. Use {@link d2z_setServer} instead.
@function {public static void} ?
@param url of the server
@author AP
*/


function setServer(server)
{
  return d2z_setServer(server);
}

/**
DEPRECIATED - use {@link d2z_doStartZoom} instead
*/

function startZoom(ajaxzoom)
{
  return d2z_doStartZoom(ajaxzoom);
}

/**
DEPRECIATED - use {@link d2z_setZoomMode} instead
*/


function setZoomMode(mode, id)
{
  return d2z_setZoomMode(mode, id);
}

/**
DEPRECIATED - use {@link d2z_handleZoomNextEvent} instead
*/

function zoomNext(id)
{
  return d2z_handleZoomNextEvent(id);
}

/**
DEPRECIATED. Use {@link d2z_setTemplate} instead
*/

function setTemplate(template, div_id)
{
  return d2z_setTemplate(template, div_id);
}

function setIdx(idx, div_id)
{
  return d2z_setIdx(idx, div_id);
}

/**
DEPRECIATED. Use {@link d2z_setZoomMagnification} instead
*/

function setZoomMagnification(magnification, div_id)
{
  return d2z_setZoomMagnification(magnification, div_id); 
}

/**
DEPRECIATED. Use {@link d2z_setNavigationPosition} instead
*/

function setNavigationPosition(pos, div_id)
{
  result = d2z_setNavigationPosition(pos, div_id); 
}

/**
DEPRECIATED - use {@link d2z_setNavigationColor} instead
*/

function setNavigationColor(color, div_id)
{
  return d2z_setNavigationColor(color, div_id);
}

/**
DEPRECIATED - use {@link d2z_setNavigationImage} instead
*/

function setNavigationImage(img, div_id)
{
  return d2z_setNavigationImage(img, div_id); 
}

/**
DEPRECIATED - use {@link d2z_setReplaceSpeed} instead
*/

function setReplaceSpeed(speed)
{
  return d2z_setReplaceSpeed(speed);
}


/**
DEPRECIATED - use {@link d2z_setNavigationSize} instead
*/

function setNavigationSize(sz, div_id)
{
  return d2z_setNavigationSize(sz, div_id);
}

/**
DEPRECIATED - use {@link d2z_setBorderColor} instead
*/

function setBorderColor(color, div_id)
{
  return d2z_setBorderColor(color, div_id)
}

/**
Assignes internal mouse-handlers to the div-element and returns an 
instance of the AjaxZoomObject. This function is depreciated. Use 
{@link d2z_getAjaxObj} instead 
@function {public static obj} ?
@param Name of the div
@returns Instance of the ajax object
@author AP
*/

function getAjaxObj(div_id)
{
  return d2z_getAjaxObj(div_id)
}

/**
DEPRECIATED - use {@link d2z_setGlobals} instead
*/

function setGlobals()
{
  return d2z_setGlobals();
}

/**
DEPRECIATED - use {@link d2z_setHeight} instead
*/

function setHeight(height, div_id)
{
  return d2z_setHeight(height, div_id);
}

/**
DEPRECIATED - use {@link d2z_setWidth} instead
*/


function setWidth(width, div_id)
{
  return d2z_setWidth(width, div_id);
}

/**
DEPRECIATED - use {@link d2z_setClient} instead
*/

function setClient(client)
{
  return d2z_setClient(client);
}


/**
DEPRECIATED - use {@link d2z_openPopup} instead
*/

function openPopup(div_id)
{
  return d2z_openPopup(div_id)
}

/**
DEPRECIATED - use {@link d2z_setPopup} instead
*/

function setPopup(width, height, pos, div_id)
{
  return d2z_setPopup(width, height, pos, div_id);
}


/**
DEPRECIATED - use {@link d2z_closePopup} instead
*/

 
function closePopup(div_id)
{
  return d2z_closePopup(div_id)
}

/**
DEPRECIATED - use {@link d2z_zoomFreePan} instead
*/

function zoomFreePan(div_id, offx, offy)
{
  return d2z_zoomFreePan(div_id, offx, offy);
}

/**
DEPRECIATED - use {@link d2z_zoomPan} instead
*/


function zoomPan(div_id, direction)
{
  return d2z_zoomPan(div_id, direction);
}

/**
DEPRECIATED - use {@link d2z_zoomReset} instead
*/

function zoomReset(div_id)
{
  return d2z_zoomReset(div_id);
}

/**
DEPRECIATED - use {@link d2z_zoomBack} instead
*/

function zoomBack(div_id)
{
  return d2z_zoomBack(div_id);
}

/**
DEPRECIATED - use {@link d2z_branchMultipleIdx} instead
*/

function branchMultipleIdx(div_id, direction)
{
  return d2z_branchMultipleIdx(div_id, direction);
}

/**
DEPRECIATED - use {@link d2z_branchRotate} instead
*/


function branchRotate(div_id, direction)
{
  return d2z_branchRotate(div_id, direction);
}

/**
DEPRECIATED - use {@link d2z_displayImage} instead
*/

function displayImage(div_id, filename_relative)
{
  return d2z_displayImage(div_id, filename_relative);
}

/**
DEPRECIATED - use {@link d2z_updateSendReqScript} instead
*/

function updateSendReqScript(div_id, params)
{
  return d2z_updateSendReqScript(div_id, params)
}

/**
DEPRECIATED - use {@link d2z_movemode_hideFreepan} instead
*/

function movemode_hideFreepan(id)
{
  return d2z_movemode_hideFreepan(id)
}

/**
DEPRECIATED - use {@link d2z_movemode_setMagnificationFactor} instead
*/

function movemode_setMagnificationFactor(factor)
{
  return d2z_movemode_setMagnificationFactor(factor);
}

/**
DEPRECIATED - use {@link d2z_changeButton} instead
*/

function changeButton(div_id, button_id, state, alt)
{
  return d2z_changeButton(div_id, button_id, state, alt)
}




