﻿Type.registerNamespace("TitanTV");

// Constructor
TitanTV.SearchInput = function(element)
{
  TitanTV.SearchInput.initializeBase(this, [element]);

  // Private variables
  this.postbackId = null;

  // UI Elements
  this.searchInput = null;
  this.searchButton = null;

  // Private methods
  this.onButtonClick = null;
  this.onKeyPress = null;
}

TitanTV.SearchInput.prototype = {

  // property accessors.

  get_postbackId: function() { return this.postbackId; },
  set_postbackId: function(value) { this.postbackId = value; },

  get_searchInput: function() { return this.searchInput; },
  set_searchInput: function(value) { this.searchInput = value; },

  initialize: function()
  {
    var element = this.get_element();

    // create Delgates
    if (this.onKeyPress === null) this.onKeyPress = Function.createDelegate(this, this._onKeyPress);
    if (this.onButtonClick === null) this.onButtonClick = Function.createDelegate(this, this._onButtonClick);

    this.searchInput.add_buttonClick(this.onButtonClick);
    this.searchInput.add_keyPress(this.onKeyPress);

    TitanTV.SearchInput.callBaseMethod(this, 'initialize');

    Sys.Debug.trace('Initialize: ' + element.id);
  },

  // Release resources before control is disposed.
  dispose: function()
  {
    var element = this.get_element();

    this.searchInput.remove_buttonClick(this.onButtonClick);
    this.searchInput.remove_keyPress(this.onKeyPress);

    if (this.onButtonClick) delete this.onButtonClick;
    if (this.onKeyPress) delete this.onKeyPress;

    TitanTV.SearchInput.callBaseMethod(this, 'dispose');

    Sys.Debug.trace('Dispose: ' + element.id);
  },

  _onButtonClick: function(sender, e)
  {
    this._doSearch();
  },

  _onKeyPress: function(sender, e)
  {
    switch (e.get_keyCode())
    {
      case Sys.UI.Key.enter:
        if (e._domEvent) e._domEvent.preventDefault();
        this._doSearch();
        return;
    }
  },

  _doSearch: function()
  {
    if (this.searchInput.get_textBoxValue().trim() == "") return;
    __doPostBack(this.postbackId, 'search');
  }
}

TitanTV.SearchInput.registerClass('TitanTV.SearchInput', Sys.UI.Control);

// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
