var ChannelDock = {
  HIDE_THRESHOLD: 0.08,
  currentDock: null,
  upperBound:  0,
  lowerBound:  0,
  leftBound:   0,
  rightBound:  0,
  processing:  false,

  /* only munge first deactivate for OSX Firefox */
  mungeFirstDeactivate: typeof(navigator) != "undefined"         && 
                        navigator.appVersion.indexOf("Mac") >= 0 &&
                        navigator.userAgent.indexOf("Firefox") >= 0,

  /* disable fade for OSX Firefox 2 */
  disableFade:          typeof(navigator) != "undefined"         && 
                        navigator.appVersion.indexOf("Mac") >= 0 &&
                        navigator.userAgent.indexOf("Firefox/2") >= 0,

  ignoreDeactivate:     false,

  enableButtons: function() {
    $$('.channel-dock-button').each(function(el) {
      Event.observe(el, "mouseover", ChannelDock.activate);
    });
  },
  
  activate: function(ev) {
    if (ChannelDock.disabled()) { return; }
    if (ChannelDock.ignoreDeactivate) { return; }
    if (!ChannelDock.processing) {
      var el = Event.element(ev);
      var dock = $$('.channels-dock').first();
      if (dock) {
        var xy = Position.cumulativeOffset(el);
        dock.style.top = (xy[1] + el.getHeight()) + 'px';
        dock.style.left = xy[0] + 'px';

        ChannelDock.currentDock = dock;
        ChannelDock.upperBound = (xy[1] + el.getHeight());
        ChannelDock.lowerBound = (xy[1] + el.getHeight()) + dock.getHeight();
        ChannelDock.leftBound = xy[0];
        ChannelDock.rightBound = xy[0] + dock.getWidth();

        ChannelDock.processing = true;

        if (ChannelDock.mungeFirstDeactivate)
          ChannelDock.ignoreDeactivate = true;

        $$('h2.sIFR-replaced embed').each(ChannelDock.invis);
        if (ChannelDock.disableFade) {
          ChannelDock.currentDock.show();
        }
        else {
          new Effect.Appear(dock, {
            duration: 0.3,
            afterFinish: function() {
              ChannelDock.processing = false;
            }
          });
        }
      }
    }
  },
  
  close: function() {
    if (ChannelDock.disabled()) { return; }
    var dock = ChannelDock.currentDock;
    ChannelDock.processing = false;
    ChannelDock.ignoreDeactivate = false;
    if (dock && dock.style.display != 'none') {
      dock.hide();
      $$('h2.sIFR-replaced embed').each(ChannelDock.vis);
    }
  },
  
  deactivate: function(ev) {
    if (ChannelDock.disabled()) { return; }
    if (ChannelDock.ignoreDeactivate) {
      ChannelDock.ignoreDeactivate = false;
      return;
    }
    var dock = ChannelDock.currentDock;
    ChannelDock.hideDock = true;
    ChannelDock.processing = false;
    if (dock && dock.style.display != 'none') {
      var y = Event.pointerY(ev);
      var x = Event.pointerX(ev);

      var threshold = ChannelDock.HIDE_THRESHOLD;
      var upper     = ChannelDock.upperBound;
      var lower     = ChannelDock.lowerBound;
      var left      = ChannelDock.leftBound;
      var right     = ChannelDock.rightBound;

      var hide =
        y < upper * (1 - threshold) ||
        y > lower * (1 + threshold) ||
        x <= left                   ||
        x >= right;
      
      if (hide && x >= 0 && y >= 0) {
        dock.hide();
        $$('h2.sIFR-replaced embed').each(ChannelDock.vis);
      }
     
    }
  },

  /* hide/show functions that check for wmode != transparent
   * before operating */
  vis:   function(el) { if (!ChannelDock.transparent(el)) el.show(); },
  invis: function(el) { if (!ChannelDock.transparent(el)) el.hide(); },

  transparent: function(el) {
    var wmode = el.attributes;
    if (wmode) wmode = wmode['wmode'];
    if (wmode) wmode = wmode.nodeValue;
    return wmode && wmode == 'transparent';
  },

  isDisabled: null,
  disabled: function() {
    if (this.isDisabled != null) return this.isDisabled;

    this.isDisabled = typeof(navigator) != "undefined"                &&
                      /linux/.test(navigator.userAgent.toLowerCase()) &&
                      /^\/($|tv|movies|channels)/.test(window.location.pathname);
    return this.isDisabled;
  }
};

Event.observe(window, 'load', function() {
  var body = $$('body').first();
  if (body) Event.observe(body, 'mouseout', ChannelDock.deactivate);
  ChannelDock.enableButtons();
});
