if (typeof(Boom) == 'undefined') {
  var Boom = {};
}

Boom.Util = Class.create({

  isIEPC : function() {
    if (this.isIE6 || this.isIE7) {
      return true;
    } else {
      return false;
    }
  },

  isIE6 : function() {
    if (window.external && (typeof window.XMLHttpRequest == "undefined")) {
      return true;
    } else {
      return false;
    }
  },

  isIE7 : function() {
    if (window.external && (typeof window.XMLHttpRequest == "object")) {
      return true;
    } else {
      return false;
    }
  }

});

Boom.Tabs = Class.create({

  initialize : function(tab_container) {
    this.activeContainer = false;
    this.activeLink = false;
    this.containers = $H({});
    this.links = [];

    this.options = {
      linkSelector: 'li a',
      activeClassName: 'active',
      showFunction: Element.show,
      hideFunction: Element.hide
    };

    if ($(tab_container)) {
      $(tab_container).getElementsBySelector(this.options.linkSelector).each(function(link) {
        this.createTab(link);
      }.bind(this));
    }
  },

  createTab : function(link) {
    this.links.push(link);
    link.key = link.getAttribute('id').replace('link_to_', '');
    this.containers[link.key] = $("content_for_" + link.key);
    if (link.hasClassName('active')) {
      // Because we're hard-coding display: none in the HTML
      // All we need to do if this is the initial active item
      // is populate this.activeContainer so we can hide it later
      // on.
      this.activeContainer = this.containers[link.key];
    }
    link["onclick"] = function(link) {
      if(window.event)
      Event.stop(window.event);
      this.makeActive(link);
      return false;
    }.bind(this, link);
  },

  makeActive : function(link) {
    if(this.activeContainer)
    this.options.hideFunction(this.activeContainer);
    this.links.each(function(item){
      item.removeClassName(this.options.activeClassName);
    }.bind(this));
    link.addClassName(this.options.activeClassName);
    this.activeContainer = this.containers[link.key];
    this.activeLink = link;
    this.options.showFunction(this.containers[link.key]);
  }

});

Boom.LightSwitch = Class.create({

  initialize : function() {
    this.options = {
      overlay_id : 'lights_out_overlay',
      overlay : '',
      startValue : 1,
      timer : ''
    };
    this.createOverlayElement();
    this.overlay = $(this.options.overlay_id);
  },

  turnLightsOff : function() {
    new Effect.Appear(this.options.overlay_id, { duration: 0.2, from: 0.0, to: 0.9 });
  },

  turnLightsOn : function() {
    new Effect.Fade(this.options.overlay_id, { duration: 0.2});
  },

  lightsAreOff : function() {
    if (this.overlay.getStyle('display') == 'block') {
      return true;
    } else {
      return false;
    }
  },

  createOverlayElement : function() {
    if (!$(this.options.overlay_id)) {
      var overlay = new Element('div', { 'id' : this.options.overlay_id });
      overlay.setStyle({
        'background' : '#000',
        'display' : 'none',
        'position' : 'absolute',
        'top' : 0,
        'left' : 0,
        'filter' : 'alpha(opacity=0)',
        '-moz-opacity' : 0,
        'opacity' : 0,
        'width' : '100%',
        'height' : '100%',
        'z-index' : '8888'
      })
      document.body.appendChild(overlay);
    }
  }

});

Boom.LightSwitch.flip = function() {
  var bl = new Boom.LightSwitch();
  bl.lightsAreOff() ? bl.turnLightsOn() : bl.turnLightsOff();
}

function updateToggleLink(link, state, name) {
  var permalink = name.toLowerCase().gsub(/ /, '_');
  if (state == 'open') {
    link.update('Close ' + name);
    link.removeClassName('open_' + permalink);
    link.toggleClassName('close_' + permalink);
  } else {
    link.update('Open ' + name);
    link.removeClassName('close_' + permalink);
    link.toggleClassName('open_' + permalink);
  }
}

function encyclopediaSliderOnFinish(obj) {
  var toggle = $('toggle_top_searches');
  if (obj.element.visible()) {
    // browser pane has been opened
    updateToggleLink(toggle, 'open', 'Top Searches');
  } else {
    // browser pane has been closed
    updateToggleLink(toggle, 'closed', 'Top Searches');
  }
}

Event.addBehavior({
  '#toggle_top_searches:click' : function() {
    Effect.toggle('top_searches', 'blind', {
      afterFinish: encyclopediaSliderOnFinish
    });
    return false;
  }
});