﻿//init variables
var rootNode = new Ext.tree.TreeNode({
	text: 'Kiválasztott objektumok'					
});

var selected_tree = new Ext.tree.TreePanel({
	root: rootNode,
	collapsed: false
}); 

/**
 * Provides info on queryable object through getFeatureInfo request
 * @param {Object} e
 */
function getFeatureInfo(e){
    collectLocatable(ujbudaVectorWMS.params.LAYERS);
	if (locatableLayers.length > 0)
	{
		var url = ujbudaVectorWMS.getFullRequestString({
			REQUEST: "GetFeatureInfo",
			VERSION: "1.1.1",
			EXCEPTIONS: "application/vnd.ogc.se_xml",
			BBOX: ujbudaVectorWMS.map.getExtent().toBBOX(),
			X: e.xy.x,
			Y: e.xy.y,
			INFO_FORMAT: 'text/xml',
			//IIS bug fix
			LAYERS: null,
			QUERY_LAYERS: locatableLayers,
			WIDTH: ujbudaVectorWMS.map.size.w,
			HEIGHT: ujbudaVectorWMS.map.size.h,
			FEATURE_COUNT: 2
		});

		OpenLayers.loadURL(url, '', this, showInfo);
		OpenLayers.Event.stop(e);
	}
}
 
			
            function load_xml_content_string(xmlData){
                if (window.ActiveXObject) {
                    //for IE
                    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                    xmlDoc.async = "false";
                    xmlDoc.loadXML(xmlData);
                    return xmlDoc;
                }
                else 
                    if (document.implementation && document.implementation.createDocument) {
                        //for Mozila
                        parser = new DOMParser();
                        xmlDoc = parser.parseFromString(xmlData, "text/xml");
                        return xmlDoc;
                    }
            }
            
            function showInfo(evt){            
                //IE bug
                var xmlobject = load_xml_content_string(evt.responseText);
				
				while(rootNode.firstChild) {
					rootNode.removeChild(rootNode.firstChild);
				}
				
				for (var i = 0; i < xmlobject.childNodes[0].childNodes.length; i++) {
                    treeElement = new Ext.tree.TreeNode({
						text: xmlobject.documentElement.childNodes[i].childNodes[0].attributes[0].nodeValue
                    });
                    rootNode.appendChild(treeElement);	

					for (var j = 0; j < xmlobject.documentElement.childNodes[i].childNodes[0].childNodes.length; j++) {
						attribElement = new Ext.tree.TreeNode({
                            text: xmlobject.documentElement.childNodes[i].childNodes[0].childNodes[j].attributes[0].nodeValue + ": " + xmlobject.documentElement.childNodes[i].childNodes[0].childNodes[j].textContent,
							attributeName: xmlobject.documentElement.childNodes[i].childNodes[0].childNodes[j].attributes[0].nodeValue					
                        });
                        treeElement.appendChild(attribElement);	
                    }
                }
			
				//sort function
				//TODO: check with the new format.
				//sortedAttributeNames = [['FH2009_FOLDRESZLET','HRSZ','ALR_JEL'],['FH2010_foldreszlet','HRSZ_felirat','Helyseg','Fekves','Kozterulet-e','HRSZ','Alreszlet','Iranyitoszam','Utcanev','Kozterulet_jellege','HSZ-tol','HSZ-ig','Betu-tol','Betu-ig','Muvelesi_ag','Kivett_tipus','Foldminoseg','Foldterulet_m2','Alreszlet_terulete_m2','Kataszteri_jovedelem','Alreszlet_jovedelem','Szelevenyszam','Jogijelleg','Bejegyzo_hatarozat']];
				var layersToBeSorted = [];

				//create an array with the layer names for which sorting information is available
				for (var i = 0; i < sortedAttributeNames.length; i++) {
					layersToBeSorted.push(sortedAttributeNames[i][0]);
				}
				
				//for all visible and queryable layers
				for (var i=0; i < rootNode.childNodes.length; i++) {
					//check whether we have sorting info about the particular layer
					for (var j=0; j < layersToBeSorted.length; j++) {
						//if we have
						if (rootNode.childNodes[i].text == layersToBeSorted[j]) {
							//look for the layer order (the 0th element is the layer name)
							for (var k=1; k < sortedAttributeNames[j].length; k++)  {
								//go through the original attribute order
								for (var l=0; l < rootNode.childNodes[i].childNodes.length; l++) {
									//get the 'old' node
									var node = rootNode.childNodes[i].findChild("attributeName", sortedAttributeNames[j][k], true);
																		
									//put it to the end
									//TODO: something is not quite right here...
									if (node) node.parentNode.appendChild(node);	
									//if (node) node.parentNode.removeChild(node);
								}									
							}		
						}
					}
				}
				
				//aktivaljuk a fulet
				//TODO: a hard-coded sorrendet kicserelni valami ertelmesebb fuggvenyre!
				Ext.getCmp("harmonika").items.get(0).expand();
				//kinyitjuk a rootNode-ot
				rootNode.expand();        
            }
