﻿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._timeframe = null;
  this._durationList = null;
  this._sortList = null;
  this._genreList = null;
  this._filterByMovies = null;
  this._filterByHD = null;
  this._filterByFavorites = null;
  this._filterByNew = null;
  this._fullWidth = null;
  this._showAdvanced = false;
  this._refreshTimer = null;

  // UI Elements
  this._advancedContainer = null;
  this._showAdvancedBtn = null;
  this._hideAdvancedBtn = null;

  // Methods
  this.pageLoading = null;
  this.setComboValue = null;
  this.setCheckboxValue = null;
  this.onFocus = null;
  this.onBlur = null;
  this.onChange = null;
  this.toggleAdvanced = null;
  this.onShowHideClick = null;
  this.onFullWidthChange = null;

  // Private methods
}

TitanTV.GridNavigation.prototype = {

  // property accessors.

  get_postbackID: function() { return this._postbackID; },
  set_postbackID: function(value) { this._postbackID = value; },

  get_showAdvanced: function() { return this._showAdvanced; },
  set_showAdvanced: function(value) { this._showAdvanced = 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_timeframe: function() { return this._timeframe; },
  set_timeframe: function(value) { this._timeframe = value; },

  get_durationList: function() { return this._durationList; },
  set_durationList: function(value) { this._durationList = value; },

  get_sortList: function() { return this._sortList; },
  set_sortList: function(value) { this._sortList = value; },

  get_genreList: function() { return this._genreList; },
  set_genreList: function(value) { this._genreList = value; },

  get_filterByMovies: function() { return this._filterByMovies; },
  set_filterByMovies: function(value) { this._filterByMovies = value; },

  get_filterByHD: function() { return this._filterByHD; },
  set_filterByHD: function(value) { this._filterByHD = value; },

  get_filterByFavorites: function() { return this._filterByFavorites; },
  set_filterByFavorites: function(value) { this._filterByFavorites = value; },

  get_filterByNew: function() { return this._filterByNew; },
  set_filterByNew: function(value) { this._filterByNew = value; },

  get_fullWidth: function() { return this._fullWidth; },
  set_fullWidth: function(value) { this._fullWidth = value; },

  get_refreshTimer: function() { return this._refreshTimer; },
  set_refreshTimer: function(value) { this._refreshTimer = value; },

  initialize: function()
  {
    var element = this.get_element();

    // create Delgates
    if (this.pageLoading === null) this.pageLoading = Function.createDelegate(this, this._pageLoading);
    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.toggleAdvanced === null) this.toggleAdvanced = Function.createDelegate(this, this._toggleAdvanced);
    if (this.onShowHideClick === null) this.onShowHideClick = Function.createDelegate(this, this._onShowHideClick);
    if (this.onFullWidthChange === null) this.onFullWidthChange = Function.createDelegate(this, this._onFullWidthChange);

    this._wireComboEvents(this._dateList);
    this._wireComboEvents(this._timeList);
    this._wireComboEvents(this._sortList);
    this._wireComboEvents(this._durationList);
    this._wireComboEvents(this._genreList);
    this._wireComboEvents(this._timeframe);

    // Hide of show the advanced portion of the menu
    this._advancedContainer = $get('advanced', element);
    this._showAdvancedBtn = $get('showAdvanced', element);
    this._hideAdvancedBtn = $get('hideAdvanced', element);

    if (this._showAdvancedBtn) $addHandler(this._showAdvancedBtn, 'click', this.onShowHideClick);
    if (this._hideAdvancedBtn) $addHandler(this._hideAdvancedBtn, 'click', this.onShowHideClick);
    if (this._fullWidth) $addHandler(this._fullWidth, 'click', this.onFullWidthChange);

    this.toggleAdvanced();

    //Sys.Debug.traceDump(this.get_dateList());

    Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(this.pageLoading);

    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_pageLoading(this.pageLoading);

    this._wireComboEvents(this._dateList, true);
    this._wireComboEvents(this._timeList, true);
    this._wireComboEvents(this._sortList, true);
    this._wireComboEvents(this._durationList, true);
    this._wireComboEvents(this._genreList, true);
    this._wireComboEvents(this._timeframe, true);

    if (this._showAdvancedBtn) $removeHandler(this._showAdvancedBtn, 'click', this.onShowHideClick);
    if (this._hideAdvancedBtn) $removeHandler(this._hideAdvancedBtn, 'click', this.onShowHideClick);
    if (this._fullWidth) $removeHandler(this._fullWidth, 'click', this.onFullWidthChange);

    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.toggleAdvanced) delete this.toggleAdvanced;
    if (this.onShowHideClick) delete this.onShowHideClick;
    if (this.onFullWidthChange) delete this.onFullWidthChange;

    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._durationList) postbackArguments = "duration";
    else if (sender === this._sortList) postbackArguments = "sort";
    else if (sender === this._genreList) postbackArguments = "genre";
    else if (sender === this._timeframe) postbackArguments = "timeframe";

    if (postbackArguments != null)
      __doPostBack(this._postbackID, postbackArguments);
  },

  _onShowHideClick: function(event)
  {
    if (event.target === this._showAdvancedBtn)
      __doPostBack(this._postbackID, "showAdvanced");
    else if (event.target === this._hideAdvancedBtn)
      __doPostBack(this._postbackID, "hideAdvanced");
  },

  _toggleAdvanced: function()
  {
    if (!this._advancedContainer) return;

    if (this._showAdvanced)
    {
      this._advancedContainer.style.display = 'block';
      if (this._showAdvancedBtn) this._showAdvancedBtn.style.display = 'none';
      if (this._hideAdvancedBtn) this._hideAdvancedBtn.style.display = 'block';
    }
    else
    {
      this._advancedContainer.style.display = 'none';
      if (this._showAdvancedBtn) this._showAdvancedBtn.style.display = 'block';
      if (this._hideAdvancedBtn) this._hideAdvancedBtn.style.display = 'none';
    }
  },

  _onFullWidthChange: function(event)
  {
    // Main Table
    var mainTableElement = $get('mainTable');
    if (mainTableElement)
    {
      var fullWidthClass = mainTableElement.getAttribute('fullWidthClass');
      if (fullWidthClass)
      {
        var hasFullWidthClass = Sys.UI.DomElement.containsCssClass(mainTableElement, fullWidthClass)

        if (event.target.checked && !hasFullWidthClass)
          Sys.UI.DomElement.addCssClass(mainTableElement, fullWidthClass);
        else if (!event.target.checked && hasFullWidthClass)
          Sys.UI.DomElement.removeCssClass(mainTableElement, fullWidthClass);
      }
    }

    // Footer Table
    var footerTableElement = $get('footerTable');
    if (footerTableElement)
    {
      var fullWidthClass = footerTableElement.getAttribute('fullWidthClass');
      if (fullWidthClass)
      {
        var hasFullWidthClass = Sys.UI.DomElement.containsCssClass(footerTableElement, fullWidthClass)

        if (event.target.checked && !hasFullWidthClass)
          Sys.UI.DomElement.addCssClass(footerTableElement, fullWidthClass);
        else if (!event.target.checked && hasFullWidthClass)
          Sys.UI.DomElement.removeCssClass(footerTableElement, fullWidthClass);
      }
    }

  },

  _pageLoading: 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 "timeframe":
            this.setComboValue(this.get_timeframe(), command[1]);
            break;

          case "duration":
            this.setComboValue(this.get_durationList(), command[1]);
            break;

          case "genre":
            this.setComboValue(this.get_genreList(), command[1]);
            break;

          case "sort":
            this.setComboValue(this.get_sortList(), command[1]);
            break;

          case "fullWidth":
            this.setCheckboxValue(this.get_fullWidth(), command[1]);
            break;

          case "movies":
            this.setCheckboxValue(this.get_filterByMovies(), command[1]);
            break;

          case "hd":
            this.setCheckboxValue(this.get_filterByHD(), command[1]);
            break;

          case "favorites":
            this.setCheckboxValue(this.get_filterByFavorites(), command[1]);
            break;

          case "new":
            this.setCheckboxValue(this.get_filterByNew(), command[1]);
            break;

          case "toggleAdvanced":
            this._showAdvanced = (command[1] == "True") ? true : false;
            this.toggleAdvanced();
            break;

          case "resetRefresh":
            if (this._refreshTimer)
            {
              this._refreshTimer._stopTimer();
              if(command[1] == "1")
                this._refreshTimer._startTimer();
            }
            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();

