/**
 *	@filename		lib.js
 *	@charset		utf-8
 *	@created		December 8, 2009
 *	@lastmodified	December 8, 2009
 *	@description	共通js
 */
(function(){


////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	DOMツリー構築完了後に実行（onload前）
//
////////////////////////////////////////////////////////////////////////////////////////////////////


//
//	スクロール用
//
$(function(){
var scroller = window.scroller = (function(){
	var START = "start",
		END   = "end",
		doc   = document,
		bdy   = doc.body,
		de    = doc.documentElement,
		sto   = setTimeout,
		sct   = scrollTo,
		utils;
	
	var handlers = {};
	handlers[START] = [];
	handlers[ END ] = [];
	
	var dispatchEvent = function(typ){
		var arr = handlers[typ] || [];
		for(var i=0, fnc, len=arr.length; i<len; i++){
			(fnc = arr[i]) && fnc.call(utils, { type : typ });
		}
	};
	
	var scroll = function(){
		var y = bdy.scrollTop || de.scrollTop;
		sct(0, y = (y << 3) - y >> 3);
		y ? sto(scroll, 16) : dispatchEvent(END);
	};
	
	return (utils = {
		START : START,
		END : END,
		scroll : function(){
			dispatchEvent(START);
			scroll();
			return false;
		},
		addEventListener : function(typ, fnc){
			var arr = handlers[typ];
			arr && arr.push(fnc);
			return this;
		},
		removeEventListener : function(typ, fnc){
			var arr = handlers[typ] || [],
				cpy = [].concat(arr),
				i = cpy.length;
			while(i--){
				fnc === cpy[i] && arr.splice(i, 1);
			}
			return this;
		}
	});
})();

//
//	このページの先頭へボタン動作
//
$(".anchorTop a,#anchorTop a").click(scroller.scroll);


//
//ポップアップリンク
//
$(".popup").click(function(){
	var url=$(this).attr("href");
	win1 = window.open(url);
	win1.focus();
	return false;
});

//印刷する
//
$("#getPrint a").click(function(){
	window.print();
	return false;
});


//
//ウィンドウを閉じる
//
$(".btnClose a").click(function(){
	window.close();
	return false;
});


//
//	imgの親aタグに class="noBorder"を付与
//
$("img").parent("a").addClass("noBorder");


//
//	ul.normalList liにclass="odd"またはclass="even"を付与
//
$("ul.normalList").each(function(){
	$("li:first",this).addClass("first");
	$("li:first",this).next().addClass("second");
	$("li:even",this).addClass("odd");
	$("li:odd",this).addClass("even");
})


//
//	dl.dateList ddにclass="odd"またはclass="even"を付与
//
$("dl.dateList").each(function(){
	$("dd:first",this).addClass("first");
	$("dd:first",this).next().addClass("second");
	$("dd:even",this).addClass("odd");
	$("dd:odd",this).addClass("even");
})

$(".withArrow li a").hover(function(){
		$(this).parent("li").addClass("hover");
	},
	function(){
		$(this).parent("li").removeClass("hover");
	});

//
//thickbox対応画像の上に、拡大アイコンを乗せる
//
$("a.thickbox").each(function(){
	if($("img",this).length!=0){
		$(this).width($("img",this).width() + 6);
		$(this).css("display","block");
		$(this).append("<span class=\"iconZoom\">&nbsp;</span>");
		$(this).hover(
			function(){
				$("img",this).fadeTo(100,0.6);
			},
			function(){
				$("img",this).fadeTo(100,1);
			}
		);
	}
});


//
//カレンダー表示制御
//
var yText = $("#calYear span.num").text();
$("#calYear span.num").addClass("num" + yText);

var mText = $("#calMonth span.num").text();
$("#calMonth span.num").addClass("num" + mText);

var dText = $("#calDay span.num").text();
$("#calDay span.num").addClass("num" + dText);

var wText = $("#calDayOfTheWeek span.num").text();
wAry = {"日":"Sun","月":"Mon","火":"Tue","水":"Wed","木":"Thu","金":"Fri","土":"Sat"};
$("#calDayOfTheWeek span.num").addClass("num" + wAry[wText]);


//
//	エリアマップ表示制御
//
var mapContainer = $("#areaMapBody");
var mapBody = $("#areaMapBody img");
var mapFocused = new Array(13);
mapFocused[0] = ["0px 0px","0px 0px"];
mapFocused[1] = ["0px 0px","0px -1px"];
mapFocused[2] = ["0px -238px","0px -78px"];
mapFocused[3] = ["0px -476px","0px -158px"];
mapFocused[4] = ["-200px 0px","0px -235px"];
mapFocused[5] = ["-200px -238px","0px -313px"];
mapFocused[6] = ["-200px -476px","0px -390px"];
mapFocused[7] = ["-400px -0px","0px -466px"];
mapFocused[8] = ["-400px -238px","0px -543px"];
mapFocused[9] = ["-400px -476px","0px -621px"];
mapFocused[10] = ["-600px -0px","0px -698px"];
mapFocused[11] = ["-600px -238px","0px -776px"];
mapFocused[12] = ["-600px -476px","0px -851px"];
//エリア名表示用
var areaName = $("<span id=\"areaNameContainer\">エリア名</span>");
//チラツキ防止用のダミー要素
var dummyName = $("<span id=\"dummyName\" style=\"position:absolute;top:-10000px;left:-10000px;\"></span>");
$("body").append(areaName);
$("body").append(dummyName);
$("map#areaMap area").hover(
	function(){
		mapContainer.css("background-position",mapFocused[Number($(this).attr("id").replace(/area/,''))][0]);
		areaName.css("background-position",mapFocused[Number($(this).attr("id").replace(/area/,''))][1]);
		mapBody.css("opacity","0.01");
		//チラツキ防止
		dummyName.stop();
		dummyName.fadeOut(0);
	},
	function(){
		//チラツキ防止
		dummyName.fadeIn(10,function(){mapBody.css("opacity","1");});
		areaName.css({"top":"-10000px","left":"-10000px"});
	}
);

$("map#areaMap area").mousemove(function(e){
	areaName.css({"top":e.pageY - 80 + "px","left":e.pageX - 50 + "px"});
});

areaName.mouseover(
	function(e){
		areaName.css({"top":e.pageY - 80 + "px","left":e.pageX - 50 + "px"});
	}
);

areaName.mousemove(function(e){
	areaName.css({"top":e.pageY - 80 + "px","left":e.pageX - 50 + "px"});
});

$("#widgetAreaSearch").mouseover(
	function(e){
		areaName.css({"top":"-10000px","left":"-10000px"});
	}
);


//
//	別ウィンドウリンク
//
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+"\"");
			win = window.open(url,"Win1","location='0',status=0,menubar=0,scrollbars=1,resizable=1,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+"\"");
				win = window.open(url,"Win1","location='0',status=0,menubar=0,scrollbars=1,resizable=1,width="+width+",height="+height+"\"");
			}else{
				width = null;
				height = null;
				win = window.open(url);
			}
		}
	}
	
	win.focus();
	return false;
});


//
//画像のマウスオーバー処理
//
$("a.ov").live("mouseover",
	function(){
		if($(this).parent("div#chishiContainer").length!=0){
			$(this).css({"width":$("img",this).attr("width"),"height":$("img",this).attr("height"),"display":"block","background-color":"#ffffff"});
		}
		$("img",this).fadeTo(100,0.5);
	}
);
$("a.ov").live("mouseout",
	function(){
		$("img",this).fadeTo(100,1);
	}
);


//
//	見出し処理
//
$("#sectionView .sectionHeading span span").each(function(){
	$(this).html($(this).text().replace(/（(.*?)）/,'<span class=\"detail\">（$1）</span>'));
});
$("#sectionDetail .sectionHeading span span").each(function(){
	$(this).html($(this).text().replace(/（(.*?)）/,'<span class=\"detail\">（$1）</span>'));
});


//
//	表の処理
//
$("td.withArrow a,th.withArrow a").hover(function(){
		$(this).parent("td,th").addClass("ov");
	},
	function(){
		$(this).parent("td,th").removeClass("ov");
	});

//
//	LocalNews
//
$("div#widgetLocalNews dt:even").addClass("even");
$("div#widgetLocalNews dd:even").addClass("even");
$("div#widgetLocalNews dt:odd").addClass("odd");
$("div#widgetLocalNews dd:odd").addClass("odd");

//
//	Tokutoku
//
$("div#widgetTokubai dt:even").addClass("even");
$("div#widgetTokubai dd:even").addClass("even");
$("div#widgetTokubai dt:odd").addClass("odd");
$("div#widgetTokubai dd:odd").addClass("odd");
$("div#widgetTokubai tr:even").addClass("even");

$("div#sectionTokubai dt:even").addClass("even");
$("div#sectionTokubai dd:even").addClass("even");
$("div#sectionTokubai dt:odd").addClass("odd");
$("div#sectionTokubai dd:odd").addClass("odd");
$("div#sectionTokubai tr:even").addClass("even");


//
//	リストの高さを揃える
//
$("ul.chirashiList").each(function(){
	$("li:nth-child(3n)",this).each(function(){
		$(this).addClass("by3");
	});
});

if($("ul.chirashiList").length!=0){
	ajustChirashiListHeight();
	
	var prevHeight = $("#copyright").height();
	
	setInterval(ajustChirashiListHeight,100);
	
	$(window).unload(function(){
		//clearInterval(ajustTwoColumnListWithFrameHeight);
	});
}

function ajustChirashiListHeight(){
	curHeight = $("#copyright").height();
	
	//if(curHeight != prevHeight){
		$("ul.chirashiList").each(function(){
			$("li:nth-child(3n+1),li:first",this).each(function(){
				var thisObj = $(this);
				var next = $(thisObj).next();
				var next2 = $(thisObj).next().next();
				
				$("a",thisObj).css("height","auto");
				$("a",next).css("height","auto");
				$("a",next2).css("height","auto");
				
				var height1 = $(thisObj).height();
				var height2 = $(next).height();
				var height3 = $(next2).height();
				
				var maxHeight = function(){
					if(height1 > height2){
						if(height1 > height3){
							return height1;	
						}else{
							return height3;	
						}
					}else{
						if(height2 > height3){
							return height2;
						}else{
							return height3;
						}
					}
				}();
				
				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 3;
					}else{
						return 3;
					}
				}();
				
				$("a",thisObj).css("height",maxHeight - padding);
				$("a",next).css("height",maxHeight - padding);
				$("a",next2).css("height",maxHeight - padding);
		  
			});
		});
	//}
	prevHeight = curHeight;
}

});
////////////////////////////////////////////////////////////////////////////////////////////////////


})();
