visdat.initAttributetable = function (map, layerArray){
	var attributeGrid;

	//Abfrage welcher Layer ist aktiv?
	for(var k = 0; k < layerArray.length; k++){
		
		if (layerArray[k][5] == true && layerArray[k][4] == true){
			layerName = layerArray[k][1];
			
			var allLayers = map.layers;
			var numLayers = map.getNumLayers(); 
			//Reihenfolge der Layer muss neu geordnet werden, so dass highlightFeature-Layer oben liegt
			for(ii= 0; ii<numLayers;ii++){
				if(allLayers[ii].name==layerName){
					var abc = allLayers[ii];
					map.removeLayer(allLayers[ii]);
					map.addLayer(abc);
				}
			};
	
		//ERstellen des Layers für das "Highlighten" (wenn Zeile im Abfrage-Grid selektiert wird)	
		var myPartnerStyles = new OpenLayers.StyleMap({
        	'default': new OpenLayers.Style({
        		externalGraphic: "./clients/eisdielen_dresden/images/ice_cream.png",
                backgroundXOffset: 0,
                backgroundYOffset: 0,
				graphicWidth: 25,
				graphicHeight: 44
        	})
		});
       
		var highlightFeature = new OpenLayers.Layer.Vector("highlightFeature", 
	    	{'spericalMercator': true, 
	    	projection: "EPSG:4326", styleMap: myPartnerStyles},
	    	{displayInLayerSwitcher: false,
            visibillity: true,
            isBaseLayer: false, 'reproject': true}
        );
        highlightFeature.id = 'highlightFeature';
        map.addLayer(highlightFeature);
     
        
        if(layerArray[k][0]=='GML'){
        
	        //zum abfragen muss der bereits bestehende Abfragelayer hier ein zweites mal erstellt werden
	        var atr_layer = new OpenLayers.Layer.GML(layerName, 
				layerArray[k][2],
				{'spericalMercator': true, projection: 'EPSG:4326'},
				{'isBaseLayer': false, reproject: true}
			);	
			
			var urlParse = layerArray[k][2];
		
				
			//Funktion wird bei click auf Zeile im Attribut-Grid aufgeführt
			function connectMap(sm,index,record){
	
				var Nummer = record.data.gid; 
			
				OpenLayers.loadURL(urlParse, "", null, parseData);
	     
		        function parseData(req) {
		        	g =  new OpenLayers.Format.GML({url: urlParse});
					var features = g.read(req.responseText);
			
					for(var beschreibung in features) {
		
						for (var j in features[beschreibung].attributes) {
				
								if (features[beschreibung].attributes[j]==Nummer){
										hw = features[beschreibung].attributes['hw'];
										rw = features[beschreibung].attributes['rw'];
																			
								};
						};
						
					};
					
					//Koordinatenumrechnung in sphericalMercator
					function lonLatToMercator(ll) {
						var lon = ll.lon * 20037508.34 / 180;
				        var lat = Math.log(Math.tan((90 + ll.lat) * Math.PI / 360)) / (Math.PI / 180);
				        lat = lat * 20037508.34 / 180;
				    return new OpenLayers.LonLat(lon, lat);
					}
	
			        var lonLat = new lonLatToMercator(new OpenLayers.LonLat(hw,rw));
					var point = new OpenLayers.Geometry.Point(lonLat.lon, lonLat.lat);
					var pointFeature = new OpenLayers.Feature.Vector(point,null,null);
					highlightFeature.addFeatures(pointFeature);
					highlightFeature.redraw();
					
										
					var xy = new OpenLayers.LonLat(hw,rw)

					var proj2 = new OpenLayers.Projection("EPSG:4326");
					xy.transform(proj2, map.getProjectionObject());
					map.panTo(xy);
		        };
			}	
	
			//Aufruf für benutzerdefinierte Definition in userScripts.js
			attributeGrid = visdat.queryAttributetable(map, layerArray, atr_layer, highlightFeature, null);
			
			//dem attributeGrid wird hier ein selectionModel hinzugefügt 
			 var sm= new Ext.grid.RowSelectionModel({ 
					singleSelect: true,
					listeners: {
						rowselect: {
							fn: function(sm,index,record) {
								connectMap(sm,index,record); 
								
							}
						},
						selectionchange: {
							fn: function(sm,index,record) {
								highlightFeature.destroyFeatures();
							}
						}
					}
				})
	    
		    attributeGrid.selModel = sm;
		
		};
	  
	  if(layerArray[k][0]=='WMS'){
	 	var atr_request = '';
	  	atr_request += layerArray[k][2]+'&SERVICE=WFS&VERSION=1.0.0&REQUEST=getfeature&TYPENAME='+layerArray[k][1];
		attributeGrid = visdat.queryAttributetable(map, layerArray, atr_layer, highlightFeature, atr_request);
	  }
	
	}
	
	}
		
	if(Ext.getCmp('attributeWin')){
		if (Ext.getCmp('attributeWin').items.items.length) {
		    Ext.getCmp('attributeWin').remove(Ext.getCmp('attributeWin').items.items[0]);
		}; 
		Ext.getCmp('attributeWin').add({
			items:attributeGrid
		});
		Ext.getCmp('attributeWin').doLayout();
	};
	    //Funktion zum synchronisieren der Ausdehnung des Gridpanels an das Attributfenster
    function sync(){
		if(Ext.getCmp('attributeWin') && Ext.getCmp('attributeGrid')){
		Ext.getCmp('attributeGrid').setWidth(Ext.getCmp('attributeWin').getWidth()-15);
		Ext.getCmp('attributeGrid').setHeight(Ext.getCmp('attributeWin').getHeight()-34);
		}
	}
	
	sync();
	
	return attributeGrid;
}

visdat.showAttributetable = function (map, layerArray){
	    	    
	    //Funktion zum synchronisieren der Ausdehnung des Gridpanels an das Attributfenster
	    function sync(){
			if(Ext.getCmp('attributeWin')){
			Ext.getCmp('attributeGrid').setWidth(Ext.getCmp('attributeWin').getWidth()-15);
			Ext.getCmp('attributeGrid').setHeight(Ext.getCmp('attributeWin').getHeight()-34);
			}
		}
	    
		// Erstellen des Attributfensters
		var attributeWin = new Ext.Window({
			id: 'attributeWin',
	        layout: 'fit',
	        title: 'Eisdielen',
	        sizeable: true,
	        width:214,
	        height:240,
	        autoScroll: true,
	        items: visdat.initAttributetable(map, layerArray),
	        listeners:{
	        	resize: sync
	        }
		});
		attributeWin.doLayout();
		
		attributeWin.on('close', function() {

			map.removeLayer(map.getLayer('highlightFeature'));
	
			if(Ext.getCmp('attribute-btn').pressed == true){
	   			Ext.getCmp('attribute-btn').toggle();
	   		}
		});
		
		attributeWin.show(this);		
}
