// function to generate the basemaps' nodes of the tree
function generateTreeBasemap(baseMapCollection) {
	tree.deleteChildItems('themes');

	for (var i=0; i<baseMapCollection.length; i++) {
		// get data
		var id = 'base' + delimiter + baseMapCollection[i].attributes.getNamedItem("id").value;
		var text = baseMapCollection[i].attributes.getNamedItem("text").value;
		// insert basemap's node
		tree.insertNewChild('themes',id ,text ,0,0,0,0,"SELECT,CALL,CHILD,CHECKED");
	}
}

// function to generate the themes' nodes of the tree
function generateTreeThemes(themeMapCollection) {
	var baseMapOld = "";
	var parentVisible = false;

	for (var i=0; i<themeMapCollection.length; i++) {
		// get data
		var basemap = 'base' + delimiter + themeMapCollection[i].attributes.getNamedItem("basemap").value;
		var id = 'theme' + delimiter + themeMapCollection[i].attributes.getNamedItem("id").value;
		var text = themeMapCollection[i].attributes.getNamedItem("text").value;
		var visible = themeMapCollection[i].attributes.getNamedItem("visible").value;
		var checked = "";

		// set the parent's node checked/unchecked (checked if we have at least one child visible, otherwise unchecked)
		if ((baseMapOld != basemap) && (baseMapOld != "")) {
			tree.setCheck(baseMapOld, parentVisible);
			parentVisible = false;
		}
		baseMapOld = basemap;

		if (visible == "true") {
			checked = ",CHECKED";
			parentVisible = true;
		}

		// insert theme's node
		tree.insertNewChild(basemap,id , text ,0,0,0,0,"SELECT,CALL,CHILD" + checked);
	}

	// set the parent's node checked/unchecked (checked if we have at least one child visible, otherwise unchecked)
	// after the insert of the last theme
	if (baseMapOld != "") {
		tree.setCheck(baseMapOld, parentVisible);
		parentVisible = false;
	}
}

// function to set the themes' visibility in the tree (eye image)
function generateTreeThemesVisibility(themeMapCollection) {
	for (var i=0; i<themeMapCollection.length; i++) {
		// get data
		var id = 'theme' + delimiter + themeMapCollection[i].attributes.getNamedItem("id").value;
		var visible = themeMapCollection[i].attributes.getNamedItem("visible").value;
		var text = tree.getItemText(id);
		var regExp;

		// construct the regexp to substitute the previous image
		if (_isIE) {
			regExp = new RegExp("<IMG([A-Za-z0-9/_:.-<>=\"\\s]+)IMG>", 'gi');
		} else {
			regExp = new RegExp("<IMG([A-Za-z0-9/_:.-=\"\\s]+)>", 'gi');
		}
		// substitution of the previous image
		text = text.replace(regExp,'');

		// set of the new image
		if ((visible == "true")) {
			tree.setItemText(id, '<img src="treeimgs/eye.gif"></img>' + text);
		} else {
			tree.setItemText(id, '<img src="treeimgs/eyeoff.gif"></img>' + text);
		}
	}
}

// function to generate the legends' nodes of the tree
function generateTreeLegend(legendCollection) {
	var themeOld = "";

	for (var i=0; i<legendCollection.length; i++) {
		// get data
		var theme = 'theme' + delimiter + legendCollection[i].attributes.getNamedItem("theme").value;
		var style = legendCollection[i].attributes.getNamedItem("style").value;
		var label = legendCollection[i].attributes.getNamedItem("label").value;
		var imagePath = legendCollection[i].attributes.getNamedItem("imagepath").value;

		// insert legend's node
		tree.insertNewChild(theme, 'legend' + theme + delimiter + style+i, label,0,0,0,0,"");

		// set the legend's image
		if (imagePath == "") {
			tree.CFsetLegendImageItem('legend'  + theme +  delimiter + style+i, mapviewerurl.replace("as-sit.comune.bolzano.it:8888","sit.comune.bolzano.it") + "?sty=" + style + "&f=png&w=25&h=15&aa=false&ds=" + datasource + "&bg=" + backgroundcolor);
		} else {
			tree.CFsetLegendImageItem('legend'  + theme +  delimiter + style+i, imagePath);
		}

		// close all items of the previous theme
		if ((themeOld != theme) && (themeOld != "")) {
			tree.closeAllItems(themeOld);
		}
		themeOld = theme;
	}

	// close all items of the last theme
	if (themeOld != "") {
		tree.closeAllItems(themeOld);
	}
}

// function to generate the foreign basemaps' nodes of the tree
function generateTreeForeignBasemap(baseMapCollection) {

	for (var i=0; i<baseMapCollection.length; i++) {
		// get data
		var id = 'fbase' + delimiter + baseMapCollection[i].attributes.getNamedItem("id").value;
		var text = baseMapCollection[i].attributes.getNamedItem("text").value;
		// insert foreign basemap's node
		tree.insertNewChild('themesadd',id ,text ,0,0,0,0,"SELECT,CALL,CHILD,CHECKED");
	}
}

// function to generate the foreign themes' nodes of the tree
function generateTreeForeignThemes(themeMapCollection) {

	for (var i=0; i<themeMapCollection.length; i++) {
		// get data
		var basemap = 'fbase' + delimiter + themeMapCollection[i].attributes.getNamedItem("fbasemap").value;
		var id = 'ftheme' + delimiter + themeMapCollection[i].attributes.getNamedItem("id").value;
		var text = themeMapCollection[i].attributes.getNamedItem("text").value;
		var visible = themeMapCollection[i].attributes.getNamedItem("visible").value;
		var checked = "";

		if (visible == "true") {
			checked = ",CHECKED";
		}
		// insert theme's node
		tree.insertNewChild(basemap,id ,text ,0,0,0,0,"SELECT,CALL,CHILD" + checked);
	}
}

// function to set the foreign themes' visibility in the tree (eye image)
function generateTreeForeignThemesVisibility(themeMapCollection) {
	for (var i=0; i<themeMapCollection.length; i++) {
		// get data
		var id = 'ftheme' + delimiter + themeMapCollection[i].attributes.getNamedItem("id").value;
		var visible = themeMapCollection[i].attributes.getNamedItem("visible").value;
		var text = tree.getItemText(id);
		var regExp;

		// construct the regexp to substitute the previous image
		if (_isIE) {
			regExp = new RegExp("<IMG([A-Za-z0-9/_:.-<>=\"\\s]+)IMG>", 'gi');
		} else {
			regExp = new RegExp("<IMG([A-Za-z0-9/_:.-=\"\\s]+)>", 'gi');
		}
		// substitution of the previous image
		text = text.replace(regExp,'');

		// set of the new image
		if ((visible == "true")) {
			tree.setItemText(id, '<img src="treeimgs/eye.gif"></img>' + text);
		} else {
			tree.setItemText(id, '<img src="treeimgs/eyeoff.gif"></img>' + text);
		}
	}
}

// function to generate the foreign legends' nodes of the tree
function generateTreeForeignLegend(legendCollection) {
	var themeOld = "";

	for (var i=0; i<legendCollection.length; i++) {
		// get data
		var theme = 'ftheme' + delimiter + legendCollection[i].attributes.getNamedItem("ftheme").value;
		var style = legendCollection[i].attributes.getNamedItem("style").value;
		var label = legendCollection[i].attributes.getNamedItem("label").value;
		var fdatasource = legendCollection[i].attributes.getNamedItem("datasource").value;
		var imagePath = legendCollection[i].attributes.getNamedItem("imagepath").value;

		// insert legend's node
		tree.insertNewChild(theme, 'flegend'  + theme +  delimiter + style+i, label,0,0,0,0,"");

		// set the legend's image
		if (imagePath == "") {
			tree.CFsetLegendImageItem('flegend'  + theme +  delimiter + style+i, mapviewerurl.replace("as-sit.comune.bolzano.it:8888","sit.comune.bolzano.it") + "?sty=" + style + "&f=png&w=25&h=15&aa=false&ds=" + fdatasource);
		} else {
			tree.CFsetLegendImageItem('flegend'  + theme +  delimiter + style+i, imagePath);
		}

		// close all items of the previous theme
		if ((themeOld != theme) && (themeOld != "")) {
			tree.closeAllItems(themeOld);
		}
		themeOld = theme;
	}

	// close all items of the last theme
	if (themeOld != "") {
		tree.closeAllItems(themeOld);
	}
}

// function to generate the query themes' nodes of the tree
function generateTreeThemeQuery(themesQueryCollection) {
	var themeSelectedId = tree.getSelectedItemId();
	for (var i=0; i<themesQueryCollection.length; i++) {
		// get data
		var id = 'qtheme' + delimiter + themesQueryCollection[i].attributes.getNamedItem("id").value;
		var text = themesQueryCollection[i].attributes.getNamedItem("text").value;
		var visible = themesQueryCollection[i].attributes.getNamedItem("visible").value;
		var checked = "";

		if (visible == "true") {
			checked = ",CHECKED";
		}
		// insert theme's node
		tree.insertNewChild('themesquery',id ,text ,0,0,0,0,"SELECT,CALL,CHILD" + checked);

		// set the current query 
		actualQuery = themesQueryCollection[i].attributes.getNamedItem("id").value;
		actualOrderBy = "";
		actualDirection = "";
	}
	tree.selectItem(themeSelectedId);
}

// function to set the query themes' visibility in the tree (eye image)
function generateTreeQueryThemesVisibility(themeMapCollection) {
	for (var i=0; i<themeMapCollection.length; i++) {
		// get data
		var id = 'qtheme' + delimiter + themeMapCollection[i].attributes.getNamedItem("id").value;
		var visible = themeMapCollection[i].attributes.getNamedItem("visible").value;
		var text = tree.getItemText(id);
		var regExp;

		// construct the regexp to substitute the previous image
		if (_isIE) {
			regExp = new RegExp("<IMG([A-Za-z0-9/_:.-<>=\"\\s]+)IMG>", 'gi');
		} else {
			regExp = new RegExp("<IMG([A-Za-z0-9/_:.-=\"\\s]+)>", 'gi');
		}
		// substitution of the previous image
		text = text.replace(regExp,'');

		// set of the new image
		if ((visible == "true")) {
			tree.setItemText(id, '<img src="treeimgs/eye.gif"></img>' + text);
		} else {
			tree.setItemText(id, '<img src="treeimgs/eyeoff.gif"></img>' + text);
		}
	}
}

// function to hadle the oncheck event
function toncheck(id,state){
	// for legend level do nothing
	if (tree.getLevel(id) == 4) {  //legend level
		return;
	}
	// for root level set the right image (when we click, automatically will set the default image of checked) and do nothing
	if (tree.getLevel(id) == 1 && id == 'themes') { //root level
		tree.CFsetLegendImageRoot(id, "treeimgs/theme.gif");
		return;
	} else if (tree.getLevel(id) == 1 && id == 'themesquery') {
		tree.CFsetLegendImageRoot(id, "treeimgs/query.gif");
		return;
	} else if (tree.getLevel(id) == 1 && id == 'themesadd') {
		tree.CFsetLegendImageRoot(id, "treeimgs/themeadd.gif");
		return;
	}

	if (tree.getLevel(id) == 2 && (tree.getParentId(id) == 'themes' || tree.getParentId(id) == 'themesadd' || tree.getParentId(id) == 'themesquery')) {//basemap level
		// in case of query
		if (tree.getParentId(id) == 'themesquery') {

			// in case we check a query
			if (state == 1) {
				var listaFigli = tree.getAllSubItems(tree.getParentId(id)).split(",");

				for (var i=0; i<listaFigli.length; i++) {
					//tree.setCheck(listaFigli[i], false);
					if (listaFigli[i].split(delimiter)[1] == actualQuery) {
						// close the query builder div and uncheck the current query
						ChiudiQueryFinestra();
					}
				}

				// set checked the choosen query
				tree.setCheck(id, true);

				// enable the checked query on the map
				performEnableQueryTheme(id.split(delimiter)[1], true, false, actualQuery);
				actualQuery = id.split(delimiter)[1];
				actualOrderBy = "";
				actualDirection = "";
			} else {
				// in case we uncheck a query
				//performEnableQueryTheme(id.split(delimiter)[1], false, false);
				if (id.split(delimiter)[1] == actualQuery) {
					// close the query builder div, disable the current query on the map and uncheck the current query
					ChiudiQueryBuilder();
				}
			}
		} else {
			// in case of basemap or foreign basemap
			// in case we check a basemap
			if (state == 1) {
				// set checked the children of the choosen basemap
				tree.setSubChecked(id, true);
				// enable the basemap on the map
				performEnableBaseTheme(id.split(delimiter)[1], true);
			} else {
				// in case we uncheck a basemap
				// set unchecked the children of the choosen basemap
				tree.setSubChecked(id, false);
				// disable the basemap on the map
				performEnableBaseTheme(id.split(delimiter)[1], false);
			}
		}
	} else if (tree.getParentId(tree.getParentId(id)) == 'themes' || tree.getParentId(tree.getParentId(id)) == 'themesadd') {//theme level
		// in case of theme or foreign theme
		// in case we check a theme
		if (state == 1) {
			var idPadre = tree.getParentId(id);
			// set checked the parent of the choosen theme
			tree.setCheck(idPadre, true);
			// enable the theme on the map
			performEnableTheme(id.split(delimiter)[1], true);
		} else {
			// in case we uncheck a theme
			var idPadre = tree.getParentId(id);

			var listaFigli = tree.getAllSubItems(idPadre).split(",");

			var bTuttiSenzaCheck = true;

			// control if we have a checked child
			for (var i=0; i<listaFigli.length; i++) {
				if (tree.isItemChecked(listaFigli[i]) == 1) {
					bTuttiSenzaCheck = false;
				}
			}

			// if we have all children unchecked, then uncheck the parent
			if (bTuttiSenzaCheck == true) {
				if (state == 1) {
					tree.setCheck(idPadre, true);
				} else {
					tree.setCheck(idPadre, false);
				}
			}

			// disable the theme on the map
			performEnableTheme(id.split(delimiter)[1], false);
		}
	}
}

// function to hadle the doubleclick event (zoom to theme)
function zoomToTheme(id) {
	//if (tree.getLevel(id) != 3) {
	//	return;
	//}
	if ((tree.getParentId(tree.getParentId(id)) == 'themes' || tree.getParentId(tree.getParentId(id)) == 'themesadd') && tree.getLevel(id) == 3) {
		// in case of theme or foreign theme
		// zoom to theme on the map
		performZoomToTheme(id.split(delimiter)[1]);
	} else if ((tree.getParentId(id) == 'themesquery') && tree.getLevel(id) == 2) {
		// in case of query
		// zoom to query on the map (spatial query)
		performZoomToQuery(id.split(delimiter)[1]);
	} else {
		// do nothing
		return;
	}
}
