/**
 * ...
 * @author DefaultUser
 */

(function() {
	MYNAMESPACE.namespace('modules.Tab');
	MYNAMESPACE.modules.Tab = function() {
		this.tabElemId = "";
		this.currentId = "";
		this.cookieName = "";
		this.cookieVal;
		this.initialize.apply(this, arguments);
	};
	MYNAMESPACE.modules.Tab.prototype = {
		// コンストラクタ
		initialize: function(tabElemId, currentId, cookieName) {
			var thisObj = this;
			thisObj.tabElemId = tabElemId;
			thisObj.currentId = currentId;
			thisObj.cookieName = cookieName;
			/*
			console.log('MYNAMESPACE.modules.Tab :: initialize');
			console.log('	thisObj.tabElemId = ' + thisObj.tabElemId);
			console.log('	thisObj.currentId = ' + thisObj.currentId);
			console.log('	thisObj.cookieName = ' + thisObj.cookieName);
			*/
		},
		
		setCookie: function(cookieName, cookieVal, days) {
			var expires;
			if (days) {
				var date = new Date();
				date.setTime(date.getTime() + (days*24*60*60*1000));
				expires = "; expires=" + date.toGMTString();
			} else {
				expires = "";
			}
			document.cookie = cookieName + "=" + cookieVal + expires + "; path=/";
		},
		
		getCookie: function(cookieName) {
			var nameEQ = cookieName + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') {
					c = c.substring(1, c.length);
				}
				if (c.indexOf(nameEQ) == 0) {
					return c.substring(nameEQ.length, c.length);
				}
			}
			return null;
		},
		
		activate: function() {
			var thisObj = this;
			$(thisObj.currentId).css("display", "none");
			var cookieVal = thisObj.getCookie(thisObj.cookieName);
			
			if (cookieVal) {
				if (cookieVal !== "#" && $(cookieVal).html() !== null && $(thisObj.tabElemId + " " + cookieVal).html() !== null) {
					thisObj.currentId = cookieVal;
				}
			}
			
			if (location.href.indexOf("kanto.html") !== -1) {
				thisObj.currentId = "#east";
				thisObj.setCookie(thisObj.cookieName, thisObj.currentId, 7);
				
			} else if (location.href.indexOf("kansai.html") !== -1) {
				thisObj.currentId = "#west";
				thisObj.setCookie(thisObj.cookieName, thisObj.currentId, 7);
			}
			
			$(thisObj.tabElemId + " .tabNav a").removeClass("active");
			$((thisObj.tabElemId + " .tabNav a[href='" + thisObj.currentId + "']")).addClass("active");
			
			$(thisObj.currentId).css("display", "block");
			
			$(thisObj.tabElemId + " .tabNav a").click(function(event) {
				var targetId = $(this).attr("href");
				thisObj.setCookie(thisObj.cookieName, targetId, 7);
			});
			
			$(thisObj.tabElemId).css("visibility", "visible");
			$(thisObj.tabElemId).css("display", "block");
		},
		
		erase: function() {
			var thisObj = this;
			thisObj.setCookie(thisObj.cookieName, null);
		}
	};
})();

