﻿Type.registerNamespace("TitanTV");

// Constructor
TitanTV.GridDisplay = function(element)
{

  TitanTV.GridDisplay.initializeBase(this, [element]);

  // Private variables
  this._siteId = null;
  this._debug = null;
  this._commandUrl = null;
  this._gmtOffset = null;
  this._observesDls = null;

  this._stationData = null;
  this._commandFrame = null;

  // UI Elements

  // Methods
  this.pageLoading = null;
  this.executePVRCommand = null;
  this.openIFrame = null;
  this.doCommand = null;
  this.getStationData = null;

  // Private methods
}

TitanTV.GridDisplay.prototype = {

  // property accessors.

  get_siteId: function() { return this._siteId; },
  set_siteId: function(value) { this._siteId = value; },

  get_debug: function() { return this._debug; },
  set_debug: function(value) { this._debug = value; },

  get_commandUrl: function() { return this._commandUrl; },
  set_commandUrl: function(value) { this._commandUrl = value; },

  get_gmtOffset: function() { return this._gmtOffset; },
  set_gmtOffset: function(value) { this._gmtOffset = value; },

  get_observesDls: function() { return this._observesDls; },
  set_observesDls: function(value) { this._observesDls = value; },

  get_stationData: function()
  {
    return TitanTV.GridDisplay.StationData[this.get_element().id];
  },

  set_stationData: function(value)
  {
    if (value == null || value == '') return;

    var stationData = Sys.Serialization.JavaScriptSerializer.deserialize(value);

    TitanTV.GridDisplay.StationData[this.get_element().id] = stationData;
  },

  initialize: function()
  {
    var element = this.get_element();

    TitanTV.GridDisplay.Instances[element.id] = this;

    // Special case for pages with only one GridDisplay Object
    // Most pages should fall under this case.
    TitanTV.GridDisplay.Instances['grid'] = this;

    // create Delgates
    if (this.pageLoading === null) this.pageLoading = Function.createDelegate(this, this._pageLoading);
    if (this.executePVRCommand === null) this.executePVRCommand = Function.createDelegate(this, this._executePVRCommandInternal);
    if (this.doCommand === null) this.doCommand = Function.createDelegate(this, this._doCommandInternal);
    if (this.openIFrame === null) this.openIFrame = Function.createDelegate(this, this._openIFrameInternal);
    if (this.getStationData === null) this.getStationData = Function.createDelegate(this, this._getStationData);

    Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(this.pageLoading);

    TitanTV.GridDisplay.callBaseMethod(this, 'initialize');

    Sys.Debug.trace('Initialize: ' + element.id);
  },

  // Release resources before control is disposed.
  dispose: function()
  {
    var element = this.get_element();

    TitanTV.GridDisplay.Instances[element.id] = null;

    // Special case for pages with only one GridDisplay Object
    // Most pages should fall under this case.
    TitanTV.GridDisplay.Instances['grid'] = null;

    Sys.WebForms.PageRequestManager.getInstance().remove_pageLoading(this.pageLoading);

    if (this.pageLoading) delete this.pageLoading;
    if (this.executePVRCommand) delete this.executePVRCommand;
    if (this.doCommand) delete this.doCommand;
    if (this.openIFrame) delete this.openIFrame;
    if (this.getStationData) delete this.getStationData;

    TitanTV.GridDisplay.callBaseMethod(this, 'dispose');

    Sys.Debug.trace('Dispose: ' + element.id);
  },

  _pageLoading: function(sender, args)
  {
    if (!args) return;

    var element = this.get_element();
    var dataItems = args.get_dataItems();

    if (dataItems[element.id] == "clear")
      element.innerHTML = '';
  },

  _getStationData: function(stationIndex)
  {
    return this.get_stationData()[stationIndex];
  },

  _executePVRCommandInternal: function(e, stationIndex, scheduleId, command)
  {
    this.doCommand(e, stationIndex, scheduleId, null, this._commandUrl, command);

    return false;
  },


  _doCommandInternal: function(e, stationIndex, scheduleId, programId, targetPage, command, action, wndWidth, wndHeight)
  {
    var pathInfo = "";

    if (this._siteId) pathInfo += this._buildPathInfo("siteId", this._siteId);

    pathInfo += this._buildPathInfo("programid", programId);
    pathInfo += this._buildPathInfo("scheduleid", scheduleId);
    pathInfo += this._buildPathInfo("command", command);

    var stationData = this.getStationData(stationIndex);

    if (stationData != null && stationData != 'undefined')
    {
      pathInfo += this._buildPathInfo("callsign", stationData.cs);
      pathInfo += this._buildPathInfo("stationid", stationData.sid);
      pathInfo += this._buildPathInfo("psipid", stationData.pid);
      pathInfo += this._buildPathInfo("channel", stationData.dc);
      pathInfo += this._buildPathInfo("contenttype", stationData.pt);
      pathInfo += this._buildPathInfo("major", stationData.maj);
      pathInfo += this._buildPathInfo("minor", stationData.min);
    }

    var href = targetPage + pathInfo + "/"

    Sys.Debug.trace("Command URL: " + href);

    if (this._debug)
    {
      var popWin = window.open(href, "T3Popup", "width=728,height=500,scrollbars=1,toolbar=0,locationbar=1,resize=1,resizable=1");
      popWin.focus();
      if (e && e.stopPropagation) e.stopPropagation();
      e.cancelBubble = true;
      return;
    }

    switch (action)
    {

      case "replace": // Replace the contents of the current window.
        document.location.href = href;
        break;

      case "window": // Open a new window. (obsolete)
        popWin = window.open(href, "T3Popup", "width=" + wndWidth + ",height=" + wndHeight + ",scrollbars=0,toolbar=0,locationbar=0,resize=0,resizable=0");
        popWin.focus();
        break;

      default: // Execute the command URL in an IFrame
        this.openIFrame(href);
        break;
    }

    // Stop the click event from propogating further
    if (e && e.stopPropagation) e.stopPropagation();
    e.cancelBubble = true;
  },

  _openIFrameInternal: function(href)
  {
    this._commandFrame = $get('commandFrame');

    if (this._commandFrame)
    {
      this._commandFrame.src = href;
    }
    else if (this._commandFrame == null && document && document.createElement)
    {
      this._commandFrame = document.createElement('iframe');
      this._commandFrame.setAttribute('id', 'commandFrame');
      this._commandFrame.setAttribute('name', 'commandFrame');
      this._commandFrame.setAttribute('src', href);
      this._commandFrame.style.position = "absolute";
      this._commandFrame.style.left = "-100px";
      this._commandFrame.style.height = "1px";
      this._commandFrame.style.width = "1px";
      document.body.appendChild(this._commandFrame);
    }

    if (this._commandFrame == null)
    {
      // Handle the case were we could not find the IFrame document
      CmdWin = window.open(href, "T3CMDWIN", "width=1,height=1,scrollbars=0,toolbar=0,locationbar=0,resize=0,resizable=0");
      CmdWin.blur();
      setTimeout('CmdWin.close();', 5000);
    }
  },

  _buildPathInfo: function(name, value)
  {
    if (value === null || value == 'undefined' || value === '')
    {
      return '';
    }

    return "/" + name + "/" + escape(value);
  }
}

TitanTV.GridDisplay.registerClass('TitanTV.GridDisplay', Sys.UI.Control);

// Static Properties and Methods

TitanTV.GridDisplay.Instances = new Object();
TitanTV.GridDisplay.StationData = new Object();
TitanTV.GridDisplay.GetInstance = function(instanceId)
{
  if (instanceId == null || instanceId == '')
    instanceId = 'grid';

  return TitanTV.GridDisplay.Instances[instanceId];
}

// 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();

