// JavaScript Document
/*[TopModuleTab]*/
function JLJS_TopModuleTab(){
	this.TAB_ID = "pnlSwitchHome";
	this.TAB_COOKIE = "hotelTab";
	this.TICKET_MODULE_ID = "JS_ticketModule";
	this.HOTEL_MODULE_ID = "JS_hotelModule";
	
	this.tabObjHash = {};
	this.fileSuffixPtn   = /(\.jpe?g|\.gif|\.png)$/i;
	this.statusSuffixPtn = "_[nso]";
	this.status = ["o", "n", "s"];
	this.contentsIDs = [this.TICKET_MODULE_ID, this.HOTEL_MODULE_ID];
}
JLJS_TopModuleTab.prototype = {
	init : function(params) {
		var self = this;
		var tabs = JLJS.getElementsByTagName("li", document.getElementById(this.TAB_ID));
		
		for(var i = 0; i < tabs.length; i ++){
			if(!this.contentsIDs[i]) {
				continue;
			}
			var tabImg = JLJS.getElementsByTagName("img", tabs[i])[0];
			tabs[i].changeoverID = this.contentsIDs[i];
			
			var hash = {
				"tab" : tabs[i],
				"tabImg" : tabImg,
				"loadFlg" : false
			};
			
			this.tabObjHash[this.contentsIDs[i]] = hash;
			
			this.preloadImage(tabImg);
			
			JLJS.addEvent(tabs[i], "mouseover", function(e){self.mouseover(e.currentTarget.changeoverID);});
			JLJS.addEvent(tabs[i], "mouseout", function(e){self.mouseout(e.currentTarget.changeoverID);});
			JLJS.addEvent(tabImg.parentNode, "click", function(e){
															   e.preventDefault();
															   self.changeover(e.currentTarget.parentNode.changeoverID);
														});
		}
		
		var hotelTabCookie = JLJS_CookieMgr.getCookie(this.TAB_COOKIE);
		if(hotelTabCookie){
			this.changeover(this.HOTEL_MODULE_ID);
		} else {
			this.changeover(this.TICKET_MODULE_ID);
		}
	},
	
	mouseover : function(id) {
		var tabObj = this.tabObjHash[id];
		
		if(!JLJS.classAttr.check(tabObj["tab"], "pseudo-current")) {
			this.changeImage(tabObj["tabImg"], "o");
		}
	},
	
	mouseout : function(id) {
		var tabObj = this.tabObjHash[id];
		
		if(!JLJS.classAttr.check(tabObj["tab"], "pseudo-current")) {
			this.changeImage(tabObj["tabImg"], "n");
		}
	},
	
	changeImage : function(tabImg, status) {
		tabImg.src.match(this.fileSuffixPtn);
		var suffix = RegExp.$1;
		var reg = new RegExp(this.statusSuffixPtn + suffix);
		tabImg.src = tabImg.src.replace(reg, "_" + status + suffix);
	},
	
	changeover : function(id) {
		for(var i = 0; i < this.contentsIDs.length; i++) {
			var tmpId = this.contentsIDs[i];
			var tabObj = this.tabObjHash[tmpId];
			
			if(!tabObj) {
				continue;
			}
			
			if(id == tmpId){
				JLJS.classAttr.add(tabObj["tab"], "pseudo-current");
				this.changeImage(tabObj["tabImg"], "s");
				document.getElementById(tmpId).style.display = "block";
				if(!tabObj["loadFlg"]){
					this.topModuleSetup(id);
					tabObj["loadFlg"] = true;
				}
				this.setupTabCookie(id);
			} else {
				JLJS.classAttr.remove(tabObj["tab"], "pseudo-current");
				this.changeImage(tabObj["tabImg"], "n");
				document.getElementById(tmpId).style.display = "none";
			}
		}
	},
	
	topModuleSetup : function(id){
		if(!id) return;
		
		if(id == this.TICKET_MODULE_ID){
			JLJS_topModuleSetup();
		} else if(id == this.HOTEL_MODULE_ID) {
			JLJS_HM.topModuleSetup();
		}
	},
	
	setupTabCookie : function(id){
		if(id == this.TICKET_MODULE_ID){
			var date = new Date();
			date.setFullYear(date.getFullYear() - 1);
			var expires = date.toGMTString();
			JLJS_CookieMgr.setCookie(this.TAB_COOKIE, false, {"expires":expires});
		} else if(this.HOTEL_MODULE_ID){
			JLJS_CookieMgr.setCookie(this.TAB_COOKIE, true);
		}
	},
	
	preloadImage : function(imgObj) {	
		imgObj.src.match(this.fileSuffixPtn);
		var suffix = RegExp.$1;
		var reg = new RegExp(this.statusSuffixPtn + suffix);
		for(var i = 0; i < this.status.length; i++) {
			JLJS.preloadImage(imgObj.src.replace(reg, "_" + this.status[i] + suffix));
		}
	}
}
var JLJS_TopModuleTab = new JLJS_TopModuleTab();
JLJS.addOnload(function(){JLJS_TopModuleTab.init();});
/*[/TopModuleTab]*/

