/**
 * JavaScript for NMH
 * 
 * @author	A.Ohnishi @ ver2
 * @require	jquery
 */
function nmh() {}

nmh.id = function(id)
{
	return document.getElementById(id);
}

/* 天気のSELECTボックスが選ばれた */
nmh.selectedWeather = function(obj,lang)
{
	if (lang==undefined) {
		lang = '';
	}
	$.ajax({
		type: 'POST',
		url: '/ajax/weather.php',
		data: 'area='+obj.value+'&lang='+lang+'&'+new Date().getTime(),
		success: function(response) {
			$('#box-weather').html(response);
		}
	});
	document.cookie='nmhweather='+obj.value+'; expires='+new Date(2099, 1).toUTCString();
}

/* 今日の時刻を選択 */
nmh.getStarTimes = function()
{
	var hour = new Date().getHours();
	var length = $('#time-s').length;
	for (var i = 0; i < hour; i++) {
		this.id('time-s').options[length+i-1] = new Option(((i<10)?'0':'')+i+':00', i);
	}
	this.id('time-s').selectedIndex = this.id('time-s').length - 1;
}

/* 今日の星空の検索チェック */
nmh.checkCurrentSearch = function()
{
	if (!this.id('area02-s').value) {
		alert('地域を選んでください');
		return false;
	}
	if (!this.id('time-s').value) {
		alert('時間を選んでください');
		return false;
	}
}

/* 星空検索チェック */
nmh.checkSearch = function()
{
	if (!this.id('s-area').value) {
		alert('地域を選んでください');
		return false;
	}
}


/* 今日までの年のSELECTを取得 */
nmh.getSelectYear = function()
{
	var year = this._getYear();
	for (var i = 2009; i <= year; i++) {
		this.id('s-year').options[this.id('s-year').length] = new Option(i, i);
	}
	this.id('s-year').selectedIndex = this.id('s-year').length - 1;
}

/* 指定した年に対する月のSELECTを取得 */
nmh.getSelectMonth = function()
{
	// 年の取得
	var year = this._getYear();
	var month = 12;
	
	// 今年だったら今月まで
	if (this.id('s-year').value==year) {
		month = this._getMonth();
	}

	this._resetSelect('s-month');
	for (var i = 1; i <= month; i++) {
		this.id('s-month').options[this.id('s-month').length] = new Option(i, i);
	}
	this.id('s-month').selectedIndex = this.id('s-month').length - 1;
}

/* 指定した月に対する日のSELECTを取得 */
nmh.getSelectDate = function()
{
	// 年と月の取得
	var year = this._getYear();
	var month = this._getMonth();
	var date = 31;
	
	// 年月のチェック
	if (this.id('s-year').value==year && this.id('s-month').value==month) {
		date = this._getDate();
	} else {
		date = this._getMonthDate(this.id('s-year').value, this.id('s-month').value);
	}
	
	this._resetSelect('s-date');
	for (var i = 1; i <= date; i++) {
		this.id('s-date').options[this.id('s-date').length] = new Option(i, i);
	}
	this.id('s-date').selectedIndex = this.id('s-date').length - 1;
}

/* 指定した月日の時のSELECTを取得 */
nmh.getSelectHour = function()
{
	// 現在日付の取得
	var year = this._getYear();
	var month = this._getMonth();
	var date = this._getDate();
	var hour = 23;
	// 今日だったら１時間前まで
	if (this.id('s-year').value==year && this.id('s-month').value==month && this.id('s-date').value==date) {
		hour = this._getHour() - 1;
	} else {
		hour = 23;
	}
	
	this._resetSelect('s-time');
	for (var i = 1; i <= hour; i++) {
		this.id('s-time').options[this.id('s-time').length] = new Option((i<10?'0':'')+i+':00', i);
	}
	this.id('s-time').selectedIndex = this.id('s-time').length - 1;
}

/* 年の取得 */
nmh._getYear = function() {
	var year = new Date().getYear();
	if (year < 1900) {
		year += 1900;
	}
	return year;
}

/* 月の取得 */
nmh._getMonth = function() {
	var month = new Date().getMonth() + 1;
	return month;
}

/* 月の日数の取得 */
nmh._getMonthDate = function(year, month) {
    return new Date(year,month,0).getDate();
}

/* 日の取得 */
nmh._getDate = function() {
	return new Date().getDate();
}

/* 時の取得 */
nmh._getHour = function() {
	return new Date().getHours();
}

/* SELECTをリセット */
nmh._resetSelect = function(id) {
	var length = this.id(id).length;
	for (var i = 0; i < length; i++) {
		this.id(id).options[i] = null;
	}
}

/* トップの星占い */
nmh.changeScope = function(obj) {
	var stars = obj.value.split(',');
	for (var i = 1; i <= 12; i++) {
		this.id('topscope_'+i+'_1').style.display='none';
		this.id('topscope_'+i+'_3').style.display='none';
	}
	this.id('topscope_'+stars[0]+'_1').style.display='';
	this.id('topscope_2').innerHTML=stars[1]+'位';
	this.id('topscope_'+stars[0]+'_3').style.display='';
	document.cookie='nmhscope='+stars[0]+'; expires='+new Date(2099, 1).toUTCString();
}
