var i;
var n;
var overImgs;

jQuery(function($) {
	/*
	 * 画像ロールオーバー
	 */
	overImgs = $("img.imgover");
	for(i=0;i<overImgs.length;i++){
		var img = $(overImgs[i]);
		var src = img.attr("src");
		var prefix = src.substr(0, src.lastIndexOf("."));
		var postfix = src.substring(src.lastIndexOf("."));
		img.attr({
			"src_out":src,
			"src_over":prefix + "_o" + postfix
		});
	}
	overImgs.bind("mouseover",onOverImgOver);
	overImgs.bind("mouseout",onOverImgOut);

	/*
	 * リンク選択時の枠線消去
	 */
	var blur = function () { this.blur() };
	for (i = 0; i < document.links.length; i++){
		document.links[i].onfocus = blur;
	}

	/*
	 * フォントサイズ設定適用
	 */
	$('#fontsizer').jfontsizer({
		applyTo: '#MainContents',
		changesmall: '2',
		changelarge: '2',
		expire: 30
	});

	/*
	 * グローバルナビ＆ローカルナビへの現在位置の反映
	 */
	var currentUrl = location.href;
//	var globalDir = location.pathname.split("/")[1];
	var globalDir = currentUrl.split("/")[1];
	var tmpHref;
	var containerDirs;
	var containerDir;

	/*
	 * グローバルナビへの現在位置の反映
	 */
	$("#globalNavi a").each(function(idx){
		tmpHref = this.href;
		var u = tmpHref.split('/');
		var p = '';
		for(var i = 0; i < u.length; i++){
			if(i >= 3){
				p = p + '/' + u[i];
			}
		}
		tmpHref = p;
		
		if (!/^(?:https?:)?\/\//i.test(tmpHref)){
			var tmpDir = tmpHref.split("/")[1];
			if(tmpDir === globalDir){
				$(this).addClass("now");
			}
		}
	});

	/*
	 * ローカルナビへの現在位置の反映
	 */
	var li;
	var d0 = 0;
	var d1 = 0;
	var d2 = 0;
	var d3 = 0;
	var dArray = new Array();
	var a0;
	var a1;
	var a2;
	var a3;
	var tmpHref;
	var myDepths;
	$("#bmenulist > li").each(function(idx) {
		li = $(this);
		tmpHref = $("a",li).attr("href");
		if (tmpHref == undefined) {
			return;
		}
		if (li.hasClass("li2")) {
			//インデックスの更新
			d0++;
			d1 =
			d2 =
			d3 = 0;
			//現在位置の表示
//			if (currentUrl.indexOf(tmpHref) > 0) {
			if (/^(?:\.\.\/)*(?:index\.html)?$/.test(tmpHref)) {
				$("a",li).addClass("now");
				a0 = d0;
			}
		} else if (li.hasClass("li2smn")) {
			//インデックスの更新
			d1++;
			d2 =
			d3 = 0;
			//現在位置の表示
			if (/^(?:\.\.\/)*(?:index\.html)?$/.test(tmpHref)) {
				$("a",li).addClass("now");
				a1 = d1;
			}
			li.data("menuDepths",String([d0,d1,d2,d3]));
		} else if (li.hasClass("li2ssmn")) {
			//インデックスの更新
			d2++;
			d3 = 0;
			//現在位置の表示
			if (/^(?:\.\.\/)*(?:index\.html)?$/.test(tmpHref)) {
				$("a",li).addClass("now");
				a2 = d2;
			}
			li.data("menuDepths",String([d0,d1,d2,d3]));
		} else if (li.hasClass("li2sssmn")) {
			//インデックスの更新
			d3++;
			//現在位置の表示
			if (/^(?:\.\.\/)*(?:index\.html)?$/.test(tmpHref)) {
				$("a",li).addClass("now");
				a3 = d3;
			}
			li.data("menuDepths",String([d0,d1,d2,d3]));
		}
	});
	//第二階層の表示・非表示
	$("#bmenulist > li.li2smn").each(function(idx) {
		myDepths = $(this).data("menuDepths").split(",");
		$(this).toggle(parseInt(myDepths[0]) == a0);
	});
	//第三階層の表示・非表示
	$("#bmenulist > li.li2ssmn").each(function(idx) {
		myDepths = $(this).data("menuDepths").split(",");
		$(this).toggle(parseInt(myDepths[0]) == a0 && parseInt(myDepths[1]) == a1);
	});
	//第四階層の表示・非表示
	$("#bmenulist > li.li2sssmn").each(function(idx) {
		myDepths = $(this).data("menuDepths").split(",");
		$(this).toggle(parseInt(myDepths[0]) == a0 && parseInt(myDepths[1]) == a1 && parseInt(myDepths[2]) == a2);
	});

	var cbs_id = setInterval(function() {
		if (typeof changeBoxSize === "function") {
			changeBoxSize();
			clearInterval(cbs_id);
		}
	}, 333);

	
	/*========== グローバルナビゲーションのプルダウン =============*/
	jQuery('#globalNavi > li')
	.mouseover(function(){
		jQuery(this).addClass('open');
	})
	.mouseout(function(){
		jQuery(this).removeClass('open');
	});
	
	/*========== 検索窓 =============*/
	jQuery('.sbox')
	.focus(function(){
		var t = jQuery(this);
		if(t.val() === 'Search'){t.val('');}
		t.css({'color':'#000'});
	})
	.blur(function(){
		var t = jQuery(this);
		if(t.val() == ''){t.val('Search');}
		t.css({'color':'#AAA'});
	})

  if ($(window).height() < $("body").outerHeight()) {
    $('<div id="pageTop"><img src="/common/img/pagetop_icon.gif" alt="" /><a href="#top">ページTOP</a></div>').prependTo("#CMN_footer");
    $("#pageTop + #CMN_footerIn").css("margin-top", "13px");
  }
  $('a[href$="#top"]').click(function() { $.scrollTo(0, { duration: 500 }); return false; });

});

jQuery(window).resize(function() {
  var pageTop = $("#pageTop");
  if ($(window).height() >= $("body").outerHeight()) {
    pageTop.remove();
    $("#CMN_footerIn").css("margin-top", "51px");
  } else if (pageTop.size() === 0) {
    $('<div id="pageTop"><img src="/common/img/pagetop_icon.gif" alt="" /><a href="#top">ページTOP</a></div>').prependTo("#CMN_footer").click(function() { $.scrollTo(0, { duration: 500 }); return false; });
    $("#pageTop + #CMN_footerIn").css("margin-top", "13px");
  }
});

//ロールオーバー系のイベントハンドラ
function onOverImgOver(e){
	$(this).attr("src",$(this).attr("src_over"));
}
function onOverImgOut(e){
	$(this).attr("src",$(this).attr("src_out"));
}


function iLocation(sURL, bIPage) {
  sURL = sURL || location.pathname.replace(/(?=\.[^\/.]+$)/, "_i") + location.search + location.hash;
  bIPage = bIPage === true;
  var bIOS = /\biP(?:ad|hone|od)\b/.test(navigator.userAgent);
  if (bIOS && !bIPage || !bIOS && bIPage) {
    location.href = sURL;
  }
}



// リンクボタンのフローティング表示
var floating_box = function(box){
	var box = jQuery('#' + box);
	box.css({
		'position': 'fixed',
		'right': '10px'
	});
	var move = function(win,first){
		var viewport_h = jQuery(win).height();
		var h = box.outerHeight();
		var sc = jQuery(win).scrollTop();
		var pos_top = viewport_h - (10) - h + sc;
		if(first){
			box.css({'top':  pos_top + 'px'});
		}else{
			box.css({'top': pos_top + 'px'}, 'fast');
		}
	}
	if(jQuery.browser.msie && jQuery.browser.version < 7){
		box.css({
			'position': 'absolute'
		});
		move(window,'first');
		jQuery(window).scroll(function(){move(this)}).resize(function(){move(this)});
	}else{
		box.css({
			'bottom': (10) + 'px'
		});
	}
}
