/*
	Tabs 
	Version: 1.0
	(c) Copyright 2008 Israel Romero | http://www.urdsign.com.
	All Rights Reserved. 
	
	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.
*/

var Tabs =  Class.create({
	initialize: function(container, options) {
		
		if (!$(container)) {
		    throw(container+" does not exist!");
		    return false;
		}
		
		this.tabs = $(container).firstDescendant();
		this.tabsLinks = this.tabs.select('a');
		this.tabsContent = $(container).select('div.tabs_content div');
		this.animating = false;
		
		this.options = Object.extend({
			animate: false,
			activeClass: 'active',
			activationEvent: 'click',
			initialTab: null
		}, options || {});
		
		this.currentTab = (this.options.initialTab) ?  this.tabsContent.indexOf($(this.options.initialTab)) :  0;
		this.tabsContent.invoke('hide');
		this.tabsLinks[this.currentTab].addClassName(this.options.activeClass);
		this.tabsContent[this.currentTab].show();
		
		
		if (this.tabsLinks) {
			this.tabsLinks.each( function(link) {
				Event.observe(link, this.options.activationEvent, this.buttonEventHandler.bindAsEventListener(this), false);
				link.onclick = function() { return false; };
			}.bind(this));
		}
		
	},
	
	buttonEventHandler: function(event) {
		var tab = event.element();
		var tabIndex = this.tabsLinks.indexOf(tab);
		if (tab == this.tabsLinks[this.currentTab]) return false;
		this.showTab(this.tabsContent[tabIndex]);
	},
	
	showTab: function(element) {
		if (this.animating) return false;
		
		if (this.tabsLinks)  this.tabsLinks[this.currentTab].removeClassName(this.options.activeClass);
		this.currentTab = this.tabsContent.indexOf($(element));
		if (this.tabsLinks)  this.tabsLinks[this.currentTab].addClassName(this.options.activeClass);
		
		if (this.options.animate) {
			this.tabsContent.each(function(tab) {
				
				if ($(tab).visible()) {
					new Effect.BlindUp(tab, { beforeStart: function() { this.animating = true; }.bind(this) });
				}
			});
			new Effect.BlindDown(element, { 
										beforeStart: function() { this.animating = true; }.bind(this),
										afterFinish: function() { this.animating = false; }.bind(this),
										queue: 'end'
									  });
		} else {
			this.tabsContent.invoke('hide');
			$(element).show();
		}
	},
	
	next: function() {
		var next = this.currentTab + 1;
		if (next >= this.tabsContent.size()) next = 0;
		this.showTab(this.tabsContent[next]);
	},
	
	previous: function() {
		var prev = this.currentTab - 1;
		if (prev < 0) prev = this.tabsContent.size() - 1;
		this.showTab(this.tabsContent[prev]);
	}
});