//@charset "utf-8";
//////////////////////////////////////////////////////////////////////////
//
// 地図
//
//////////////////////////////////////////////////////////////////////////
$(document).ready(function() {
	$('div#map').each(function(){
	
	
	/* 地図マウスオーバー */
		var distance = 10;
		var time = 300;
		var hideDelay = 100;
		var hideDelayTimer = null;
		
		var beingShown = false;
		var shown = "";
		
		var trigger = $('map#m_map area');
		var popup = $('div.areaPanel').css('opacity', 0);
		
		var topArray = new Array();
		topArray = ['149','174','152','220','163','179'];
		
		//otherMouseOut($('div.areaPanel'));
		
		//北海道・東北
		var popup1 = $('div#area01');
		$([trigger.get(0),trigger.get(1)]).mouseover(
			function () {
				otherMouseOut($('div.areaPanel:not(div#area01):visible'));
				onMouseOverFunc(popup1);
			}
		).mouseout(
			function () {
				onMouseOutFunc(popup1);
			}
		);
		$(popup.get(0)).mouseover(
			function(){
				onMouseOverPopFunc(popup1);
			}
		).mouseout(
			function () {
				onMouseOutFunc(popup1);
			}
		);
		
		//関東
		var popup2 = $('div#area02');
		$([trigger.get(2)]).mouseover(
			function () {
				otherMouseOut($('div.areaPanel:not(div#area02):visible'));
				onMouseOverFunc(popup2);
			}
		).mouseout(
			function () {
				onMouseOutFunc(popup2);
			}
		);
		$(popup.get(1)).mouseover(
			function(){
				onMouseOverPopFunc(popup2);
			}
		).mouseout(
			function () {
				onMouseOutFunc(popup2);
			}
		);
		
		//中部
		var popup3 = $('div#area03').css('opacity', 0);
		$([trigger.get(3)]).mouseover(
			function () {
				otherMouseOut($('div.areaPanel:not(div#area03):visible'));
				onMouseOverFunc(popup3);
			}
		).mouseout(
			function () {
				onMouseOutFunc(popup3);
			}
		);
		$(popup.get(2)).mouseover(
			function(){
				onMouseOverPopFunc(popup3);
			}
		).mouseout(
			function () {
				onMouseOutFunc(popup3);
			}
		);
		
		//関西
		var popup4 = $('div#area04').css('opacity', 0);
		$([trigger.get(4)]).mouseover(
			function () {
				otherMouseOut($('div.areaPanel:not(div#area04):visible'));
				onMouseOverFunc(popup4);
			}
		).mouseout(
			function () {
				onMouseOutFunc(popup4);
			}
		);
		$(popup.get(3)).mouseover(
			function(){
				onMouseOverPopFunc(popup4);
			}
		).mouseout(
			function () {
				onMouseOutFunc(popup4);
			}
		);
		
		//中国・四国
		var popup5 = $('div#area05').css('opacity', 0);
		$([trigger.get(5),trigger.get(6)]).mouseover(
			function () {
				otherMouseOut($('div.areaPanel:not(div#area05):visible'));
				onMouseOverFunc(popup5);
			}
		).mouseout(
			function () {
				onMouseOutFunc(popup5);
			}
		);
		$(popup.get(4)).mouseover(
			function(){
				onMouseOverPopFunc(popup5);
			}
		).mouseout(
			function () {
				onMouseOutFunc(popup5);
			}
		);
		
		//九州・沖縄
		var popup6 = $('div#area06').css('opacity', 0);
		$([trigger.get(7),trigger.get(8)]).mouseover(
			function () {
				otherMouseOut($('div.areaPanel:not(div#area06)'));
				onMouseOverFunc(popup6);
			}
		).mouseout(
			function () {
				onMouseOutFunc(popup6);
			}
		);
		$(popup.get(5)).mouseover(
			function(){
				onMouseOverPopFunc(popup6);
			}
		).mouseout(
			function () {
				onMouseOutFunc(popup6);
			}
		);
		
		function onMouseOverFunc(paraPopup){//エリアmouseover時
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			
			if (beingShown || shown == paraPopup.attr("id")) {
				return;
			} else {
				beingShown = true;
				shown = paraPopup.attr("id");
				paraPopup.css({
					display: 'block'
				})
				.animate({
					top: '-=' + distance + 'px',
					opacity: 1
					}, time, 'swing',
					function() {
						beingShown = false;
					}
				);
			}
		}
		
		function onMouseOverPopFunc(paraPopup){//POPUP mouseover時
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			
			if (beingShown || shown == paraPopup.attr("id")) {
				return;
			} else {
				beingShown = true;
				paraPopup.css({
					display: 'block'
				});
				beingShown = false;
				shown = paraPopup.attr("id");
			}
			
		}
		
		function onMouseOutFunc(paraPopup){//エリアmouseover時
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				beingShown = false;
				paraPopup.animate({
					top: '+=' + distance + 'px',
					opacity: 0
					}, time, 'swing',
					function () {
						shown = null;
						paraPopup.css('display', 'none');
					}
				);
			}, hideDelay);
		}
		
		
		
		function otherMouseOut(other){
			$('div.areaPanel:visible').each(function(){
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
				hideDelayTimer = null;
				beingShown = false;
				if(shown == other.attr("id")){
					shown = null;
				}
				for(i=0;i<other.length;i++){
					var index = $('div.areaPanel').index(other[i]);
					$('div.areaPanel:eq('+ index +'):visible').animate({
						top: topArray[index]+ 'px',
						opacity: 0
						}, 'swing').css('display', 'none');
				}
			});
		}
	});
	
	
});
