// JavaScript Document

// ************************************************************
// 	設定項目
// ************************************************************

// グラフ描画可能期間［strDate 〜 endDate］
	var strDate = "2011/07/01";
	var endDate = "2011/09/30";

	var strday  = strDate.split("/");
	var strYY = strday[0];
	var strMM = strday[1];
	var strDD = strday[2];
	
	var endday  = endDate.split("/");
	var endYY = endday[0];
	var endMM = endday[1];
	var endDD = endday[2];
	
// カレンダー表示期間設定
	var calMinDate = new Date(strYY, strMM - 1, strDD);
	var calMaxDate = new Date(endYY, endMM - 1, endDD);
	
// グラフオプション 共通項目
	var pty1min	= 240;		/* Y軸1:sunrise-min  4:00 */
	var pty1max	= 390;		/* Y軸1:sunrise-max  6:30 */
	var pty2min	= 1020;		/* Y軸2:sunset -min 17:00 */
	var pty2max	= 1170;		/* Y軸2:sunset -max 19:30 */
	var ptyTick	= 6;		/* Y軸数 */

	var gTtl = '富士山頂付近 日出入時刻<span class="ttlperiod">';

// カレンダーアイコンURL
	var calButtonImg = 'css/images/calendar.png';

// ロード時初期表示
	var ingTitle	= gTtl + '2011年7月〜2011年9月</span>';
	var indtmin	= '2011/7/1';
	var indtmax	= '2011-09-30';
	var inxTick	= 10;
	var incaldy = '2011/07/01';


// ************************************************************
// 	function actions
// ************************************************************

$(function(){
	//
	// ロード時初期表示
	$("#graph").html("");
	graphPlot(ingTitle,indtmin,indtmax,inxTick,pty1min,pty1max,pty2min,pty2max,ptyTick,srAlldata,ssAlldata);
	$("#protall").find("span").addClass('ui-icon-signal');
	$("#protdate").val(incaldy);
	$("#protdate").css('color','#fff');
	//
	// ボタンクリック処理
	$("#ctrl .btn").click(function(){
		$("#ctrl .btn").find("span").removeClass('ui-icon-signal');
		$(this).find("span").addClass('ui-icon-signal');
	});
	//
	// 全期間
	$("#protall").click(function(){
		var ptgTitle	= gTtl + '2011年7月〜2011年9月</span>';
		var ptdtmin	= '2011/7/1';
		var ptdtmax	= '2011-09-30';
		var ptxTick	= 10;
		//
		$("#graph").html("");
		graphPlot(ptgTitle,ptdtmin,ptdtmax,ptxTick,pty1min,pty1max,pty2min,pty2max,ptyTick,srAlldata,ssAlldata);
	});
	// 7月
	$("#protm07").click(function(){
		var ptgTitle	= gTtl + '2011年7月</span>';
		var ptdtmin	= '2011/7/1';
		var ptdtmax	= '2011-07-31';
		var ptxTick	= 7;
		//
		$("#graph").html("");
		graphPlot(ptgTitle,ptdtmin,ptdtmax,ptxTick,pty1min,pty1max,pty2min,pty2max,ptyTick,srDataSt10,ssDataSt10);
	});
	// 8月
	$("#protm08").click(function(){
		var ptgTitle	= gTtl + '2011年8月</span>';
		var ptdtmin	= '2011/8/1';
		var ptdtmax	= '2011-08-31';
		var ptxTick	= 7;
		//
		$("#graph").html("");
		graphPlot(ptgTitle,ptdtmin,ptdtmax,ptxTick,pty1min,pty1max,pty2min,pty2max,ptyTick,srDataSt10,ssDataSt10);
	});
	// 9月
	$("#protm09").click(function(){
		var ptgTitle	= gTtl + '2011年9月</span>';
		var ptdtmin	= '2011/9/1';
		var ptdtmax	= '2011-09-30';
		var ptxTick	= 7;
		//
		$("#graph").html("");
		graphPlot(ptgTitle,ptdtmin,ptdtmax,ptxTick,pty1min,pty1max,pty2min,pty2max,ptyTick,srDataSt10,ssDataSt10);
	});
	// 日付指定
	$("#protdate").datepicker({
		showOn: 'button',
		buttonImage: calButtonImg,
		buttonText: '日付を選択',
		buttonImageOnly: true,
		//dateFormat: 'yy/mm/dd',
		minDate: (calMinDate),
		maxDate: (calMaxDate),
		numberOfMonths: 3,
		onClose : function(date) {
			// グラフ描画
			graphProt(date);
			// 日出入時刻
			lookupTime(date);
			// X軸ラベル変更
			strongDate(date);
			//
			$("#protdate").css('color','');
			$("#ctrl .btn").find("span").removeClass('ui-icon-signal');
		}
	});

});



// ************************************************************
// 	jquery-ui-custom
// ************************************************************

$(function(){
	// Tabs
	$('#graphplot').tabs();

	// Datepicker
	$('#datepicker').datepicker({
		inline: true
	});
	
	// Slider
	$('#slider').slider({
		range: true,
		values: [17, 67]
	});
	
	//hover states on the static widgets
	$('#dialog_link, ul#icons li').hover(
		function() { $(this).addClass('ui-state-hover'); }, 
		function() { $(this).removeClass('ui-state-hover'); }
	);
	
});


// ************************************************************
// 	lookupTime :: 日出入時刻計算
// ************************************************************

function lookupTime(date){
	var lookupSrTime = lookupTime2(date,srDataSt10);
	var lookupSsTime = lookupTime2(date,ssDataSt10);
	$("#suntime").html("日出 - "+lookupSrTime+"<br />日入 - "+lookupSsTime);
}
function lookupTime2(date,dataname){
	var arrDate = "";
	var arrTime = "";
	// 該当日の経過分数を調べる
	$.each(dataname, function(){
		arrDate = this[0];
		arrTime = this[1];
		return (arrDate != date);
	});
	// 経過分数を時刻に変換
	var timeMM = arrTime % 60;
	var timeHH = (arrTime - timeMM) / 60;
	timeMM = ("0" + timeMM).slice(-2);
	timeHH = ("0" + timeHH).slice(-2);
	arrTime = timeHH +":"+timeMM;
	return(arrTime);
}


// ************************************************************
// 	graphProt :: グラフ描画期間計算 & グラフ描画処理指示
// ************************************************************

function graphProt(date){
		var ptYY = date.split("/")[0];
		var ptMM = date.split("/")[1];
		var ptDD = date.split("/")[2];
		//
		var ptdtmin = "";
		var ptdtmax	= "";
		var ptxTick	= 7;
		//
		var minMM = 0;
		var minDD = 0;
		var maxMM = 0;
		var maxDD = 0;

		// 描画期間計算
		if(ptMM == strMM && ptDD <= parseInt(strDD, 10)+3){
			// 掲載開始日から4日後以前の場合
			maxDD = parseInt(strDD, 10) + 6;
			ptdtmin = strYY + "/" + strMM + "/" + strDD;
			ptdtmax = strYY + "-" + strMM + "-" + maxDD;
		} else
		if(ptMM == endMM && ptDD >= parseInt(endDD, 10)-3){
			// 掲載終了日から4日前以降の場合
			minDD = parseInt(endDD, 10) - 6;
			ptdtmin = endYY + "/" + endMM + "/" + minDD;
			ptdtmax = endYY + "-" + endMM + "-" + endDD;
		} else {
			// 月末日
			var thisMMend = 31;
			var prevMMend = 31;
			//
			// 描画開始日
			minMM = ptMM;
			minDD = ptDD - 3;
			if(minDD < 1){
				minMM = ptMM - 1;
				minDD = prevMMend + minDD;
			}
			ptdtmin = ptYY + "/" + minMM + "/" + minDD;
			//
			// 描画終了日
			maxMM = ptMM;
			maxDD = parseInt(ptDD, 10) + 3;
			if(maxDD > thisMMend){
				maxMM = parseInt(ptMM, 10) + 1;
				maxDD = maxDD - thisMMend;
			}
			ptdtmax = ptYY + "-" + maxMM + "-" + maxDD;
			//
		}
		//
		// グラフタイトル設定
		var ttlYYmin = ptdtmin.split("/")[0];
		var ttlMMmin = ("0" + ptdtmin.split("/")[1]).slice(-2);
		var ttlDDmin = ("0" + ptdtmin.split("/")[2]).slice(-2);
		var ttlYYmax = ptdtmax.split("-")[0];
		var ttlMMmax = ("0" + ptdtmax.split("-")[1]).slice(-2);
		var ttlDDmax = ("0" + ptdtmax.split("-")[2]).slice(-2);
		var ttlstr = ttlYYmin + "年" + ttlMMmin + "月" +  ttlDDmin + "日";
		ttlstr += " 〜 ";
		ttlstr += ttlYYmax + "年" + ttlMMmax + "月" +  ttlDDmax + "日";
		var ptgTitle = gTtl + ttlstr + '</span>';
		//
		// 描画エリアをクリア後、グラフ描画
		$("#graph").html("");
		graphPlot(ptgTitle,ptdtmin,ptdtmax,ptxTick,pty1min,pty1max,pty2min,pty2max,ptyTick,srDataSt10,ssDataSt10);
}


// ************************************************************
// 	jqPlot :: グラフ描画
// ************************************************************

function graphPlot(gTitle,dtmin,dtmax,xTick,y1min,y1max,y2min,y2max,yTick,data1,data2){
	plot1 = $.jqplot('graph', [data1,data2], {
		title: {
			text: gTitle,
			textAlign: 'left'
		},
		seriesDefaults:{
			shadow: false,
			lineWidth:2.5,
			markerOptions:{style:'filledCircle', lineWidth: 1, size:8, shadow: false}
		},
		series:[
			{label:'日出時刻'},
			{yaxis:'y2axis',label:'日入時刻'}
		],
		axesDefaults:{useSeriesColor: true},
		axes: {
			xaxis:{
				renderer: $.jqplot.DateAxisRenderer,
				min:dtmin,
				max:dtmax,
				numberTicks: xTick,
				tickOptions:{formatString:'%m/%d'}
			},
			yaxis:{
				min: y1min,
				max:y1max,
				numberTicks: yTick,
				tickOptions:{formatString:'%d'}
			},
			y2axis:{
				min: y2min,
				max:y2max,
				numberTicks: yTick,
				tickOptions:{formatString:'%d'}
			}
		},
		legend:{
			show:true,
			location: 'ne',
			yoffset: -20,
			xoffset: 6
		},
		grid:{
			gridLineColor: '#cccccc',
			background: '#fffdf6',
			borderColor: '#999999',
			borderWidth: 1.0,
			shadow: false
		},
		highlighter: {
			fadeTooltip: false,
			tooltipLocation: 'n',
			tooltipSeparator: ','
		}
	});
	
	// tickTranscribe time
	tickTranscribe();
}


// ************************************************************
// 	tickTranscribe :: Y軸ラベル表示変更［経過分数 >> 時刻］ 
// ************************************************************

function tickTranscribe(){
	$(".jqplot-yaxis-tick,.jqplot-y2axis-tick").each(function () {
		var tim = $(this).text();
		// timに:が含まれていれば処理をしない
		if(tim.indexOf(":") < 0){
			var tickmm = tim % 60;
			var tickhh = (tim - tickmm) / 60;
			tickmm = ("0" + tickmm).slice(-2);
			$(this).text(tickhh+":"+tickmm);
		}
	});
}


// ************************************************************
// 	strongDate :: X軸ラベル該当日付スタイル変更
// ************************************************************

function strongDate(date){
	$(".jqplot-xaxis-tick").each(function () {
		var srgday  = date.split("/");
		var srgDate = srgday[1] + "/" + srgday[2];
		var day = $(this).text();
		if(day == srgDate){
			$(this).html('<span id="pickDate">'+day+'</span>');
		}
	});
}


// ************************************************************
// 	インラインフレームの高さを自動調整
// ************************************************************

function iFrameHeight() {
	var h = 0;

	// Opera
	if (window.opera)
	{
		h = document.getElementById('suntimefooter').contentDocument.getElementById('inHeight').offsetHeight + 20;
		document.getElementById('suntimefooter').setAttribute("height",h);
	}

	// Safari ~ Chrome
	else if (/WebKit/i.test(navigator.userAgent))
	{
		var posVersion = navigator.userAgent.indexOf("WebKit/");
		var version = navigator.userAgent.substring(posVersion + 7, posVersion + 10);
		if (parseInt(version) >= 523) // Safari 3+
		{
			document.getElementById('suntimefooter').style.height = '0px';
		}
		else // Safari 1+ or 2+
		{
			return document.getElementById('suntimefooter').style.height = '152px';
		}
		h = document.getElementById('suntimefooter').contentDocument.height;
		document.getElementById('suntimefooter').style.height = h + 'px';
	}

	// IE
	else if( document.all )
	{
		h = document.frames('suntimefooter').document.body.scrollHeight;
		document.all.suntimefooter.style.height = h + 20 + 'px';
	}

	// Misc
	else
	{
		h = document.getElementById('suntimefooter').contentDocument.height;
		document.getElementById('suntimefooter').style.height = h + 20 + 'px';
	}
	
	//
	$('iframe').contents().find('#suntimesfooter a').attr("target","_parent");
}



