/* area.jsより先に読み込む事 */

// 設定情報を保持しておくいれもの
var active = {
	day: new Array(),
	//地域関係なしで最後の予報コードを種別ごとにとっておく
	kind: new Array(),
	//地域ごとに最後の予報コードを種別ごとにとっておく
	areakind: new Array()
};

//DOM読み込み時に実行
$(document).ready(function(){

	//地域切り替えイベント設定[地図上]
	$("#area00 .icons img").click(function(){
		//classを分解[上2桁が地域コード]
		var area = $(this).attr("class").substr(0,2);
		selArea(area);
	});

	//地域切り替えイベント設定[タブ]
	$("#areaTab li").click(function(){
		//classを分解[上2桁が地域コード]
		var area = $(this).attr("class").substr(0,2);
		selArea(area);
	});
	//クッキーがセットされている場合それを使用
	var d = $.cookie('activeday');
	if(d != null)
	{
		var w = $.cookie('activekindw');
		var h = $.cookie('activekindh');
		if ( weather[w]==null )
		{
			w = getDefaultWeatherKind();
		}
		if ( health[h]==null )
		{
			h = getDefaultHealthKind();
		}
		selWitem(weather[w]['num'],w,true);
		selHitem(health[h]['num'],h,true);
		selDay(d,true);
		selectKind(w,'w',d);
		selectKind(h,'h',d);
		//selectKind($.cookie('activekindw'),'w',d);
		//selectKind($.cookie('activekindh'),'h',d);
	}
	//クッキーがセットされていない場合、デフォルト
	else
	{
		selectKind(getDefaultWeatherKind(),'w',0);
		selectKind(getDefaultHealthKind(),'h',0);		
	}
	//ticker処理
	$("#HeadLine").append("<ul id=\"ticker\"></ul>");
	var texts = $("#tickercomment").text().split("|||");
	$.each(texts,function(){
		$("#ticker").append("<li><span>" + this + "</span></li>");
	});
	$("#ticker").liScroll({travelocity: 0.04});
});

var icon_id_prefix = new Array();
var icon_prefix = new Array();
var icon_postfix = new Array();
// 気象アイコンのあるdivのIDの前につくもの
icon_id_prefix['w'] = "Icon";
// 気象アイコン画像の前につくもの
icon_prefix['w'] = "/wicon/iwicon/p";
// 気象アイコン画像の後につくもの
icon_postfix['w'] = ".png";
// 健康予報アイコンのあるdivのIDの前につくもの
icon_id_prefix['h'] = "icon";
// 健康予報アイコン画像の前につくもの
icon_prefix['h'] = "/images/bws/icon/032x032/i";
// 健康予報アイコン画像の後につくもの
icon_postfix['h'] = ".png";
// 健康予報凡例のあるdivのIDの前につくもの
var legend_id_prefix = "Exp";
// 健康予報凡例のある画像の前につくもの
var legend_prefix = "/images/bws/legend/155x035/ih";
// 健康予報凡例のある画像の後につくもの
var legend_postfix = ".png";

//健康予報の凡例のリンク先や名称
//ここで定義するようにするかDB等php経由でとるか・・・未定
//
//宮下注 2009.10.04
//ここでは、
//
//var healthItems = {
//	'518':{'name':'熱中症','link':'/bws/health/heatstroke/518heatstroke_title.htm'},
//
//のように全項目のリストを管理し、表示する項目を、
//
//var healthItemList = [518,505,503,508];
//として、php経由で与え、タブを構成してはいかがでしょう
/*
var health = {
	'518':{'name':'熱中症','num':0,'link':'/bws/health/heatstroke/518heatstroke_title.htm'},
	'505':{'name':'紫外線','num':1,'link':'/bws/health/uv/505uv_title.htm'},
	'503':{'name':'関節痛','num':2,'link':'/bws/health/ra/503ra_title.htm'},
	'508':{'name':'洗濯指数','num':3,'link':'/bws/life/wash/508wash01.htm'}
};
var weather = {
	'229':{'name':'天気','num':0},
	'004':{'name':'気温','num':1},
	'215':{'name':'降水確率','num':3}
}
*/

/* 天気種別等変更時の処理
 * 最初はclass等を利用した相対指定で作成したが
 * IE6でのレスポンスがよくなかったため、ID指定に
 * 変更した。
 *
 * @param k 予報コード：229, 508等
 * @param type 予報種別：h => 健康予報, w=> 天気予報
 * @param d 日付指定 0=> 今日, 1=> 明日
 */
function selectKind(k,type,d)
{
	if(typeof active.areakind[type] == "undefined")
	{
		active.areakind[type] = new Array();
	}

	if(k=='')
	{
		k=active.areakind[type][area];
	}

	//dの指定がない場合日付を変えない
	if(typeof d == "undefined")
	{
		d = active.day[type];
	}


	//日付きりかえ
	// 地域ラベル部まではID指定
	// 後はspanでもレスポンスはそれほど変わらないと判断
	if(d==0 || d=='')
	{
		$("#MapTitle" + area + "h span.date").text(day1['name']);
		$("#MapTitle" + area + "w span.date").text(day1['name']);
		$("#MapTitle" + area + "w span.daytitle").text('今日の天気');

		$("#MapTitle" + area + "w span.date").removeClass("sat sun");
		$("#MapTitle" + area + "h span.date").removeClass("sat sun");
		if(day1['class']!='')
		{
			$("#MapTitle" + area + "h span.date").addClass(day1['class']);
			$("#MapTitle" + area + "w span.date").addClass(day1['class']);
		}
	}
	else if(d==1)
	{
		$("#MapTitle" + area + "h span.date").text(day2['name']);
		$("#MapTitle" + area + "w span.date").text(day2['name']);
		$("#MapTitle" + area + "w span.daytitle").text('明日の天気');

		$("#MapTitle" + area + "w span.date").removeClass("sat sun");
		$("#MapTitle" + area + "h span.date").removeClass("sat sun");
		if(day2['class']!='')
		{
			$("#MapTitle" + area + "h span.date").addClass(day2['class']);
			$("#MapTitle" + area + "w span.date").addClass(day2['class']);
		}
	}

	//種別が切り替えられるか、日付が切り替えられた場合
	//(一度表示した地域の予報については再読み込みしない)
	if(k!=active.areakind[type][area] || d != active.day[type])
	{
		var a = area;
		for(num in points[a])
		{
			if(type=='w')
			{
				if(k=="004")
				{
					$("#" + icon_id_prefix[type] + num + type + "004tMax").text(data['005'][points[a][num]][d] + "℃");
					$("#" + icon_id_prefix[type] + num + type + "004tMin").text(data['003'][points[a][num]][d] + "℃");
					$("#" + icon_id_prefix[type] + num + type + "215").hide();
					$("#" + icon_id_prefix[type] + num + type + "icon").hide();
					$("#" + icon_id_prefix[type] + num + type + "004").show();
				}
				else if(k=="215")
				{
					$("#" + icon_id_prefix[type] + num + type + "215pp").text(data['215'][points[a][num]][d] + "%");
					$("#" + icon_id_prefix[type] + num + type + "icon").hide();
					$("#" + icon_id_prefix[type] + num + type + "004").hide();
					$("#" + icon_id_prefix[type] + num + type + "215").show();
				}
				else
				{
					$("#" + icon_id_prefix[type] + num + type + "iconimg").attr("src",icon_prefix[type] + data[k][points[a][num]][d] + icon_postfix[type]);
					$("#" + icon_id_prefix[type] + num + type + "icon").show();
					$("#" + icon_id_prefix[type] + num + type + "004").hide();
					$("#" + icon_id_prefix[type] + num + type + "215").hide();
				}
			}
			else
			{
				$("#" + icon_id_prefix[type] + num + type + "img").attr("src",icon_prefix[type] + k + data[k][points[a][num]][d] + icon_postfix[type]);
			}
		}
		//健康予報なら凡例とタイトルも変更
		if(type=='h')
		{
			// 凡例をかこっているdiv部まではID指定
			// 後はimg指定やa指定でもレスポンスはそれほど変わらないと判断
			$("#" + legend_id_prefix + a + "h img").attr("src",legend_prefix + k + legend_postfix);
			$("#" + legend_id_prefix + a + "h a").attr("href",health[k]['link']);
			// タイトルの書き換え
			$("#MapTitle" + a + "htitle").text(health[k]['name']);
		}
	}
	active.areakind[type][area] = k;
	active.kind[type] = k;
	active.day[type] = d;

	$.cookie('activekind' + type,active.kind[type],{ path: '/', expires: 365 });
	$.cookie('activeday',active.day[type],{ path: '/', expires: 365 });
}

// 項目タブの切替関数
function selDay(no,onlyTab) {
// 0:今日 1:明日 2:明後日
// タブ表示を時間帯によって「今夜」に切り替える必要がある

	var id = "DayTabs";
	
	setTab(id, no);
	if(onlyTab != true)
	{
		selectKind('','w',no);
		selectKind('','h',no);
	}
}

function selWitem(no,kind,onlyTab) {

	var id = "wItemTabs";

	setTab(id, no);
	if(onlyTab != true)
	{
		selectKind(kind,'w');
	}
}

function selHitem(no,kind,onlyTab) {

	var id = "hItemTabs";

	setTab(id, no);

	if(onlyTab != true)
	{
		selectKind(kind,'h');
	}
}
