/*
 * em_toc.js
 * Enchanted Mountains TOC javascript
 * modification of the following
*/

//---------------------------------------------------------------
// 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.
//
// Notice: This code may be freely distributed, used and
//         modified provided that this comment remains intact.
//---------------------------------------------------------------

/*
 * See aimsLayers.js
 * var LayerName = new Array();
 * var LayerID = new Array();
 * var LayerVisible = new Array();
 * var ActiveLayerIndex = 0;
 * var ActiveLayer;
 * var layerCount;
*/

var debugOn = 0;

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 = 'images/legend/';                   // path to icon images
	this.swatchPath = 'images/legend/';                 // path to swatch images
	this.legendPath = 'images/legend/';                 // path to legend images

	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); };
}
var _cache = new _TOC_IMAGE_CACHE();

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();
}

_TOC = TOC.prototype;

_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;
}
// Events
_TOC.onVisibleClick = function(tocid,value) {
	var item = this.root.findItemByTocID(tocid);
	item.onVisibleClick(value);
	/*this.refresh();
	this.refreshMap();*/
	return false;
}


_TOC.addItem = function(item) {
	return this.root.addItem(item);
}

_TOC.addGroup = _TOC.addItem;
_TOC.addLayer = _TOC.addItem;

_TOC.refresh = function() {
    return this.writeHTML();
}

_TOC.refreshMap = function() {
	if ((this.autoRefreshMap) & (okToSend))
		sendMapXML();
}

_TOC.writeHTML = function() {
	var s = "";
	if (!this.isInited)	this.init();
	if (!this.isInited) return '';
	//--------
	// CASCADE
	//--------
	s += this.root.writeHTML();

	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;

// 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.onVisibleClick = function(value) {
	this.setVisible(value);
}
_GROUP.setVisible = function(value) {
	for (var i=0, n=this.items.length; i<n; i++)
		this.items[i].setVisible(value);
}

_GROUP.addItem = function(item) {
	item.parent = this;
	this.items[this.items.length] = item;
	return item;
}

_GROUP.addLayer = _GROUP.addItem;
_GROUP.addGroup = _GROUP.addItem;

_GROUP.getItem = function(index) {
	return this.items[index];
}

_GROUP.writeHTML = function() {
    var s = '';
    if (this.tocid == 0) {
      //
    } else {
	    s += '\n<div id="group_'+ this.tocid +'" class="toc_group">\n';
	    s += ' <span><input type="checkbox" title="Show all '+ this.caption +' layers"/><a href="#'+ this.caption +'" title="Close the '+ this.caption +' Category">'+ this.caption +'</a></span>';
        s += '\n <ul id="group_'+ this.tocid +'_ul">';
    }

     //class="ul_hide"

	//--------
	// CASCADE
	//--------
	//if (this.opened) {
		for (var i=0, n=this.items.length; i<n; i++) {
			s += this.items[i].writeHTML();
		}
	//}
	if (this.tocid == 0) {
      //
    } else {
	    s += '\n </ul>\n</div>\n';
    }
	return s;
}

//-----------------
// LAYER
//-----------------

function LAYER(name, caption, swatch, legend, labelField, layerType) {
	// 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;
	this.layerType = layerType || '';
};

_LAYER = LAYER.prototype;

// 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.setVisible = function(value) {
    LayerVisible[this.index] = value;
    //setVisibility(this.index);
}

_LAYER.writeHTML = function() {
	var s = '';
	//--------------
	// LAYER
	//--------------
	LayerSwatch[this.index] = this.swatch;
	s += '\n  <li><input id="input_'+ this.index +'" type="checkbox" title="Click to see ' + this.caption+' '+this.index + '"';
    s += (LayerVisible[this.index] == 1?' checked="checked" ':'')+'/>';		
    s += (this.swatch!=null?'<img src="'+this.swatch.src+'" alt="Icon for '+this.caption+'" id="icon_'+this.index+'" class="icon_toc" width="16" height="16" />':'');
    //s += (this.swatch!=null?(isIE?'<div class="icon_toc" id="icon_'+this.index+'" title="Icon for '+this.caption+'"><div style="background-image:none; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+this.swatch.src+'\', sizingMethod=\'crop\')">&nbsp;</div></div>':'<img src="'+this.swatch.src+'" alt="Icon for '+this.caption+'" id="icon_'+this.index+'" class="icon_toc" />'):'');
    //s += (this.swatch!=null?'<img src="' + this.swatch.src + '"' + _cache.strIconSize + ' alt="Icon for '+this.caption+'" id="icon_'+this.index+'">':'');
    s += '<label for="input_' + this.index + '" title="Click to see '+this.caption+' ['+this.index+':'+this.tocid+']">' + this.caption + '</label>';
    s += '</li>';
	return s;
}
//alert("em TOC");
