﻿if (!iMap){
	var iMap=new Object();
}

iMap.action_filter_clearAll;
iMap.action_filter_apply ;
iMap.filterSettings="";

iMap.filterstorLoaded=false;
function filterdataLoaded(store){
	
}
iMap.filterHttpProxy=new Ext.data.HttpProxy({url: 'cmdHandler.ashx?cmd=loadFilters'});

iMap.FilterRecordDef = Ext.data.Record.create([
	{name: 'filtertext'},
	{name: 'checked', type: 'boolean'},
	{name: 'icon'},
	{name: 'filtergroup'},
	{name: 'value'},
	{name: 'groupKey'},
	{name: 'description'}
]);

Ext.onReady(function(){
		function clearFilters(store){
			var rs=iMap.imm_filterGrid.getStore().data;
			for (var i=0;i<rs.getCount();i++){
				var record = iMap.imm_filterGrid.getStore().getAt(i);
				record.set('checked', false);
			}
		}
		function getFilterBounds(store){
		var rs=iMap.imm_filterGrid.getStore().data;
			iMap.filterSettings="";
			for (var i=0;i<rs.getCount();i++){
				var record = iMap.imm_filterGrid.getStore().getAt(i);
				if	(record.get('checked')){
					
					iMap.filterSettings +=";" + record.id;
				}
				
			}
		  Ext.Ajax.request({
			url: 'getMarkers.ashx',
			success: updateFilteredView,
			method:'GET',
			failure: this.failedRequest,
			headers: {'iMap-header': 'iMap'},
			params: { boundsOnly:1 ,filters:iMap.filterSettings,windowKey:iMap.windowKey,sessionId:iMap.sessionId}
	       });
	       	_st("iMap.showPanel();",800);
		
		}
		function updateFilteredView(r){
		    if (r.status==200){
		        eval(r.responseText);
		        //alert("update view:" + r.responseText);
		    }
		}
		function applyFilters(store){
			var rs=iMap.imm_filterGrid.getStore().data;
			iMap.filterSettings="";
			for (var i=0;i<rs.getCount();i++){
				var record = iMap.imm_filterGrid.getStore().getAt(i);
				if	(record.get('checked')){
					
					iMap.filterSettings +=";" + record.id;
				}
				
			}
			_st("iMap.showPanel();",500);
			iMap.getData();
			
		}
			iMap.filterStatus=new Ext.Toolbar.TextItem({
		   
            text: '&nbsp;'
});
		
		iMap.action_filter_clearAll = new Ext.Button({
				text: '<img src="css/images/filterClear.png" height="16" />',
				handler: function(){
					clearFilters(iMap.imm_filterStore)
				}
		});
		iMap.action_filter_clearAll.addListener("mouseover",function(){setTimeout('iMap.setFilterTipp(1)',30);});
	    iMap.action_filter_clearAll.addListener("mouseout",function(){iMap.setFilterTipp(0);});
		
		iMap.action_filter_apply = new Ext.Button({
				text: '<img src="css/images/filterApply.png" height="16" />',
				handler: function(){
					applyFilters(iMap.imm_filterStore)
				}
		});
		iMap.setFilterTipp=function(msgId){
		   var msg="&nbsp;";
		    switch (msgId){
		        case 1: msg=iMap.lang.filterButtonClearAll;
		            break;
		        case 2: msg=iMap.lang.filterButtonCloseApply;
		            break;
		        case 3: msg=iMap.lang.filterButtonCloseApplyBounds;
		            break;
		            
		    }
		    var html='<div><img src="css/images/blue.png" width="8" height="8" />' + msg + '<div>'
		    iMap.filterStatus.el.innerHTML= html;
		}
		
		iMap.action_filter_apply.addListener("mouseover",function(){setTimeout('iMap.setFilterTipp(2)',30);});
	    iMap.action_filter_apply.addListener("mouseout",function(){iMap.setFilterTipp(0);});
		iMap.action_filter_setBounds = new Ext.Button({
				text: '<img src="css/images/filterApplyBounds.png" height="16" />',
				handler: function(){
					getFilterBounds(iMap.imm_filterStore)
				}
		});
		
		iMap.action_filter_setBounds.addListener("mouseover",function(){setTimeout('iMap.setFilterTipp(3)',30);});
		iMap.action_filter_setBounds.addListener("mouseout",function(){iMap.setFilterTipp(0);});
	
		
		Ext.grid.CheckColumn = function(config){
			Ext.apply(this, config);
			if(!this.id){
				this.id = Ext.id();
			}
			this.renderer = this.renderer.createDelegate(this);
		};
		
		Ext.grid.CheckColumn.prototype ={
			init : function(grid)
			{
				this.grid = grid;
				this.grid.on('render', function(){
						var view = this.grid.getView();
						view.mainBody.on('mousedown', this.onMouseDown, this);
				}, this);
			},
			
			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]);
				}
			},
			renderer : function(v, p, record)
			{
			
				p.css += ' x-grid3-check-col-td' ;
				return '<div class=" x-grid3-check-col'+(v?'-on':'')+' x-grid3-cc-'+this.id+'" style="padding-top:4px;">&#160;</div>';
			}
		};
		var checkColumn = new Ext.grid.CheckColumn({
				header: "",
				dataIndex: 'checked',
				width: 35
		});
		

iMap.filterBar=new Ext.Panel({
//height:42,
width:350,
border:false,
bodyStyle:"color:white;background-image: url(css/images/blue.png)",


items:[new Ext.Toolbar({items:[
 iMap.action_filter_clearAll,{xtype: 'tbtext', text: '  '},
    iMap.action_filter_apply,{xtype: 'tbtext', text: '  '},
     iMap.action_filter_setBounds]}),iMap.filterStatus
]
});
	
		Ext.QuickTips.init();
		
		var xg = Ext.grid;
		
		iMap.xmlFilterReader = new Ext.data.XmlReader({
				record: "row",      
				id: "id"            
		}, iMap.FilterRecordDef);
		
		iMap.imm_filterStore=new Ext.data.GroupingStore({
				listeners:{ 'update':{fn:function(astore){iMap.imm_filterStore.commitChanges();},scope:this},
				'load':{fn:function(astore){filterdataLoaded},scope:this}},
				reader: iMap.xmlFilterReader,
				proxy: iMap.filterHttpProxy,
				sortInfo:{field: 'filtertext', direction: "ASC"},
				groupField:'filtergroup'
		});
		iMap.imm_filterGrid = new xg.GridPanel({
    store: iMap.imm_filterStore,
    loadMask: 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: 'filtertext'},
    {header: "checked", width: 0,hidden:true, sortable: true, dataIndex: 'checked'},
    {header: "", width: 0,hidden:true, sortable: false, dataIndex: 'filtergroup'},
    {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: 350,
  // autoHeight:true,
  height:200,
    hideHeaders :true,
    visible:true,
    collapsible: true,
    collapsed:false,
    enableHdMenu: false,
    iconCls: 'icon-grid',
    bbar:iMap.filterBar/*
   new Ext.Button(iMap.action_filter_clearAll),
    iMap.action_filter_apply,
     new Ext.Button(iMap.action_filter_setBounds),
     {xtype: 'tbspacer', width: 150},
     iMap.filterStatus*/
    
    ,
    renderTo: 'filter_panel'
  });
  setTimeout("iMap.resizeFilterPanel(42)",1000);
	
 
});







