/*
 * Ext JS Library 2.0 Alpha 1
 * Copyright(c) 2006-2007, Ext JS, LLC.
 * licensing@extjs.com
 *
 * http://extjs.com/license
 */

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:120,
    hasSearch : false,
    paramName : 'query',
	pageSize: 10,

    onTrigger1Click : function(){
        if(this.hasSearch){
        	
//        	if (this.history) {		/* Modify the Search Function to accept Ext.History */
//        		Ext.History.add(this.history);
        	if (this.handler) {		/* Modify the Search Function to accept Ext.History */
        		this.handler('');
        	} else {
	        	if (this.store.lastOptions && this.store.lastOptions.params) {
	        		var o = this.store.lastOptions.params;
	        	} else var o = {};
	        	if (!o.start && this.store.baseParams.limit) o.start = 0;
	            o[this.paramName] = null;
	            if (o['start']) o['start']=0;
	            this.store.reload({params:o});
        	}
            this.el.dom.value = '';
            this.triggers[0].hide();
            this.hasSearch = false;
        }
    },

    onTrigger2Click : function(){
        var v = this.getRawValue();
        if(v.length < 1){
            this.onTrigger1Click();
            return;
        }
//        if (this.history) {		/* Modify the Search Function to accept Ext.History */
//        		Ext.History.add(v+this.history);
        if (this.handler) {		/* Modify the Search Function to accept Ext.History */
        	this.handler(v);
        } else {
	        if (this.store.lastOptions && this.store.lastOptions.params) {
	    		var o = this.store.lastOptions.params;
	        } else var o = {};
	        if (!o.start && this.store.baseParams.limit) o.start = 0;
	        o[this.paramName] = v;
	        if (o['start']) o['start']=0;
	        this.store.reload({params:o});
        }
        this.hasSearch = true;
        this.triggers[0].show();
    }
});