/**
 *  mod_map_tab.js:
 *
 *  Last Checked In By $Author: sasman $
 *  $Date: 2005/03/17 22:18:28 $
 *  $Revision: 1.5 $
 *
 *  (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
 *
 *  REVISION HISTORY
 *
 *  $Log: mod_map_tab.js,v $
 *  Revision 1.5  2005/03/17 22:18:28  sasman
 *  *** empty log message ***
 *
 *  Revision 1.4  2005/01/08 19:41:21  larryg
 *  Bug Fixes
 *
 *  Revision 1.3  2004/12/15 14:09:03  larryg
 *  *** empty log message ***
 *
 *  Revision 1.2  2004/12/03 11:47:32  larryg
 *  *** empty log message ***
 *
 *  Revision 1.1  2004/10/27 20:19:24  larryg
 *  *** empty log message ***
 *
 *  Revision 1.10  2004/10/16 16:44:42  larryg
 *  *** empty log message ***
 *
 *  Revision 1.9  2004/10/07 08:28:02  larryg
 *  *** empty log message ***
 *
 *  Revision 1.8  2004/09/30 08:43:55  larryg
 *  *** empty log message ***
 *
 *  Revision 1.7  2004/09/30 00:11:53  larryg
 *  *** empty log message ***
 *
 *  Revision 1.6  2004/09/25 16:53:07  larryg
 *  *** empty log message ***
 *
 *  Revision 1.5  2004/09/23 16:54:55  larryg
 *  *** empty log message ***
 *
 *  Revision 1.4  2004/09/22 01:55:38  larryg
 *  *** empty log message ***
 *
 *  Revision 1.3  2004/09/21 14:26:26  larryg
 *  *** empty log message ***
 *
 *  Revision 1.2  2004/09/21 10:45:08  larryg
 *  *** empty log message ***
 *
 *  Revision 1.1  2004/09/15 00:09:16  larryg
 *  *** empty log message ***
 *
 *  Revision 1.2  2004/09/03 11:14:38  larryg
 *  *** empty log message ***
 *
 *  Revision 1.1  2004/08/27 04:06:28  larryg
 *  *** empty log message ***
 *
 *  Revision 1.4  2004/08/24 04:56:31  larryg
 *  *** empty log message ***
 *
 *  Revision 1.3  2004/08/23 09:15:32  larryg
 *  *** empty log message ***
 *
 *  Revision 1.2  2004/08/22 04:54:57  larryg
 *  *** empty log message ***
 *
 *  Revision 1.1  2004/08/19 19:05:40  larryg
 *  *** empty log message ***
 *
 *
 */

////////////////////////////////////////////////////////////////////////////////////////



/************************************************************************

	MAP MANAGER OBJECT EXTENSIONS - see mod_map_common.js for base implementation

************************************************************************/

//--------------------------------------------------------------------
MapManager.Init = function( obj )
{
	this.CommonInit( obj);
	this.objId = obj.pageId;
	this.test = document.getElementById( "Test");

	this.listingPosition = document.getElementById( "ListingPosition" );
	this.showListing = true;

	longSliceHidden = document.getElementById( "LongSlice" );
	this.longSlice = new Array( 3 );
	this.longSlice[1] = longSliceHidden.value;
	this.longSlice[2] = this.longSlice[1]/this.mapScale;

	latSliceHidden = document.getElementById( "LatSlice" );
	this.latSlice = new Array( 3 );
	this.latSlice[1] = latSliceHidden.value;
	this.latSlice[2] = this.latSlice[1]/this.mapScale;

	this.mapTD = document.getElementById( "MapTD" );
	this.mapTD.style.left= "50px";
	this.zoomOut.style.display = "inline";
	this.zoomIn.style.display = "none";

	this.BuildNeighbors();

	if( HomePageManager.view == "SCPage" ) {
		this.GetCroftMapInfo();
	} else {
		this.GetListingMapInfo();
	}

	//alert( "Map = " + this.mapId );
	this.GetMap( this.mapId );
	this.UpdateMap( this.mapId);

	//alert( "value = " + (this.fixedSize/2) - this.offsetY[this.zoomLevel] );
	/*this.PanMap(
				this.offsetX[this.zoomLevel] - (this.fixedSize/2),
				this.offsetY[this.zoomLevel] - (this.fixedSize/2)
				);
  */
	//reset centering functionality
	this.hasMoved 	= false;
/*
	this.customerMapTable = document.getElementById( "CustomerMapTable" );
    this.customerMapTable.style.display = "inline";
*/

};

//--------------------------------------------------------------------
MapManager.GetListingMapInfo = function( ) {


	this.zoomMapId = new Array(3);
	this.offsetX = new Array(3);
	this.offsetY = new Array(3);

	response = core.xmlrpc.Call( this.listingServer, base.key, 'Listing.GetListingPosition', [this.objId]);

	if (response instanceof XMLRPCFault)
	{
		base._handleFault(response);
	}
	else
	{

		this.position = response;
		this.zoomMapId[1] = parseInt(this.position.miZoom1Id);
		this.zoomMapId[2] = parseInt(this.position.miZoom2Id);

		this.offsetX[1] = parseInt(this.position.miZoom1X);
		this.offsetY[1] = parseInt(this.position.miZoom1Y);
		this.offsetX[2] = parseInt(this.position.miZoom2X);
		this.offsetY[2] = parseInt(this.position.miZoom2Y);

	//HARD-WIRED for Testing!!!!!!!!!!!!!
	/*
		this.zoomMapId[1] = 245297;
		this.zoomMapId[2] = 245301;

		this.offsetX[1] = 146;
		this.offsetY[1] = 119;
		this.offsetX[2] = 80;
		this.offsetY[2] = 357;
	*/
		this.zoomLevel = 2;
		this.mapSelector.style.width = this.mapInsetSlice/this.mapScale;
		this.mapSelector.style.height = this.mapInsetSlice/this.mapScale;

		this.mapId = this.zoomMapId[ this.zoomLevel ];
	}
}

//--------------------------------------------------------------------
MapManager.GetCroftMapInfo = function( ) {


	this.zoomMapId = new Array(3);
	this.offsetX = new Array(3);
	this.offsetY = new Array(3);

	response = core.xmlrpc.Call( this.mapServer, base.key, 'Map.GetCroftMapLocation', [this.objId]);

	if (response instanceof XMLRPCFault)
	{
		base._handleFault(response);
	}
	else
	{

		this.position = response;
		this.zoomMapId[1] = parseInt(this.position.miZoom1Id);
		this.zoomMapId[2] = parseInt(this.position.miZoom2Id);

		this.offsetX[1] = parseInt(this.position.miZoom1X);
		this.offsetY[1] = parseInt(this.position.miZoom1Y);
		this.offsetX[2] = parseInt(this.position.miZoom2X);
		this.offsetY[2] = parseInt(this.position.miZoom2Y);

		this.zoomLevel = 2;
		this.mapSelector.style.width = this.mapInsetSlice/this.mapScale;
		this.mapSelector.style.height = this.mapInsetSlice/this.mapScale;

		this.mapId = this.zoomMapId[ this.zoomLevel ];
	}
}

//--------------------------------------------------------------------
MapManager.UpdateTestVal = function( ) {

	//this.test.value= ( "L=" + this.mapImage.style.posLeft + ", T=" +this.mapImage.style.posTop + ", PH=" + this.panHorz );
    this.test.value= this.listingPosition.style.left

}

MapManager.AdjustListing = function( ) {

	this.mapId = this.zoomMapId[ this.zoomLevel ];
	this.listingPosition.style.display="none";
	this.showListing = false;

	//adjust offsets if it is not on this map
	if( this.zoomLevel == 1 ) {
		//alert("Adjust Listings: check zoom 1 id = " + aListing.miZoom1Id + " map id = " + this.map.miId );
		if( this.zoomMapId[ 1 ] == parseInt(this.map.miId) ){
			//alert("matched zoom map 1");
			this.showListing = true;
			this.offsetX[1] = parseInt(this.position.miZoom1X);
			this.offsetY[1] = parseInt(this.position.miZoom1Y);
			//alert("matched zoom1 map " + iListing + " setting offset to (" + aListing.miOffsetX + "," + aListing.miOffsetY + ")" );
		} else {
         	//check the neighbors
			for( i=0; i < this.neighbors.length; i++ ) {
				if( this.neighbors[i] != 0 ) {
					map = this.neighbors[i];
					//alert("Adjust Listings: check zoom 1 id = " + aListing.miZoom1Id + " neighbor id = " + parseInt( map.miId ) );
					if( parseInt( map.miId ) == this.zoomMapId[ 1 ] ) {
						//Adjust the offsets
						this.showListing = true;
						this.offsetX[1] = parseInt(this.position.miZoom1X) + this.neighborFactorLeft[i] * this.fixedSize;
						this.offsetY[1] = parseInt(this.position.miZoom1Y) + this.neighborFactorTop[i] * this.fixedSize;
						break;
					}
				}
			}

		}
	} else
	if( this.zoomLevel == 2 ) {
		//alert( "this.map.miId = " + this.map.miId + "this.zoomMapId[ 2 ] = " + this.zoomMapId[ 2 ] );
		if( this.zoomMapId[ 2 ] == parseInt(this.map.miId) ){
			//alert("matched zoom map 2");
			this.showListing = true;
			this.offsetX[2] = parseInt(this.position.miZoom2X);
			this.offsetY[2] = parseInt(this.position.miZoom2Y);
			//alert("matched zoom2 map  setting offset to (" + this.offsetX[2] + "," + this.offsetY[2] + ")" );
		} else {
         	//check the neighbors
			for( i=0; i < this.neighbors.length; i++ ) {
				if( this.neighbors[i] != 0 ) {
					map = this.neighbors[i];
					//alert("Adjust Listings: check zoom 2 id = " + this.zoomMapId[ 2 ] + " neighbor id = " + parseInt( map.miId ) );
					if( parseInt( map.miId ) == this.zoomMapId[ 2 ] ) {
						//Adjust the offsets
						this.showListing = true;
						this.offsetX[2] = parseInt(this.position.miZoom2X) + this.neighborFactorLeft[i] * this.fixedSize;
						this.offsetY[2] = parseInt(this.position.miZoom2Y) + this.neighborFactorTop[i] * this.fixedSize;
						//alert("adjusted zoom2 map  setting offset to (" + this.offsetX[2] + "," + this.offsetY[2] + ")" );
            			break;
					}
				}
			}

		}
	}
}

//--------------------------------------------------------------------
MapManager.MapAdjustments = function() {

	//empty stub required for common inheritance
	this.AdjustListing();
	//if we haven't moved anywhere keep centering the customer location
	if( !this.hasMoved ) {
		//alert("Center Location: "+ this.offsetX[this.zoomLevel] +", " + this.offsetY[this.zoomLevel] );
		this.CenterLocation( this.offsetX[this.zoomLevel], this.offsetY[this.zoomLevel] );
		//reset the moved flag
		this.hasMoved = false;
	}

}

//--------------------------------------------------------------------
MapManager.OnMapClick = function( ){

	//alert("Map Clicked" );

}
//--------------------------------------------------------------------
MapManager.OnInsetSelectorClick = function(evt){
    core.preventBubble(evt);
}

//--------------------------------------------------------------------
MapManager.UpdateListingPosition = function( ) {

	//alert( this.panHorz );
 	if( this.showListing == false ) {
		this.listingPosition.style.display="none";
	} else {
	   	offsetX = this.mapImage.offsetLeft + ( this.offsetX[ this.zoomLevel]);// - this.panHorz );
	   	offsetY = this.mapImage.offsetTop + ( this.offsetY[ this.zoomLevel]);// - this.panVert ) ;

		//alert( "x=" + offsetX + ", y = " + offsetY );
	 	this.listingPosition.style.left = (offsetX + this.ICON_OFFSET_X)+"px";
		this.listingPosition.style.top =  (offsetY + this.ICON_OFFSET_Y)+"px";
    	if (this.listingPositionShadow)
        {
            this.listingPositionShadow.style.left = (offsetX + this.ICON_SHADOW_OFFSET_X)+"px";
    	    this.listingPositionShadow.style.top =  (offsetY + this.ICON_SHADOW_OFFSET_Y)+"px";
        }
        this.listingPosition.style.display="block";
	}
}


//--------------------------------------------------------------------
MapManager.OnMapDblClick = function( ){

	//alert("Map Double Clicked" );

}