/**
 *	@filename		lib.js
 *	@charset		utf-8
 *	@created		October 28, 2009
 *	@lastmodified	October 28, 2009
 *	@description	共通js
 */
(function(){



//
//	スクロール用
//
var moveObject = function(){
	var y = document.body.scrollTop || document.documentElement.scrollTop;
	var target = Math.max(Math.floor(y - (y / 8)));
	scrollTo(0, target);
	if(target > 0){
		setTimeout(moveObject,10);
	}
};

////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	DOMツリー構築完了後に実行（onload前）
//
////////////////////////////////////////////////////////////////////////////////////////////////////
$(function(){


//
//	このページの先頭へボタン動作
//
$("p.anchorTop a").click(function(){
	moveObject();
	return false;
});
$("p#anchorTop a").click(function(){
	moveObject();
	return false;
});


//
//ポップアップリンク
//
$(".popup").click(function(){
	var url=$(this).attr("href");
	win1 = window.open(url);
	win1.focus();
	return false;
});


//
//チラシ画像の上に、拡大アイコンを乗せる
//
$("div#adButton p a").each(function(){
	$(this).append("<span class=\"iconZoom\">&nbsp;</span>");
});



//
//バナー画像のマウスオーバー処理
//
$(".banner a").live("mouseover",function(){
							$(this).css({"width":$("img",this).attr("width"),"height":$("img",this).attr("height"),"display":"block","background-color":"#ffffff"});
							$("img",this).fadeTo(100,0.5);
						});
$(".banner a").live("mouseout",function(){
							$("img",this).fadeTo(100,1);
						});

//
//チラシ画像のマウスオーバー処理
//
$("#adButton a").hover(function(){
							$(this).css({"width":$("img",this).attr("width"),"height":$("img",this).attr("height"),"display":"block","background-color":"#ffffff"});
							$("img",this).fadeTo(100,0.5);
						},function(){
							$("img",this).fadeTo(100,1);
						});


//印刷する
//
$("#getPrint a").click(function(){
	window.print();
	return false;
});


//
//ウィンドウを閉じる
//
$(".btnClose a").click(function(){
	window.close();
	return false;
});



//
//	別ウィンドウリンク
//
var win = null;
var width = null;
var height = null;

$("a.popupWindow").click(function(){
							
	var href = $(this).attr("href");
	var url = href.split("#")[0];
	var q = href.split("#")[1].split("&");
	var params = new Array();
	
	for(var i = 0; i != q.length; i++) {
		params[q[i].split('=')[0]]
			  =  q[i].split('=')[1];
	}
	
	//Win1ウィンドウが無い場合
	if(win==null||win.closed==true){
		if(params["width"]&&params["height"]){
			width = params["width"];
			height = params["height"];
			win = window.open(url,"Win1","location='0',status=0,menubar=0,scrollbars=1,resizable=0,width="+width+",height="+height+"\"");
		}else{
			width = null;
			height = null;
			win = window.open(url);
		}
	}
	//Win1ウィンドウが在る場合
	else{
		//新規ウィンドウのwidth とheight が同じ場合はlocationだけ変更
		if(!width && !height && !(params["width"]&&params["height"])){
			win.location = url;
		}else if((width == params["width"])&&(height == params["height"])){
			win.location = url;
		}
		//そうでない場合はwinを閉じて、Win1ウィンドウが無い場合と同じ処理
		else{
			win.close();
			if(params["width"]&&params["height"]){
				width = params["width"];
				height = params["height"];
				win = window.open(url,"Win1","location='0',status=0,menubar=0,scrollbars=1,resizable=0,width="+width+",height="+height+"\"");
			}else{
				width = null;
				height = null;
				win = window.open(url);
			}
		}
	}
	
	win.focus();
	return false;
});



//
//	最後のsectionにclass="last"を付与
//
$("div#mainContent div.section:last").addClass("last");


//
//	div#mainContent div.section:lastの後ろに<p class="clearFix"></p>付与
//
$('<p class="clearFix"></p>').insertAfter("div#mainContent div.section:last");


//
//	div#subContentの後ろに<p class="clearFix"></p>付与
//
$('<p class="clearFix"></p>').insertAfter("div#subContent");


//
//	トピックス一覧にclass="odd"を付与
//
$("dl.topicsList dd:even").addClass("odd");


//
//	矢印アイコンの右余白を設定
//
$("div.section img").each(function(){
	if($(this).attr("src").match("arrow_gray.gif")){
		$(this).css({"margin-right":"5px"});
	}
	if($(this).attr("src").match("arrow_blue.gif")){
		$(this).css({"margin-right":"5px"});
	}
	if($(this).attr("src").match("arrow_no_square.gif")){
		$(this).css({"margin-right":"5px"});
	}
})

//
//	見出し要素に子要素を挿入
//
if($("body.webPage").length!=0){
	$("div.section h2").wrapInner("<span class=\"inner\"></span>");
}
if($("body#home").length==0){
	$("div.section h3").wrapInner("<span class=\"inner\"></span>");
	$("div.section h4").wrapInner("<span class=\"inner\"></span>");
}

//
//	キャプション付き画像にキャプションを付ける
//
$("img.withCaption").each(function(){
	$(this).wrap("<span class=\"captionBox\"></span>").after("<span class=\"caption\">"+ $(this).attr("alt") +"</span>").parent("span.captionBox").css({"width":$(this).attr("width")});
});


//
//	リストの高さを揃える
//

if($("ul.twoColumnListWithFrame li").length!=0){
	ajustTwoColumnListWithFrameHeight();
	
	var prevHeight = $("body").height();
	
	setInterval(ajustTwoColumnListWithFrameHeight,100);
	
	$(window).unload(function(){
		//clearInterval(ajustTwoColumnListWithFrameHeight);
	});
}

function ajustTwoColumnListWithFrameHeight(){
	curHeight = $("body").height();
	
	if(curHeight != prevHeight){
		$("ul.twoColumnListWithFrame > li:even").each(function(){
		
			var next = $(this).next();
			
			$("div.contentMiddle",this).css("height","auto");
			$("div.contentMiddle",next).css("height","auto");
			
			$("div.contentHeader",this).css("height","auto");
			$("div.contentHeader",next).css("height","auto");
			
			$("div.contentFooter",this).css("height","auto");
			$("div.contentFooter",next).css("height","auto");
			
			var mHeight1 = $("div.contentMiddle",this).height();
			var mHeight2 = $("div.contentMiddle",next).height();
			
			var hHeight1 = $("div.contentHeader",this).height();
			var hHeight2 = $("div.contentHeader",next).height();
			
			var fHeight1 = $("div.contentFooter",this).height();
			var fHeight2 = $("div.contentFooter",next).height();
			
			var mMaxHeight = function(){
				if(mHeight1 > mHeight2){
					return mHeight1;
				}else{
					return mHeight2;
				}
			}();
			
			var hMaxHeight = function(){
				if(hHeight1 > hHeight2){
					return hHeight1;
				}else{
					return hHeight2;
				}
			}();
			
			var fMaxHeight = function(){
				if(fHeight1 > fHeight2){
					return fHeight1;
				}else{
					return fHeight2;
				}
			}();
			
			var b = jQuery.browser;
			var v = jQuery.browser.version;
			var padding = function(){
				if(b.msie&&v=="6.0"){
					return -8;
				}else if(b.msie&&v=="8.0"){
					return 0;
				}else{
					return 0;
				}
			}();
			
			$("div.contentMiddle",this).css({"height":mMaxHeight - padding+"px"});
			$("div.contentMiddle",next).css({"height":mMaxHeight - padding+"px"});
			
			$("div.contentHeader",this).css({"height":hMaxHeight - padding+"px"});
			$("div.contentHeader",next).css({"height":hMaxHeight - padding+"px"});
			
			$("div.contentFooter",this).css({"height":fMaxHeight - padding+"px"});
			$("div.contentFooter",next).css({"height":fMaxHeight - padding+"px"});
	  
		});
	}
	prevHeight = curHeight;
}

if($("ul.threeColumnListWithFrame li").length!=0){
	ajustThreeColumnListHeight();
	
	var prevHeight = $("body").height();
	
	setInterval(ajustThreeColumnListHeight,100);
	
	$(window).unload(function(){
		//clearInterval(ajustThreeColumnListHeight);
	});
}

function ajustThreeColumnListHeight(){
	curHeight = $("body").height();
	
	if(curHeight != prevHeight){
		$("ul.threeColumnListWithFrame > li").each(function(){
			if(parseInt($(this).parent("ul.threeColumnListWithFrame").children("li").index(this))%3==0){
				var next = $(this).next();
				var next2 = next.next();
				
				$("div.contentMiddle",this).css("height","auto");
				$("div.contentMiddle",next).css("height","auto");
				$("div.contentMiddle",next2).css("height","auto");
				
				$("div.contentHeader",this).css("height","auto");
				$("div.contentHeader",next).css("height","auto");
				$("div.contentHeader",next2).css("height","auto");
				
				$("div.contentFooter",this).css("height","auto");
				$("div.contentFooter",next).css("height","auto");
				$("div.contentFooter",next2).css("height","auto");
				
				var mHeight1 = $("div.contentMiddle",this).height();
				var mHeight2 = $("div.contentMiddle",next).height();
				var mHeight3 = $("div.contentMiddle",next2).height();
				
				var hHeight1 = $("div.contentHeader",this).height();
				var hHeight2 = $("div.contentHeader",next).height();
				var hHeight3 = $("div.contentHeader",next2).height();
				
				var fHeight1 = $("div.contentFooter",this).height();
				var fHeight2 = $("div.contentFooter",next).height();
				var fHeight3 = $("div.contentFooter",next2).height();
				
				var mMaxHeight = function(){
					if(mHeight1 > mHeight2){
						if(mHeight1 > mHeight3){
							return mHeight1;	
						}else{
							return mHeight3;	
						}
					}else{
						if(mHeight2 > mHeight3){
							return mHeight2;
						}else{
							return mHeight3;
						}
					}
				}();
				
				var hMaxHeight = function(){
					if(hHeight1 > hHeight2){
						if(hHeight1 > hHeight3){
							return hHeight1;	
						}else{
							return hHeight3;	
						}
					}else{
						if(hHeight2 > hHeight3){
							return hHeight2;
						}else{
							return hHeight3;
						}
					}
				}();
				
				var fMaxHeight = function(){
					if(fHeight1 > fHeight2){
						if(fHeight1 > fHeight3){
							return fHeight1;	
						}else{
							return fHeight3;	
						}
					}else{
						if(fHeight2 > fHeight3){
							return fHeight2;
						}else{
							return fHeight3;
						}
					}
				}();
				
				var b = jQuery.browser;
				var v = jQuery.browser.version;
				var padding = function(){
					if(b.msie&&v=="6.0"){
						return -8;
					}else if(b.msie&&v=="8.0"){
						return 0;
					}else{
						return 0;
					}
				}();
				
				$("div.contentMiddle",this).css({"height":mMaxHeight - padding+"px"});
				$("div.contentMiddle",next).css({"height":mMaxHeight - padding+"px"});
				$("div.contentMiddle",next2).css({"height":mMaxHeight - padding+"px"});
				
				$("div.contentHeader",this).css({"height":hMaxHeight - padding+"px"});
				$("div.contentHeader",next).css({"height":hMaxHeight - padding+"px"});
				$("div.contentHeader",next2).css({"height":hMaxHeight - padding+"px"});
				
				$("div.contentFooter",this).css({"height":fMaxHeight - padding+"px"});
				$("div.contentFooter",next).css({"height":fMaxHeight - padding+"px"});
				$("div.contentFooter",next2).css({"height":fMaxHeight - padding+"px"});
		   }
		});
	}
	prevHeight = curHeight;
}


if($("ul.twoColumnList li").length!=0){
	$("ul.twoColumnList > li:even").addClass("odd");
}

if($("ul.twoColumnList li").length!=0){
	ajustTwoColumnListHeight();
	
	var prevHeight = $("body").height();
	
	setInterval(ajustTwoColumnListHeight,100);
	
	$(window).unload(function(){
		//clearInterval(ajustTwoColumnListWithFrameHeight);
	});
}

function ajustTwoColumnListHeight(){
	curHeight = $("body").height();
	
	if(curHeight != prevHeight){
		$("ul.twoColumnList > li:even").each(function(){
		
			var next = $(this).next();
			
			$(this).css("height","auto");
			$(next).css("height","auto");
			
			var height1 = $(this).height();
			var height2 = $(next).height();
			
			var maxHeight = function(){
				if(height1 > height2){
					return height1;
				}else{
					return height2;
				}
			}();
			
			var b = jQuery.browser;
			var v = jQuery.browser.version;
			var padding = function(){
				if(b.msie&&v=="6.0"){
					return 0;
				}else if(b.msie&&v=="8.0"){
					return 0;
				}else{
					return 0;
				}
			}();
			
			$(this).css({"height":maxHeight - padding+"px"});
			$(next).css({"height":maxHeight - padding+"px"});
	  
		});
	}
	prevHeight = curHeight;
}

});
////////////////////////////////////////////////////////////////////////////////////////////////////


})();
