﻿if (!iMap){
	var iMap=new Object();
}

iMap.action_layer_clearAll;
iMap.action_layer_apply ;
iMap.layerSettings="";

iMap.layerstorLoaded=false;
function layerdataLoaded(store){
//alert("loaded");
	var rs=iMap.imm_layerGrid.getStore().data;
	for (var i=0;i<rs.getCount();i++){
		var record = iMap.imm_layerGrid.getStore().getAt(i);
		var url=record.data['kmlUrl'];
		if (record.data['checked']==true){
			    iMap.showOverlay(record.data['kmlUrl'],record.data['minZoomLevel'],record.data['maxZoomLevel']);
		}
		if (iMap.layerOverlays[url] && iMap.layerOverlays[url]!=null){
		    record.set('checked', true);
		}
		
	}
	iMap.updateActiveOverlays();
	iMap.imm_layerGrid.doLayout();
	
}

iMap.layerHttpProxy=new Ext.data.HttpProxy({url: 'cmdHandler.ashx?cmd=loadLayers'});

iMap.layerRecordDef = Ext.data.Record.create([
	{name: 'layertext'},
	{name: 'checked', type: 'boolean'},
	{name: 'icon'},
	{name: 'groupName'},
	{name: 'value'},
	{name: 'groupKey'},
	{name: 'description'},
	{name:'kmlUrl'},
	{name:'minZoomLevel'},
	{name:'maxZoomLevel'},
	{name:'hint'}
]);

Ext.onReady(function(){

Ext.grid.GridView.override({
    layout : function(){
        if(!this.mainBody){
            return; // not rendered
        }
        var g = this.grid;
        var c = g.getGridEl();
        var csize = c.getSize(true);
        var vw = csize.width;

        if(vw < 20 || csize.height < 20){ // display: none?
            return;
        }

        if(g.autoHeight){        
            if (g.maxHeight) {
                var hdHeight = this.mainHd.getHeight();
                var vh = Math.min(csize.height, g.maxHeight);
                this.el.setSize(csize.width, vh);    
                this.scroller.setSize(vw, vh - hdHeight);            
            } else {
                this.scroller.dom.style.overflow = 'visible';                
            }
        }else{
            this.el.setSize(csize.width, csize.height);

            var hdHeight = this.mainHd.getHeight();
            var vh = csize.height - (hdHeight);

            this.scroller.setSize(vw, vh);
            if(this.innerHd){
                this.innerHd.style.width = (vw)+'px';
            }
        }
        if(this.forceFit){
            if(this.lastViewWidth != vw){
                this.fitColumns(false, false);
                this.lastViewWidth = vw;
            }
        }else {
            this.autoExpand();
            this.syncHeaderScroll();
        }
        this.onLayout(vw, vh);
    }
}); 




        function clearLayers(){
			var rs=iMap.imm_layerGrid.getStore().data;
			for (var i=0;i<rs.getCount();i++){
				var record = iMap.imm_layerGrid.getStore().getAt(i);
				record.set('checked', false);
			}
			iMap.activeOverlays=0;
		}
		
		iMap.xmlLayerReader = new Ext.data.XmlReader({
				record: "row",      
				id: "id"            
		}, iMap.layerRecordDef);
		
		iMap.imm_layerStore=new Ext.data.GroupingStore({
				listeners:{ 'update':{fn:function(astore){iMap.imm_layerStore.commitChanges();},scope:this},
				'load':{fn:function(astore){layerdataLoaded(astore)},scope:this}},
				reader: iMap.xmlLayerReader,
				proxy: iMap.layerHttpProxy,
				sortInfo:{field: 'layertext', direction: "ASC"},
				groupField:'groupName'
		});
		
		iMap.imm_layerLayerHint=function(itemKey,value){
		 var i;
		    for (i=0;i<iMap.imm_layerStore.data.length;i++){
		       var record = iMap.imm_layerStore.getAt(i);
		       if (record.data['kmlUrl']==itemKey){
		           record.data['hint']=value;
		           record.commit();
		       }
				
		    }
		}
		iMap.updateActiveOverlays=function(){
		    iMap.activeOverlays=0;
		    var i;
		    for (i=0;i<iMap.imm_layerStore.data.length;i++){
		        var record = iMap.imm_layerStore.getAt(i);
		        if (record.data['checked']){
		            iMap.activeOverlays++;
		        }
		    }
		}
		var checkColumn = new Ext.grid.CheckColumn({
				header: "",
				dataIndex: 'checked',
				onMouseDown : function(e, t)
			{
			    if(t.className && t.className.indexOf('x-grid3-cc-'+this.id) != -1){
					e.stopEvent();
					var index = this.grid.getView().findRowIndex(t);
					var record = this.grid.store.getAt(index);
					record.set(this.dataIndex, !record.data[this.dataIndex]);
					if (record.data[this.dataIndex]){
					    iMap.showOverlay(record.data['kmlUrl'],record.data['minZoomLevel'],record.data['maxZoomLevel']);
					} else {
						iMap.hideOverlay(record.data['kmlUrl']);
				
					}
					iMap.updateActiveOverlays();
					
				}
			    
			    },
				width: 35
		});
		
		iMap.imm_layerGrid = new Ext.grid.GridPanel({
            store: iMap.imm_layerStore,
            loadMask: true,
            maxHeight:600,
            autoHeight:true,
            
            columns: [checkColumn,
                {id:'id',header: "id", width: 60,hidden:true, sortable: true, dataIndex: 'id'},
                {header: "", width: 20, sortable: false, dataIndex: 'icon'},
                {header: "text", width: 130, sortable: true, dataIndex: 'layertext'},
                {header: "", width: 20, sortable: true, dataIndex: 'hint'},
                {header: "checked", width: 0,hidden:true, sortable: true, dataIndex: 'checked'},
                {header: "", width: 0,hidden:true, sortable: false, dataIndex: 'groupName'}
              //  ,{header: "description", width: 90, sortable: true,  dataIndex: 'description'}
            ],
            frame:false,
            view: new Ext.grid.GroupingView(
            {
                forceFit:true,
                getRowClass:function(){return "myRow";},
                groupTextTpl:'{text}'
            }),
            plugins:checkColumn,
            width: 300,
             autoHeight:true,
             
          //  height:200,
            hideHeaders :true,
            visible:true,
            collapsible: true,
            collapsed:false,
            enableHdMenu: false,
            iconCls: 'icon-grid',
            renderTo: 'layer_panel'
  });
 


})
