
function ListView(containerId, container, mapObject) {
	this.mapObject = mapObject;
	this.loaded = false;
	this.HilightColor = '#FFFF00';
	this.BackgroundColor = '#F0F0F0';
	this.font = 'Verdana, Arial, Helvetica, sans-serif';
	this.fontColor = '#000000';
	this.fontBold = 'normal';
	this.fontSize = '8pt';
	this.catFont = 'Verdana, Arial, Helvetica, sans-serif';
	this.catFontColor = '#000000';
	this.catFontBold = 'normal';
	this.catFontSize = '8pt';
	this.catBgChecked = '#E0F0E8';
	this.catBgUnChecked = '#F0D0D0';
	this.imgPlus = 'img/plus_blue.gif';
	this.imgMinus = 'img/minus_blue.gif';
	this.SelectedIndex = -1;
	this.onItemClick = null;
	this.onIconClick = null;
	this.onItemCheck = null;
	this.Box = document.createElement('DIV');
	this.Box.style.position = 'relative';
	this.Box.style.display = 'none';
	this.Box.style.top = 0;
	this.Box.style.left = 0;
	this.Box.style.width = '200px';
	this.Box.style.height = '200px';
	this.Box.style.border = '1px inset silver';
	this.Box.style.overflow = 'auto';
	this.Box.style.fontSize = '9pt';
	this.Box.style.textAlign = 'left';
	this.Box.style.backgroundColor = this.BackgroundColor;
	if (container == null) {
		if (containerId != null) {
			container = document.getElementById(containerId);
		}
	}
	if (container == null) {
		container = document.body;
	}
	container.appendChild(this.Box);
	this.allItems = new Array();
	this.allCats = new Array();
	this.orderedItems = new Array();
	this.orderedCats = new Array();
}


ListView.prototype.catClick = function(ev, obj) {
	if (obj != null) {
		var sId = obj.id;
		var sElement = sId.substr(0, 4);
		var iNodeId = parseInt(sId.substr(4));
		var oCat = this.allCats[iNodeId];
		switch (sElement) {
			case 'plus':
				oCat.expand();
				break;
			case 'labl':
				oCat.expand();
				break;
		}
	}
}

ListView.prototype.listitemClick = function(ev, obj) {
	if (obj != null) {
		var sId = obj.id;
		var sElement = sId.substr(0, 4);
		var iNodeId = parseInt(sId.substr(4));
		switch (sElement) {
			case 'cent':
				this.selectItem(iNodeId);
				break;
			case 'labl':
				this.selectItem(iNodeId);
				break;
		}
	}
}

ListView.prototype.SetPlusMinusIcons = function(sPlus, sMinus) {
	this.imgPlus = sPlus;
	this.imgMinus = sMinus;
}

ListView.prototype.SetVisible = function(bVisible) {
	if (bVisible) {
		this.Box.style.display = 'block';
	} else {
		this.Box.style.display = 'none';
	}
}

ListView.prototype.SetHeight = function(iHeight) {
	//this.Box.style.height = iHeight + 'px';
	this.Box.style.height = iHeight;
	this.refresh();
}

ListView.prototype.SetWidth = function(iWidth) {
	//this.Box.style.width = iWidth + 'px';
	this.Box.style.width = iWidth;
	this.refresh();
}

ListView.prototype.GetDOMContainer = function() {
	return this.Box;
}

ListView.prototype.SetBackgroundColor = function(bgcol) {
	this.BackgroundColor = bgcol;
	this.Box.style.backgroundColor = this.BackgroundColor;
}

ListView.prototype.SetHilightColor = function(hlcol) {
	this.HilightColor = hlcol;
}

ListView.prototype.SetFont = function(sFont) {
	this.font = sFont;
}
ListView.prototype.SetFontColor = function(sFontCol) {
	this.fontColor = sFontCol;
}
ListView.prototype.SetFontBold = function(bBold) {
	if (bBold) {
		this.fontBold = 'bold';
	} else {
		this.fontBold = 'normal';
	}
}
ListView.prototype.SetFontSize = function(iFontSize) {
	this.fontSize = iFontSize + 'pt';
}

ListView.prototype.SetCatFont = function(sFont) {
	this.catFont = sFont;
}
ListView.prototype.SetCatFontColor = function(sFontCol) {
	this.catFontColor = sFontCol;
}
ListView.prototype.SetCatFontBold = function(bBold) {
	if (bBold) {
		this.catFontBold = 'bold';
	} else {
		this.catFontBold = 'normal';
	}
}
ListView.prototype.SetCatFontSize = function(iFontSize) {
	this.catFontSize = iFontSize + 'pt';
}
ListView.prototype.SetCatBGColors = function(scolNormal, scolUnchecked) {
	this.catBgChecked = scolNormal;
	this.catBgUnChecked = scolUnchecked;
}

ListView.prototype.setBorder = function(sBorderStyle) {
	this.Box.style.border = sBorderStyle;
}

ListView.prototype.refresh = function() {
	var iTop = 4;
	for (var i = 0; i < this.orderedCats.length; i++) {
		if (this.allCats[this.orderedCats[i]] != null) {
			var oCat = this.allCats[this.orderedCats[i]];
			iTop+=6;
			oCat.oDiv.style.top = iTop + 'px';
			iTop+=20;
			if (oCat.allChildren.length < 1) {
				oCat.oPlus.src = oCat.imgBlank;
			} else {
				if (oCat.Expanded) {
					oCat.oPlus.src = oCat.imgMinus;
					for (var k = 0; k < oCat.allChildren.length; k++) {
						if (oCat.allChildren[k] != null) {
							oCat.allChildren[k].oDiv.style.top = iTop + 'px';
							oCat.allChildren[k].oDiv.style.visibility = 'visible';
							iTop+=20;
						}
					}
				} else {
					oCat.oPlus.src = oCat.imgPlus;
					for (var k = 0; k < oCat.allChildren.length; k++) {
						if (oCat.allChildren[k] != null) {
							oCat.allChildren[k].oDiv.style.visibility = 'hidden';
						}
					}
				}
			}
		}
	}
}

ListView.prototype.addCat = function(vId, sLabel) {
	var iId = parseInt(vId);
	var newItem = new CatItem(this, iId, sLabel);
	if (newItem != null) {
		this.allCats[iId] = newItem;
		this.orderedCats.push(iId);
		this.Box.appendChild(newItem.oDiv);
	}
	return newItem;
}

ListView.prototype.addItem = function(vId, sType, sLabel, oMapObject) {
	var iId = parseInt(vId);
	var newItem = new ListItem(this, iId, sType, sLabel, oMapObject);
	if (newItem != null) {
		this.allItems[iId] = newItem;
		this.orderedItems.push(iId);
		this.Box.appendChild(newItem.oDiv);
	}
	return newItem;
}

ListView.prototype.removeItem = function(vId) {
	var iId = parseInt(vId);
	var oItem = this.allItems[iId];
	this.Box.removeChild(oItem.oDiv);
	if (oItem.mapObject != null) {
		map.removeOverlay(oItem.mapObject);
	}
	this.allItems[iId] = null;
	if (this.SelectedIndex != -1) {
		this.SelectedIndex = -1;
	}
}

ListView.prototype.removeCat = function(vId) {
	var iId = parseInt(vId);
	var oItem = this.allCats[iId];
	this.Box.removeChild(oItem.oDiv);
	if (oItem.mapObject != null) {
		map.removeOverlay(oItem.mapObject);
	}
	this.allCats[iId] = null;
	if (this.SelectedIndex != -1) {
		this.SelectedIndex = -1;
	}
}

ListView.prototype.checkItem = function(sId, bChecked) {
	if (this.loaded) {
		var iId = parseInt(sId);
		this.allItems[iId].Enabled = bChecked;
		if (this.onItemCheck != null) {
			this.onItemCheck(this.allItems[iId], bChecked);
		}
	}
}

ListView.prototype.iconClick = function(sId) {
	if (this.onIconClick != null) {
		var iId = parseInt(sId);
		if (this.allItems[iId].Enabled) {
			this.onIconClick(this.allItems[iId]);
		}
	}
}

ListView.prototype.selectItem = function(sId) {
	var iId = parseInt(sId);
	if (this.SelectedIndex != iId) {
		if (this.SelectedIndex > 0) {
			this.allItems[this.SelectedIndex].Select(false);
		}
		this.SelectedIndex = iId;
		this.allItems[iId].Select(true);
		this.iconClick(sId);
	}
}

ListView.prototype.removeAll = function() {
	for (var i = 0; i < this.allItems.length; i++) {
		if (this.allItems[i]) {
			this.removeItem(i);
		}
	}
	for (var i = 0; i < this.allCats.length; i++) {
		if (this.allCats[i]) {
			this.removeCat(i);
		}
	}
	this.allItems.length = 0;
	this.allCats.length = 0;
	this.orderedItems.length = 0;
	this.orderedCats.length = 0;
}

ListView.prototype.loadObjects = function() {
	var d = new Date();
	sPolygonsUrl = 'getpolygonlist.php?dummy=' + d.getTime();
	this.mapObject.downloadUrl(sPolygonsUrl, this.createAllObjectsCallback, this);
}

ListView.prototype.createAllObjectsCallback = function(httpObj, owner) {
	if (httpObj.readyState == 4) {
		if (httpObj.status == 200) {
			owner.processXML(GXml.parse(httpObj.responseText));
		}
	}
}

ListView.prototype.processXML = function(xmlobj) {
	this.removeAll();
	var xdoc = xmlobj.documentElement;
	var cats = xdoc.getElementsByTagName("CATEGORY");
	if (cats.length > 0) {
		var xcat = null;
		var titolo = '';
		var catId = 0;
		for (var j = 0; j < cats.length; j++) {
			xcat = cats[j];
			titolo = GXml.value(xcat.getElementsByTagName("TITOLO")[0]);
			if (titolo == '') {
				titolo = 'Senza nome';
			}
			catId = parseInt(xcat.getAttribute("id"));
			this.addCat(catId, titolo);
		}
	}
	var polygons = xdoc.getElementsByTagName("POLYGON");
	if (polygons.length > 0) {
		var xpolygon = null;
		var titolo = '';
		var polId = 0;
		var catId = 0;
		var punti = null;
		var path = null;
		var nvertici = 0;
		var vertici = null;
		var tmpList = new Array();
		for (var j = 0; j < polygons.length; j++) {
			tmpList[parseInt(polygons[j].getAttribute("id"))] = true;
		}
		for (var j = 0; j < this.allItems.length; j++) {
			if (this.allItems[j] != null) {
				if (tmpList[j] != true) {
					this.removeItem(j);
				}
			}
		}
		for (var j = 0; j < polygons.length; j++) {
			xpolygon = polygons[j];
			titolo = GXml.value(xpolygon.getElementsByTagName("TITOLO")[0]);
			if (titolo == '') {
				titolo = 'Senza nome';
			}
			catId = parseInt(xpolygon.getAttribute("id_category"));
			polId = parseInt(xpolygon.getAttribute("id"));
			if (this.allItems[polId] == null) {
				this.allCats[catId].addChild(this.addItem(polId, 'list_item', titolo, xpolygon));
			}
		}
	}
	this.refresh();
	this.loaded = true;
}


function CatItem(oContainer, nId, sLabel) {
	this.cont = oContainer;
	this.id = nId;
	this.label = sLabel;
	this.Expanded = false;
	this.iconWidth = 20;
	this.iconHeight = 16;
	this.imgBlank = 'img/blank.gif';
	this.imgPlus = this.cont.imgPlus;
	this.imgMinus = this.cont.imgMinus;
	this.oDiv = document.createElement('DIV');
	this.oPlus = document.createElement('IMG');
	this.oTag = document.createElement('SPAN');
	this.oDiv.style.position = 'absolute';
	this.oDiv.style.cursor = 'pointer';
	this.oDiv.style.whiteSpace = 'nowrap';
	this.oDiv.style.backgroundColor = this.cont.catBgChecked;
	this.oDiv.style.left = '4px';
	//this.oDiv.style.width = (this.cont.Box.style.pixelWidth - 20) + 'px';
	GEvent.bindDom(this.oPlus, "click", this.cont, this.cont.catClick);
	this.oPlus.id = 'plus' + nId;
	this.oPlus.listview = this.cont;
	this.oPlus.style.position = 'relative';
	this.oPlus.style.width = this.iconWidth;
	this.oPlus.style.height = this.iconHeight;
	this.oPlus.src = this.imgMinus;
	this.oTag.listview = this.cont;
	this.oTag.id = 'labl' + nId;
	GEvent.bindDom(this.oTag, "click", this.cont, this.cont.catClick);
	this.oTag.style.position = 'relative';
	this.oTag.style.height = '16px';
	this.oTag.style.verticalAlign = 'bottom';
	this.oTag.style.paddingLeft = '4px';
	this.oTag.style.paddingRight = '6px';
	this.oTag.style.whiteSpace = 'nowrap';
	this.oTag.style.color = this.cont.catFontColor;
	this.oTag.style.fontFamily = this.cont.catFont;
	this.oTag.style.fontSize = this.cont.catFontSize;
	this.oTag.style.fontWeight = this.cont.catFontBold;
	this.oTag.innerHTML = this.label;
	this.oDiv.appendChild(this.oPlus);
	//this.oDiv.appendChild(this.oCheck);
	this.oDiv.appendChild(this.oTag);
	this.allChildren = new Array();
}

CatItem.prototype.addChild = function(item) {
	this.allChildren.push(item);
}

CatItem.prototype.expand = function() {
	this.Expanded = !this.Expanded;
	this.cont.refresh();
}



function ListItem(oContainer, nId, sType, sLabel, oMapObject) {
	this.Enabled = true;
	this.Selected = false;
	this.cont = oContainer;
	this.id = nId;
	this.objType = sType;
	this.label = sLabel;
	this.oDiv = document.createElement('DIV');
	this.oTypeIcon = document.createElement('IMG');
	this.oTag = document.createElement('SPAN');
	this.oDiv.style.position = 'absolute';
	this.oDiv.style.left = '27px';
	this.oDiv.style.cursor = 'pointer';
	this.oDiv.style.whiteSpace = 'nowrap';
	this.oDiv.style.verticalAlign = 'middle';
	this.oTypeIcon.src = 'img/' + sType + '.gif';
	this.oTypeIcon.listview = this.cont;
	this.oTypeIcon.id = 'cent' + nId;
	GEvent.bindDom(this.oTypeIcon, "click", this.cont, this.cont.listitemClick);
	this.oTag.listview = this.cont;
	this.oTag.id = 'labl' + nId;
	GEvent.bindDom(this.oTag, "click", this.cont, this.cont.listitemClick);
	this.oTag.style.position = 'relative';
	this.oTag.style.height = '16px';
	this.oTag.style.verticalAlign = 'bottom';
	this.oTag.style.paddingLeft = '4px';
	this.oTag.style.paddingRight = '6px';
	this.oTag.style.whiteSpace = 'nowrap';
	this.oTag.style.color = this.cont.fontColor;
	this.oTag.style.fontFamily = this.cont.font;
	this.oTag.style.fontSize = this.cont.fontSize;
	this.oTag.style.fontWeight = this.cont.fontBold;
	this.oTag.innerHTML = this.label;
	this.oDiv.appendChild(this.oTypeIcon);
	this.oDiv.appendChild(this.oTag);
	this.objXML = oMapObject;
	this.mapObject = null;
}

ListItem.prototype.listitem_click = function() {
	//alert('ListItem ' + arguments.length);
	this.cont.listitem_click(this);
}

ListItem.prototype.Select = function(isSelected) {
	if (isSelected) {
		this.oTag.style.backgroundColor = this.cont.HilightColor;
		this.Selected = true;
	} else {
		this.oTag.style.backgroundColor = 'transparent';
		this.Selected = false;
	}
}


