<!--
Date.prototype.toY4MDString = function(delim) {
	if (delim == undefined) delim = "";
	var year = this.getFullYear().toString();
	var month = this.getMonth() + 1;
	var day = this.getDate();
	month = (month < 10 ? "0" : "") + month;
	day = (day < 10 ? "0" : "") + day;
	return year + delim + month + delim + day;
};

var MoonGateSequence;
var pivotDate = null;
var weekdays = ['일','월','화','수','목','금','토'];
var mgIdObjArray = new Array(40);


var prototype = {
	incDate : function() {
		pivotDate.setHours(24);
		this.updatePage(pivotDate);
		return false;
	},

	decDate : function() {
		pivotDate.setHours(-24);
		this.updatePage(pivotDate);
		return false;
	},

	updatePage : function() {
		var absdays   = Math.floor((pivotDate - 0) / (24 * 60 * 60 * 1000));
		var gateshift = (40 * absdays) % MoonGateSequence.length;
		var midnight  = new Date(); midnight.setHours(0,0,0,0);
		var current   = new Date();
		var isToday   = (pivotDate - midnight == 0);

		if (isToday) {
			$('ymd').innerHTML = "<font color=blue>" + pivotDate.toY4MDString("-") + "</font>";
		} else {
			$('ymd').innerHTML = "<font color=gray>" + pivotDate.toY4MDString("-") + "</font>";
		}
		$('weekday').innerHTML = weekdays[pivotDate.getDay()];

		var i, j;
		// 자정부터흐른시간을 이용하여 몇번째인지를 알아냄
		// 36분의 에린의 하루 및 이웨카 출몰이 에린기준 자정과의 차이가 현실시간 9분임을 고려
		var active = Math.floor((current - midnight - 540000) / 2160000);
		// 문게이트가 열린상태인지 닫힌상태인지 알아내는 로직
		var erinnTime = (current - midnight - 540000) % 2160000;
		var isMGOpen = (erinnTime > 1080000);
		for (i=0, j=gateshift; i<40; i++, j++) {
			if (j >= MoonGateSequence.length) j = 0;
			if (isToday && (active == i)) {
				if (isMGOpen) {
					mgIdObjArray[i].innerHTML  = '<font color="blue"><b>' + MoonGateSequence[j] + '</b></font>';
					mgIdObjArray[i].innerHTML += ' - <font color="blue"><b>열림</b></font>';
				} else {
					mgIdObjArray[i].innerHTML  = '<font color="black"><b>' + MoonGateSequence[j] + '</b></font>';
					mgIdObjArray[i].innerHTML += ' - <font color="black">대기</font>';
				}
			} else {
				mgIdObjArray[i].innerHTML = MoonGateSequence[j];
			}
		}

		// 재실행 예약 - 1초후 재실행
		setTimeout('prototype.updatePage()', 1000);
	},

	ajax : function(sType) {
		new Ajax.Request('/ajax/mabinogi_hint.php',
		{
			method: 'POST',
			parameters: {'type':sType},
			onSuccess: function(oResult) {
				MoonGateSequence = oResult.responseText.evalJSON();
				prototype.updatePage();
			}
		});
	},

	initDoc : function() {
		for (var i=0; i<mgIdObjArray.length; i++) {
			mgIdObjArray[i] = $('mgId' + i);
		}
		pivotDate = new Date();
		pivotDate.setHours(0,0,0,0); // 금일 자정으로 설정
		this.ajax('MoonGate');
	}
};


window.onload = function() {
	prototype.initDoc();
};
//-->
