/*****************************************************************************
 *
 * File     : MapPanel.js
 * 
 * Project  : WMS Viewer Client
 * 
 * Contents : 
 *
 * Author: Milan Trninic
 *
 * Copyright 1999-2005 Galdos Systems, Inc.
 * All rights reserved.
 * 
 ***|***************************|***********************|*******************|*/

/************************************************************************
*
*	Global declarations
*
*
*************************************************************************/

/************************************************************************
*
*	function:	MapPanel
*
*	purpose:	Constructor
*
*************************************************************************/
function MapPanel()
{
	// superclass
	var uiElement = document.getElementById("mapPanel");
	this._superClass = StackedPanel;
	this._superClass(uiElement);

	// method assignments
	this.init = MapPanel.init;
	this.setSize = MapPanel.setSize;
	this.setToolbarEnabled = MapPanel.setToolbarEnabled;
	this.isToolbarEnabled = MapPanel.isToolbarEnabled;
	this.initiateGetRequest = MapPanel.initiateGetRequest;
	this.getMapSize = MapPanel.getMapSize;
	this.onResultReceived = MapPanel.onResultReceived;
	this.activateControl = MapPanel.activateControl;
	this.activateControlAdapter = MapPanel.activateControlAdapter;
	this.zoomToFullExtent = MapPanel.zoomToFullExtent;
	this.zoomToFullExtentAdapter = MapPanel.zoomToFullExtentAdapter;
	this.getResultType = MapPanel.getResultType;
	this.showLayer = MapPanel.showLayer;
	
	/*
	this.getMapViewUI = MapPanel.getMapViewUI;
*/
	// instance field declarations
	this._mapPanelUI;
	this._toolbar;
	this._mapView;

	// initialization
	this.init();
}

/************************************************************************
*
*	function:	init
*
*	purpose:	initializes the globals
*
*************************************************************************/
function MapPanel.init()
{
	this._mapPanelUI = document.getElementById("mapPanel");;
	this._mapView = new MapView();

	var toolbarUI = document.getElementById("mapControlToolbar");
	this._toolbar = new Toolbar(toolbarUI);
	
	var selectButton = this._toolbar.getButton("selectButton");
	var zoomInButton = this._toolbar.getButton("zoomInButton");
	var zoomOutButton = this._toolbar.getButton("zoomOutButton");
	var panButton = this._toolbar.getButton("panButton");
	var fullExtentButton = this._toolbar.getButton("fullExtentButton");
	
	this._mapPanelUI.setAttribute("dataTransfer", this);
	selectButton.attachEvent("onclick", MapPanel.activateControlAdapter);
	zoomInButton.attachEvent("onclick", MapPanel.activateControlAdapter);
	zoomOutButton.attachEvent("onclick", MapPanel.activateControlAdapter);
	panButton.attachEvent("onclick", MapPanel.activateControlAdapter);
	fullExtentButton.attachEvent("onclick", MapPanel.zoomToFullExtentAdapter);

	selectButton.setDisabledImagePath("images/cursorArrowDisabled.gif");
	//selectButton.setSelectedImagePath("images/cursorArrowActive.gif");
	zoomInButton.setDisabledImagePath("images/zoomInDisabled.gif");
	//zoomInButton.setSelectedImagePath("images/zoomInActive.gif");
	zoomOutButton.setDisabledImagePath("images/zoomOutDisabled.gif");
	//zoomOutButton.setSelectedImagePath("images/zoomOutActive.gif");
	panButton.setDisabledImagePath("images/handDisabled.gif");
	//panButton.setSelectedImagePath("images/handActive.gif");
	fullExtentButton.setDisabledImagePath("images/fullExtentDisabled.gif");
	//fullExtentButton.setSelectedImagePath("images/fullExtentActive.gif");
	
	selectButton.setSelected(true);
	this._toolbar.setEnabled(false);
}
	
/************************************************************************
*
*	function:	setSize
*
*	purpose:	
*
*************************************************************************/
function MapPanel.setSize(width, height)
{
	// For explanation of the purpose of the temp variable, see the documentation
	this.temp = this._superClass.setSize;
	this.temp(width, height);
	this._mapView.setSize(width, width);
}

/************************************************************************
*
*	function:	getMapViewUI
*
*	purpose:	gets the map view UI (iframe)
*
*************************************************************************/
function MapPanel.getMapViewUI()
{
	return this._mapView.getMapViewUI();
}
	
/************************************************************************
*
*	function:	initiateGetRequest
*
*	purpose:	initiates the GET request on map view
*
*************************************************************************/
function MapPanel.initiateGetRequest(request)
{
	return this._mapView.initiateGetRequest(request);
}
	
/************************************************************************
*
*	function:	getMapSize
*
*	purpose:	
*
*************************************************************************/
function MapPanel.getMapSize()
{
	return this._mapView.getSize();
}

/************************************************************************
*
*	function:	onResultReceived
*
*	purpose:	Called when the result is received
*
*************************************************************************/
function MapPanel.onResultReceived(boundingBox, mapSize)
{
	this._mapView.onResultReceived();
	var resultType = this._mapView.getResultType();
	if (resultType != "image") this._toolbar.setEnabled(false);
	else 
	{
		this._toolbar.setEnabled(true);
		this.activateControl();
		this._mapView.setParameters(boundingBox, mapSize);
	}
}

/************************************************************************
*
*	function:	setToolbarEnabled
*
*	purpose:	
*
*************************************************************************/
function MapPanel.setToolbarEnabled(enable)
{
	if (enable != null) 
	{
		this._toolbar.setEnabled(enable);
		return;
	}
	
	var resultType = this._mapView.getResultType();
	if (resultType != "image") this._toolbar.setEnabled(false);
	else this._toolbar.setEnabled(true);
}

/************************************************************************
*
*	function:	isToolbarEnabled
*
*	purpose:	
*
*************************************************************************/
function MapPanel.isToolbarEnabled()
{
	return this._toolbar.isEnabled();
}

/************************************************************************
*
*	function:	activateControl
*
*	purpose:	
*
*************************************************************************/
function MapPanel.activateControl(action)
{
	if (this._mapView.getResultType() == "unknown") return; 
	if (action != null)
	{
		this._mapView.activateControl(action);
		return;
	}
	var button = this._toolbar.getSelectedButton();
	var id = button.getId();
	if (id == "selectButton") action = "select";
	else if (id == "zoomInButton") action = "zoomIn";
	else if (id == "zoomOutButton") action = "zoomOut";
	else action = "pan";
	this._mapView.activateControl(action);
}

/************************************************************************
*
*	function:	zoomToFullExtent
*
*	purpose:	
*
*************************************************************************/
function MapPanel.zoomToFullExtent(action)
{
	zoomToFullExtent();
}

/************************************************************************
*
*	function:	getResultType
*
*	purpose:	
*
*************************************************************************/
function MapPanel.getResultType()
{
	return this._mapView.getResultType();
}

/************************************************************************
*
*	function:	showLayer
*
*	purpose:	
*
*************************************************************************/
function MapPanel.showLayer(layerName, show)
{
	return this._mapView.showLayer(layerName, show);
}

/************************************************************************
*
*	function:	Event handler adapters
*
*	purpose:	
*
*************************************************************************/
function MapPanel.activateControlAdapter(event){target(event, MapPanel).activateControl();}
function MapPanel.zoomToFullExtentAdapter(event){target(event, MapPanel).zoomToFullExtent();}

