/*

Script: Smoothtabs.js
	Smoothtabs - A mootools based site navigation effects script

Version: 0.2

License:
  Creative Commons Attribution 3.0 License. 
  To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/us/

Smoothtabs Copyright:
	copyright (c) 2008 Corey Wilson, <http://2catdesigns.com>
	
*/

var Smoothtabs = new Class({

  div_height : Array(),
  orig_div : Array(),
  
  initialize: function() {

    // data divs
    dd = $$('.smoothtab');

    // display our content divs so we can get the height
    for (i=0; i<dd.length; i++) {
      dd[i].setStyle('display', 'block');
    }

    tabs = $$('.smoothtabs-menu li');

    // get the height of each one of our content divs
    tabs.each(function(tab) {
      var base_name = tab.id.replace(/-current/, '');
      this.div_height[base_name] = $(base_name + '-data').scrollHeight;
      this.orig_div[base_name] = $(base_name + '-data').cloneNode(true);
    }.bind(this));    

    // now that we know the height, hide the divs
    for (i=0; i<dd.length; i++) {
      dd[i].setStyle('display', 'none');
    }

    tabs.each(function(tab) {
    	tab.addEvent('click', this.select_tab.pass(tab.id, this));
    }.bind(this));

  },

  select_tab: function(tab_id) {
    //var e = new Event();
    tab_id = tab_id.replace(/-current/,'');
    slide = $('paneles').effect('height', {duration: 250, wait:false, transition: Fx.Transitions.Elastic.easeInOut});

   var cur_data = $E('.smoothtab-current');
    var sel_data = $(tab_id + '-data');

    if ( !sel_data || cur_data.id == sel_data.id ) { return; }

    var cur_base = cur_data.id.substr(0, cur_data.id.indexOf('-data'));
    var sel_base = tab_id;

    var height = this.div_height[sel_base];
    var orig = this.orig_div[sel_base].clone();

    $(cur_base + '-current').id = cur_base;
    $(tab_id).id = tab_id + '-current';

    // out with the old, in with the new
    new Fx.Style(cur_data, 'opacity').start(0.0).chain(function () {

      slide.start(parseInt(height)).chain(function () {

        cur_data.setStyle('display', 'none');

        orig.setStyles({
                         display: 'block',
                         height: height + 'px'
                       });                          

        sel_data.setStyles({
                             display: 'block',
                             height: height + 'px',
                             opacity: 0
                          });

        new Fx.Style(sel_data, 'opacity').start(1.0).chain(function () {

          orig.className = 'panel smoothtab-current';
          cur_data.className = 'panel smoothtab';

          sel_data.replaceWith(orig);
        })
      })
    });
//e.stop();
  }

});