var tabs=function(JSON) {
	this.options={
		id:JSON.id,
		tabId:JSON.tabId||JSON.id,
		tabTn:JSON.tabTn||'li',
		tabCn:JSON.tabCn,
		conId:JSON.conId||JSON.id,
		conTn:JSON.conTn||'div',
		conCn:JSON.conCn,
		bnsId:JSON.bnsId,
		focus:JSON.focus,
		current:JSON.current||0,
		eType:JSON.eType||'click',
		interval:JSON.interval||3000,
		curCn:JSON.curCn||'current',
		tempCn:[]
	}
	var opt=this.options;
	opt.tabs = opt.tabCn?getByCn(opt.tabCn,id(opt.tabId)):id(opt.tabId).getElementsByTagName(opt.tabTn);
	opt.cons = opt.conCn?getByCn(opt.conCn, id(opt.conId)):id(opt.conId).getElementsByTagName(opt.conTn);

	// check if the number of tabs equals the cons's;
	if (opt.tabs.length != opt.cons.length) {
		throw new Error("Match Failed");
		return;
	} else opt.levels = opt.tabs.length;
	
	for (var i = 0,l=opt.levels;i<l;i++){
		( function() {
			var b = i;
			opt.tempCn[i] = opt.tabs[i].className;
			addEvent(opt.tabs[i],opt.eType,function() {
				opt.current = b;
				opt.runId&&clearTimeout(opt.runId);
				opt.runId = setTimeout(opt.start, 10);
			})
		})()
	}
	if(opt.bnsId&&opt.bnsId.length==2){
		opt.prevBtn=id(opt.bnsId[0]);
		opt.nextBtn=id(opt.bnsId[1]);
		addEvent(opt.prevBtn,'click',function() {
			if(!opt.focus)opt.current--;
			else opt.current-=2;
			opt.runId&&clearTimeout(opt.runId);
			opt.start();
		})
		addEvent(opt.nextBtn,'click',function() {
			opt.runId&&clearTimeout(opt.runId);
			if(!opt.focus)opt.current++;
			opt.start();
		})
	}

	(opt.start = function() {
		opt.runId&&clearTimeout(opt.runId);
		if (opt.current >= opt.levels)
			opt.current = 0;
		if (opt.current < 0)
			opt.current = opt.levels-1;
		for (var i=0,l=opt.levels;i<l;i++){
			opt.tabs[i].className = (opt.current == i) ? opt.curCn+' '+opt.tempCn[i] : opt.tempCn[i];
			opt.cons[i].style.display = (opt.current == i) ? 'block' : 'none';
		}
		if(opt.focus){
			opt.current++;
			opt.runId = setTimeout(opt.start, opt.interval);
		}
	})()
}
var id=function(s){return document.getElementById(s);}
var getByCn=function (cn, from,onlyOne) {
	var re = [],from=from?((from.nodeType==1)?from.getElementsByTagName('*'):from):document.getElementsByTagName('*');
	for ( var i = 0, l =from.length ; i < l; i++) {
		if (hasClass(cn, from[i])){
			re.push(from[i]);
			if(onlyOne) break;
		}
	}
	return re;
}
var hasClass=function(val, elem) {
	var re = new RegExp("(^|\\s)" + val + "(\\s|$)");
	if (re.test(elem.className))
		return true;
}
var addEvent=function ( obj, type, fn ) {
	 if (obj.addEventListener)
		 obj.addEventListener( type, fn, false );
	 else if (obj.attachEvent) {
		 obj["e"+type+fn] = fn;
		 obj.attachEvent( "on"+type, function() { obj["e"+type+fn]()} );
	 }
}

