﻿Type.registerNamespace("TitanTV");

// Constructor
TitanTV.GridNavigation = function(element)
{

  TitanTV.GridNavigation.initializeBase(this, [element]);

  // Private variables
  this._postbackID = null;
  this._dateList = null;
  this._timeList = null;
  this._stationList = null;
  this._filterByHD = null;
  this._allowPrevious = true;
  this._allowNext = true;

  // UI Elements
  this._previousElement;
  this._nextElement;

  // Methods
  this.pageLoaded = null;
  this.setComboValue = null;
  this.setCheckboxValue = null;
  this.onFocus = null;
  this.onBlur = null;
  this.onChange = null;
  this.onClick = null;
  this.showPrevious = null;
  this.togglePreviousNext = null;

  // Private methods
}

TitanTV.GridNavigation.prototype = {

  // property accessors.

  get_postbackID: function() { return this._postbackID; },
  set_postbackID: function(value) { this._postbackID = value; },

  get_dateList: function() { return this._dateList; },
  set_dateList: function(value) { this._dateList = value; },

  get_timeList: function() { return this._timeList; },
  set_timeList: function(value) { this._timeList = value; },

  get_stationList: function() { return this._stationList; },
  set_stationList: function(value) { this._stationList = value; },

  get_allowPrevious: function() { return this._allowPrevious; },
  set_allowPrevious: function(value) { this._allowPrevious = value; },

  get_allowNext: function() { return this._allowNext; },
  set_allowNext: function(value) { this._allowNext = value; },

  get_filterByHD: function() { return this._filterByHD; },
  set_filterByHD: function(value) { this._filterByHD = value; },

  initialize: function()
  {
    var element = this.get_element();

    // create Delgates
    if (this.pageLoaded === null) this.pageLoaded = Function.createDelegate(this, this._pageLoaded);
    if (this.setComboValue === null) this.setComboValue = Function.createDelegate(this, this._setComboValue);
    if (this.setCheckboxValue === null) this.setCheckboxValue = Function.createDelegate(this, this._setCheckboxValue);
    if (this.onFocus === null) this.onFocus = Function.createDelegate(this, this._onFocus);
    if (this.onBlur === null) this.onBlur = Function.createDelegate(this, this._onBlur);
    if (this.onChange === null) this.onChange = Function.createDelegate(this, this._onChange);
    if (this.onClick === null) this.onClick = Function.createDelegate(this, this._onClick);
    if (this.togglePreviousNext === null) this.togglePreviousNext = Function.createDelegate(this, this._togglePreviousNext);

    this._wireComboEvents(this._dateList);
    this._wireComboEvents(this._timeList);
    this._wireComboEvents(this._stationList);

    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(this.pageLoaded);

    this._previousElement = $get('prev', element);
    if (this._previousElement) this._previousElement.disabledClassName = this._previousElement.getAttribute('classDisabled')

    this._nextElement = $get('next', element);
    if (this._nextElement) this._nextElement.disabledClassName = this._nextElement.getAttribute('classDisabled')

    this.togglePreviousNext(this._previousElement, this._allowPrevious);
    this.togglePreviousNext(this._nextElement, this._allowNext);

    TitanTV.GridNavigation.callBaseMethod(this, 'initialize');

    Sys.Debug.trace('Initialize: ' + element.id);
  },

  // Release resources before control is disposed.
  dispose: function()
  {
    var element = this.get_element();

    Sys.WebForms.PageRequestManager.getInstance().remove_pageLoaded(this.pageLoaded);

    this._wireComboEvents(this._dateList, true);
    this._wireComboEvents(this._timeList, true);
    this._wireComboEvents(this._stationList, true);

    if (this._previousElement) $clearHandlers(this._previousElement);
    if (this._nextElement) $clearHandlers(this._nextElement);

    if (this.pageLoading) delete this.pageLoading;
    if (this.setComboValue) delete this.setComboValue;
    if (this.setCheckboxValue) delete this.setCheckboxValue;
    if (this.onFocus) delete this.onFocus;
    if (this.onBlur) delete this.onBlur;
    if (this.onChange) delete this.onChange;
    if (this.onClick) delete this.onClick;
    if (this.togglePreviousNext) delete this.togglePreviousNext;

    TitanTV.GridNavigation.callBaseMethod(this, 'dispose');

    Sys.Debug.trace('Dispose: ' + element.id);
  },

  _setComboValue: function(comboControl, value)
  {
    if (comboControl != null)
    {
      var comboItem = comboControl.findItemByValue(value);
      if (comboItem != null && comboControl.get_selectedIndex() != comboItem.get_index())
      {
        comboItem.select();
      }
    }
  },

  _setCheckboxValue: function(control, value)
  {
    if (control != null)
    {
      control.checked = (value == "1") ? true : false;
    }
  },

  _wireComboEvents: function(comboControl, remove)
  {
    if (!comboControl) return;

    if (remove)
    {
      comboControl.remove_selectedIndexChanged(this.onChange);
      comboControl.remove_onClientFocus(this.onFocus);
      comboControl.remove_onClientBlur(this.onBlur);
    }
    else
    {
      comboControl.add_selectedIndexChanged(this.onChange);
      comboControl.add_onClientFocus(this.onFocus);
      comboControl.add_onClientBlur(this.onBlur);
    }
  },

  _onFocus: function(sender, args)
  {
    sender.hasFocus = true;
  },

  _onBlur: function(sender, args)
  {
    sender.hasFocus = false;
  },

  _onChange: function(sender, args)
  {
    if (this._postbackID === null || !sender.hasFocus) return;

    var postbackArguments = null;

    if (sender === this._dateList) postbackArguments = "date";
    else if (sender === this._timeList) postbackArguments = "time";
    else if (sender === this._stationList) postbackArguments = "station";

    if (postbackArguments != null)
      __doPostBack(this._postbackID, postbackArguments);
  },

  _onClick: function(event)
  {
    if (this._postbackID === null) return;

    var postbackArguments = null;

    if (event.target === this._previousElement) postbackArguments = "previous";
    if (event.target === this._nextElement) postbackArguments = "next";

    if (postbackArguments != null)
      __doPostBack(this._postbackID, postbackArguments);
  },

  _togglePreviousNext: function(element, allow)
  {
    if (!element) return;

    // Unwire/Wire event handler
    $clearHandlers(element);
    if (allow) $addHandler(element, 'click', this.onClick);

    // If we are not using disabled class names, toggle display
    if (!element.disabledClassName)
    {
      element.style.display = allow ? 'block' : 'none';
      return;
    }

    // Add or remove the disabled class
    var containsDisabledClass = Sys.UI.DomElement.containsCssClass(element, element.disabledClassName)
    if (!allow && !containsDisabledClass)
      Sys.UI.DomElement.addCssClass(element, element.disabledClassName);
    else if (allow && Sys.UI.DomElement.containsCssClass(element, element.disabledClassName))
      Sys.UI.DomElement.removeCssClass(element, element.disabledClassName);
  },

  _pageLoaded: function(sender, args)
  {
    if (!args) return;

    var element = this.get_element();

    var data = args.get_dataItems()[element.id];

    if (data)
    {
      var updateCommands = data.split(',');
      for (var i = 0; i < updateCommands.length; i++)
      {
        var command = updateCommands[i].split('|');
        switch (command[0])
        {
          case "date":
            this.setComboValue(this.get_dateList(), command[1]);
            break;

          case "time":
            this.setComboValue(this.get_timeList(), command[1]);
            break;

          case "station":
            this.setComboValue(this.get_stationList(), command[1]);
            break;

          case "hd":
            this.setCheckboxValue(this.get_filterByHD(), command[1]);
            break;

          case "allowPrevious":
            var allow = (command[1] == 'True') ? true : false;
            if (allow != this._allowPrevious)
            {
              this._allowPrevious = allow;
              this.togglePreviousNext(this._previousElement, allow);
            }
            break;

          case "allowNext":
            var allow = (command[1] == 'True') ? true : false;
            if (allow != this._allowNext)
            {
              this._allowNext = allow;
              this.togglePreviousNext(this._nextElement, allow);
            }
            break;
        }
      }
    }
  }
}

TitanTV.GridNavigation.registerClass('TitanTV.GridNavigation', 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();

