﻿Type.registerNamespace("TitanTV");

// Constructor
TitanTV.SearchInput = function(element)
{
  TitanTV.SearchInput.initializeBase(this, [element]);

  // Private variables
  this.postbackId
  this.focused = false;
  this.mouseOverBox = false;
  this.lastSearchValue = null;
  this.keyPressTimeout = null;
  this.searchItems = null;
  this.searchItemTemplate = null;

  // UI Elements
  this.searchInput = null;
  this.searchInputBox = null;
  this.savedSearchBox = null;

  // Private methods
  this.onButtonClick = null;
  this.onKeyPress = null;
  this.onKeyDown = null;
  this.onFocus = null;
  this.onBlur = null;
  this.onMouseOver = null;
  this.onMouseOut = null;
  this.onClick = null;
  this.findSavedSearches = null;
  this.findSavedSearchesCallback = null;
  this.close = 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; },

  get_searchInputBox: function() { return this.searchInputBox; },
  set_searchInputBox: function(value) { this.searchInputBox = value; },

  initialize: function()
  {
    var element = this.get_element();

    this.savedSearchBox = $get('savedSearches', element);

    if (this.searchInputBox && this.savedSearchBox)
    {
      var b = Sys.UI.DomElement.getBounds(this.searchInputBox);
      Sys.UI.DomElement.setLocation(this.savedSearchBox, b.x + 1, b.y + b.height);
      this.savedSearchBox.style.width = (b.width - 4) + "px";
      this.searchItems = $get("searchItems", this.savedSearchBox);
      this.searchItemTemplate = $get("searchItemTemplate", this.savedSearchBox);
    }

    // create Delgates
    if (this.onButtonClick === null) this.onButtonClick = Function.createDelegate(this, this._onButtonClick);
    if (this.onKeyPress === null) this.onKeyPress = Function.createDelegate(this, this._onKeyPress);
    if (this.onKeyDown === null) this.onKeyDown = Function.createDelegate(this, this._onKeyDown);
    if (this.onFocus === null) this.onFocus = Function.createDelegate(this, this._onFocus);
    if (this.onBlur === null) this.onBlur = Function.createDelegate(this, this._onBlur);
    if (this.findSavedSearches === null) this.findSavedSearches = Function.createDelegate(this, this._findSavedSearches);
    if (this.findSavedSearchesCallback === null) this.findSavedSearchesCallback = Function.createDelegate(this, this._findSavedSearchesCallback);
    if (this.onMouseOver === null) this.onMouseOver = Function.createDelegate(this, this._onMouseOver);
    if (this.onMouseOut === null) this.onMouseOut = Function.createDelegate(this, this._onMouseOut);
    if (this.close === null) this.close = Function.createDelegate(this, this._close);
    if (this.onClick === null) this.onClick = Function.createDelegate(this, this._onClick);

    if (this.searchInput)
    {
      this.searchInput.add_blur(this.onBlur);
      this.searchInput.add_keyPress(this.onKeyPress);
      this.searchInput.add_focus(this.onFocus);
    }

    if (this.searchInputBox) $addHandler(this.searchInputBox, 'keydown', this.onKeyDown);

    if (this.savedSearchBox)
    {
      $addHandler(this.savedSearchBox, 'mouseover', this.onMouseOver);
      $addHandler(this.savedSearchBox, 'mouseout', this.onMouseOut);
    }

    if (this.searchItems) $addHandler(this.searchItems, 'click', this.onClick);

    this.searchInput.add_buttonClick(this.onButtonClick);

    TitanTV.SearchInput.callBaseMethod(this, 'initialize');

    Sys.Debug.trace('Initialize: ' + element.id);
  },

  // Release resources before control is disposed.
  dispose: function()
  {
    var element = this.get_element();

    if (this.searchInput)
    {
      this.searchInput.remove_blur(this.onBlur);
      this.searchInput.remove_focus(this.onFocus);
      this.searchInput.remove_keyPress(this.onKeyPress);
    }

    this.searchInput.remove_buttonClick(this.onButtonClick);

    if (this.savedSearchBox) $clearHandlers(this.savedSearchBox);

    if (this.searchItems) $removeHandler(this.searchItems, 'click', this.onClick);

    if (this.searchInputBox) $removeHandler(this.searchInputBox, 'keydown', this.onKeyDown);

    if (this.onButtonClick) delete this.onButtonClick;
    if (this.onKeyPress) delete this.onKeyPress;
    if (this.onKeyDown) delete this.onKeyDown;
    if (this.onFocus) delete this.onFocus;
    if (this.onBlur) delete this.onBlur;
    if (this.findSavedSearches) delete this.findSavedSearches;
    if (this.findSavedSearchesCallback) delete this.findSavedSearchesCallback;
    if (this.onMouseOver) delete this.onMouseOver;
    if (this.onMouseOut) delete this.onMouseOut;
    if (this.close) delete this.close;
    if (this.onClick) delete this.onClick;

    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.backspace:
      case Sys.UI.Key.tab:
      case Sys.UI.Key.esc:
      case Sys.UI.Key.pageUp:
      case Sys.UI.Key.pageDown:
      case Sys.UI.Key.end:
      case Sys.UI.Key.home:
      case Sys.UI.Key.left:
      case Sys.UI.Key.up:
      case Sys.UI.Key.right:
      case Sys.UI.Key.down:
        return;
      case Sys.UI.Key.enter:
        clearTimeout(this.keyPressTimeout);
        if (e._domEvent) e._domEvent.preventDefault();
        this._doSearch();
        return;
    }

    clearTimeout(this.keyPressTimeout);

    this.keyPressTimeout = setTimeout(this.findSavedSearches, 500);
  },

  _onKeyDown: function(e)
  {
    // Have to handle backspace seperately since IE doesn't catch it in KeyPress
    if (e.keyCode != Sys.UI.Key.backspace) return;

    clearTimeout(this.keyPressTimeout);

    this.keyPressTimeout = setTimeout(this.findSavedSearches, 500);
  },

  _onFocus: function(sender, e)
  {
    if (!this.focused)
    {
      this.focused = true;
      this.keyPressTimeout = setTimeout(this.findSavedSearches, 500);
    }
  },

  _onMouseOver: function(e)
  {
    var target = e.target;

    if (target && target.getAttribute("hoverClass")) Sys.UI.DomElement.toggleCssClass(target, target.getAttribute("hoverClass"));

    this.mouseOverBox = true;
  },

  _onMouseOut: function(e)
  {
    var target = e.target;

    if (target && target.getAttribute("hoverClass")) Sys.UI.DomElement.toggleCssClass(target, target.getAttribute("hoverClass"));

    this.mouseOverBox = false;
  },

  _onBlur: function(sender, e)
  {
    setTimeout(this.close, 100);
  },

  _findSavedSearches: function()
  {
    Sys.Debug.trace("FindSavedSearches");

    if (!this.searchInput) return;

    WebApp.Services.SearchInputService.FindSavedSearches(this.searchInput.get_value().trim(), 10, this.findSavedSearchesCallback);
  },

  _findSavedSearchesCallback: function(result, context, methodName)
  {
    if (!result || result.length == 0)
    {
      if (this.savedSearchBox) this.savedSearchBox.style.display = 'none';
      Sys.Debug.trace('no matches');
      return;
    }

    if (!this.searchItems || !this.searchItemsTemplate);

    this.searchItems.innerHTML = "";

    for (var i = 0; i < result.length; i++)
    {
      var savedSearch = result[i];

      var itemNode = this.searchItemTemplate.cloneNode(true);
      itemNode.removeAttribute("id");
      itemNode.innerHTML = savedSearch.SN;
      itemNode.style.display = "block";
      itemNode.setAttribute("searchId", savedSearch.SID);
      this.searchItems.appendChild(itemNode);
    }

    if (this.focused && this.savedSearchBox) this.savedSearchBox.style.display = 'block';
  },

  _close: function()
  {
    if (this.mouseOverBox)
    {
      this.searchInput.focus();
      return;
    }

    this.focused = false;
    if (this.savedSearchBox) this.savedSearchBox.style.display = 'none';
  },

  _onClick: function(e)
  {
    if (e.target.getAttribute("searchId"))
    {
      if (this.savedSearchBox) this.savedSearchBox.style.display = 'none';
      __doPostBack(this.postbackId, e.target.getAttribute("searchId"));
    }
  },

  _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();