//---------------------------------------------------------------
// dbgtCode.js v.1.5a
// Copyright (C) 2002,2003 David Bollinger (davebollinger@hotmail.com)
//
// Support code for the "dbGroupToc" modification - A grouped
// table of contents for ArcIMS 3.1+ HTML viewer sites.
//
// This version: 1.5a_mod_wnn_001 of dbgtCode.js was modified by Wolf Naegeli.
// Modifications Copyright (c) 2003-2004 by Wolf Naegeli (wndef@nageli.net>.
//
// The original code by David Bollinger can be downloaded from the 
// ArcScripts area at <http://support.esri.com/>.
//
// Notice: This code may be freely distributed, used and
//         modified provided that this comment remains intact.
//---------------------------------------------------------------

//------------------
// _TOC_ID_GENERATOR
//------------------

function _TOC_ID_GENERATOR() {
	this.nextID = 0;
	this.getID = function() {
		var id = this.nextID;
		this.nextID = this.nextID + 1;
		return id;
	}
}
var _idgen = new _TOC_ID_GENERATOR();

//-----------------
// _TOC_IMAGE_CACHE
//-----------------

function _TOC_IMAGE_CACHE() {
	this.iconPath = 'dbGroupToc/';                   // path to icon images
	this.swatchPath = 'dbGroupToc/';                 // path to swatch images
	this.legendPath = 'dbGroupToc/';                 // path to legend images

//wnn 20040805 disabled sizing constraint
//	this.strIconSize = ' WIDTH="16" HEIGHT="16" ';
this.strIconSize = '';

	this.loadImage = function(path,file) {
		if ((file || '') != '') {
			var img = new Image();
			img.src = path + file;
			return img;
		} else {
			return null;
		}
	};
	this.loadIcon = function(file) { return this.loadImage(this.iconPath,file); };
	this.loadSwatch = function(file) { return this.loadImage(this.swatchPath,file); };
	this.loadLegend = function(file) { return this.loadImage(this.legendPath,file); };

	this.iconPixel = this.loadIcon('icon_pixel.gif');           // single transparent pixel
	this.iconBlank = this.loadIcon('icon_blank.gif');           // all transparent placeholder
	this.iconClosed = this.loadIcon('icon_closed.gif');         // closed group folder
	this.iconOpened = this.loadIcon('icon_opened.gif');         // opened group folder
	this.iconChild = this.loadIcon('icon_child.gif');           // connecting line to child item (continued)
	this.iconChildLast = this.loadIcon('icon_childlast.gif');   // connecting line to child item (last one)
	this.iconSibling = this.loadIcon('icon_sibling.gif');       // vertical connector to sibling item
	this.iconLabelOn = this.loadIcon('icon_labelon.gif');       // labelling enabled
	this.iconLabelOff = this.loadIcon('icon_labeloff.gif');     // labelling disabled
	this.iconLayer = this.loadIcon('icon_layer.gif');           // map layer icon
	this.iconHidden = this.loadIcon('icon_hidden.gif');         // layer not visible, unselected checkbox
	this.iconVisible = this.loadIcon('icon_visible.gif');       // layer visible, selected checkbox
	this.iconVisZoomIn = this.loadIcon('icon_viszoomin.gif');   // layer visible, but only when zoomed in closer
	this.iconVisZoomOut = this.loadIcon('icon_viszoomout.gif'); // layer visible, but only when zommed out farther
	this.iconTristate = this.loadIcon('icon_tristate.gif');     // tri-state checkbox, some layers visible
//	this.iconActive = this.loadIcon('icon_active.gif');         // selected radio
//	this.iconInactive = this.loadIcon('icon_inactive.gif');     // unselected radio
//	this.iconHelp = this.loadIcon('icon_help.gif');             // not currently used
	this.layersHelp = this.loadIcon('layersHelp.gif');          // help for LAYERS list

}
var _cache = new _TOC_IMAGE_CACHE();


//-----------------
// TOC
//-----------------

function TOC(title,caption,autoRefreshMap,swatch) {
	// PROPERTIES
	this.title = title || 'LAYERS';
	this.caption = caption || title || 'LAYERS';
	this.root = new GROUP(caption,true,swatch);
	this.isInited = false;
	this.divToc = null;
	this.divTocHelp = null;
	this.autoRefreshMap = autoRefreshMap || false;
	this.LayersGroups = new Array();
}

function TOCNR(title,caption,autoRefreshMap,swatch) {
	var toc = new TOC(title,caption,autoRefreshMap,swatch);
	inherit(this,toc);

	// override GROUP writeHTML for toc.root
	this.root.writeHTML = function(child_shim, sibling_shim) {
		var s = "";
		for (var i=0, n=this.items.length; i<n; i++) {
			s += this.items[i].writeHTML('','');
		}
		return s;
	};

	return this;
}

_TOC = TOC.prototype;

// EVENTS

_TOC.onIconClick = function(tocid) {
	var item = this.root.findItemByTocID(tocid);
	item.onIconClick();
	this.refresh();
	return false;
}

_TOC.onVisibleClick = function(tocid,value) {
	var item = this.root.findItemByTocID(tocid);
	item.onVisibleClick(value);
	this.refresh();
	this.refreshMap();
	return false;
}

_TOC.onActiveClick = function(tocid) {
	var item = this.root.findItemByTocID(tocid);
	item.onActiveClick();
	this.refresh();
	return false;
}

_TOC.onSwatchClick = function(tocid) {
	var item = this.root.findItemByTocID(tocid);
	item.onSwatchClick();
	this.refresh();
	return false;
}

_TOC.onCaptionClick = function(tocid) {
	var item = this.root.findItemByTocID(tocid);
	item.onCaptionClick();
	this.refresh();
}

_TOC.onLabelClick = function(tocid) {
	var item = this.root.findItemByTocID(tocid);
	item.onLabelClick();
	this.refresh();
	return false;
}

// METHODS

_TOC.init = function() {
	if (LayerName.length == 0) return;
  if (this.root.items.length == 0) {
		var addTo;
		for (var i=0, n=LayerName.length; i<n; i++) {
			addTo = this;
			if (this.LayersGroups) {
				if ((this.LayersGroups.length > i) && (this.LayersGroups[i] != '')) {
					addTo = this.root.findItemByAxlID(this.LayersGroups[i]);
					if (addTo == null) {
						addTo = this.root.addGroup( new GROUP( this.LayersGroups[i], true, null ) );
					}
				}
			}
			addTo.addLayer( new LAYER(LayerID[i], LayerName[i], null, null, null) );
		}
	}
	this.root.init();
	this.isInited = true;
}

_TOC.setOutput = function(divToc, divTocHelp) {
	this.divToc = divToc;
	this.divTocHelp = divTocHelp;
}

_TOC.addItem = function(item) {
	return this.root.addItem(item);
}

_TOC.addGroup = _TOC.addItem;
_TOC.addLayer = _TOC.addItem;

_TOC.refresh = function() {
	if (this.divToc != null)
		this.divToc.innerHTML = this.writeHTML();
	if (this.divTocHelp != null)
		this.divTocHelp.innerHTML = this.writeHelpHTML();
}

_TOC.refreshMap = function() {
	if ((this.autoRefreshMap) & (okToSend))
		sendMapXML();
}

_TOC.writeHTML = function() {
	var s = "";
	if (!this.isInited)	this.init();
	if (!this.isInited) return '';

	//------
	// TITLE
	//------
	s += '<div class="LayerListTitle">' + this.title + '<br><a id="LayConHlp" href="#HELP">Layer Controls Help</a></div>';

	//-------------------
	// START OF TOC TABLE
	//-------------------
	s += '<TABLE WIDTH="96%"	BORDER="0" CELLSPACING="0" CELLPADDING="0" NOWRAP>';

	//--------
	// CASCADE
	//--------
	s += this.root.writeHTML('','');

	//-----------------
	// END OF TOC TABLE
	//-----------------
	s += '</TABLE><BR>\n';
	return s;
}

_TOC.writeHelpHTML = function() {
	var s = '';
	s += '<hr><a name="HELP"><img src="' + _cache.layersHelp.src + '"></a>';
	return s;
}

//-----------------
// BUNDLE
// Customization by Wolf Naegeli 2003-2004
// Bundles multiple feature datasets/classes into what appears to the user as a single layer.
// Designed to include sets of layers that are activated (through maxscale and minscale
// in the AXL file) at different scale ranges, as well as complementary datasets that cover
// different geographic areas. Uses quad-state visibility indicator checkbox. Since the user
// cannot selectively turn on or off individual feature datasets/classes, there is no grey
// check mark state. States are: layer off (none of the grouped datasets visible), layer
// visible (at least one of the bundled datasets is displayed), layer on but nothing visible--
// need to zoom in closer, layer on but nothing visible--need to zoom out further.
// There must not be any other bundles or groups included in a bundle!
//-----------------

function BUNDLE(caption,swatch) {
	// PROPERTIES
	this.parent = null;
	this.tocid = _idgen.getID();
	this.caption = caption || '';
	this.axlid = this.caption;
	this.items = new Array();
	this.activeLayers = new Array();
	this.icon = _cache.iconLayer;
	this.swatch = _cache.loadSwatch(swatch);
}

_BUNDLE = BUNDLE.prototype;

// EVENTS

_BUNDLE.onIconClick = function() {
	this.setActive();
}

_BUNDLE.onVisibleClick = function(value) {
	this.toggleVisible();
}

_BUNDLE.onSwatchClick = function() {
	this.setActive();
}

_BUNDLE.onCaptionClick = function() {
	this.setActive();
}

// METHODS

_BUNDLE.init = function() {
	for (var i=0, n=this.items.length; i<n; i++) {
		this.items[i].init();
	}
}

_BUNDLE.findItemByAxlID = function(axlid) {
	if (this.axlid == axlid)
		return this;
	for (var i=0, n=this.items.length; i<n; i++) {
		var item = this.items[i].findItemByAxlID(axlid);
		if (item) return item;
	}
	return null;
}

_BUNDLE.findItemByTocID = function(tocid) {
	if (this.tocid == tocid)
		return this;
	return null;
}

_BUNDLE.addItem = function(item) {
	item.parent = this;
	this.items[this.items.length] = item;
	return item;
}

_BUNDLE.addLayer = _BUNDLE.addItem;
_BUNDLE.addBUNDLE = _BUNDLE.addItem;

_BUNDLE.getItem = function(index) {
	return this.items[index];
}

_BUNDLE.setActive = function() {
//	setActiveLayer(this.index,this.caption,this.items);
	setActiveLayer(this.items[0].index);
}

_BUNDLE.getVisible = function() {
	for (var i=0, n=this.items.length, v=0; i<n; i++)
		v += this.items[i].getVisible();
	return (v==0) ? 0 : 1;
}

_BUNDLE.setVisible = function(value) {
	for (var i=0, n=this.items.length; i<n; i++)
		this.items[i].setVisible(value);
}

_BUNDLE.toggleVisible = function() {
	for (var i=0, n=this.items.length; i<n; i++)
		this.items[i].toggleVisible();
}

_BUNDLE.writeHTML = function(child_shim, sibling_shim) {
	var s = "";
	var bundleHighlight = false;
	for (var i=0, n=this.items.length; i<n; i++)
		if (ActiveLayerIndex == this.items[i].index) bundleHighlight = true;

	//-------------------
	// START OF BUNDLE ROW
	//-------------------
	var highlight = bundleHighlight ? ' CLASS="Highlight"' : '';
	s += '<TR' + highlight + '><TD NOWRAP>';

	//-----------------
	// CONNECTING LINES
	//-----------------
	s += child_shim;
	s += '<IMG SRC="' + this.icon.src + '"'  + _cache.strIconSize + ' onmousedown="javascript:t.toc.onIconClick(' + this.tocid + ');" alt="" >';

	//-----------------
	// VISIBLE CHECKBOX
	//-----------------
	var vis = this.getVisible();
	if (vis == 0)
		s += '<IMG SRC="' + _cache.iconHidden.src + '"' + _cache.strIconSize + ' onmousedown="t.toc.onVisibleClick(' + this.tocid + ',1);" alt="Click to show">';
	else
		s += '<IMG SRC="' + _cache.iconVisible.src + '"' + _cache.strIconSize + ' onmousedown="t.toc.onVisibleClick(' + this.tocid + ',0);" alt="Click to hide">';

	//-------------
	// SWATCH IMAGE
	//-------------
	if (this.swatch != null) {
		s += '<IMG SRC="' + this.swatch.src + '"' + _cache.strIconSize + ' onmousedown="t.toc.onSwatchClick(' + this.tocid + ');" alt="">';
	}

	//--------------
	// BUNDLE CAPTION
	//--------------
	s += '<A HREF="javascript:t.toc.onCaptionClick(' + this.tocid + ')">' + this.caption + '</A>';

	//-----------------
	// END OF BUNDLE ROW
	//-----------------
	s += '</TD></TR>';

	return s;
}

//-----------------
// GROUP
//-----------------

function GROUP(caption,opened,swatch) {
	// PROPERTIES
	this.parent = null;
	this.tocid = _idgen.getID();
	this.caption = caption || '';
	this.axlid = this.caption;
	this.opened = opened || false;
	this.items = new Array();
	this.iconOpened = _cache.iconOpened;
	this.iconClosed = _cache.iconClosed;
	this.swatch = _cache.loadSwatch(swatch);
}

_GROUP = GROUP.prototype;

// EVENTS

_GROUP.onIconClick = function() {
	this.toggleOpened();
}

_GROUP.onVisibleClick = function(value) {
	this.setVisible(value);
}

_GROUP.onActiveClick = function() {
	// nop
}

_GROUP.onSwatchClick = function() {
	// nop
}

_GROUP.onCaptionClick = function() {
	this.toggleOpened();
}

_GROUP.onLabelClick = function() {
	// nop
}

// METHODS

_GROUP.init = function() {
	for (var i=0, n=this.items.length; i<n; i++) {
		this.items[i].init();
	}
}

_GROUP.findItemByAxlID = function(axlid) {
	if (this.axlid == axlid)
		return this;
	for (var i=0, n=this.items.length; i<n; i++) {
		var item = this.items[i].findItemByAxlID(axlid);
		if (item) return item;
	}
	return null;
}

_GROUP.findItemByTocID = function(tocid) {
	if (this.tocid == tocid)
		return this;
		for (var i=0, n=this.items.length; i<n; i++) {
			var item = this.items[i].findItemByTocID(tocid);
			if (item) return item;
	}
	return null;
}

_GROUP.addItem = function(item) {
	item.parent = this;
	this.items[this.items.length] = item;
	return item;
}

_GROUP.addLayer = _GROUP.addItem;
_GROUP.addGroup = _GROUP.addItem;
_GROUP.addBundle = _GROUP.addItem;

_GROUP.getItem = function(index) {
	return this.items[index];
}

_GROUP.toggleOpened = function() {
	this.opened = !this.opened;
}

_GROUP.setActive = function() {
	// nop
	// though might be useful as part of a modified identify-all tool
}

_GROUP.toggleLabel = function() {
	for (var i=0, n=this.items.length; i<n; i++)
		this.items[i].toggleLabel();
}

_GROUP.getVisible = function() {
	for (var i=0, n=this.items.length, v=0; i<n; i++)
		v += this.items[i].getVisible();
	return (v==0) ? 0 : (v==n) ? 1 : 99999; // 99999=tristate
}

_GROUP.setVisible = function(value) {
	for (var i=0, n=this.items.length; i<n; i++)
		this.items[i].setVisible(value);
}

_GROUP.toggleVisible = function() {
	for (var i=0, n=this.items.length; i<n; i++)
		this.items[i].toggleVisible();
}

_GROUP.writeHTML = function(child_shim, sibling_shim) {
	var s = "";

	//-------------------
	// START OF GROUP ROW
	//-------------------
	s += '<TR><TD NOWRAP>';

	//------------
	// FOLDER ICON
	//------------
	s += child_shim;
	if (this.opened) {
		s += '<IMG SRC="' + this.iconOpened.src + '"' + _cache.strIconSize + ' onmousedown="t.toc.onIconClick(' + this.tocid + ');" alt="Click to close">';
	} else {
		s += '<IMG SRC="' + this.iconClosed.src + '"' + _cache.strIconSize + ' onmousedown="t.toc.onIconClick(' + this.tocid + ');" alt="Click to open">';
	}

	//-----------------
	// VISIBLE CHECKBOX
	//-----------------
	var vis = this.getVisible();
	if (vis == 0)
		s += '<IMG SRC="' + _cache.iconHidden.src + '"' + _cache.strIconSize + ' onmousedown="t.toc.onVisibleClick(' + this.tocid + ',1);" alt="Click to show">';
	else if (vis == 1)
		s += '<IMG SRC="' + _cache.iconVisible.src + '"' + _cache.strIconSize + ' onmousedown="t.toc.onVisibleClick(' + this.tocid + ',0);" alt="Click to hide">';
	else
		s += '<IMG SRC="' + _cache.iconTristate.src + '"' + _cache.strIconSize + ' onmousedown="t.toc.onVisibleClick(' + this.tocid + ',1);" alt="Click to show">';

	//-------------
	// SWATCH IMAGE
	//-------------
	if (this.swatch != null) {
		s += '<IMG SRC="' + this.swatch.src + '"' + _cache.strIconSize + ' onmousedown="t.toc.onSwatchClick(' + this.tocid + ');" alt="">';
	}

	//--------------
	// GROUP CAPTION
	//--------------
	s += '<A HREF="javascript:t.toc.onCaptionClick(' + this.tocid + ')">' + this.caption + '</A>';

	//-----------------
	// END OF GROUP ROW
	//-----------------
	s += '</TD></TR>';

	//--------
	// CASCADE
	//--------
	if (this.opened) {
		for (var i=0, n=this.items.length; i<n; i++) {
			var new_child_shim = sibling_shim + '<img src="' +
				((i==n-1) ? _cache.iconChildLast.src : _cache.iconChild.src) + '"' + _cache.strIconSize + '>';
			var new_sibling_shim = sibling_shim + '<img src="' + 
				((i==n-1) ? _cache.iconBlank.src : _cache.iconSibling.src) + '"' + _cache.strIconSize + '>';
			s += this.items[i].writeHTML( new_child_shim, new_sibling_shim );
		}
	}

	return s;
}

//-----------------
// LAYER
//-----------------

function LAYER(name,caption,swatch,legend,labelField) {
	// PROPERTIES
	this.parent = null;
	this.tocid = _idgen.getID();
	this.name = name || '';
	this.axlid = this.name;
	this.caption = caption || name || '';
	this.index = -1;
	this.icon = _cache.iconLayer;
	this.swatch = _cache.loadSwatch(swatch);
	this.legend = _cache.loadLegend(legend);
	this.legendVisible = false;
	this.labelField = labelField || '';
	this.labelled = false;
};

_LAYER = LAYER.prototype;

// EVENTS

_LAYER.onIconClick = function() {
	this.setActive();
}

_LAYER.onVisibleClick = function(value) {
	this.toggleVisible();
}

_LAYER.onActiveClick = function() {
	this.setActive();
}

_LAYER.onSwatchClick = function() {
	this.setActive();
//	this.legendVisible = !this.legendVisible;
}

_LAYER.onCaptionClick = function() {
	this.setActive();
}

_LAYER.onLabelClick = function() {
	this.toggleLabel();
}

// METHODS

_LAYER.init = function() {
	for (var i=0, n=LayerID.length; i<n; i++)
		if (LayerID[i] == this.name) {
			this.index = i;
			return;
		}
	for (var i=0, n=LayerName.length; i<n; i++)
		if (LayerName[i] == this.name) {
			this.index = i;
			return;
		}
	if (debugOn > 0)
		alert('Possible error in TOC definition.\nUnable to get layer index for "' + this.name + '".\nCheck TOC definition in dbgtData.js.');
}

_LAYER.findItemByAxlID = function(axlid) {
	if (this.axlid == axlid)
		return this;
	return null;
}

_LAYER.findItemByTocID = function(tocid) {
	if (this.tocid == tocid)
		return this;
	return null;
}

_LAYER.setActive = function() {
	setActiveLayer(this.index);
	var isOk = checkHyperLinkLayer(this.index);
	if (toolMode==15) {
		if (!isOk) {
			currentHyperLinkLayer='';
			currentHyperLinkField='';
			currentHyperLinkPrefix='';
			currentHyperLinkSuffix='';
			alert('This layer does not have any Hyperlinks.');
		}
	}
}

_LAYER.toggleLabel = function() {
	this.labelled = !this.labelled;
}

_LAYER.getVisible = function() {
	return LayerVisible[this.index];
}

_LAYER.setVisible = function(value) {
	LayerVisible[this.index] = value;
}

_LAYER.toggleVisible = function() {
	LayerVisible[this.index] = 1 - LayerVisible[this.index];
}

_LAYER.writeHTML = function(child_shim, sibling_shim) {
	var s = '';

	//-------------------
	// START OF LAYER ROW
	//-------------------
	var highlight = (ActiveLayerIndex == this.index) ? ' CLASS="Highlight"' : '';
	s += '<TR' + highlight + '><TD NOWRAP>';

	//-----------------
	// CONNECTING LINES
	//-----------------
	s += child_shim;
	s += '<IMG SRC="' + this.icon.src + '"'  + _cache.strIconSize + ' onmousedown="javascript:t.toc.onIconClick(' + this.tocid + ');" alt="" >';

	//--------------------
	// VISIBILITY CHECKBOX
	//--------------------
	if (LayerVisible[this.index]) {
		if ((mapScaleFactor>=LayerMinScale[this.index]) && (mapScaleFactor<=LayerMaxScale[this.index])) {
			s += '<IMG SRC="' + _cache.iconVisible.src + '"' + _cache.strIconSize + ' onmousedown="t.toc.onVisibleClick(' + this.tocid + ');" alt="Click to hide">';
		} else if (mapScaleFactor>=LayerMaxScale[this.index]) {
			s += '<IMG SRC="' + _cache.iconVisZoomIn.src + '"' + _cache.strIconSize + ' onmousedown="t.toc.onVisibleClick(' + this.tocid + ');" alt="Click to hide">';
		} else {
			s += '<IMG SRC="' + _cache.iconVisZoomOut.src + '"' + _cache.strIconSize + ' onmousedown="t.toc.onVisibleClick(' + this.tocid + ');" alt="Click to hide">';
		}
	} else {
		s += '<IMG SRC="' + _cache.iconHidden.src + '"' + _cache.strIconSize + ' onmousedown="t.toc.onVisibleClick(' + this.tocid + ');" alt="Click to show">';
	}

	//-------------
	// SWATCH IMAGE
	//-------------
	if (this.swatch != null) {
		s += '<IMG SRC="' + this.swatch.src + '"' + _cache.strIconSize + ' onmousedown="t.toc.onSwatchClick(' + this.tocid + ');" alt="">';
	}

	//--------------
	// LAYER CAPTION
	//--------------
	s += '<A HREF="javascript:t.toc.onCaptionClick(' + this.tocid + ');">' + this.caption + '</A>';	

	//-------------
	// LABEL TOGGLE
	//-------------
	if (this.labelField != "") {
		var labelicon = (this.labelled) ? _cache.iconLabelOn.src : _cache.iconLabelOff.src;
		s += '<IMG SRC="' + labelicon + '"' + _cache.strIconSize + ' onmousedown="t.toc.onLabelClick(' + this.tocid + ');" alt="Toggle Labels">';
	}

	//-----------------
	// END OF LAYER ROW
	//-----------------
	s += '</TD></TR>';

	//--------------------
	// OPTIONAL LEGEND ROW
	//--------------------
	if ((this.legend != null) && (this.legendVisible)) {
		s += '<TR><TD NOWRAP>' + sibling_shim; // connecting lines
		s += '<IMG SRC="' + _cache.iconBlank.src + '"' + _cache.strIconSize + '>'; // layer icon
		s += '<IMG SRC="' + _cache.iconBlank.src + '"' + _cache.strIconSize + '>'; // visible checkbox
		s += '<IMG SRC="' + _cache.iconBlank.src + '"' + _cache.strIconSize + '>'; // active radio
		s += '<IMG SRC="' + this.legend.src + '" WIDTH="' + this.legend.width + '" HEIGHT="' + this.legend.height + '">'; // line up legend with swatch
		s += '</TD></TR>';
	}

	return s;
}


//---------------------------------------------------------------------------
// UTILITY
//---------------------------------------------------------------------------

function inherit(sub,zuper) {
	sub.zuper = zuper;
	for (var i in zuper) {
		sub[i] = zuper[i];
	}
	return sub;
}


//-----------------
// eof
//-----------------



