AE.namespace('AE.widget');
if(typeof AE.widget.tab == 'undefined') {
	AE.widget.tab = function(){
		this.dfConfig = {
			tabClass           : 't',         // 标签的 class
			tabTagName         : 'div',       // 标签的 tagName
			tabContainerId     : '',          // 标签的父层 id
			currentClass       : 'current',   // 选中情况下标签的 class
	
			contentClass       : 'content',   // 内容的 class
			contentTagName     : 'div',       // 内容的 tagName
			contentContainerId : '',          // 内容父层的 id
			defaultIndex       : 0,           // 默认选中
	
			eventName          : 'mouseover', // 触发事件名称
			preventDefault     : true,        // 是否去掉 tab 默认的 click 事件(链接)
			hideFocus          : true         // 是否去掉 tab 链接的虚线框
		};
	};
	AE.widget.tab.prototype = {
		init : function(customConfig){
			this.config = YL.merge(this.dfConfig, customConfig || {});
			this.tabs = YUD.getElementsByClassName(this.config.tabClass, this.config.tabTagName, this.config.tabContainerId);
			this.contents = YUD.getElementsByClassName(this.config.contentClass, this.config.contentTagName, this.config.contentContainerId);
			var mlen = (this.tabs.length < this.contents.length ? this.tabs.length : this.contents.length);
	
			if( typeof this.config.defaultIndex != 'number' || this.config.defaultIndex < 0 )
				this.config.defaultIndex = 0;
			else if( this.config.defaultIndex > mlen-1 )
				this.config.defaultIndex = mlen-1;
	
			this.currentIndex = this.config.defaultIndex;
	
			for(var i=0,l=this.tabs.length; i<l; i++) {
				(this.currentIndex == i ? YUD.addClass : YUD.removeClass)(this.tabs[i], this.config.currentClass);
				this.tabs[i].index = i;
				
				if(this.config.hideFocus) { //去掉链接虚线框
					var links = this.tabs[i].getElementsByTagName('a');
					YUD.setStyle(links, 'outline', 'none');
					for(var j=0; j<links.length; j++) {
						links[j].hideFocus = true;
					}
				}
			}
			for(i=0,l=this.contents.length; i<l; i++) {
				this.contents[i].style.display = i == this.currentIndex ? '' : 'none'
			}
	
			var _this = this;
			YUE.on(this.tabs, this.config.eventName, function(e) {
				if(this.index == _this.currentIndex || this.index > mlen-1)
					return YUE.stopEvent(e);
				
				YUD.addClass(this, _this.config.currentClass);
				YUD.removeClass(_this.tabs[_this.currentIndex], _this.config.currentClass);
					
				_this.contents[this.index].style.display = '';
				_this.contents[_this.currentIndex].style.display = 'none';
				
				_this.currentIndex = this.index;
				YUE.stopEvent(e);
			});
			if(this.config.preventDefault) {
				YUE.on(this.tabs, 'click', function(e){YUE.stopEvent(e);});
			}
		}
	};
}