// =============================================================
//
// Copyright (c) 2000-2003 GE Smallworld. All Rights Reserved.
//
// =============================================================


//Create a array to hold content listeners
var contentListeners = new Array(MAX_LISTENERS);

// number of content listeners
var LISTENER_COUNT = 0;


function registerContentListener(aContentType, aFunction, aTargetPanel) {
//	----------------------------------------------------------------------
//	Mechanism for registering a listener object to the contentListeners array
//	Parameters:
//		aContentType: content type id that will trigger the listener function to execute
//		aFunction: pointer to the function in listeners.js that will be executed
//		aTargetPanel: panel (if any) that the listener will target
//	----------------------------------------------------------------------
	if (LISTENER_COUNT + 1 == MAX_LISTENERS) {
		alert('SIAS HTML Client:\nMaximum listeners (' + MAX_LISTENERS + ') reached.\n'+
			'<' + aContentType + '> listener could not be registered.');
		return;
	}

	var index = -1;

	// check to see if this content has already registered a listener.  If so then 
	// we will ignore the request to avoid registering two identical listeners
	for (var i=0; i < LISTENER_COUNT; i++) {
		// get request object
		var aListener = contentListeners[i];

		// check to see if this is for the same function name anc content type
		if ((aListener.functionRef == aFunction) 
			&& (aListener.contentType == aContentType) 
			&& (aListener.targetPanel == aTargetPanel)) {
			index = i;
			//alert('Duplicate');
			return;
		}
	}

	if (index==-1) {
		// by default the index of this request will be equal to the
		// current request count
		index = LISTENER_COUNT;
		//increment request count since this is a new request
		LISTENER_COUNT ++;
	}

	// now create a new listener object
	var aListener = new Listener(aContentType, aFunction, aTargetPanel);

	// now save the listener
	contentListeners[index] = aListener;
}

function unregisterContentListener(aPanel) {
//	----------------------------------------------------------------------
//	Function that strips all listeners for a specific panel from the 
//	contentListeners array.
//	Parameters:
//		aPanel: panel to remove all listener objects from
//	----------------------------------------------------------------------
	//alert('Unregister Listener');
	for (var i=0; i < LISTENER_COUNT; i++) {
		// get request object
		var aListener = contentListeners[i];

		// check to see if this is for the same function name anc content type
		if (aListener.targetPanel == aPanel) {
			removeListener(i);
		}
	}
}

function removeListener(nPosition) {
//	----------------------------------------------------------------------
//	This function is used by unregisterContentListener to remove each object
//	from the listener array recursively.
//	Parameters:
//		nPosition: position in the array of the object to be removed
//	----------------------------------------------------------------------
	if ((nPosition + 1) == LISTENER_COUNT)
	{
		contentListeners[nPosition] = null;
		LISTENER_COUNT = nPosition;
		return;
	}
	else
	{
		contentListeners[nPosition].contentType = contentListeners[nPosition + 1].contentType;
		contentListeners[nPosition].functionRef = contentListeners[nPosition + 1].functionRef;
		contentListeners[nPosition].targetPanel = contentListeners[nPosition + 1].targetPanel;
		removeListener(nPosition + 1);
	}
}


function Listener(aContentType, aFunction, aTargetPanel) {
//	----------------------------------------------------------------------
//	This class defines a JavaScript object, called listener, that defines
//	a content type (map, query, plot) and a function call to be executed 
//	which updates the listener (a panel) once the content comes back from
//	a server request
//	Parameters:
//		aContentType: content type id that will trigger the listener function to execute
//		aFunction: pointer to the function in listeners.js that will be executed
//		aTargetPanel: panel (if any) that the listener will target
//	----------------------------------------------------------------------
	// define instance variables
	this.contentType = aContentType;
	this.functionRef = aFunction;
	this.targetPanel = aTargetPanel;
	
	return this;
}

function Listener_contentType() {
	return this.contentType;
}

function Listener_functionRef() {
	return this.functionRef;
}

function Listener_targetPanel() {
	return this.targetPanel;
}


function updateAcePanel(aTargetPanel) {
//	----------------------------------------------------------------------
//	Display Settings listener, executes a map and ace request whenever the
//	listener is activated (generally for map content types only)
//	Parameters:
//		aTargetPanel: target panel where the request will be executed
//	---------------------------------------------------------------------- 

	// set up request
	aRequest =  '<request>'+
						'<name>map</name>'+
						'<format>xml</format>'+
					'</request>'+
					'<request>'+
						'<name>ace</name>'+
						'<command>aces</command>'+
						'<format>xml</format>'+
					'</request>';
	
	aStylesheet = 'panel_ace.xsl';
	
	header.writePanelTitle(DISPLAY_SETTINGS_TITLE);
	
	//alert('Execute Ace Request');
	executeRequest(aTargetPanel, aRequest, aStylesheet);
}

function updateFeatureSummary(aTargetPanel) {
//	----------------------------------------------------------------------
//	Feature Summary listener, executes a feature_info request with a 
//	select_set command to get the currently selected set of features.
//	Parameters:
//		aTargetPanel: target panel where the request will be executed
//	---------------------------------------------------------------------- 

	// get panel batch form 
	// note: hcf_panel should be replaced by a parameter passed to the listener
	//var aBatchForm = top.getForm(aTargetPanel, '', 'BatchForm');

	aRequest =	'<request>'+
				'<name>feature_info</name>'+
				'<format>xml</format>'+
				'<swldy_command>select_set</swldy_command>'+
				'<swldy_write_features>true</swldy_write_features>'+
				'<swldy_write_dd>true</swldy_write_dd>'+		
				'<swldy_write_xsd_namespace>false</swldy_write_xsd_namespace>'+
				'<swldy_feature_count>1</swldy_feature_count>'+
				'<swldy_feature_start>1</swldy_feature_start>'+
			'</request>';

	aStylesheet = 'panel_feature_summary.xsl';
	
	header.writePanelTitle(SELECTIONS_TITLE);

	executeRequest(aTargetPanel, aRequest, aStylesheet);
}

function updateQuerySummary(aTargetPanel) {
//	----------------------------------------------------------------------
//	Query Summary listener that executes a request dependent on the last 
//	query service type executed (query, geocoder, etc). 
//	Parameters:
//		aTargetPanel: target panel where the request will be executed
//	---------------------------------------------------------------------- 

	aRequest =	'<request>'+
				'<name>' + top.LAST_QUERY_SERVICE + '</name>'+
				'<format>xml</format>'+
				'<swldy_write_features>true</swldy_write_features>'+
				'<swldy_write_dd>true</swldy_write_dd>'+		
				'<swldy_write_xsd_namespace>false</swldy_write_xsd_namespace>'+
				'<swldy_feature_count>1</swldy_feature_count>'+
				'<swldy_feature_start>' + top.LAST_FEATURE_START + '</swldy_feature_start>'+
			'</request>';

	aStylesheet = 'panel_feature_summary.xsl';
	
	header.writePanelTitle(FEATURES_TITLE);

	executeRequest(aTargetPanel, aRequest, aStylesheet);
}

function updateTrailPanel(aTargetPanel) {
//	----------------------------------------------------------------------
//	Trail panel listener that executes a measurement request in order to get
//	a list of trail segments. These are displayed in the trail panel.
//	Parameters:
//		aTargetPanel: target panel where the request will be executed
//	---------------------------------------------------------------------- 

	var aStylesheet = 'panel_trail.xsl';
	
	var aForm = getForm(aTargetPanel, '', 'trailForm');

	aRequest =	'<request><name>measurement</name><format>xml</format>';

	if (aForm) {
		aRequest += '<display_units>' + getSelected(aForm.units) + '</display_units>' +
						'<decimal_places>' + aForm.places.value + '</decimal_places>';
	} else {
		aRequest += '<display_units>metric</display_units>' +
						'<decimal_places>2</decimal_places>';
	}
	aRequest += '</request>';

	header.writePanelTitle(TRAIL_TITLE);

	executeRequest(aTargetPanel, aRequest, aStylesheet);
}

function updateNavPanel(aTargetPanel) {
//	----------------------------------------------------------------------
//	Update Navigation panel listener that shows a list of views in the panel.
//	Calls the ace service to get the list.
//	Parameters:
//		aTargetPanel: target panel where the request will be executed
//	---------------------------------------------------------------------- 
	// set up request
	aRequest = 	'<request>'+
						'<name>ace</name>'+
						'<command>aces</command>'+
						'<format>xml</format>'+
				'</request>';
	
	aStylesheet = 'panel_navigation.xsl';
	
	header.writePanelTitle('');
	
	//alert('Execute Ace Request');
	executeRequest(aTargetPanel, aRequest, aStylesheet);
}

function updateSettingsPanel(aTargetPanel) {
//	----------------------------------------------------------------------
//	Settings panel listener that executes a map, ace and coordinate_systems
//	request in order to bring up the settings panel.
//	Parameters:
//		aTargetPanel: target panel where the request will be executed
//	---------------------------------------------------------------------- 

	// set up request
	aRequest =  '<request>'+
						'<name>map</name>'+
						'<format>xml</format>'+
					'</request>'+
					'<request>'+
						'<name>ace</name>'+
						'<command>aces</command>'+
						'<format>xml</format>'+
				'</request>'+
				'<request>'+
						'<name>coordinate_systems</name>'+
						'<format>xml</format>'+
				'</request>';

	
	aStylesheet = 'panel_settings.xsl';
	
	header.writePanelTitle(PREFS_TITLE);

	executeRequest(aTargetPanel, aRequest, aStylesheet);
}

function updateLayersPanel(aTargetPanel) {
//	----------------------------------------------------------------------
//	Settings panel listener that executes a map, ace and coordinate_systems
//	request in order to bring up the settings panel.
//	Parameters:
//		aTargetPanel: target panel where the request will be executed
//	---------------------------------------------------------------------- 

	// set up request
	aRequest =  '<request>'+
						'<name>layers</name>'+
						'<format>xml</format>'+
				'</request>';
	
	aStylesheet = 'panel_layers.xsl';
	
	header.writePanelTitle(LAYERS_TITLE);

	executeRequest(aTargetPanel, aRequest, aStylesheet);
}
