/*
 * Vroomtrap
 * Copyright(c) 2008, Nubesoft, LLC.
 */
Ext.namespace('Ext.app');
Ext.app.SearchField = Ext.extend(Ext.form.TwinTriggerField, {
    initComponent : function(){
        Ext.app.SearchField.superclass.initComponent.call(this);
        this.on('specialkey', function(f, e){
            if(e.getKey() == e.ENTER){
                this.onTrigger2Click();
            }
        }, this);
    },
    validationEvent:false,
    validateOnBlur:false,
    trigger1Class:'x-form-clear-trigger',
    trigger2Class:'x-form-search-trigger',
    hideTrigger1:true,
    width: 400,
    hasSearch : false,
    paramName : 'query',
    onTrigger1Click : function(){
        if(this.hasSearch){
            this.el.dom.value = '';
        }
    },
    onTrigger2Click : function(){
      if (Ext.getCmp('searchaddy').pressed){
        var searchValue = this.getRawValue();      
        if(searchValue.length < 1){
            this.onTrigger1Click();
            return;
        }        
        Ext.getCmp('map').geoCodeLookup(searchValue);
        nubesoft.google.Vroomtrap.loadMarkers(); 
      }
      if (Ext.getCmp('searchtrap').pressed){
        var searchValue = this.getRawValue();      
        if(searchValue.length < 1){
            this.onTrigger1Click();
            return;
        }
        Ext.Ajax.request({
        	url : 'searchForTrap' , 
        	params : { tpid : searchValue , devi : 'ws'},
        	method: 'POST',
        	success: function ( result, request ) { 
        	  data = Ext.util.JSON.decode(result.responseText);

        	  if (data.status == "success" ){
        	    Ext.getCmp('speedtraps').toggle(true);
        	    trapLocation = new GLatLng(data.lat, data.lng);
        	    Ext.getCmp('map').getMap().setCenter(trapLocation, 17);
        	    nubesoft.google.Vroomtrap.loadMarkers(); 
        	      
        	  } else if (data.status == "failed") {
        	    Ext.MessageBox.alert('Not Found', 'Vroomtrap could not locate the Trap with ID: ' + searchValue);     
        	  }
        	},
        	failure: function ( result, request) { 
        		Ext.MessageBox.alert('Failed', result.responseText); 
        	} 
        });
      }
    }
});

