/*

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>

*/


window.addEvent('domready', function() {
  Smooth = new Smoothtabs();
});


var Smoothtabs = new Class({

  div_height : Array(),
  orig_div : Array(),

  initialize: function() {

    // data divs
    dd = $$('[class="smoothtab"]');

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

    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').getStyle("height");
      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].setOpacity(0);
    }

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

  },

  select_tab: function(tab_id) {

    tab_id = tab_id.replace(/-current/,'');
    slide = $('banner_contenido').effect('height', {duration: 100, wait:false, transition: Fx.Transitions.Cubic.easeOut});

    var cur_data = $E('[class="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
                       });

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

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

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

          sel_data.replaceWith(orig);
        })
      })
    });

  }

});

