/*****************************************************************************
 *
 * File     : ControlPanel.js
 * 
 * Project  : WMS Viewer Client
 * 
 * Contents : 
 *
 * Author: Milan Trninic
 *
 * Copyright 1999-2005 Galdos Systems, Inc.
 * All rights reserved.
 * 
 ***|***************************|***********************|*******************|*/

/************************************************************************
*
*	Global declarations
*
*
*************************************************************************/

/************************************************************************
*
*	function:	ControlPanel
*
*	purpose:	Constructor
*
*************************************************************************/
function ControlPanel()
{
	// method assignments
	this.init = ControlPanel.init;
	this.setSize = ControlPanel.setSize;
	this.getSize = ControlPanel.getSize;
	this.changePanel = ControlPanel.changePanel;
	this.showHelp = ControlPanel.showHelp;
	this.setToolbarEnabled = ControlPanel.setToolbarEnabled;
	this.validateMapRequest = ControlPanel.validateMapRequest;
	this.validateGmlRequest = ControlPanel.validateGmlRequest;
	
	this.getWmsAddress = ControlPanel.getWmsAddress;
	this.getBoundingBox = ControlPanel.getBoundingBox;
	this.setBoundingBox = ControlPanel.setBoundingBox;
	this.getMapSize = ControlPanel.getMapSize;
	this.setMapSize = ControlPanel.setMapSize;
	this.fitToWindow = ControlPanel.fitToWindow;
	this.getMapOutputFormat = ControlPanel.getMapOutputFormat;
	this.getGmlOutputFormat = ControlPanel.getGmlOutputFormat;
	this.getExceptionOutputFormat = ControlPanel.getExceptionOutputFormat;
	this.getSelectedLayers = ControlPanel.getSelectedLayers;
	this.harvestWms = ControlPanel.harvestWms;
	this.getMap = ControlPanel.getMap;
	this.getGml = ControlPanel.getGml;
	this.getCapabilities = ControlPanel.getCapabilities;
	this.isDirect = ControlPanel.isDirect;
	this.getGmlRendering = ControlPanel.getGmlRendering;
	this.getSelectionColor = ControlPanel.getSelectionColor;
	this.getOriginalBoundingBox = ControlPanel.getOriginalBoundingBox;
	this.activateLayers = ControlPanel.activateLayers;
	
	// instance field declarations
	this._uiElement;
	this._tabbedPanel;
	this._toolbar;
	this._directUI;

	// initialization
	this.init();
}

/************************************************************************
*
*	function:	init
*
*	purpose:	initialization
*
*************************************************************************/
function ControlPanel.init()
{
	this._uiElement = document.getElementById("controlPanel");
	this._tabbedPanel = new TabbedPanel();
	this._directUI = document.getElementById("directCapabilities");
	var toolbarUI = document.getElementById("requestToolbar");
	this._toolbar = new Toolbar(toolbarUI);

	var capabilitiesButton = this._toolbar.getButton("capabilitiesButton");
	var harvestButton = this._toolbar.getButton("harvestButton");
	var getMapButton = this._toolbar.getButton("getMapButton");
	var getGmlButton = this._toolbar.getButton("getGmlButton");
	
	capabilitiesButton.setDisabledImagePath("images/notebookDisabled.gif");
	//capabilitiesButton.setSelectedImagePath("images/notebookActive.gif");
	harvestButton.setDisabledImagePath("images/computerDisabled.gif");
	//harvestButton.setSelectedImagePath("images/computerActive.gif");
	getMapButton.setDisabledImagePath("images/mapDisabled.gif");
	//getMapButton.setSelectedImagePath("images/mapActive.gif");
	getGmlButton.setDisabledImagePath("images/xmlDocDisabled.gif");
	//getGmlButton.setSelectedImagePath("images/xmlDocActive.gif");

	capabilitiesButton.attachEvent("onclick", getCapabilities);
	harvestButton.attachEvent("onclick", harvestWms);
	getMapButton.attachEvent("onclick", getMap);
	getGmlButton.attachEvent("onclick", getGml);
	
	var wmsAddress = this.getWmsAddress();
	if (wmsAddress == null || trim(wmsAddress) == "")
	{
		this._toolbar.setEnabled(false);
	}
	else
	{
		this._toolbar.setEnabled(true);
	}
}

/************************************************************************
*
*	function:	getSize
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.getSize()
{
	var result = new Point(this._uiElement.style.width, this._uiElement.style.height);
	return result;
}

/************************************************************************
*
*	function:	setSize
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.setSize(width, height)
{
	this._uiElement.style.height = height;
	this._uiElement.style.width = width;
	this._tabbedPanel.setSize(width * 0.97, height * 0.85);
}

/************************************************************************
*
*	function:	changePanel
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.changePanel(button)
{
	this._tabbedPanel.changePanel(button);
}

/************************************************************************
*
*	function:	setToolbarEnabled
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.setToolbarEnabled(enable)
{
	this._toolbar.setEnabled(enable);
}

/************************************************************************
*
*	function:	getWmsAddress
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.getWmsAddress()
{
	return this._tabbedPanel.getWmsAddress();
}
	
/************************************************************************
*
*	function:	getBoundingBox
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.getBoundingBox()
{
	return this._tabbedPanel.getBoundingBox();
}

/************************************************************************
*
*	function:	setBoundingBox
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.setBoundingBox(bbox)
{
	return this._tabbedPanel.setBoundingBox(bbox);
}

/************************************************************************
*
*	function:	getOriginalBoundingBox
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.getOriginalBoundingBox()
{
	return this._tabbedPanel.getOriginalBoundingBox();
}

/************************************************************************
*
*	function:	getMapSize
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.getMapSize()
{
	return this._tabbedPanel.getMapSize();
}

/************************************************************************
*
*	function:	setMapSize
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.setMapSize(size)
{
	this._tabbedPanel.setMapSize(size);
}

/************************************************************************
*
*	function:	fitToWindow
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.fitToWindow()
{
	return this._tabbedPanel.fitToWindow();
}

/************************************************************************
*
*	function:	getSelectionColor
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.getSelectionColor()
{
	return this._tabbedPanel.getSelectionColor();
}

/************************************************************************
*
*	function:	getMapOutputFormat
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.getMapOutputFormat()
{
	return this._tabbedPanel.getMapOutputFormat();
}

/************************************************************************
*
*	function:	getGmlOutputFormat
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.getGmlOutputFormat()
{
	return this._tabbedPanel.getGmlOutputFormat();
}

/************************************************************************
*
*	function:	getExceptionOutputFormat
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.getExceptionOutputFormat()
{
	return this._tabbedPanel.getExceptionOutputFormat();
}

/************************************************************************
*
*	function:	getSelectedLayers
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.getSelectedLayers()
{
	return this._tabbedPanel.getSelectedLayers();
}

/************************************************************************
*
*	function:	harvestWms
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.harvestWms()
{
	this._tabbedPanel.harvestWms();
}

/************************************************************************
*
*	function:	isDirect
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.isDirect()
{
	return this._directUI.checked;
}

/************************************************************************
*
*	function:	getGmlRendering
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.getGmlRendering()
{
	return this._tabbedPanel.getGmlRendering();
}

/************************************************************************
*
*	function:	getCapabilities
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.getCapabilities()
{
	var wmsAddress = trim(this.getWmsAddress());
	
	mapViewForm.request.value = "getCapabilities";
	mapViewForm.url.value = wmsAddress;
	mapViewForm.usecache.value = "false";
	
	mapViewForm.action = "fpsPortal";
	mapViewForm.submitButton.click()
}

/************************************************************************
*
*	function:	getMap
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.getMap()
{
	var wmsAddress = trim(this.getWmsAddress());
	var bbox = this.getBoundingBox();
	var format = trim(this.getMapOutputFormat());
	var layersAndNs = this.getSelectedLayers();
	var ns = layersAndNs.get(0);
	var layers = layersAndNs.get(1);
	var size = this.getMapSize();
	var exceptionFormat = trim(this.getExceptionOutputFormat());
	
	this.validateMapRequest(wmsAddress, bbox, format, size, layers);
	
	mapViewForm.request.value = "getMap";
	mapViewForm.url.value = wmsAddress;
	mapViewForm.srs.value = bbox.getSrsName();
	mapViewForm.minx.value = bbox.getMinX();
	mapViewForm.miny.value = bbox.getMinY();
	mapViewForm.maxx.value = bbox.getMaxX();
	mapViewForm.maxy.value = bbox.getMaxY();
	mapViewForm.width.value = size.getWidth();
	mapViewForm.height.value = size.getHeight();
	mapViewForm.format.value = format;
	mapViewForm.exceptionformat.value = exceptionFormat;
	mapViewForm.layers.value = layers;
	mapViewForm.namespaces.value = ns;
	
	mapViewForm.action = "fpsPortal";
	mapViewForm.submitButton.click()
}

/************************************************************************
*
*	function:	getGml
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.getGml()
{
	var wmsAddress = trim(this.getWmsAddress());
	var bbox = this.getBoundingBox();
	var mapFormat = trim(this.getMapOutputFormat());
	var gmlFormat = trim(this.getGmlOutputFormat());
	var layersAndNs = this.getSelectedLayers();
	var ns = layersAndNs.get(0);
	var layers = layersAndNs.get(1);
	var size = this.getMapSize();
	var gmlAsText = this.getGmlRendering();

	this.validateGmlRequest(wmsAddress, bbox, mapFormat, gmlFormat, layers);
	
	gmlViewForm.request.value = "getGml";
	gmlViewForm.url.value = wmsAddress;
	gmlViewForm.srs.value = bbox.getSrsName();
	gmlViewForm.minx.value = bbox.getMinX();
	gmlViewForm.miny.value = bbox.getMinY();
	gmlViewForm.maxx.value = bbox.getMaxX();
	gmlViewForm.maxy.value = bbox.getMaxY();
	gmlViewForm.width.value = size.getWidth();
	gmlViewForm.height.value = size.getHeight();
	gmlViewForm.format.value = mapFormat;
	gmlViewForm.infoformat.value = gmlFormat;
	gmlViewForm.gmlastext.value = gmlAsText;
	gmlViewForm.layers.value = layers;
	gmlViewForm.namespaces.value = ns;

	gmlViewForm.action = "fpsPortal";
	gmlViewForm.submitButton.click()
}

/************************************************************************
*
*	function:	validateMapRequest
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.validateMapRequest(wmsAddress, bbox, format, size, layers)
{
	if (wmsAddress == null || wmsAddress == "") throw new Exception("WMS Address not specified.");
	if (format == null || format == "") throw new Exception("Output format not specified.");
	
	if (size == null) throw new Exception("Map size not specified.");
	if (size.getWidth() < 0 || size.getHeight() < 0) 
	{
		throw new Exception("Bad map size: " + size.getWidth() + "," + size.getHeight());
	}
	
	if (bbox == null) throw new Exception("Bounding box not specified.");
	var srsName = trim(bbox.getSrsName());
	if (srsName == null || srsName == "") throw new Exception("SRS not specified.");
	
	if (layers == null || layers == "") throw new Exception("No layers selected.");
}

/************************************************************************
*
*	function:	validateGmlRequest
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.validateGmlRequest(wmsAddress, bbox, mapFormat, gmlFormat, layers)
{
	if (wmsAddress == null || wmsAddress == "") throw new Exception("WMS Address not specified.");
	if (mapFormat == null || mapFormat == "") throw new Exception("Map format not specified.");
	//if (gmlFormat == null || gmlFormat == "") throw new Exception("Output format not specified.");
	
	if (bbox == null) throw new Exception("Bounding box not specified.");
	var srsName = trim(bbox.getSrsName());
	if (srsName == null || srsName == "") throw new Exception("SRS not specified.");
	
	if (layers == null || layers == "") throw new Exception("No layers selected.");
}

/************************************************************************
*
*	function:	showHelp
*
*	purpose:	A convenience method that shows Help panel (invoked from splashScreen)
*
*************************************************************************/
function ControlPanel.showHelp()
{
	this._tabbedPanel.showHelp();
}

/************************************************************************
*
*	function:	activateLayers
*
*	purpose:	
*
*************************************************************************/
function ControlPanel.activateLayers(activate)
{
	this._tabbedPanel.activateLayers(activate);
}
