/*****************************************************************************
*
* File     : MapPropertiesPanel.js
* 
* Project  : WMS Viewer Client
* 
* Contents : 
*
* Author: Milan Trninic
*
* Copyright 1999-2005 Galdos Systems, Inc.
* All rights reserved.
* 
***|***************************|***********************|*******************|*/

/************************************************************************
*
*	Global declarations
*
*
*************************************************************************/

/************************************************************************
*
*	function:	MapPropertiesPanel
*
*	purpose:	Constructor
*
*************************************************************************/
function MapPropertiesPanel()
{
	// superclass
	var uiElement = document.getElementById("mapPropertiesPanel");
	this._superClass = StackedPanel;
	this._superClass(uiElement);
	
	// method assignments
	this.init = MapPropertiesPanel.init;
	this.getMapSize = MapPropertiesPanel.getMapSize;
	this.setMapSize = MapPropertiesPanel.setMapSize;
	this.fitToWindow = MapPropertiesPanel.fitToWindow;
	this.getGmlRendering = MapPropertiesPanel.getGmlRendering;
	this.getSelectionColor = MapPropertiesPanel.getSelectionColor;
	
	this.getMapOutputFormat = MapPropertiesPanel.getMapOutputFormat;
	this.getGmlOutputFormat = MapPropertiesPanel.getGmlOutputFormat;
	this.getExceptionOutputFormat = MapPropertiesPanel.getExceptionOutputFormat;
	this.sendHarvestRequest = MapPropertiesPanel.sendHarvestRequest;
	this.handleHarvestResponse = MapPropertiesPanel.handleHarvestResponse;
	this.getXmlHttpObject = MapPropertiesPanel.getXmlHttpObject;
	
	this.mapFormatChanged = MapPropertiesPanel.mapFormatChanged;
	this.gmlFormatChanged = MapPropertiesPanel.gmlFormatChanged;
	this.exceptionFormatChanged = MapPropertiesPanel.exceptionFormatChanged;

	this.mapFormatChangedAdapter = MapPropertiesPanel.mapFormatChangedAdapter;
	this.gmlFormatChangedAdapter = MapPropertiesPanel.gmlFormatChangedAdapter;
	this.exceptionFormatChangedAdapter = MapPropertiesPanel.exceptionFormatChangedAdapter;

	// instance field declarations
	this._superClass;
	this._transparencyUI;
	this._widthUI;
	this._heightUI;
	this._mapFormatList;
	this._gmlFormatList;
	this._exceptionFormatList;
	this._fitMapUI;
	this._gmlRenderingUI;
	this._selectionColorUI;
	this._xmlhttp;

	// constants
	this.MIME_SVG = "image/xml+svg";
	this.MIME_GIF = "image/gif";
	this.MIME_JPEG = "image/jpeg";
	this.MIME_PNG = "image/png";
	this.MIME_TIFF = "image/tiff";
	
	// initialization
	this.init();
}

/************************************************************************
*
*	function:	init
*
*	purpose:	initializes the globals
*
*************************************************************************/
function MapPropertiesPanel.init()
{
	this._transparencyUI = document.getElementById("transparency");
	this._fitMapUI = document.getElementById("fitMap");
	this._gmlRenderingUI = document.getElementById("gmlAsText");
	this._widthUI = document.getElementById("mapWidth");
	this._heightUI = document.getElementById("mapHeight");
	this._selectionColorUI = document.getElementById("selectionColor");
	
	var mapFormatListUI = document.getElementById("mapFormat");
	this._mapFormatList = new JsList(mapFormatListUI);
	
	var gmlFormatListUI = document.getElementById("gmlFormat");
	this._gmlFormatList = new JsList(gmlFormatListUI);
	
	var exceptionFormatListUI = document.getElementById("exceptionFormat");
	this._exceptionFormatList = new JsList(exceptionFormatListUI);
	
	this._uiElement.setAttribute("dataTransfer", this);
	this._mapFormatList.attachEvent("onchange", MapPropertiesPanel.mapFormatChangedAdapter);
	this._gmlFormatList.attachEvent("onchange", MapPropertiesPanel.gmlFormatChangedAdapter);
	this._exceptionFormatList.attachEvent("onchange", MapPropertiesPanel.exceptionFormatChangedAdapter);
}

/************************************************************************
*
*	function:	mapFormatChanged
*
*	purpose:	
*
*************************************************************************/
function MapPropertiesPanel.mapFormatChanged()
{
	var format = this._mapFormatList.getSelectedItem();
	format = format.toLowerCase();
	if (format == this.MIME_GIF || format == this.MIME_PNG) this._transparencyUI.disabled = false;
	else 
	{
		this._transparencyUI.disabled = true;
		this._transparencyUI.checked = false;
	}
}

/************************************************************************
*
*	function:	gmlFormatChanged
*
*	purpose:	
*
*************************************************************************/
function MapPropertiesPanel.gmlFormatChanged()
{
}

/************************************************************************
*
*	function:	exceptionFormatChanged
*
*	purpose:	
*
*************************************************************************/
function MapPropertiesPanel.exceptionFormatChanged()
{
}

/************************************************************************
*
*	function:	sendHarvestRequest
*
*	purpose:	
*
*************************************************************************/
function MapPropertiesPanel.sendHarvestRequest(wmsAddress, handler)
{
	this._xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	this._xmlhttp.onreadystatechange = handler;
	// Use POST because no time for finding how to disable IE caching of GET
	this._xmlhttp.open("POST", "fpsPortal?request=getMapProperties&usecache=true&url=" + escape(wmsAddress), true);
	this._xmlhttp.send();
}

/************************************************************************
*
*	function:	handleHarvestResponse
*
*	purpose:	
*
*************************************************************************/
function MapPropertiesPanel.handleHarvestResponse()
{
	var formats = this._xmlhttp.responseText.split("&");

	var mapFormats = formats[0].split(",");
	this._mapFormatList.removeAllItems();
	for (var index = 0; index < mapFormats.length; index++)
	{
		var format = mapFormats[index];
		if (format == this.MIME_TIFF) continue;
		this._mapFormatList.addItem(mapFormats[index]);
	}
	this.mapFormatChanged();
	
	var gmlFormats = formats[1].split(",");
	this._gmlFormatList.removeAllItems();
	for (var index = 0; index < gmlFormats.length; index++)
	{
		this._gmlFormatList.addItem(gmlFormats[index]);
	}
	this.gmlFormatChanged();
	
	var exceptionFormats = formats[2].split(",");
	this._exceptionFormatList.removeAllItems();
	for (var index = 0; index < exceptionFormats.length; index++)
	{
		this._exceptionFormatList.addItem(exceptionFormats[index]);
	}
	this.exceptionFormatChanged();
}

/************************************************************************
*
*	function:	getXmlHttpObject
*
*	purpose:	
*
*************************************************************************/
function MapPropertiesPanel.getXmlHttpObject()
{
	return this._xmlhttp;
}

/************************************************************************
*
*	function:	getMapSize
*
*	purpose:	
*
*************************************************************************/
function MapPropertiesPanel.getMapSize()
{
	var size = new Rectangle(this._widthUI.value, this._heightUI.value);
	return size;
}

/************************************************************************
*
*	function:	setMapSize
*
*	purpose:	
*
*************************************************************************/
function MapPropertiesPanel.setMapSize(size)
{
	this._widthUI.value = size.getWidth();
	this._heightUI.value = size.getHeight();
}

/************************************************************************
*
*	function:	fitToWindow
*
*	purpose:	
*
*************************************************************************/
function MapPropertiesPanel.fitToWindow()
{
	return this._fitMapUI.checked;
}

/************************************************************************
*
*	function:	getGmlRendering
*
*	purpose:	
*
*************************************************************************/
function MapPropertiesPanel.getGmlRendering()
{
	return this._gmlRenderingUI.checked;
}

/************************************************************************
*
*	function:	getSelectionColor
*
*	purpose:	
*
*************************************************************************/
function MapPropertiesPanel.getSelectionColor()
{
	return this._selectionColorUI.value;
}

/************************************************************************
*
*	function:	getMapOutputFormat
*
*	purpose:	
*
*************************************************************************/
function MapPropertiesPanel.getMapOutputFormat()
{
	var format = this._mapFormatList.getSelectedItem();
	return format;
}

/************************************************************************
*
*	function:	getGmlOutputFormat
*
*	purpose:	
*
*************************************************************************/
function MapPropertiesPanel.getGmlOutputFormat()
{
	var format = this._gmlFormatList.getSelectedItem();
	return format;
}

/************************************************************************
*
*	function:	getExceptionOutputFormat
*
*	purpose:	
*
*************************************************************************/
function MapPropertiesPanel.getExceptionOutputFormat()
{
	var format = this._exceptionFormatList.getSelectedItem();
	return format;
}

/************************************************************************
*
*	function:	Event handler adapters
*
*	purpose:	
*
*************************************************************************/
function MapPropertiesPanel.mapFormatChangedAdapter(){target(event, MapPropertiesPanel).mapFormatChanged(event);}
function MapPropertiesPanel.gmlFormatChangedAdapter(){target(event, MapPropertiesPanel).gmlFormatChanged(event);}
function MapPropertiesPanel.exceptionFormatChangedAdapter(){target(event, MapPropertiesPanel).exceptionFormatChanged(event);}

