//
// MapControl.js
//
// enables panning and zooming for an image HTML tag

function MapControl( mapImageId, aMapArray )
{
	// -- constructor for MapControl object
	// -- declare properties --

	// id tag identifier for <img> tag representing the map window
	this.imageTagId = mapImageId;
	// array of map objects registered with self
	this.maps = aMapArray;
	// array of Strings for zoom scales
	this.zoomScales = new Array();
	// blank image used in zoom control
	this.blankImg = new Image();
	// pointer image used in zoom control
	this.pointerImg = new Image();
	// current map object
	this.currentMap = null;

	function setCurrentMap( aMap )
	{
		this.currentMap = aMap;
	}

	function getCurrentMap()
	{
		return this.currentMap;
	}

	function setMap( aMapName )
	{
		// unhighlight map if necessary
		var aMapHighlight = document.getElementById( "mapHighlight" );

		if( aMapHighlight != null )
			aMapHighlight.style.visibility = "hidden";

		// updates map window with aMapName

//alert (aMapName)

		var aMap = this.getMap( aMapName );

		var myMapImage = document.getElementById( this.imageTagId );


		myMapImage.src = aMap.getImage();


//alert (aMap.getImageMap());

		myMapImage.useMap = aMap.getImageMap();

		//fd change, 5/22
		aMapHighlight.useMap = aMap.getImageMap();

		if( this.getCurrentMap() != null )
		{
			this.updateDecorations( this.getCurrentMap(), "hidden" );
		}

		this.updateDecorations( aMap, "visible" );
		this.updateZoomControl( aMap.getZoomScale() );
		this.setCurrentMap( aMap );



	}

	function getMap( aMapName )
	{
	  var myMaps = this.getMaps();
	  for( var i=0; i<myMaps.length; i++ )
	  {
		var aMap = myMaps[i];
		if( aMapName == aMap.getMapName() )
		{
//ctw mod
			//alert(aMapName);
			return aMap;
		}
	  }
	  return null;
	}

	function updateDecorations( aMap, visibleType )
	{
		var mapDecorations = aMap.getDecorations();
		for( var i=0; i<mapDecorations.length; i++ )
		{
			var anElement = document.getElementById( mapDecorations[i] );
			anElement.style.visibility = visibleType;
		}
	}

	function zoomOut( aZoomScale )
	{
		var aMap = this.getCurrentMap();
		var myZoomScales = this.getZoomScales();
		var currentScale = -1;
		var gotoScale = -1;

		if( aMap == null )
			return;

		for( var i=0; i<myZoomScales.length; i++ )
		{
			 if( myZoomScales[i] == aZoomScale )
				gotoScale = i;

			 if( myZoomScales[i] == aMap.getZoomScale() )
				currentScale = i;
		}

		if( gotoScale == currentScale - 2 )
		{
			this.setMap("overview");
		}
		else if( gotoScale < currentScale )
		{
			pMap = this.getParentMap( aZoomScale, aMap );
			this.setMap ( pMap.getMapName() );
		}
	}

	function getParentMap( aZoomScale, aMap )
	{
		var aTest = false;

		do
		{
			var aMapName = aMap.getParentMap();
			var bMap = this.getMap( aMapName );

			if( aZoomScale == 'up' || ( aZoomScale == bMap.getZoomScale() ) )
			{
				aTest = true;
			}
			return bMap;

		} while( !aTest );
	}

	function setMaps( aMapArray)
	{
		this.maps = aMapArray;
	}

	function getMaps()
	{
		return this.maps;
	}

	function setZoomScales( zoomScaleArray )
	{
		this.zoomScales = zoomScaleArray;
	}

	function getZoomScales()
	{
		return this.zoomScales;
	}

	function setZoomScale( aZoomScale )
	{
	//	var aZoom = document.getElementById( aZoomScale );
	}

	function getBlankImg()
	{
		return this.blankImg;
	}

	function setBlankImg( anImage )
	{
		this.blankImg = anImage;
	}

	function getPointImg()
	{
		return this.pointerImg;
	}

	function setPointImg( anImage )
	{
		this.pointerImg = anImage;
	}

	function updateZoomControl( aZoomScale )
	{
	  // updates zoom control in

	  var myScales = this.getZoomScales();
	  var currentScaleIdx = -1;
	  var aScale = "";

	  for( var i=0; i<myScales.length; i++ )
	  {
		aScale = myScales[i];
		if( aScale == aZoomScale )
		{
		  currentScaleIdx = i;
		}
	  }

	  // fix other view scales
      for( var i=0; i<myScales.length; i++ )
	  {
	    aScale = myScales[i];
	    var imgScale = document.getElementById( "zoomImg" + aScale );
	    var txtScale = document.getElementById( "zoomTxt" + aScale );

        if( aScale != aZoomScale )
	    {
	      imgScale.src = this.getBlankImg().src;

	      if( currentScaleIdx < i )
	      {
	        txtScale.className = "zoomControl";

	      } else {
	        txtScale.className = "zoomControlActive";
          }

	    } else {

	      imgScale.src = this.getPointImg().src;
	      txtScale.className = "zoomControl";
	    }
	  }

	}

	// -- assign methods to self
	this.getParentMap = getParentMap;
	this.zoomOut = zoomOut;
	this.updateDecorations = updateDecorations;
	this.setCurrentMap = setCurrentMap;
	this.getCurrentMap = getCurrentMap;
	this.setMap = setMap;
	this.getMap = getMap;
	this.setMaps = setMaps;
	this.getMaps = getMaps;
	this.getPointImg = getPointImg;
	this.setPointImg = setPointImg;
	this.getBlankImg = getBlankImg;
	this.setBlankImg = setBlankImg;
	this.updateZoomControl = updateZoomControl;
	this.setZoomScale = setZoomScale;
	this.getZoomScales = getZoomScales;
	this.setZoomScales = setZoomScales;

	// ----------------------------------
	//var test = "MapControl";
	//alert(test);
	//alert(updateZoomControl);

}