/*
 * Fabtabulous! Simple tabs using Prototype
 * http://tetlaw.id.au/view/blog/fabtabulous-simple-tabs-using-prototype/
 * Andrew Tetlaw
 * version 1.1 2006-05-06
 * http://creativecommons.org/licenses/by-sa/2.5/
 */
var Fabtabs = Class.create();

Fabtabs.prototype = {
	initialize : function(element) {
    this.changeTimeout = 8000;
    this.effectDuration = 2.0;
    
    this.duration = TabsDuration;
    
		this.element = $(element);
		var options = Object.extend({}, arguments[1] || {});
		this.menu = $A(this.element.getElementsByTagName('a'));
		this.show(this.getInitialTab());
		this.menu.each(this.setupTab.bind(this));
		
		Event.observe($('tab_prev'),'click',this.prev.bindAsEventListener(this),false);
		Event.observe($('tab_next'),'click',this.next.bindAsEventListener(this),false);
		
		this.nextChange();
	},
	setupTab : function(elm) {
		Event.observe(elm,'click',this.activate.bindAsEventListener(this),false)
	},
	activate :  function(ev) {
	  if( this.blocked ) {
	    Event.stop(ev);
	    return true;
	  }
	  this.animate = false;
		var elm = Event.findElement(ev, "a");
		Event.stop(ev);
	  this.show(elm);
		this.menu.without(elm).each(this.hide.bind(this));
		this.resetChange();
	},
	resetChange : function() {
	  try{
	    window.clearTimeout( this.timeout );
	  }
	  catch(e) {
	    
	  }
	  this.nextChange();
	},
	nextChange : function() {
	  this.timeout = window.setTimeout( this.change.bind(this), this.duration[this.getActiveTab()] != 0 ? this.duration[this.getActiveTab()] : this.changeTimeout );
	},
	change : function() {
    if( this.blocked ) return;
	  this.next( true );
	  this.nextChange();
	},
	getActiveTab : function() {
	  var i = false;
	  for ( var i=0; i<this.menu.length; i++ ) {
	    if( this.menu[i].hasClassName('active-tab') ) {
	        break;
	      }
	  }
	  return i;
	},
	prev : function( turn ) {
	  if( this.blocked ) {
	    return true;
	  }
	  if( turn == true ) this.animate = true; else this.animate = false;
	  
	  if( this.menu.length < 2 ) return;
	  
	  var elm = false;
	  for ( var i=1; i<this.menu.length; i++ ) {
	    if( this.menu[i].hasClassName('active-tab') ) {
	        elm = this.menu[i-1];
	        break;
	      }
	  }
	  
	  if( !elm ) {
	    elm = this.menu[this.menu.length - 1];
	  }
	
	  if( elm ) {
	    if( this.animated ) {
	      this.menu.without(elm).each(this.hide.bind(this));
		    this.show(elm);
	    }
	    else {
		    this.show(elm);
		    this.menu.without(elm).each(this.hide.bind(this));
	    }
	  }
	  
	  if( turn != true ) this.resetChange();
	  
	  return false;
	},
	next : function( turn ) {
	  if( this.blocked ) {
	    return true;
	  }
	  if( turn == true ) this.animate = true; else this.animate = false;
	  
	  if( this.menu.length < 2 ) return;
	  
	  var elm = false;
	  for ( var i=0; i<this.menu.length - 1; i++ ) {
	    if( this.menu[i].hasClassName('active-tab') ) {
	        elm = this.menu[i+1];
	        break;
	      }
	  }
	
	  if( !elm ) {
	    elm = this.menu[0];
	  }
	  
    if( elm ) {
	    if( this.animated ) {
	      this.menu.without(elm).each(this.hide.bind(this));
		    this.show(elm);
	    }
	    else {
		    this.show(elm);
		    this.menu.without(elm).each(this.hide.bind(this));
	    }
	  }
	  
	  if( turn != true ) this.resetChange();
	  
	  return false;
	},
	hide : function(elm) {
	  
	  if( !$(elm).hasClassName('active-tab') ) return;
	  
		if( this.animate && $(this.tabID(elm)).getElementsByTagName('img').length ) {
		  new Effect.Fade($(this.tabID(elm)), { duration: this.effectDuration }); 
		  
		}
		else {
		  $(this.tabID(elm)).hide();
		}
		$(elm).removeClassName('active-tab');
		$(this.tabID(elm)).removeClassName('active-tab-body');
	},
	show : function(elm) {
		if( this.animate ) { 
		  if( $(this.tabID(elm)).getElementsByTagName('img').length ) {
		    this.blocked = true;
		    var elem = this.tabID(elm);
		    //new Effect.Appear($(this.tabID(elm)), { duration: this.effectDuration, afterFinish: this.afterfinish.bind(this) });
		    new Effect.Parallel(
      				[
      					new Effect.Appear(elem,{sync:true})
      				],
      				{
      					duration:this.effectDuration,
      					afterFinish: this.afterfinish.bind(this)
      				}
		      );
		  }
		  else {
		    window.setTimeout( '$(\'' + this.tabID(elm) + '\').show()', 1 );
		  }
		}
		else {
		  $(this.tabID(elm)).show();
		}
		$(elm).addClassName('active-tab');
		$(this.tabID(elm)).addClassName('active-tab-body');

	},
	afterfinish : function() {
	  this.blocked = false;
	},
	tabID : function(elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},
	getInitialTab : function() {
		return this.menu.first();
	}
}

var FabtabsObject;

Event.observe(window,'load',function(){ FabtabsObject = new Fabtabs('tabs'); },false);

