/*****************************************************************************
 *
 * File     : Main.js
 * 
 * Project  : WMS Viewer Client
 * 
 * Contents : 
 *
 * Author: Milan Trninic
 *
 * Copyright 1999-2004 Galdos Systems, Inc.
 * All rights reserved.
 * 
 ***|***************************|***********************|*******************|*/

/************************************************************************
*
*	Global declarations
*
*
*************************************************************************/
var _controlPanel;
var _resultPanel;
var _sizer;
var _logoSpinnerUI;
var _log;

/************************************************************************
*
*	Constants
*
*************************************************************************/
/*
 * This is the WMS version that client supports. Can be [<blank> | 1.1.1 | 1.3.0]
 * Blank means that the version will be discovered from Capabilities
 * Currently the HTML / Javascript client only displays Capabilities, so it will
 * work with any version. The server part of the client actually parses results
 * according to the version of capabilities it receives.
 */
var WMS_VERSION = "";
/************************************************************************
*
*	function:	initialize
*
*	purpose:	initializes the globals
*
*************************************************************************/
function initialize()
{
	_log = new LogPanel();
	var timer = _log.timerStart();
	_controlPanel = new ControlPanel();
	_resultPanel = new ResultPanel();
	_sizer = new Sizer();
	this._logoSpinnerUI = document.getElementById("logoSpinner");
	initializeSizes();
	if (_controlPanel.getWmsAddress() != "") harvestWms();
	document.onstop=stopped;
	_log.timerEnd("Main initialization.", timer);
}

/************************************************************************
*
*	function:	stopped
*
*	purpose:	Called when the user presses the Stop button in the browser
*
*************************************************************************/
function stopped()
{
	activateControls();
}

/************************************************************************
*
*	function:	boundingBoxChanged
*
*	purpose:	Called when the user completes visual zoom or pan
*
*************************************************************************/
function boundingBoxChanged(bbox)
{
	_controlPanel.setBoundingBox(bbox);
	getMap();
}

/************************************************************************
*
*	function:	zoomToFullExtent
*
*	purpose:	
*
*************************************************************************/
function zoomToFullExtent(action)
{
	var boundingBox = _controlPanel.getOriginalBoundingBox();
	if (boundingBox != null)
	{
		_controlPanel.setBoundingBox(boundingBox);
		getMap();
	}
}

/************************************************************************
*
*	function:	deactivateControls
*
*	purpose:	
*
*************************************************************************/
function deactivateControls()
{
	_resultPanel.enableMapControls(false);
	_controlPanel.setToolbarEnabled(false);
	spinLogo();
}

/************************************************************************
*
*	function:	activateControls
*
*	purpose:	
*
*************************************************************************/
function activateControls()
{
	stopLogo();
	_controlPanel.setToolbarEnabled(true);
	_resultPanel.enableMapControls();
	_resultPanel.activateMapControl();
}

/************************************************************************
*
*	function:	getCapabilities
*
*	purpose:	
*
*************************************************************************/
function getCapabilities()
{
	deactivateControls();
	if (_controlPanel.isDirect() == false)
	{
		_controlPanel.getCapabilities();
	}
	else
	{
		var wmsAddress = _controlPanel.getWmsAddress();
		var uri = wmsAddress + "?request=GetCapabilities&service=WMS&version=" + WMS_VERSION;
		_resultPanel.getMapPanel().initiateGetRequest(uri);
	}
}

/************************************************************************
*
*	function:	harvestWms
*
*	purpose:	
*
*************************************************************************/
function harvestWms()
{
	deactivateControls();
	_controlPanel.harvestWms();
}

/************************************************************************
*
*	function:	harvestCompleted
*
*	purpose:	
*
*************************************************************************/
function harvestCompleted()
{
	activateControls();
}

/************************************************************************
*
*	function:	getMap
*
*	purpose:	
*
*************************************************************************/
function getMap()
{
	try
	{
		deactivateControls();
		if (_controlPanel.fitToWindow()) 
		{
			var size = _resultPanel.getMapSize();
			_controlPanel.setMapSize(size);
		}
		_controlPanel.getMap();
	}
	catch (exception)
	{
		alert("Error: " + exception.message);
		activateControls();
	}
}

/************************************************************************
*
*	function:	getGml
*
*	purpose:	GetGML from the server using the GetFeatureInfo request.
*
*************************************************************************/
function getGml()
{
	try
	{
		// Only deactivate for display formats because for ZIP formats,
		// the view will never refresh and onMapViewResult() won't be called 
		var gmlOutputFormat = _controlPanel.getGmlOutputFormat().toLowerCase();
		
		if (gmlOutputFormat == "gml3-zip" || 
				gmlOutputFormat == "application/x-gzip")
		{
			deactivateControls();
		}
		_controlPanel.getGml();
	}
	catch (exception)
	{
		alert("Error: " + exception.message);
		activateControls();
	}
}

/************************************************************************
*
*	function:	onMapViewResult
*
*	purpose:	Called when the result in map view is received
*
*************************************************************************/
function onMapViewResult()
{
	_resultPanel.setActivePanel("map");		
	try
	{
		var boundingBox = _controlPanel.getBoundingBox();
		var mapSize = _controlPanel.getMapSize();
		_resultPanel.onMapViewResult(boundingBox, mapSize);

		var requestedFormat = _controlPanel.getMapOutputFormat();
		var resultType = _resultPanel.getResultType();
		if ( (requestedFormat == "svgz" || requestedFormat == "image/svg+xml") && 
				 resultType == "image")  _controlPanel.activateLayers(true);
		else _controlPanel.activateLayers(false);

		activateControls();
	}
	catch (exception)
	{
		alert("Error: " + exception.message);
		activateControls();
	}
}

/************************************************************************
*
*	function:	onGmlViewResult
*
*	purpose:	Called when the result in gml view is received
*
*************************************************************************/
function onGmlViewResult()
{
	_resultPanel.setActivePanel("gml");
	activateControls();
}

/************************************************************************
*
*	function:	spinLogo
*
*	purpose:	
*
*************************************************************************/
function spinLogo()
{
	this._logoSpinnerUI.src="images/spinningLogo.gif";
}

/************************************************************************
*
*	function:	stopLogo
*
*	purpose:	
*
*************************************************************************/
function stopLogo()
{
	if (this._logoSpinnerUI != null) this._logoSpinnerUI.src="images/logo_white.gif";
}

/************************************************************************
*
*	function:	initializeSizes
*
*	purpose:	initializes the sizes of UI elements according to user screen resolution
*
*************************************************************************/
function initializeSizes()
{
	if (screen.width == 1600 && screen.height == 1200)
	{
		_resultPanel.setSize(940, 900);
		_controlPanel.setSize(500, 900);
	}
	else if (screen.width == 1280 && screen.height == 1024)
	{
		_resultPanel.setSize(760, 840);
		_controlPanel.setSize(360, 840);
	}
	else if (screen.width == 1280 && screen.height == 960)
	{
		_resultPanel.setSize(700, 700);
		_controlPanel.setSize(430, 500);
	}
	else if (screen.width == 1280 && screen.height == 800)
	{
		_resultPanel.setSize(500, 470);
		_controlPanel.setSize(620, 320);
	}
	else if (screen.width == 1280 && screen.height == 768)
	{
		_resultPanel.setSize(500, 500);
		_controlPanel.setSize(620, 500);
	}
	else if (screen.width == 1280 && screen.height == 720)
	{
		_resultPanel.setSize(480, 480);
		_controlPanel.setSize(620, 270);
	}
	else if (screen.width == 1152 && screen.height == 864)
	{
		_resultPanel.setSize(600, 660);
		_controlPanel.setSize(390, 600);
	}
	else if (screen.width == 1024 && screen.height == 768)
	{
		_resultPanel.setSize(502, 550);
		_controlPanel.setSize(360, 500);
	}
	else if (screen.width == 960 && screen.height == 600)
	{
		_resultPanel.setSize(350, 350);
		_controlPanel.setSize(400, 150);
	}
	else if (screen.width == 900 && screen.height == 600)
	{
		_resultPanel.setSize(350, 350);
		_controlPanel.setSize(350, 150);
	}
	else if (screen.width == 800 && screen.height == 600)
	{
		_resultPanel.setSize(315, 360);
		_controlPanel.setSize(320, 260);
	}
	else
	{
		_resultPanel.setSize(500, 470);
		_controlPanel.setSize(370, 300);
	}
	document.body.style.visibility="visible";
}
