(function($) {
	$.tools = $.tools || {version: '@VERSION'};
	$.tools.tabs = { conf: {
		tabs: 'a',
		current: 'current',
		onBeforeClick: null,
		onClick: null,
		effect: 'default',
		initialIndex: 0,
		event: 'click'
		//rotate: false,
		//slideUpSpeed: 400,
		//slideDownSpeed: 400, history: false
		},
		addEffect: function(name, fn) { effects[name] = fn; }
	};
	var effects = {
		'default': function(i, done) {
			this.getPanes().hide().eq(i).show();
			done.call();
		},
	/*
		fade: function(i, done) {
			var conf = this.getConf(), speed = conf.fadeOutSpeed, panes = this.getPanes();
			if (speed) panes.fadeOut(speed);
			else panes.hide();
			panes.eq(i).fadeIn(conf.fadeInSpeed, done);
		},
		slide: function(i, done) {
		  var conf = this.getConf();
			this.getPanes().slideUp(conf.slideUpSpeed);
			this.getPanes().eq(i).slideDown(conf.slideDownSpeed, done);
		},
		ajax: function(i, done)  {
			this.getPanes().eq(0).load(this.getTabs().eq(i).attr("href"), done);
		}
	*/
	};

	var animating, w;
	$.tools.tabs.addEffect("horizontal", function(i, done) {
	  if (animating) return;
	  var nextPane = this.getPanes().eq(i),
		currentPane = this.getCurrentPane();
		w || (w = this.getPanes().eq(0).width());
		animating = true;
		nextPane.show();
		currentPane.animate({width: 0}, {
			step: function(now){ nextPane.css("width", w-now); },
			complete: function(){
				$(this).hide();
				done.call();
				animating = false;
			}
		});
		if (!currentPane.length){
			done.call();
			animating = false;
		}
	});

	function Tabs(root, paneSelector, conf) {
		var self = this,
			trigger = root.add(this),
			tabs = root.find(conf.tabs),
			panes = paneSelector.jquery ? paneSelector : root.children(paneSelector),
			current;
		if (!tabs.length)  { tabs = root.children(); }
		if (!panes.length) { panes = root.parent().find(paneSelector); }
		//if (!panes.length) { panes = $(paneSelector); }
		$.extend(this, {
			click: function(i, e) {
				var tab = tabs.eq(i);
				//if (typeof i == 'string' && i.replace("#", "")) {
				if (typeof i == 'string') {
					//tab = tabs.filter("[href*=" + i.replace("#", "") + "]");
					tab = tabs.filter("a.corner5px_header");
					i = Math.max(tabs.index(tab), 0);
				}
				/*
				if (conf.rotate) {
					var last = tabs.length -1;
					if (i < 0) { return self.click(last, e); }
					if (i > last) { return self.click(0, e); }
				}
				*/
				if (!tab.length) {
					if (current >= 0) { return self; }
					i = conf.initialIndex;
					tab = tabs.eq(i);
				}
				if (i === current) { return self; }
				e = e || $.Event();
				e.type = "onBeforeClick";
				trigger.trigger(e, [i]);
				//if (e.isDefaultPrevented()) { return; }
				effects[conf.effect].call(self, i, function() {
					current = i;
					e.type = "onClick";
					trigger.trigger(e, [i]);
				});
				tabs.removeClass(conf.current);
				tab.addClass(conf.current);
				return self;
			},
			getConf: function() { return conf; },
			getTabs: function() { return tabs; },
			getPanes: function() { return panes; },
			getCurrentPane: function() { return panes.eq(current); },
			getCurrentTab: function() { return tabs.eq(current); },
			getIndex: function() { return current; },
			next: function() { return self.click(current + 1); },
			prev: function() { return self.click(current - 1); },
			destroy: function() {
				tabs.unbind(conf.event).removeClass(conf.current);
				panes.find("a").unbind("click.T");
				return self;
			}
		});
		$.each("onBeforeClick,onClick".split(","), function(i, name) {
			if ($.isFunction(conf[name])) $(self).bind(name, conf[name]);
			self[name] = function(fn) {
				if (fn) { $(self).bind(name, fn); }
				return self;
			};
		});
		/*
		if (conf.history && $.fn.history) {
			$.tools.history.init(tabs);
			conf.event = 'history';
		}
		*/
		tabs.each(function(i) {
			$(this).bind(conf.event, function(e) {
				self.click(i, e);
				return e.preventDefault();
			});
		});
		panes.find("a[href^=#]").bind("click.T", function(e) {
			self.click($(this).attr("href"), e);
		});
		
		//if (location.hash && conf.tabs == "a" && root.find("[href=" +location.hash+ "]").length)
			//self.click(location.hash);
		//else {
			if (conf.initialIndex === 0 || conf.initialIndex > 0) self.click(conf.initialIndex);
		//}
		
	}
	$.fn.tabs = function(paneSelector, conf) {
		var el = this.data("tabs");
		if (el) {
			el.destroy();
			this.removeData("tabs");
		}
		if ($.isFunction(conf)) conf = {onBeforeClick: conf};
		conf = $.extend({}, $.tools.tabs.conf, conf);
		this.each(function() {
			el = new Tabs($(this), paneSelector, conf);
			$(this).data("tabs", el);
		});
		return conf.api ? el: this;
	};
}) (jQuery);

