/*****************************************************************************
 *
 * File     : Boundary.js
 * 
 * Project  : WMS Viewer Client
 * 
 * Contents : Represents a Boundary object as loaded from the boundaries.xml file
 *
 * Author: Milan Trninic
 *
 * Copyright 1999-2005 Galdos Systems, Inc.
 * All rights reserved.
 * 
 ***|***************************|***********************|*******************|*/

/************************************************************************
*
*	Global declarations
*
*	
*
*************************************************************************/

/************************************************************************
*
*	function:	Boundary
*
*	purpose:	Constructor
*
*************************************************************************/
function Boundary(doc)
{
	// method assignments
	this.init = Boundary.init;
	this.getDefaultSrsName = Boundary.getDefaultSrsName;
	this.getBoundaryZones = Boundary.getBoundaryZones;
	this.getBoundaryExtents = Boundary.getBoundaryExtents;
  	this.getBoundaryLayers = Boundary.getBoundaryLayers;
  	this.getBoundaryFeatures = Boundary.getBoundaryFeatures;
  	this.getBoundarySrsNames = Boundary.getBoundarySrsNames;
  	this.getBoundaryBbox = Boundary.getBoundaryBbox;
  	this.getNodesByName = Boundary.getNodesByName;
  
	// instance field declarations
	this._doc;
	this._defaultSrsName;

	// initialization
	this.init(doc);
}

/************************************************************************
*
*	function:	init
*
*	purpose:	initialization
*
*************************************************************************/
function Boundary.init(node) {
	this._defaultSrsName = node.getAttribute("defaultSrsName");
	
	this._doc = new ActiveXObject("Msxml2.DOMDocument.3.0");
	this._doc.async = false;
	this._doc.appendChild(node);
	this._doc.setProperty("SelectionLanguage", "XPath");
	this._doc.setProperty("SelectionNamespaces",
		"xmlns='http://www.galdosinc.com/fps' xmlns:fps='http://www.galdosinc.com/fps'");
}

/************************************************************************
*
*	function:	getDefaultSrsName
*
*	purpose:	Returns the default srs name
*
*************************************************************************/
function Boundary.getDefaultSrsName() {	
	return this._defaultSrsName;
}

/************************************************************************
*
*	function:	getBoundaryZones
*
*	purpose:	Returns a list of zones for this Boundary
*
*************************************************************************/
function Boundary.getBoundaryZones() {	
	var xpath = "/Boundary/BoundaryZone";
	return this.getNodesByName(xpath);
}

/************************************************************************
*
*	function:	getBoundaryExtents
*
*	purpose:	Returns a list of extents for a zone in this Boundary
*
*************************************************************************/
function Boundary.getBoundaryExtents(zone) {		
	var xpath = "/Boundary/BoundaryZone[@name='" + zone + "']/BoundaryExtent";
	return this.getNodesByName(xpath);
}

/************************************************************************
*
*	function:	getBoundaryLayers
*
*	purpose:	Returns a list of layers for an extent in this Boundary
*
*************************************************************************/
function Boundary.getBoundaryLayers(zone, extent) {		
	var xpath = "/Boundary/BoundaryZone[@name='"
		+ zone + "']/BoundaryExtent[@name='"
		+ extent + "']/BoundaryLayer";
	return this.getNodesByName(xpath);
}

/************************************************************************
*
*	function:	getBoundaryFeatures
*
*	purpose:	Returns a list of features for a layer in this Boundary
*
*************************************************************************/
function Boundary.getBoundaryFeatures(zone, extent, layer) {		
	var xpath = "/Boundary/BoundaryZone[@name='"
		+ zone + "']/BoundaryExtent[@name='"
		+ extent + "']/BoundaryLayer[@name='"
		+ layer + "']/BoundaryFeature";
	return this.getNodesByName(xpath);
}

/************************************************************************
*
*	function:	getBoundarySrsNames
*
*	purpose:	Returns a list of srsNames for a feature in this Boundary
*
*************************************************************************/
function Boundary.getBoundarySrsNames(zone, extent, layer, feature) {
	var xpath = "/Boundary/BoundaryZone[@name='"
		+ zone + "']/BoundaryExtent[@name='"
		+ extent + "']/BoundaryLayer[@name='"
		+ layer + "']/BoundaryFeature[@name='"
		+ feature + "']/BoundingBox";
	var nodes = this._doc.selectNodes(xpath);
	var result = new Collection();
	for (var index = 0; index < nodes.length; index++) {
		var node = nodes.item(index);
		result.add(node.getAttribute("srsName") + " " + node.getAttribute("srsTag"));
	}	
	return result;	
}

/************************************************************************
*
*	function:	getBoundaryBbox
*
*	purpose:	Returns the BBOX for a srsName in this Boundary
*
*************************************************************************/
function Boundary.getBoundaryBbox(zone, extent, layer, feature, srsName) {
	var xpath = "/Boundary/BoundaryZone[@name='"
		+ zone + "']/BoundaryExtent[@name='"
		+ extent + "']/BoundaryLayer[@name='"
		+ layer + "']/BoundaryFeature[@name='"
		+ feature + "']/BoundingBox[@srsName='"
		+ srsName + "']";		
	var nodes = this._doc.selectNodes(xpath);
	// Just take first match
	var node = nodes.item(0);
	return new BoundingBox(node.getAttribute("minx"), node.getAttribute("miny")
		,node.getAttribute("maxx"), node.getAttribute("maxy"), srsName);
}

/************************************************************************
*
*	function:	getNodesByName
*
*	purpose:	Returns a list of nodes by name.
*
*************************************************************************/
function Boundary.getNodesByName(xpath) {
	var nodes = this._doc.selectNodes(xpath);
	var result = new Collection();
	for (var index = 0; index < nodes.length; index++) {
		var node = nodes.item(index);
		result.add(node.getAttribute("name"));
	}	
	return result;
}

