﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Web");

Web.SearchBehavior = function(element) {
    Web.SearchBehavior.initializeBase(this, [element]);

    this._onKeyPressHandler = null;
    this._timer = null;
    this._searchFunction = null;
    this._time = 1000;
}

Web.SearchBehavior.prototype = {
    initialize: function() {
        Web.SearchBehavior.callBaseMethod(this, 'initialize');

        this._onKeyPressHandler = Function.createDelegate(this, this.onKeyPress);
        $addHandler(this.get_element(), 'keyup', this._onKeyPressHandler);
        $addHandler(this.get_element(), 'change', this._onKeyPressHandler);
    },

    dispose: function() {
        $removeHandler(this.get_element(), 'keyup', this._onKeyPressHandler);
        $removeHandler(this.get_element(), 'change', this._onKeyPressHandler);

        Web.SearchBehavior.callBaseMethod(this, 'dispose');
    },

    onKeyPress: function() {
        if (this._timer != null)
            window.clearTimeout(this._timer);

        if (this._searchFunction != null)
            this._timer = window.setTimeout(Function.createDelegate(this, this.search), this._time);
    },

    search: function() {
        eval(this._searchFunction);
    }
}
Web.SearchBehavior.registerClass('Web.SearchBehavior', Sys.UI.Behavior);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
