/*------------------------------------------------------------------------------
Function:       TabInterface()
Author:         Aaron Gustafson (aaron at easy-designs dot net)
Creation Date:  7 December 2006
Version:        0.1
Homepage:       http://www.easy-designs.net/code/tabinterface/
License:        Copyright (c) 2006 Easy! Designs LLC
                Permission is hereby granted, free of charge, to any person
                obtaining a copy of this software and associated documentation
                files (the "Software"), to deal in the Software without
                restriction, including without limitation the rights to use,
                copy, modify, merge, publish, distribute, sublicense, and/or
                sell copies of the Software, and to permit persons to whom the
                Software is furnished to do so, subject to the following
                conditions:

                The above copyright notice and this permission notice shall
                be included in all copies or substantial portions of the
                Software.

                THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
                EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
                OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
                NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
                HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
                WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
                FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
                OTHER DEALINGS IN THE SOFTWARE.
Note:           If you change or improve on this script, please let us know by
                emailing the author (above) with a link to your demo page.
------------------------------------------------------------------------------*/
var TabInterface = Class.create();
TabInterface.prototype = {
  Version:    '0.1',
  _id:        false,
  _cabinet:   false,
  _active:    false,
  _tag:       false,
  _index:     $ul(),
  _tab:       $li(),
  _folder:    $div(),
  initialize: function( el, index ){
    // set the cabinet
    this._cabinet = el;
    Element.cleanWhitespace( this._cabinet );

    // set the id
    this._id = el.getAttribute( 'id' ) || 'folder-' + index;
    if( !el.getAttribute( 'id' ) ){
      el.setAttribute( 'id', this._id );
    }

    // find the first heading
    var hs = ( 'h1|h2|h3|h4|h5|h6' ).split( '|' );
    var curr = this._cabinet.firstChild;
    while( !hs.inArray( curr.nodeName.toLowerCase() ) ){
      curr = DOM.nextElement( curr );
    }
    this._tag = curr.nodeName.toLowerCase();

    // set up the elements
    Element.addClassName( this._index, 'tab-list' );
    Element.addClassName( this._tab, 'folder-tab' );
    Element.addClassName( this._folder, 'folder' );

    // establish the folders
    var rexp = new RegExp( '<(' + this._tag + ')', 'ig' );
    var arr  = this._cabinet.innerHTML.replace( rexp, "||||<$1" ).split( '||||' );
        arr.shift();
        trace( 'found ' + arr.length + ' files' );
    this._cabinet.innerHTML = '';
    Element.removeClassName( this._cabinet, 'tabbed' );
    Element.addClassName( this._cabinet, 'tabbed-on' );
    var tabs = this._index.cloneNode( true );
    $A( arr ).each( function( html, i ){
      // build the div
      var folder = this._folder.cloneNode( true );
          folder.setAttribute( 'id', this._id + '-' + i );
          folder.innerHTML = html;
      // build the tab
      var tab = this._tab.cloneNode( true );
          tab.folder = folder.getAttribute( 'id' );
          tab.setAttribute( 'id', tab.folder + '-tab' );
      var heading = folder.getElementsByTagName( this._tag )[0];
      if( heading.getAttribute( 'title' ) ){
        tab.innerHTML = heading.getAttribute( 'title' )
      } else {
        tab.innerHTML = heading.innerHTML;
        heading.style.position = 'absolute';
        heading.style.top  = '0';
        heading.style.left = '-999em';
      }
      // active?
      if( i == 0 ){
        Element.addClassName( folder, 'visible' );
        this._active = folder.getAttribute( 'id' );
        Element.addClassName( tab, 'active-tab' );
      }
      tabs.appendChild( tab );
      tab.onclick = this.swap.bindAsEventListener( this );
      // append the folder
      this._cabinet.appendChild( folder );
    }.bind( this ) );
    this._cabinet.appendChild( tabs );
  },
  swap:       function( e ){
    trace( 'was active: ' + this._active );
    var tab = Event.element( e );
    Element.removeClassName( $( this._active + '-tab' ), 'active-tab' );
    Element.removeClassName( $( this._active ), 'visible' );
    Element.addClassName( tab, 'active-tab' );
    Element.addClassName( $( tab.folder ), 'visible' );
    this._active = tab.folder;
    trace( 'now active: ' + this._active );
  }
};

// if Prototype, lowpro & required DOM methods are available
if( typeof( Prototype ) != 'undefined' &&
    typeof( LowPro ) != 'undefined' &&
    document.getElementById &&
    document.getElementsByTagName &&
    document.createElement &&
    document.getElementsByTagName( 'div' ) ){
  Event.onReady( function(){
    trace( 'ready' );
    var cabinets = Array();
    $$( 'div.tabbed' ).each( function( el, i ){
      cabinets[i] = new TabInterface( el, i );
    } );
  } );
}

