/*
 * 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/
 *
 * 修改版,由ybbk修改.修改内容：
 *  1.增加排除功能：不绑定到链接其它页面的Tab( 在链接标签上增加属性untab 如<a href='sss.html' untab>Tab</a>)
 *	2.点击Tab后,执行页面上的回调方法(在链接标签上增加callback属性,其值指向一个js函数,如<a href='sss.html' callback='javascript:jsFunc()'>Tab</a>)
 */
var Fabtabs = Class.create();

Fabtabs.prototype = {
	initialize : function(element) {
		if(!element) return;
		this.element = $(element);
		if(!this.element) return;
		var options = Object.extend({}, arguments[1] || {});
		var elms = this.element.getElementsByTagName('a');
		this.menu = $A(this.element.getElementsByTagName('a'));
		this.menu.each(this.setupTab.bind(this));
		if(this.menu.length==0)
			return;
		this.show(this.getInitialTab());
	},
	setupTab : function(elm) {
			// 修改版 排除不需要触发动作的Tab(一般这些Tab直接链接到其它页面)
			if(elm.getAttribute('untab')!=null){
				this.menu = this.menu.without(elm);
				return;
			}
			Event.observe(elm,'click',this.activate.bindAsEventListener(this),false)
	},
	activate :  function(ev) {
		var elm = Event.findElement(ev, "a");
		this.show(elm);
		this.menu.without(elm).each(this.hide.bind(this));
	},
	hide : function(elm) {
		$(elm).removeClassName('active-tab');
		var vv = $(this.tabID(elm))
		if(vv)
			vv.removeClassName('active-tab-body');
	},
	show : function(elm) {
		$(elm).addClassName('active-tab');
		$(this.tabID(elm)).addClassName('active-tab-body');
		this.callBack(elm);
	},
	callBack : function(elm){
			var callback = elm.getAttribute('callback');
			if(!callback || callback==null){
					return;
			}else{
					eval(callback);
			}
	},
	tabID : function(elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},
	getInitialTab : function() {
		if(document.location.href.match(/#(\w.+)/)) {
			var loc = RegExp.$1;
			var elm = this.menu.find(function(value) { return value.href.match(/#(\w.+)/)[1] == loc; });
			return elm || this.menu.first();
		}else{
			return this.menu.first();
		}
	}
};

Event.observe(window,'load',function(){ fabtbs = new Fabtabs('cont-tabs-nav'); },false);
