;(function($){
	$.ui = $.ui || {};
	$.fn.tab = function(options) {
        return this.each(function() {
			var opts = $.extend({},options);
			if(opts !== false) new $.ui.tab(this, opts);
		});
    };

	$.ui.tab = function(obj, options) {
		this.options = $.extend({}, $.ui.tab.setting, options);
		this.element = obj;

		this.refresh();
		this.attachEvents();
	}
	$.ui.tab.prototype = {
		refresh: function() {
			this.index = this.options.index || 0;
			if (this.index < 0 || this.index > $(this.element).find(this.options.tabs).length - 1) {
				this.index = 0;
			}
			$(this.element).find(this.options.tabs).removeClass(this.options.cur).eq(this.index).addClass(this.options.cur);
			$(this.element).find(this.options.box).hide().eq(this.index).show();
		},
		attachEvents: function() {
			var _this = this;
			$(this.element).find(this.options.tabs).bind(this.options.evtype, function(event) {
				if (!$(this).hasClass(_this.options.cur)) {
					var that = this;
					//if (event.target == this) {
						setTimeout(function() {
							var tab = $(_this.element).find(_this.options.tabs);
							var index = $.inArray(that, tab);
							tab.filter("." + _this.options.cur).removeClass(_this.options.cur).end().eq(index).addClass(_this.options.cur);
							//tab.removeClass(_this.options.cur).eq(index).addClass(_this.options.cur);
							$(_this.element).find(_this.options.box).hide().eq(index).show();
						}, _this.options.delay)
					//}
					event.stopPropagation();
					return false;
				}
			});
		}
	}
	$.extend($.ui.tab, {
		setting: {
			tabs: ">ol li",
			delay: 500,
			index: 0,
			cur: 'cur',
			evtype: "click",
			box: ">.main"
		}
	});
})(jQuery);
