/**
 * WooderdChiarie Javascript Inst (work with jQuery)
 * http://www.wooderdchiarie.com/
 *
 * Copyright (c) 2009 tokyointerstellartravel.org
 * http://www.tokyointerstellartravel.org/
 *
 *	Function junoScheduleCalender(_nodeID)
 *	@use
 *		Create new Instance of "junoScheduleCalender" on HTMLcode 
 *	@api
 *		Void refresh_data(month_adjust) 
 */

function junoScheduleCalender(){ /* constructor */};

junoScheduleCalender.prototype = {
	indicator  : $('#junoCalenderIndicator'),
	tbody      : $('#junoCalenderBody'),
	controller : $('#junoCalenderCtrl'),
	cells      : $('#junoCalenderBody td'),
	
	cur_year  : wooderdchiarie.cur_year,
	cur_month : wooderdchiarie.cur_month,
	cur_day   : wooderdchiarie.cur_day,
	month_end : [31,28,31,30,31,30,31,31,30,31,30,31]
};


/**
 *	Void function refresh_data(_year, _month)
 *	@params
 *		- String month_adjust	// 取得したいデータ月とcur_monthとの月の差
 *	@return
 *		なし。カレンダー更新functionにJsonを渡す。
 */
junoScheduleCalender.prototype.refresh_data = 
junoScheduleCalender.refresh_data = function(month_adjust){
	var _this = this;
	var prev_year;
	var prev_month;
	var next_year;
	var next_month;
	switch(this.cur_month + month_adjust){
		case 0:
			this.cur_year--;
			this.cur_month = 12;
			prev_year  = this.cur_year;
			prev_month = 11;
			next_year  = this.cur_year + 1;
			next_month = 1;
			break;
		case 13:
			this.cur_year++;
			this.cur_month = 1;
			prev_year  = this.cur_year - 1;
			prev_month = 12;
			next_year  = this.cur_year;
			next_month = 2;
			break;
		default:
			this.cur_month += month_adjust;
			prev_year  = this.cur_year;
			prev_month = this.cur_month - 1;
			next_year  = this.cur_year;
			next_month = this.cur_month + 1;
	};
	
	$.ajax({
		url:"/live/" + this.cur_year +"/" + (String(this.cur_month).length == 1 ? "0"+String(this.cur_month) : this.cur_month) +"/monthly.json",
		success:function(data){
		//	$.getJSON(msg,
		//		function(data){
					_this.refresh_calendar($(eval("("+data+")").schedule));
					_this.refresh_indicator(_this.cur_year, _this.cur_month);
		//		}
		//	)
		},
		error:function(){
			alert("指定された月のスケジュールはまだ決まっていません。");
			return;
		}
	});
	$.ajax({
		url:"/live/" + prev_year +"/" + (String(prev_month).length == 1 ? "0"+String(prev_month) : prev_month) +"/monthly.json",
		success:function(data){
			$('.prev', _this.controller).show();
		},
		error:function(){
			$('.prev', _this.controller).hide();
		}
	});
	$.ajax({
		url:"/live/" + next_year +"/" + (String(next_month).length == 1 ? "0"+String(next_month) : next_month) +"/monthly.json",
		success:function(data){
			$('.next', _this.controller).show();
		},
		error:function(){
			$('.next', _this.controller).hide();
		}
	});
};


/**
 *	Void function refresh_calendar(_response)
 *	@params
 *		- Object _response	// Json data
 *	@return
 *		なし。渡されたJsonでカレンダー更新。
 */
junoScheduleCalender.prototype.refresh_calendar = 
junoScheduleCalender.refresh_calendar = function(_response){
	this.clear_calendar();
	var _this = this;
	var new_data        = _response;
	var cur_month_start = (new Date(this.cur_year,this.cur_month-1,1).getDay() - 1 + 7) % 7;
	
	this.cells.each(function(i,cell){
		if(i < cur_month_start || _this.month_end[_this.cur_month-1] < i-cur_month_start + 1){
			$(cell).attr('class', 'blur');
		}else{
			$(cell).attr('_index', i-cur_month_start + 1);
			$('.day',cell).html(i-cur_month_start + 1);
		};
	});
	new_data.each(function(i,liveshow){
		if(liveshow.event_date){
			$('.event', _this.cells[Number(liveshow.event_date) + cur_month_start - 1]).html(
				  '<div class="location">'+ liveshow.event_at +'</div>'
				+ '<div class="title"><a href="'+ liveshow.permalink +'">'+ liveshow.event_title +'</a></div>'
			);
		};
	});
};


/**
 *	Void function refresh_calender(_year, _month)
 *	@params
 *		- String _year		// 更新後のyyyy
 *		- String _month 	// 更新後のmm
 *	@return
 *		なし。indicatorを更新。
 */
junoScheduleCalender.prototype.refresh_indicator = 
junoScheduleCalender.refresh_indicator = function(_year, _month){
	this.indicator.html(_year + '-' + (String(_month).length == 1 ? '0'+String(_month) : _month));
	$('.prev', this.indicator).html(_year + '-' + (String(_month).length == 1 ? '0'+String(_month) : _month));
};


/**
 *	Void function refresh_calendar(_response)
 *	@params
 *		- Object _response	// Json data
 *	@return
 *		なし。渡されたJsonでカレンダー更新。
 */
junoScheduleCalender.prototype.clear_calendar = 
junoScheduleCalender.clear_calendar = function(){
	this.cells.each(function(i,cell){
		$(cell).removeAttr('_index');
		$(cell).removeAttr('class');
		$('.day',cell).html("");
		$('.event',cell).html("");
	});
};
