/*****************************************************************************
 *
 * File     : TabbedPanel.js
 * 
 * Project  : WMS Viewer Client
 * 
 * Contents : This class implements the tabbed panel behaviour. 
 *
 * Author: Milan Trninic
 *
 * Copyright 1999-2005 Galdos Systems, Inc.
 * All rights reserved.
 * 
 ***|***************************|***********************|*******************|*/

/************************************************************************
*
*	Global declarations
*
*	
*
*************************************************************************/

/************************************************************************
*
*	function:	TabbedPanel
*
*	purpose:	Constructor
*
*************************************************************************/
function TabbedPanel()
{
	// method assignments
	this.init = TabbedPanel.init;
	this.setSize = TabbedPanel.setSize;
	this.getWmsAddress = TabbedPanel.getWmsAddress;
	this.getBoundingBox = TabbedPanel.getBoundingBox;
	this.setBoundingBox = TabbedPanel.setBoundingBox;
	this.getOriginalBoundingBox = TabbedPanel.getOriginalBoundingBox;
	this.getMapSize = TabbedPanel.getMapSize;
	this.setMapSize = TabbedPanel.setMapSize;
	this.fitToWindow = TabbedPanel.fitToWindow;
	this.getGmlRendering = TabbedPanel.getGmlRendering;
	this.getSelectionColor = TabbedPanel.getSelectionColor;
	this.showHelp = TabbedPanel.showHelp;
	this.activateLayers = TabbedPanel.activateLayers;
	
	this.getMapOutputFormat = TabbedPanel.getMapOutputFormat;
	this.getGmlOutputFormat = TabbedPanel.getGmlOutputFormat;
	this.getExceptionOutputFormat = TabbedPanel.getExceptionOutputFormat;
	this.getSelectedLayers = TabbedPanel.getSelectedLayers;
	this.changePanel = TabbedPanel.changePanel;
	this.changePanelAdapter = TabbedPanel.changePanelAdapter;
	this.harvestWms = TabbedPanel.harvestWms;
	this.changeExtentPanel = TabbedPanel.changeExtentPanel;
	this.changeExtentAdapter = TabbedPanel.changeExtentAdapter;

	this.layersRetrieved = TabbedPanel.layersRetrieved;
	this.propertiesRetrieved = TabbedPanel.propertiesRetrieved;
	this.extentsRetrieved = TabbedPanel.extentsRetrieved;
	this.scaleRetrieved = TabbedPanel.scaleRetrieved;

	this.layersRetrievedAdapter = TabbedPanel.layersRetrievedAdapter;
	this.propertiesRetrievedAdapter = TabbedPanel.propertiesRetrievedAdapter;
	this.extentsRetrievedAdapter = TabbedPanel.extentsRetrievedAdapter;
	this.scaleRetreivedAdapter = TabbedPanel.scaleRetrievedAdapter;

	// instance field declarations
	this._uiElement;
	this._toolbar;
	this._layersPanel;
	this._extentPanel;
	this._wmsPanel;
	this._mapPropertiesPanel;
	this._scalePanel;
	this._helpPanel;
	this._logPanel;
	this._visiblePanel;
	this._censusExtentPanel;
	this._basicExtentPanel;
	this._panelMap;
	
	// initialization
	this.init();
}

/************************************************************************
*
*	function:	init
*
*	purpose:	initializes the globals
*
*************************************************************************/
function TabbedPanel.init()
{
	this._uiElement = document.getElementById("tabbedPanel");
	
	var toolbarUI = document.getElementById("tabbedToolbar");
	this._toolbar = new Toolbar(toolbarUI);
	this._toolbar.setEnabled(true);

	var layersButton = this._toolbar.getButton("layersButton");
	var extentButton = this._toolbar.getButton("extentButton");
	var wmsButton = this._toolbar.getButton("wmsButton");
	var mapPropertiesButton = this._toolbar.getButton("mapPropertiesButton");
	var scaleButton = this._toolbar.getButton("scaleButton");
	var helpButton = this._toolbar.getButton("helpButton");
	var logButton = this._toolbar.getButton("logButton");

	this._uiElement.setAttribute("dataTransfer", this);
	layersButton.attachEvent("onclick", TabbedPanel.changePanelAdapter);
	extentButton.attachEvent("onclick", TabbedPanel.changePanelAdapter);
	wmsButton.attachEvent("onclick", TabbedPanel.changePanelAdapter);
	mapPropertiesButton.attachEvent("onclick", TabbedPanel.changePanelAdapter);
	scaleButton.attachEvent("onclick", TabbedPanel.changePanelAdapter);
	helpButton.attachEvent("onclick", TabbedPanel.changePanelAdapter);
	logButton.attachEvent("onclick", TabbedPanel.changePanelAdapter);
	
	var simpleExtentLink = document.getElementById("showSimpleExtent");
	simpleExtentLink.attachEvent("onclick", TabbedPanel.changeExtentAdapter);
	var advancedExtentLink = document.getElementById("showAdvancedExtent");
	advancedExtentLink.attachEvent("onclick", TabbedPanel.changeExtentAdapter);
	
	layersButton.setDisabledImagePath("images/listDisabled.gif");
	//layersButton.setSelectedImagePath("images/listActive.gif");
	extentButton.setDisabledImagePath("images/extentDisabled.gif");
	//extentButton.setSelectedImagePath("images/extentActive.gif");
	wmsButton.setDisabledImagePath("images/computerDisabled.gif");
	//wmsButton.setSelectedImagePath("images/computerActive.gif");
	mapPropertiesButton.setDisabledImagePath("images/mapDisabled.gif");
	//mapPropertiesButton.setSelectedImagePath("images/mapActive.gif");
	scaleButton.setDisabledImagePath("images/scaleDisabled.gif");
	//scaleButton.setSelectedImagePath("images/scaleActive.gif");
	helpButton.setDisabledImagePath("images/questionDisabled.gif");
	//helpButton.setSelectedImagePath("images/questionActive.gif");
	logButton.setDisabledImagePath("images/notebookDisabled.gif");
	//logButton.setSelectedImagePath("images/notebookActive.gif");
		
	this._layersPanel = new LayersPanel();
	this._basicExtentPanel = new ExtentPanel();
	this._censusExtentPanel = new CensusExtentPanel();
	// Basic panel is default
	this._extentPanel = this._basicExtentPanel;
	this._wmsPanel = new WmsPanel();
	this._mapPropertiesPanel = new MapPropertiesPanel();
	this._scalePanel = new ScalePanel();
	this._helpPanel = new HelpPanel();
	// _log has been instantiated globally for easy access
	this._logPanel = _log;

	this._panelMap = new Hashtable();
	
	this._panelMap.put(layersButton, this._layersPanel);
	this._panelMap.put(extentButton, this._extentPanel);
	this._panelMap.put(wmsButton, this._wmsPanel);
	this._panelMap.put(mapPropertiesButton, this._mapPropertiesPanel);
	this._panelMap.put(scaleButton, this._scalePanel);
	this._panelMap.put(helpButton, this._helpPanel);
	if (LOG_ENABLED) this._panelMap.put(logButton, this._logPanel);
	else
	{
		logButton.setVisible(false);
		this._logPanel.setVisible(false);
	}

	// Init layer panel by default
	this._layersPanel.setVisible(true);
	this._visiblePanel = this._layersPanel;
	var button = this._toolbar.getButton("layersButton");
	button.setSelected(true);

	// If address is not set then set wms panel current
	var wmsAddress = this._wmsPanel.getActiveServer();
	if (wmsAddress == null || trim(wmsAddress) == "")
	{
		this._toolbar.getButton("wmsButton").setSelected(true);
		this.changePanel();
	}

	// The data transfer object for xmlhttp.onreadystatechange handler
	document.body.setAttribute("harvestHandler", this);
}

/************************************************************************
*
*	function:	setSize
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.setSize(width, height)
{
	this._uiElement.style.height = height;
	this._uiElement.style.width = width;
	var stackedPanels = this._panelMap.getValues();
	var size = stackedPanels.getSize();
	for (var index = 0; index < size; index++)
	{
		var stackedPanel = stackedPanels.get(index);
		stackedPanel.setSize(width, height * 0.95);
	}
	// this one is not in the map since it flip-flops with the basic extent panel which is the default
	this._censusExtentPanel.setSize(width, height * 0.95);
}

/************************************************************************
*
*	function:	changePanel
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.changePanel(button)
{
	this._visiblePanel.setVisible(false);
	
	if (button == null) button = this._toolbar.getSelectedButton();
	var panel = this._panelMap.get(button);
	panel.setVisible(true);
	this._visiblePanel = panel;

}

/************************************************************************
*
*	function:	getWmsAddress
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.getWmsAddress()
{
	return this._wmsPanel.getActiveServer();
}
	
/************************************************************************
*
*	function:	getBoundingBox
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.getBoundingBox()
{
	return this._extentPanel.getBoundingBox();
}

/************************************************************************
*
*	function:	setBoundingBox
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.setBoundingBox(bbox)
{
	return this._extentPanel.setBoundingBox(bbox);
}

/************************************************************************
*
*	function:	getOriginalBoundingBox
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.getOriginalBoundingBox()
{
	return this._extentPanel.getOriginalBoundingBox();
}

/************************************************************************
*
*	function:	getMapSize
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.getMapSize()
{
	return this._mapPropertiesPanel.getMapSize();
}

/************************************************************************
*
*	function:	setMapSize
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.setMapSize(size)
{
	this._mapPropertiesPanel.setMapSize(size);
}

/************************************************************************
*
*	function:	fitToWindow
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.fitToWindow()
{
	return this._mapPropertiesPanel.fitToWindow();
}

/************************************************************************
*
*	function:	getGmlRendering
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.getGmlRendering()
{
	return this._mapPropertiesPanel.getGmlRendering();
}

/************************************************************************
*
*	function:	getSelectionColor
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.getSelectionColor()
{
	return this._mapPropertiesPanel.getSelectionColor();
}

/************************************************************************
*
*	function:	getMapOutputFormat
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.getMapOutputFormat()
{
	return this._mapPropertiesPanel.getMapOutputFormat();
}

/************************************************************************
*
*	function:	getGmlOutputFormat
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.getGmlOutputFormat()
{
	return this._mapPropertiesPanel.getGmlOutputFormat();
}

/************************************************************************
*
*	function:	getExceptionOutputFormat
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.getExceptionOutputFormat()
{
	return this._mapPropertiesPanel.getExceptionOutputFormat();
}

/************************************************************************
*
*	function:	getLayers
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.getSelectedLayers()
{
	return this._layersPanel.getSelectedLayers();
}

/************************************************************************
*
*	function:	getVisiblePanel
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.getVisiblePanel()
{
	return _visiblePanel;
}

/************************************************************************
*
*	function:	harvestWms
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.harvestWms(wmsAddress)
{
	try
	{
		var wmsAddress = this._wmsPanel.getActiveServer();
		this._layersPanel.sendHarvestRequest(wmsAddress, TabbedPanel.layersRetrievedAdapter);
	}
	catch (exception)
	{
		alert(exception.name + ": " + exception.message);
		harvestCompleted();
	}
}

/************************************************************************
*
*	function:	layersRetrieved
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.layersRetrieved()
{
	var xmlhttp = this._layersPanel.getXmlHttpObject();
	if (xmlhttp.readyState != 4) return;
	try
	{
		this._layersPanel.handleHarvestResponse();
		var wmsAddress = this._wmsPanel.getActiveServer();
		this._mapPropertiesPanel.sendHarvestRequest(wmsAddress, TabbedPanel.propertiesRetrievedAdapter);
	}
	catch (exception)
	{
		alert(exception.name + ": " + exception.message);
		this._layersPanel.setToolbarEnabled();
		harvestCompleted();
	}
}

/************************************************************************
*
*	function:	propertiesRetrieved
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.propertiesRetrieved()
{
	var xmlhttp = this._mapPropertiesPanel.getXmlHttpObject();
	if (xmlhttp.readyState != 4) return;
	try
	{
		this._mapPropertiesPanel.handleHarvestResponse();
		var wmsAddress = this._wmsPanel.getActiveServer();
		this._basicExtentPanel.sendHarvestRequest(wmsAddress, TabbedPanel.extentsRetrievedAdapter);	
	}
	catch (exception)
	{
		alert(exception.name + ": " + exception.message);
		harvestCompleted();
	}
}

/************************************************************************
*
*	function:	extentsRetrieved
*
*	purpose:	Handle the retrieval of basic extents and load boundaries.
*
*************************************************************************/
function TabbedPanel.extentsRetrieved()
{
	var xmlhttp = this._basicExtentPanel.getXmlHttpObject();
	if (xmlhttp.readyState != 4) return;
	try
	{
		this._basicExtentPanel.handleHarvestResponse();
		var wmsAddress = this._wmsPanel.getActiveServer();
		var boundary = this._censusExtentPanel.getBoundaries().get(wmsAddress);
		var extentVisible = this._visiblePanel == this._extentPanel;
		// Just display the retrieved panel
		if (boundary == null) {
			this._censusExtentPanel.loadEmptyBoundary();
			this._extentPanel = this._basicExtentPanel;					
		}
		// Load the boundaries
		else {
			// Load the extents and display the appropriate panel
			this._censusExtentPanel.loadBoundary(boundary);
			this._extentPanel = this._censusExtentPanel;		
		}
		// Refresh panel in Hashmap
		var extentButton = this._toolbar.getButton("extentButton");
		this._panelMap.put(extentButton, this._extentPanel);
		
		// Refresh UI
		this.changePanel();
		
		var wmsAddress = this._wmsPanel.getActiveServer();
		this._scalePanel.sendHarvestRequest(wmsAddress, TabbedPanel.scaleRetrievedAdapter);	
	}
	catch (exception)
	{
		alert(exception.name + ": " + exception.message);
		harvestCompleted();
	}		
}

/************************************************************************
*
*	function:	scaleRetrieved
*
*	purpose:	Called when the scale is retrieved.
*
*************************************************************************/
function TabbedPanel.scaleRetrieved() {
	var xmlhttp = this._scalePanel.getXmlHttpObject();
	if (xmlhttp.readyState != 4) return;
	try
	{
		this._scalePanel.handleHarvestResponse();
	}
	catch (exception)
	{
		alert(exception.name + ": " + exception.message);
	}
	// Show layers panel
	this._toolbar.getButton("layersButton").setSelected(true);
	this.changePanel();

	harvestCompleted();
}

/************************************************************************
*
*	function:	changeExtentPanel
*
*	purpose:	Switch the extent panel view.
*
*************************************************************************/
function TabbedPanel.changeExtentPanel()
{
	if (this._extentPanel == this._censusExtentPanel) {
		this._extentPanel = this._basicExtentPanel;
	}
	else {
		this._extentPanel = this._censusExtentPanel;			
	}
	// Refresh panel in Hashmap
	var extentButton = this._toolbar.getButton("extentButton");
	this._panelMap.put(extentButton, this._extentPanel);
	
	// Refresh UI
	this.changePanel();
}

/************************************************************************
*
*	function:	showHelp
*
*	purpose:	A convenience method that shows Help panel (invoked from splashScreen)
*
*************************************************************************/
function TabbedPanel.showHelp()
{
	var helpButton = this._toolbar.getButton("helpButton");
	helpButton.onClicked();
	this.changePanel(helpButton);
}


/************************************************************************
*
*	function:	activateLayers
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.activateLayers(activate)
{
	this._layersPanel.activateLayers(activate);
}

/************************************************************************
*
*	function:	Event handler adapters
*
*	purpose:	
*
*************************************************************************/
function TabbedPanel.layersRetrievedAdapter(){document.body.harvestHandler.layersRetrieved();}
function TabbedPanel.propertiesRetrievedAdapter(){document.body.harvestHandler.propertiesRetrieved();}
function TabbedPanel.extentsRetrievedAdapter(){document.body.harvestHandler.extentsRetrieved();}
function TabbedPanel.scaleRetrievedAdapter(){document.body.harvestHandler.scaleRetrieved();}
function TabbedPanel.changePanelAdapter(){target(event, TabbedPanel).changePanel();}
function TabbedPanel.changeExtentAdapter(){target(event, TabbedPanel).changeExtentPanel();}
