var tab = {
	init: function(){
		tabs = this.setup.tabs;
		pages = this.setup.pages;
		restartTime = tab.setup.restart - tab.setup.interval;
		for(i=0; i<pages.length; i++) {
			if(i != 0) pages[i].style.display = 'none';
			
			tabs[i].onclick = function(){
				tab.showpage(this);
				if(tab.setup.interval > 0){
					clearInterval(timer);
					if(tab.setup.restart >= tab.setup.interval) setTimeout(tab.auto, restartTime);
				}
				return false;
			};
		}
		this.start();
	},

	auto: function(){
		if(tab.setup.interval > 0) timer = setInterval("tab.showpage(tabs[num])", tab.setup.interval);
	},
		
	showpage: function(obj){
		allpage_flag = "off";
		for(i=0; i<tabs.length; i++) {
			if(tabs[i] == obj) {
				if(tab.setup.allpageID && tabs[i].className.match(tab.setup.allpageID)) {
					tab.allshow();
					allpage_flag = "on";
				}else{
					pages[i].style.display = 'block';
				}
				if (!tabs[i].className.match('current')) tabs[i].className = tabs[i].className + ' ' + 'current';
				num = i+1;
				if(num > tabs.length-1) num = 0;
			}else{
				if(tab.setup.allpageID && tabs[i].className.match(tab.setup.allpageID)) {
					pages[i].style.display = 'block';
				}else{
					if(allpage_flag=="off") pages[i].style.display = 'none';
				}
				tabs[i].className = tabs[i].className.replace(/current/g, '').replace(/ +/g,' '); //currentクラス＆無駄な空白を消す
			}
		}
	},

	allshow: function(){
		for(a=0; a<tabs.length; a++) {
			pages[a].style.display = 'block';
		//	alert("test");
		}
	},
		
	start: function(){
		if(window.location.hash) {
			//ハッシュがあればハッシュを取得
			var hash = window.location.hash;
			var startObj;
			hash = hash.split("?");
			hash = hash[0].split("#");
			startObj = document.getElementById(hash[1]);
			for(i=0; i<pages.length; i++) {
				if(pages[i] == startObj) startObj = tabs[i];
			}
		}else {
			//ハッシュが無ければカレントタブを取得
			for(i=0; i<tabs.length; i++) {
				if (tabs[i].className.match('current')) startObj = tabs[i];
			}
		}

		//取得できない場合は1番目のタブに設定
		if(!startObj) startObj = tabs[0];
		
		this.showpage(startObj);
		this.auto();
	}
}

//IEでの画像のちらつきを防止
try { 
    document.execCommand('BackgroundImageCache', false, true); 
} catch(e) {} 

