/*
	基本のJS
	作成日：080211
	更新日：080211
*/


var yuga = {
	// imageのプリローダー
	preloader: {
		loadedImages: [],
		load: function (url){
			var img = this.loadedImages;
			var l = img.length;
			img[l] = new Image();
			img[l].src = url;
		}
	},
	// URIを解析したオブジェクトを返すfunction
	URI: function(s){
		this.originalPath = s;
		//絶対パスを取得
		this.getAbsolutePath = function(path){
			if (!path.match(/^(mailto:)|(javascript:)/)) {
				var img = new Image();
				img.src = path;
				path = img.src;
				img.src = '#';
			}
			return path;			
					};
		this.absolutePath = this.getAbsolutePath(s);
		//同じ文書にリンクしているかどうか
		this.isSelfLink = (this.absolutePath == location.href);
/*		if(absolutePath =~ /word-of-mouth/){
			this.isSelfLink = absolutePath;
		} else if (absolutePath =~ /tour/){
			this.isSelfLink = absolutePath;
		} else if (absolutePath =~ /ticket/){
			this.isSelfLink = absolutePath;
		} else if (absolutePath =~ /hotel/){
				this.isSelfLink = absolutePath;
		}*/
		//絶対パスを分解
		var fields = {'schema' : 2, 'username' : 5, 'password' : 6, 'host' : 7, 'path' : 9, 'query' : 10, 'fragment' : 11};
		var r = /^((\w+):)?(\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/.exec(this.absolutePath);
		for (var field in fields) {
			this[field] = r[fields[field]]; 
		}
	}
};

$(function(){
	
	//class="btn"はロールオーバーを設定（src属性を_on付きのものに差し替える）
	$('.btn').each(function(){
		this.originalSrc = $(this).attr('src');
		this.rolloverSrc = this.originalSrc.replace(/(\.gif|\.jpg|\.png)/, "_on$1");
		yuga.preloader.load(this.rolloverSrc);
	});
	//通常ロールオーバー
	$('.navbtn').not($('.btngroup .btn')).hover(function(){
		$(this).attr('src',this.rolloverSrc);
	},function(){
		$(this).attr('src',this.originalSrc);
	});
	//グループ化されたロールオーバー
	$('.btngroup').hover(function(){
		$(this).find('.btn').each(function(){
			$(this).attr('src',this.rolloverSrc);
		});
	},function(){
		$(this).find('.btn').each(function(){
			$(this).attr('src',this.originalSrc);
		});
	});
	
	//現在のページへのリンク
	$('a[@href]').each(function(){
		var href = new yuga.URI(this.getAttribute('href'));
		if (href.isSelfLink && !href.fragment) {
			$(this).addClass('current');
			//img要素が含まれていたら現在用画像（_cr）に設定
			$(this).find('img').each(function(){
				//ロールオーバーが設定されていたら削除
				$(this).unbind('mouseover');
				$(this).unbind('mouseout');
				this.currentSrc = this.getAttribute('src').replace(/(\.gif|\.jpg|\.png)/, "_cr$1");
				$(this).attr('src',this.currentSrc);
			});
		}
	});
});

/*kuchikomi下層コンテンツ*/
$(function(){
var word = location.href;
if (word.match("message")) {
	$("#nav .word-of-mouth img").remove();
	$("#nav .word-of-mouth").prepend('<img src="/common/images/utilitynav-kuchikomi_cr.png" alt="ベトナム口コミ情報" />');
} else if (word.match("directory")) {
	$("#nav .word-of-mouth img").remove();
	$("#nav .word-of-mouth").prepend('<img src="/common/images/utilitynav-kuchikomi_cr.png" alt="ベトナム口コミ情報" />');
};
});

$(function(){
var word = location.href;
if (word.match("hotel")) {
	$("#nav .hotel img").remove();
	$("#nav .hotel").prepend('<img src="/common/images/utilitynav-hotel_cr.png" alt="ホテルを探す" />');
};
});

/* ちらつき回避 */
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

$(function(){
	// 見出しの前にロケータがある場合
	$("div#mainContents div.locator h2:first-child").addClass("siblingA");//margin-top: 10px;
	$("div#mainContents div.locator h3:first-child").addClass("firstChild");//margin-top: 10px;
	$("div#mainContents div.locator h4:first-child").addClass("firstChild");//margin-top: 10px;
	$("div#mainContents div.locator + h2").addClass("siblingA");//margin-top: 10px;
	$("div#mainContents div.locator + h3").addClass("siblingA");//margin-top: 10px;
	$("div#mainContents div.locator + h4").addClass("siblingA");//margin-top: 10px;

	// 見出し要素の後ろにカラムがある場合
	$("div#mainContents h2 + div.locator h3:first-child").addClass("siblingB");//margin-top: 0;

	// 見出しが連続する場合
	$("div#mainContents h2 + h3").addClass("siblingB");//margin-top: 0;
	$("div#mainContents h3 + h4").addClass("siblingB");//margin-top: 0;

	// lastChild
	$("div#platform p:last-child").addClass("lastChild");
	$("div#platform ul:last-child").addClass("lastChild");
	$("div#platform ol:last-child").addClass("lastChild");
	$("div#platform li:last-child").addClass("lastChild");
	$("div#platform dl:last-child").addClass("lastChild");
	$("div#platform dt:last-child").addClass("lastChild");
	$("div#platform dd:last-child").addClass("lastChild");
	$("div.inner > :last-child").addClass("directLastChild");

	// lastChild
	$("div#platform p:first-child").addClass("firstChild");
	$("div#platform ul:first-child").addClass("firstChild");
	$("div#platform ol:first-child").addClass("firstChild");
	$("div#platform li:first-child").addClass("firstChild");
	$("div#platform dl:first-child").addClass("firstChild");
	$("div#platform dt:first-child").addClass("firstChild");
	$("div#platform dd:first-child").addClass("firstChild");

	$("div#mainContents p.symbol + ul").addClass("symbol");

	urlEn = location.href.indexOf("/en/");
	if (urlEn == -1){
		$('[@href^="http"]').filter('[@href!*="'+document.domain+'"]').attr("title","別ウィンドウが開きます");
		$('a[@href$=".pdf"]').attr("title","別ウィンドウが開きます");
	
		//プリントボタン
		//$('p.printBtn img').click(function(){
		//	window.open("/print.html?"+location.href);
		//});
	} else {
		$('[@href^="http"]').filter('[@href!*="'+document.domain+'"]').attr("title","Another window opens.");
		$('a[@href$=".pdf"]').attr("title","Another window opens.");
		
		//プリントボタン
		//$('p.printBtn img').click(function(){
		//	window.open("/en/print.html?"+location.href);
		//});	
	}
	
	//奇数、偶数を自動追加
	$('ul').each(function(){
		$(this).find('li:odd').addClass('even');
		$(this).find('li:even').addClass('odd');
	});
	$('table').each(function(){
		$(this).find('tr:odd').addClass('even');
		$(this).find('tr:even').addClass('odd');
	});
	
	//簡単カレント表示
	$('div#localNav a[@href]').each(function(){
		if (this.href == location.href) {
			$(this.parentNode).addClass('current');
		}
	});
	
	//カレント表示
	if($('div#topicPath ul li').length){
		var pathFirst = $('div#topicPath ul li')[1].innerHTML.replace(/\s/g,'').replace(/\<[^>]*\>/g, "");
		$('div#localNav ul li a[@href]').each(function(){
			var categoryTxt = this.innerHTML.replace(/\s/g,'').replace(/\<[^>]*\>/g, "");
			//alert(pathFirst+'\n'+categoryTxt);
			if (categoryTxt == pathFirst) {
				$(this.parentNode).addClass('current');
			}
			//if (this.href == location.href) {
				//$(this.parentNode).addClass('current');
			//}
		});
	}
	
	//画像へ直リンクするとthickboxで表示(thickbox.js利用)
	tb_init('a[@href$=".jpg"], a[@href$=".gif"], a[@href$=".png"]');
});

