/*
 * Chris Peery
 * version 1 2009-07-25
 */

var Homepage = Class.create();
Object.extend(Object.extend(Homepage.prototype, Abstract.prototype), {
  initialize : function(featured, baseball, football, basketball, options) {
    new Simpletabs('view-select-tabs');
    new Simpletabs('photo-view-select-tabs');
    new Simpletabs('list-view-select-tabs');

    this.events = {
      beforePostChange: this.beforePostChange,
      afterPostChange: this.afterPostChange
    };

    $$('div.post-list-container').each( function(element) {
      var listId = ( element.id.indexOf("-") != -1 )
        ? element.id.substring(0, element.id.indexOf("-"))
        : '';

      var scroller = new Scroller(element.id, element.down().id, {
        nextItemId: listId + '-next-item',
        previousItemId: listId + '-previous-item',
        afterScroll:this.events.afterPostChange,
        beforeScroll:this.events.beforePostChange
      });
    }.bind(this));

    $$('li.tooltip').each( function(element) {
      var idParts = element.id.split(":");
      var postId = (idParts.length == 2) ? idParts[1] : 0;
      var post = $('post-' + postId);
      if ( post )
      {
        new Tip(element, post.innerHTML, {
          closeButton: false,
          fixed: true,
          hideAfter: 1,
          hideOn: 'mouseout',
          hideOthers: true,
          offset: { x: 0, y: 10 },
          hook: { target: 'topMiddle', tip: 'bottomLeft' },
          stem: { position:'bottomMiddle', width:30, height:30 },
          radius: 1,
          style: 'protogrey',
          width: 550
        });
      }
    }.bind(this));

    new Slider('viewport', 'slider', 'groupControl', 'slideControl', {});

    this.options = Object.extend({
      beforeSlide: Prototype.emptyFunction,
      afterSlide: Prototype.emptyFunction,
      listHeight: 85,
      duration: 1.0,
      defaultListItemIndex: 0,
      itemEvent: 'click',
      nextItemEvent: 'click',
      previousItemEvent: 'click',
      nextItemClassName: 'next-item',
      previousItemClassName: 'previous-item'
    },options || {});
  },
  afterPostChange: function()
  {
    var post = $('post-' + this.activeItemPostId);
  },
  beforePostChange: function()
  {
    var post = $('post-' + this.activeItemPostId);
    var container = this.viewport.up();

    if ( null != post )
    {
      var postHash = new Hash();
      post.childElements().each(function(child) {
        var classNames = $w(child.className);
        if ( classNames.length >= 1 )
          postHash.set($w(child.className)[0], child);
      }.bind(postHash));

      if ( postHash.keys().length >= 1 )
      {
        container.childElements().each(function(child) {
          var match = $w(child.className).find(function(name) {
            if ( postHash.get(name) != undefined ) return true;
          }.bind(postHash));

          if ( postHash.get(match) != undefined ) child.innerHTML = postHash.get(match).innerHTML;
        }.bind(postHash));
      }
    }

  }
});

Event.observe(window,'load',function(){ new Homepage('featured', 'baseball', 'football', 'basketball', {}); });
