/**
 *  mod_map_common.js:
 *
 *  Last Checked In By $Author: sasman $
 *  $Date: 2005/03/22 14:42:05 $
 *  $Revision: 1.18 $
 *
 *  (c) 2004 Copyright Basis Applied Technology
 *  CONFIDENTIAL INFORMATION
 *  All rights are reserved. Copying or other reproduction of
 *  this program except for archival purposes is prohibited
 *  without the prior written consent of Basis Applied Technology
 *
 *  Basis Applied Technology
 *  Gibsons, BC
 *  604.886.0721
 */

////////////////////////////////////////////////////////////////////////////////////////

core.loadUnit("css");

MapManager  = new Object();

/**
*/

/**
*/
function Master( topLeft, bottomRight )
{
	this.msTopLeft = topLeft;
	this.msBottomRight = bottomRight;
}

/**
*/
function Map( id, name, location, visible, image )
{
	this.miId = id;
	this.msName = name;
	this.miLocationId = location;
	this.mbVisible = visible;
	this.msImageFile = image;
}

function MapPosition( id, title, zoom1Id, zoom2Id, longitude, latitude, zoom1X, zoom1Y, zoom2X, zoom2Y )
{
	this.miId = id;
	this.msTitle = title;
	this.miZoom1Id = zoom1Id;
	this.miZoom2Id = zoom2Id;
	this.mfLong = longitude;
	this.mfLat = latitude;
	this.miZoom1X = zoom1X;
	this.miZoom1Y = zoom1Y;
	this.miZoom2X = zoom2X;
	this.miZoom2Y = zoom2Y;
}



function MapLocation( iX, iY, iSize, iZoomLevel)
{
	this.miX = iX;
	this.miY = iY;
	this.miSize = iSize;
	this.miZoomLevel = iZoomLevel;
}


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

	MAP MANAGER OBJECT - extended in mod_map_search.js and mod_map_customer.js

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

//--------------------------------------------------------------------
MapManager.CommonInit = function( obj )
{
    this.InitSelector();

	//assign passed in args
	this.mapServer = obj.mapServer;
	this.leftNavServer = obj.leftNavServer;
	this.listingServer = obj.listingServer;

	this.zoomLevel 	= 1;
	this.zoomStep 	= 0;
	this.parentId 	= obj.id;
	this.mapId 		= obj.id;
	this.hasMoved 	= false;

	this.mapImage = document.getElementById( "Map" );
	this.mapImage.style.width = (this.mapImage.width + "px");
	this.mapImage.style.height = (this.mapImage.height + "px");

	this.mapDIV = document.getElementById( "MapDIV" );
	this.mapInset = document.getElementById( "MapInset" );
	this.mapInset.style.zIndex = 100000;

   	this.mapSelector = document.getElementById( "MapSelector" );
	this.mapSelector.ondblclick = function(){ MapManager.OnMapDblClick(); };
       this.mapSelector.style.zIndex = 100001;

	this.insetLink = document.getElementById( "InsetLink" );
	this.showInset = true;

	this.zoomIn = document.getElementById( "ZoomIn" );
	this.zoomIn.onclick = function(){ MapManager.OnZoomIn(); };
	this.zoomOut = document.getElementById( "ZoomOut" );
	this.zoomOut.onclick = function(){ MapManager.OnZoomOut(); };
	this.zoomOut.style.display = "none";

	this.scrollRight = document.getElementById( "ScrollRight" );
 	this.scrollLeft = document.getElementById( "ScrollLeft" );
 	this.scrollUp = document.getElementById( "ScrollUp" );
 	this.scrollDown = document.getElementById( "ScrollDown" );

    core.addEvent(this.mapDIV, 'click', function(E){ MapManager.OnMapClick(E); });
    core.addEvent(this.mapSelector, 'click', function(E){ MapManager.OnInsetSelectorClick(E); });
    core.addEvent(this.mapInset, 'click', function(E){ MapManager.OnMapInsetClick(E); });

    core.addEvent(this.scrollRight, 'mousedown', function(E){ core.getTarget(E).src="/scca/html/images/panright1.gif"; MapManager.OnScrollRight(E); });
    core.addEvent(this.scrollLeft, 'mousedown', function(E) { core.getTarget(E).src="/scca/html/images/panleft1.gif"; MapManager.OnScrollLeft(E); });
    core.addEvent(this.scrollUp, 'mousedown', function(E){ core.getTarget(E).src="/scca/html/images/panup1.gif"; MapManager.OnScrollUp(E); });
    core.addEvent(this.scrollDown, 'mousedown', function(E){ core.getTarget(E).src="/scca/html/images/pandown1.gif"; MapManager.OnScrollDown(E); });
    core.addEvent(this.scrollRight, 'mouseup', function(E){ core.getTarget(E).src="/scca/html/images/panright.gif"; MapManager.EndScroll(E); });
    core.addEvent(this.scrollLeft, 'mouseup', function(E){ core.getTarget(E).src="/scca/html/images/panleft.gif"; MapManager.EndScroll(E); });
    core.addEvent(this.scrollUp, 'mouseup', function(E){ core.getTarget(E).src="/scca/html/images/panup.gif"; MapManager.EndScroll(E); });
    core.addEvent(this.scrollDown, 'mouseup', function(E){ core.getTarget(E).src="/scca/html/images/pandown.gif"; MapManager.EndScroll(E); });
    core.addEvent(this.scrollRight, 'mouseout', function(E){ core.getTarget(E).src="/scca/html/images/panright.gif"; MapManager.EndScroll(E); });
    core.addEvent(this.scrollLeft, 'mouseout', function(E){ core.getTarget(E).src="/scca/html/images/panleft.gif"; MapManager.EndScroll(E); });
    core.addEvent(this.scrollUp, 'mouseout', function(E){ core.getTarget(E).src="/scca/html/images/panup.gif"; MapManager.EndScroll(E); });
    core.addEvent(this.scrollDown, 'mouseout', function(E){ core.getTarget(E).src="/scca/html/images/pandown.gif"; MapManager.EndScroll(E); });

	mapInsetSizeHidden = document.getElementById( "MapInsetSize" );
    this.mapInsetSize = mapInsetSizeHidden.value;

	mapSizeHidden = document.getElementById( "MapSize" );
    this.mapSize = mapSizeHidden.value;

	mapScaleHidden = document.getElementById( "MapScale" );
	this.mapScale = mapScaleHidden.value;

	this.mapScale2 = this.mapScale * this.mapScale;
	this.mapSlice = this.mapImage.width /  this.mapScale;
	this.mapInsetSlice = this.mapInset.width / this.mapScale;
	this.mapInsetSlice2 = this.mapInsetSlice / this.mapScale;

	this.zoomSlice = new Array(3);
	this.zoomSlice[0] = this.mapSize;
	this.zoomSlice[1] = this.mapSize / this.mapScale;
	this.zoomSlice[2] = this.mapSize / this.mapScale2;

   	this.mapSelector.style.left = (this.mapInset.offsetLeft + "px"); //default position
   	this.mapSelector.style.top = (this.mapInset.offsetTop + "px"); //default position

	this.mapSelector.style.width = (this.mapInsetSlice + "px");
	this.mapSelector.style.height = (this.mapInsetSlice + "px");

	this.mapSelector.style.display = "block";

	this.zoomAmount = 180;
	this.fixedSize = 360;
	this.selectorZoomAmount = (this.zoomAmount * (parseFloat(this.mapSelector.style.width) / this.fixedSize ));
    this.scrollAmount = 5;
	this.scrollCount = 0;
    this.panHorz = 0;
	this.panVert = 0;

};


MapManager.InitSelector = function()
{
    var img = core.getElm('ListingPosition');
    var sel = core.getElm('ListingPositionSelected');

    if (core.isIE)
    {
        MapManager.ICON_OFFSET_X = -9;
        MapManager.ICON_OFFSET_Y = -31;
        MapManager.ICON_SHADOW_OFFSET_X = -7;
        MapManager.ICON_SHADOW_OFFSET_Y = -31;
        img.src = 'scca/html/images/thumb.gif';

        this.listingPositionShadow = core.createElm(img.parentNode,-1000,-1000,null,null,null,null,'img');
        this.listingPositionShadow.src = 'scca/html/images/thumb_shadow.gif';
        this.listingPositionShadow.zIndex=100;
        this.listingPositionShadow.setAlpha(65);
        if (sel)
        {
            sel.src = 'scca/html/images/thumb_selected.gif';
        }
    }
    else
    {
        MapManager.ICON_OFFSET_X = -9;
        MapManager.ICON_OFFSET_Y = -31;
        img.src = 'scca/html/images/thumb.png';
        if (sel)sel.src = 'scca/html/images/thumb_selected.png';
    }
};

MapManager.CheckMapBounds = function (){

	//alert( "left = " + parseInt(this.mapImage.style.left) );
	if( parseInt(this.mapImage.style.left) < 0 ) {
     	//make sure there is a neighbor to the right
     	if( this.neighbors[2] == 0 ) { //no neighbor
    		this.mapImage.style.left = "0px"; //set to full map width display
		}

	} else
	if( parseInt(this.mapImage.style.left) > 0 ) {
     	//make sure there is a neighbor to the left
     	if( this.neighbors[6] == 0 ) { //no neighbor
    		this.mapImage.style.left = "0px"; //set to full map width display
		}
 	}

	if( parseInt(this.mapImage.style.top) < 0 ) {
     	//make sure there is a neighbor to the bottom
     	if( this.neighbors[4] == 0 ) { //no neighbor
    		this.mapImage.style.top = "0px"; //set to full map height display
		}

	} else
	if( parseInt(this.mapImage.style.top) > 0 ) {
     	//make sure there is a neighbor to the left
     	if( this.neighbors[0] == 0 ) { //no neighbor
    		this.mapImage.style.top = "0px"; //set to full map height display
		}
	}
}

MapManager.NewMapFromOffset = function ( iX, iY ){

   	//alert("Map Offset (" + iX + "," + iY + ")" );
	oMapLocation = new MapLocation( iX, iY, this.mapInsetSize, this.zoomLevel );
	var response = core.xmlrpc.Call( this.mapServer, base.key, 'Map.GetMapByLocation', [oMapLocation]);

	if (response instanceof XMLRPCFault)
	{
		base._handleFault(response);
	}
	else
	{
       	//alert("Map ID = " + response.miId );
		this.map = response;
		this.mapImage.src = response.msImageFile;
       	this.parentImage = this.mapImage.src;
		//alert("yIndex =" + yIndex);
		this.mapIndex = ( this.mapScale * this.map.miHIndex ) + this.map.miVIndex;
		//alert("map index =" + this.mapIndex);
		this.parentId = response.miId;
		//this.GetMaps( this.parentId );

  		this.mapImage.style.left = (this.Map2Display( iX, parseInt(this.map.miOffsetX))) + "px";
 		this.mapImage.style.top = (this.Map2Display( iY, parseInt(this.map.miOffsetY))) + "px";

 		this.SetPanRange();
		this.UpdateTestVal();


		//this.GetListings( this.parentId );
		this.GetNeighbors( this.parentId )
 		//adjust for neighbors
 		this.CheckMapBounds();
		this.PositionNeighborImages();
   		this.MapAdjustments();
		this.UpdateListingPosition();
       	this.UpdateSelectorPosition();

	}

}

MapManager.PanMap = function ( iX, iY ){

	this.hasMoved 	= true;
	this.mapImage.style.left = (parseInt(this.mapImage.style.left) - iX) + "px";
	this.mapImage.style.top = (parseInt(this.mapImage.style.top) - iY) + "px";

	this.panHorz += iX;
	this.panVert += iY;

	this.PositionNeighborImages();
	this.UpdateListingPosition();
	this.UpdateSelectorPosition();

}

//--------------------------------------------------------------------
MapManager.UpdateMap = function( id ) {

	this.mapImage.style.left = "0px";
	this.mapImage.style.top = "0px";

	this.mapImage.src = this.map.msImageFile;
	this.mapIndex = ( this.mapScale * this.map.miHIndex ) + this.map.miVIndex;
	//alert("map index =" + this.mapIndex);
	this.parentId = this.map.miId;

   	this.parentImage = this.mapImage.src;

	//this.GetMaps( this.parentId );
	this.SetPanRange();

	this.PositionNeighborImages();
	//this.GetListings( this.parentId );
	this.GetNeighbors( this.parentId )
	this.MapAdjustments();
	this.UpdateListingPosition();
   	this.UpdateSelectorPosition();

	this.UpdateTestVal();
}

//--------------------------------------------------------------------
MapManager.GetMap = function( id ) {

   	var response = core.xmlrpc.Call( this.mapServer, base.key, 'Map.GetMap', [id]);

	if (response instanceof XMLRPCFault)
	{
		base._handleFault(response);
	}
	else
	{
		this.map = response;
	}
}

MapManager.OnMapInsetClick = function (evt){

	this.hasMoved 	= true;
    var oMapManager = (core.isIE)? this : MapManager;
    var iX = iY = 0;
    var xIndex = yIndex = 0;

    if(core.isIE)
        event.cancelBubble = true;
    else
        evt.stopPropagation();

    iX = (core.isIE)? event.offsetX : evt.layerX;
    iY = (core.isIE)? event.offsetY : evt.layerY;

	if( oMapManager.zoomLevel == 1 ) {
		iX = iX - ( iX % oMapManager.mapInsetSlice);
		iY = iY - ( iY % oMapManager.mapInsetSlice);
 		//determine the index
		xIndex = Math.round( oMapManager.mapScale * ( iX / oMapManager.mapInset.width ));
		yIndex = Math.round( oMapManager.mapScale * ( iY / oMapManager.mapInset.height ));
	} else {
   		iX = iX - ( iX % oMapManager.mapInsetSlice2);
   		iY = iY - ( iY % oMapManager.mapInsetSlice2);
		//determine the index
		xIndex = Math.round( oMapManager.mapScale2 * ( iX / oMapManager.mapInset.width ));
		yIndex = Math.round( oMapManager.mapScale2 * ( iY / oMapManager.mapInset.height ));
	}

 	var response = core.xmlrpc.Call( oMapManager.mapServer, base.key, 'Map.GetIndexedMap', [xIndex, yIndex, oMapManager.zoomLevel] );

	if (response instanceof XMLRPCFault)
	{
		base._handleFault(response);
	}
	else
	{
		oMapManager.map = response;
		oMapManager.UpdateMap();
	}
}

//--------------------------------------------------------------------
MapManager.UpdateSelectorPosition = function( ) {

	switch( this.zoomLevel ) {
		case 1:
            offsetX = (this.map.miHIndex * this.mapInsetSlice) + ( -parseInt(this.mapImage.style.left) * (this.mapInsetSlice/parseInt(this.mapImage.style.width)));
			offsetY = (this.map.miVIndex * this.mapInsetSlice) + ( -parseInt(this.mapImage.style.top) * (this.mapInsetSlice/parseInt(this.mapImage.style.height)));
            this.mapSelector.style.width = (this.mapInsetSlice + "px");
	        this.mapSelector.style.height = (this.mapInsetSlice + "px");
            break;
		case 2:
            offsetX = (this.map.miHIndex * (this.mapInsetSlice/this.mapScale)) + ( -parseInt(this.mapImage.style.left) * ((this.mapInsetSlice/this.mapScale)/parseInt(this.mapImage.style.width)));
			offsetY = (this.map.miVIndex * (this.mapInsetSlice/this.mapScale)) + ( -parseInt(this.mapImage.style.top) * ((this.mapInsetSlice/this.mapScale)/parseInt(this.mapImage.style.height)));
			this.mapSelector.style.width = '6px';//(this.mapInsetSlice2 + "px");
	        this.mapSelector.style.height = '6px';//(this.mapInsetSlice2 + "px");
			break;

	}
    if (isNaN(offsetX))offsetX=0;
    if (isNaN(offsetY))offsetY=0;

  	this.mapSelector.style.left = (this.mapInset.offsetLeft + offsetX) + "px";
 	this.mapSelector.style.top = (this.mapInset.offsetTop + offsetY) + "px";

	this.UpdateTestVal();
}

//--------------------------------------------------------------------
MapManager.BuildNeighbors = function( vals ) {

	//add bordering maps

	//set up a matrix for position determination

    this.neighborFactorLeft = new Array(8);
    this.neighborFactorTop = new Array(8);

	//NORTH
    this.neighborFactorLeft[0] = 0;
    this.neighborFactorTop[0] = -1;

	//NORTH EAST
    this.neighborFactorLeft[1] = 1;
    this.neighborFactorTop[1] = -1;

	//EAST
    this.neighborFactorLeft[2] = 1;
    this.neighborFactorTop[2] = 0;

	//SOUTH EAST
    this.neighborFactorLeft[3] = 1;
    this.neighborFactorTop[3] = 1;

	//SOUTH
    this.neighborFactorLeft[4] = 0;
    this.neighborFactorTop[4] = 1;

	//SOUTH WEST
    this.neighborFactorLeft[5] = -1;
    this.neighborFactorTop[5] = 1;

	//WEST
    this.neighborFactorLeft[6] = -1;
    this.neighborFactorTop[6] = 0;

	//NORTH WEST
    this.neighborFactorLeft[7] = -1;
    this.neighborFactorTop[7] = -1;


	this.mapNeighbor = new Array(8);
	//alert( baseTop );
	for( var i=0; i < 8 ; i++ ) {
		img = document.createElement("IMG");
		img.width= this.mapImage.width;
		img.height= this.mapImage.height;
		img.style.width = (img.width + "px");
		img.style.height = (img.height + "px");
		img.id = "Map" + i;
		img.style.position ="absolute";
		this.mapNeighbor[i] = img;
		this.mapDIV.appendChild( img );
	}

	this.PositionNeighborImages();

}
//--------------------------------------------------------------------
MapManager.PositionNeighborImages = function() {

    var baseTop = parseInt(core.css.getCurrentStyle(this.mapImage, "top"));
    var baseLeft = parseInt(core.css.getCurrentStyle(this.mapImage, "left"));
    var baseWidth = parseInt(core.css.getCurrentStyle(this.mapImage, "width"));
    var baseHeight = parseInt(core.css.getCurrentStyle(this.mapImage, "height"));

	for( var i=0; i < 8 ; i++ ) {
		img = this.mapNeighbor[i];

		img.style.left = (baseLeft + (this.neighborFactorLeft[i] * baseWidth)) + "px";
		img.style.top = (baseTop + (this.neighborFactorTop[i] * baseWidth)) + "px";
 		img.style.width = (baseWidth + "px");
		img.style.height = (baseHeight + "px");
	}
}


//--------------------------------------------------------------------
MapManager.UpdateNeighbors = function( vals ) {

	this.neighbors = vals;

	for( var i=0; i < this.neighbors.length; i++ ) {
		if( this.neighbors[i] != 0 ) {
			map = this.neighbors[i];
			this.mapNeighbor[i].src= map.msImageFile;
			//alert( "Map " + i + ": " + this.mapNeighbor[i].src + " H=" + map.miHIndex + " V=" + map.miVIndex );
			this.mapNeighbor[i].style.display="block";
		} else {
			this.mapNeighbor[i].src="";
			this.mapNeighbor[i].style.display="none";
		}
	}
}
//--------------------------------------------------------------------
MapManager.DisplayNeighbors = function( display ) {

	for( var i=0; i < this.neighbors.length; i++ ) {
		if( this.neighbors[i] != 0 ) {
			this.mapNeighbor[i].style.display=display;
		}
	}
}

//--------------------------------------------------------------------
MapManager.GetNeighbors = function( id ) {

	//alert( id );

	response = core.xmlrpc.Call( this.mapServer, base.key, 'Map.GetNeighbors', [id]);

	if (response instanceof XMLRPCFault)
	{
		base._handleFault(response);
	}
	else if (response)
	{
    	/*NEIGHBOR ARRAY
          	0 = NORTH
          	1 = NORTHEAST
          	2 = EAST
          	3 = SOUTHEAST
          	4 = SOUTH
          	5 = SOUNTHWEST
          	6 = WEST
          	7 = NORTHWEST
    	*/
		this.UpdateNeighbors(response);

	}


}

//--------------------------------------------------------------------
MapManager.MoveToNeighbor = function( horz, vert ){

	this.scrolling = false;

    if(core.isIE)
    {
    	if( window.event != null ) {
        	window.event.returnValue = false;
    	}
    }
	zoomAmountH = -this.zoomAmount * this.zoomStep;
	zoomAmountV = -this.zoomAmount * this.zoomStep;
	var iMap = -1;
	switch( vert ) {
		case -1: //north
			switch( horz ) {
				case -1: //north west
					iMap = 7;
					break;
				case 0: //north
					iMap = 0;
					break;
				case 1: //north east
					iMap = 1;
					break;

			}
			break;
		case  0: //flat
			switch( horz ) {
				case -1: //west
                	iMap = 6;
					break;
				case 0: //no change
					return;
				case 1: //east
                	iMap = 2;
  				break;

			}
			break;
		case  1: //south
			switch( horz ) {
				case -1: //south west
                	iMap = 5;
					break;
				case 0: //south
                	iMap = 4;
					break;
				case 1: //south east
                	iMap = 3;
					break;

			}
			break;
		default:
			return;
	}
	if(( iMap < 0 ) || ( this.neighbors[iMap] == 0 )) {
     	return;
	}
	this.map = this.neighbors[iMap];

	this.mapImage.src = this.map.msImageFile;
   	this.parentImage = this.mapImage.src;
    //determine the offset from center
    iVOffset = 0;
    iHOffset = 0;
	if( vert == 0 ) { //might be a vertical offset
		iVOffset = (this.zoomAmount * this.zoomStep) + parseInt(this.mapImage.style.top);
	} else
	if( horz == 0 ) { //might be a horizontal offset
		iHOffset = (this.zoomAmount * this.zoomStep) + parseInt(this.mapImage.style.left);
	}

	this.mapImage.style.top = (zoomAmountV + iVOffset) + "px";
	this.mapImage.style.posLeft = (zoomAmountH + iHOffset) + "px";

	this.SetPanRange();
	this.PositionNeighborImages();
	this.GetNeighbors( this.map.miId );
	this.MapAdjustments();
	this.UpdateListingPosition();
}

//--------------------------------------------------------------------
MapManager.Scroll = function( scroll ){

	this.hasMoved 	= true;

    if(core.isIE)
    {
    	if( window.event != null ) {
        	window.event.cancelBubble = true;
    	}
    }

	//alert(" selectorZoomAmount = " +selectorZoomAmount);
	if( this.scrolling ) {
		window.setTimeout( "MapManager.Scroll('" + scroll + "');" , 100 );
	}

	this.panActive = true;
	this.scrollCount++;

	if( this.scrollCount > 10 ) {
     	this.scrollAmount = 5 + ( this.scrollCount / 10 );
	}

	scrollAmountH 	= 0;
	scrollAmountV 	= 0;

	switch( scroll ) {
		case "right":

			if( this.panHorz < this.maxPanHorz ) {
				_scroll=true;

				if( this.panHorz + this.scrollAmount > this.maxPanHorz ) {
					scrollAmountH = this.maxPanHorz - this.panHorz;
				} else {
					scrollAmountH = this.scrollAmount;
				}

                this.mapImage.style.left = (parseInt(this.mapImage.style.left) - scrollAmountH) + "px";

				this.panHorz += scrollAmountH;

			} else {
             	this.MoveToNeighbor( 1, 0 );
             	return;
			}
			break;
		case "left":
			if( this.panHorz > this.minPanHorz ) {

				if( this.panHorz - this.scrollAmount < this.minPanHorz ) {
					scrollAmountH = this.panHorz - this.minPanHorz;
				} else {
					scrollAmountH = this.scrollAmount;
				}
                this.mapImage.style.left = (parseInt(this.mapImage.style.left) + scrollAmountH) + "px";
				//scrollAmountH.test = scrollAmountH.test2.test3;
				this.panHorz -= scrollAmountH;
				//alert( "scrollAmountH="+scrollAmountH + ", this.panHorz="+this.panHorz );


			} else {
             	this.MoveToNeighbor( -1, 0 );
             	return;
			}
			break
		case "down":
			if( this.panVert < this.maxPanVert ) {

				if( this.panVert + this.scrollAmount > this.maxPanVert ) {
					scrollAmountV = this.maxPanVert - this.panVert;
				} else {
					scrollAmountV = this.scrollAmount;
				}

                this.mapImage.style.top = (parseInt(this.mapImage.style.top) - scrollAmountV) + "px";

                scrollAmountV = - this.scrollAmount;
				this.panVert += this.scrollAmount;

			} else {
             	this.MoveToNeighbor( 0, 1 );
             	return;
			}
			break;
		case "up":
			if( this.panVert > this.minPanVert ) {

				if( this.panVert - this.scrollAmount < this.minPanVert ) {
					scrollAmountV = this.panVert - this.minPanVert;
				} else {
					scrollAmountV = this.scrollAmount;
				}

                this.mapImage.style.top = (parseInt(this.mapImage.style.top) + scrollAmountV) + "px";

				this.panVert -= scrollAmountV;


			} else {
             	this.MoveToNeighbor( 0, -1 );
             	return;
			}
			break
	}
	//alert( "scrollAmountH="+scrollAmountH + " - scrollAmountV="+scrollAmountV );

	this.PositionNeighborImages();
	this.UpdateListingPosition();
	this.UpdateSelectorPosition();

}

//--------------------------------------------------------------------
MapManager.HideInset = function() {
	this.showInset = !this.showInset;
	if( this.showInset ) {
		this.mapInset.style.display= "inline";
		this.mapSelector.style.display= "inline";
		this.insetLink.innerText = "Hide Inset"
	} else {
		this.mapInset.style.display= "none";
		this.mapSelector.style.display= "none";
		this.insetLink.innerText = "Show Inset"
	}
	//alert(this.insetLink.innerText );

}

//--------------------------------------------------------------------
MapManager.Display2Map = function( val, offset ) {

	zoomRatio = this.zoomSlice[this.zoomLevel]/this.fixedSize;
	r =  val/this.fixedSize;
	//alert( r );
	offset += this.zoomSlice[this.zoomLevel]/2;
	//alert( offset );
	j=r*this.zoomSlice[this.zoomLevel];
	//alert( j );
	mapPoint = offset - j;
	return mapPoint;
}

//--------------------------------------------------------------------
MapManager.Map2Display = function( mapPoint, offset ) {
	//alert( "mapPoint = " + mapPoint );
	offset += this.zoomSlice[this.zoomLevel]/2;
	//alert( "offset = " + offset );
	zoomInRatio = this.fixedSize/this.zoomSlice[this.zoomLevel];
	//alert( zoomInRatio );
	displayPoint = (offset - mapPoint)*zoomInRatio;
	//alert( "displayPoint = " + displayPoint );
	return displayPoint;
}

//--------------------------------------------------------------------
MapManager.OnZoomIn = function() {

	if( this.zoomLevel < 2 ) {    //max 10 zoom steps, maybe should be less
		mapPointLeft = this.Display2Map( parseInt(this.mapImage.style.left), parseInt(this.map.miOffsetX));
		mapPointTop = this.Display2Map( parseInt(this.mapImage.style.top), parseInt(this.map.miOffsetY));
 		this.zoomLevel++;
 		this.zoomOut.style.display = "inline";
 		this.zoomIn.style.display = "none";

		this.mapSelector.style.width = (this.mapInsetSlice/this.mapScale) + "px";
		this.mapSelector.style.height = (this.mapInsetSlice/this.mapScale) + "px";

 		this.NewMapFromOffset(mapPointLeft, mapPointTop )
	}
}

//--------------------------------------------------------------------
MapManager.OnZoomOut = function() {

	if( this.zoomLevel > 1 ) { //can only zoom out from zoom2
		mapPointLeft = this.Display2Map( parseInt(this.mapImage.style.left), parseInt(this.map.miOffsetX));
		mapPointTop = this.Display2Map( parseInt(this.mapImage.style.top), parseInt(this.map.miOffsetY));
		this.zoomLevel--;
 		this.zoomOut.style.display = "none";
 		this.zoomIn.style.display = "inline";
 		this.NewMapFromOffset(mapPointLeft, mapPointTop )
		this.mapSelector.style.width = (this.mapInsetSlice + "px");
		this.mapSelector.style.height = (this.mapInsetSlice + "px");
 	}
}
//--------------------------------------------------------------------
MapManager.SetPanRange = function() {

	this.panHorz = -parseInt( this.mapImage.style.left);
    if(!core.isIE) this.panHorz += 1;
	this.panVert = -parseInt( this.mapImage.style.top);
	sizeHigh = this.fixedSize;
	sizeLow   = 0;
	if( this.map.miHIndex > 0 ) {
		this.minPanHorz = -sizeHigh;
	} else {
		this.minPanHorz = -sizeLow;
	}
	if( this.map.miVIndex > 0 ) {
		this.minPanVert = -sizeHigh;
	} else {
		this.minPanVert = -sizeLow;
	}
	if( this.map.miHIndex < Math.pow(this.mapScale, this.zoomLevel ) - 1 ) {
		this.maxPanHorz = sizeHigh;
	} else {
		this.maxPanHorz = sizeLow;
	}
	if( this.map.miVIndex < Math.pow(this.mapScale, this.zoomLevel ) - 1 ) {
		this.maxPanVert = sizeHigh;
	} else {
		this.maxPanVert = sizeLow;
	}

}


//--------------------------------------------------------------------
MapManager.OnScrollLeft = function(evt) {
    core.preventBubble(evt);
    this.scrolling = true;
    this.Scroll( "left" );
}
//--------------------------------------------------------------------
MapManager.OnScrollRight = function(evt) {
    core.preventBubble(evt);
	this.scrolling = true;
	this.Scroll( "right" );
}
//--------------------------------------------------------------------
MapManager.OnScrollUp = function(evt) {
    core.preventBubble(evt);
    this.scrolling = true;
	this.Scroll( "up" );
}
//--------------------------------------------------------------------
MapManager.OnScrollDown = function(evt) {
    core.preventBubble(evt);
    this.scrolling = true;
	this.Scroll( "down" );
}
//--------------------------------------------------------------------
MapManager.EndScroll = function(evt) {
    core.preventBubble(evt);
    this.scrolling = false;
	this.scrollAmount = 5;
	this.scrollCount = 0;
}
//--------------------------------------------------------------------
MapManager.CenterLocation = function( offsetX, offsetY ) {

   	if( offsetX < this.zoomAmount ) {
   		//save the existing scrollAmount
   		save = this.scrollAmount;
   		//try to center the listing position
   		this.scrollAmount = this.zoomAmount - offsetX;
   		//make sure to include the amount already scrolled from any previous centering
   		this.scrollAmount += this.panHorz;
     	this.Scroll( "left" );
     	//reset the scroll amount
     	this.scrollAmount = save;
	} else
   	if( offsetX > this.zoomAmount ) {
   		//save the existing scrollAmount
   		save = this.scrollAmount;
   		//try to center the listing position
   		this.scrollAmount = offsetX - this.zoomAmount;
   		//make sure to include the amount already scrolled from any previous centering
   		this.scrollAmount -= this.panHorz;
     	this.Scroll( "right" );
     	//reset the scroll amount
     	this.scrollAmount = save;
	}
   	if( offsetY < this.zoomAmount ) {
   		//save the eyisting scrollAmount
   		save = this.scrollAmount;
   		//try to center the listing position
   		this.scrollAmount = this.zoomAmount - offsetY;
   		//make sure to include the amount already scrolled from any previous centering
   		this.scrollAmount += this.panVert;
     	this.Scroll( "up" );
     	//reset the scroll amount
     	this.scrollAmount = save;
	} else
   	if( offsetY > this.zoomAmount ) {
   		//save the eyisting scrollAmount
   		save = this.scrollAmount;
   		//try to center the listing position
   		this.scrollAmount = offsetY - this.zoomAmount;
   		//make sure to include the amount already scrolled from any previous centering
   		this.scrollAmount -= this.panVert;
     	this.Scroll( "down" );
     	//reset the scroll amount
     	this.scrollAmount = save;
	}
}