/*====================================================================================================
//////////////////////////////////////////////////////////////////////////////////////////////////////

 Author : http://www.metaphase.co.jp/
 created: 2010/02/18
 update : 2010/04/19
          2010/05/25 Cookie Controlの追加

//////////////////////////////////////////////////////////////////////////////////////////////////////
====================================================================================================*/

var tabControl = {

	// 設定
	conf : {
		// ナビゲーションのID
		tabNavigationId : 'tabNavi',
		// デフォルトアクティブとする要素に付加するクラス名
		activeClass : 'activeTab',
		// 以下、処理の中で使用する変数（編集不可）
		tabMainId : [],
		cookieName : "__TAB_SAVED__"
	},

	setHTML : function(){
		tabControl.setDefState();
		tabControl.setTrigger();
	},

	setDefState : function(){
		var i, count = 0;
		var tabItem = document.getElementById(tabControl.conf.tabNavigationId);
		var anchorAdd = !Cookies.getCookies(tabControl.conf.cookieName) ? "" : Cookies.getCookies(tabControl.conf.cookieName);

		for(i=0;i<tabItem.children.length;i++){
			tabControl.conf.tabMainId[count] = tabItem.children[i].children[0].hash;
			//tabControl.conf.tabMainId[count] = tabItem.children[i].children[0].id;
			tabItem.children[i].children[0].href = "javascript:;";
			var dispItem = document.getElementById(tabControl.conf.tabMainId[count].slice(1));
			//var dispItem = document.getElementById(tabControl.conf.tabMainId[count]);
			if( anchorAdd == dispItem.id ){
				dispItem.style.display = 'block';
				for(k=0;k<tabItem.children.length;k++) {
					tabItem.children[k].className = "";
				}
				tabItem.children[i].className = tabControl.conf.activeClass;
				
				if(tabItem.children[i].children[0].children.length > 0){
					var tmpURL = tabItem.children[i].children[0].children[0].src;
					tmpURL = tmpURL.replace( /^(.+_)[an]\.(.+)$/,
						function(str, p1, p2) {
							return(p1 + "a." + p2);
						});
					tabItem.children[i].children[0].children[0].src = tmpURL;
				}
			} else if( anchorAdd == "" && tabControl.conf.activeClass == tabItem.children[i].className ){
				dispItem.style.display = 'block';
				if(tabItem.children[i].children[0].children.length > 0){
					var tmpURL = tabItem.children[i].children[0].children[0].src;
					tmpURL = tmpURL.replace( /^(.+_)[an]\.(.+)$/,
						function(str, p1, p2) {
							return(p1 + "a." + p2);
						});
					tabItem.children[i].children[0].children[0].src = tmpURL;
				}
			} else {
				dispItem.style.display = 'none';
			}
			count++;
		}

		
	},


	setTrigger : function(){
		var i, count = 0;
		var tabItem = document.getElementById(tabControl.conf.tabNavigationId);
		
		for(i=0;i<tabItem.children.length;i++){

			// ラッパー使用して、onclickに引数を割り当てる
			tabItem.children[i].onclick = (function (this_, count_) {
				return function() { tabControl.action(this_, count_); }
			})(tabItem.children[i], count);

			count++;
		}
		
	},

	action : function( thisNode, count ){
	
		var i;
		
		var tabItem = document.getElementById(tabControl.conf.tabNavigationId);
		
		for(i=0;i<tabItem.children.length;i++){
			tabItem.children[i].className = "";
			if(tabItem.children[i].children[0].children.length > 0){
				var tmpURL = tabItem.children[i].children[0].children[0].src;
				tmpURL = tmpURL.replace( /^(.+_)[an]\.(.+)$/,
					function(str, p1, p2) {
						return(p1 + "n." + p2);
					});
				tabItem.children[i].children[0].children[0].src = tmpURL;
			}
		}
		
		thisNode.className = tabControl.conf.activeClass;
		if(thisNode.children[0].children.length > 0){
			var tmpURL = thisNode.children[0].children[0].src;
			tmpURL = tmpURL.replace( /^(.+_)[an]\.(.+)$/,
				function(str, p1, p2) {
					return(p1 + "a." + p2);
				});
			thisNode.children[0].children[0].src = tmpURL;
		}
		
		for(i=0;i<tabControl.conf.tabMainId.length;i++){
			var switchItem = document.getElementById(tabControl.conf.tabMainId[i].slice(1));
			if(count == i){
				switchItem.style.display = 'block';
				Cookies.createCookies(tabControl.conf.cookieName, switchItem.id,100000);
			} else {
				switchItem.style.display = 'none';
			}
		}
		return false;
	},

	addEvent : function(){
		if(window.addEventListener) {
			window.addEventListener("load", this.setHTML, false);
		}
		else if(window.attachEvent) {
			window.attachEvent("onload", this.setHTML);
		}
	}
}


tabControl.addEvent();
