var aryEventR = {};
function makeCal(f, Y, M)
{
	var aryEvent ={};
	var fwdY,fwdM, prvY,prvM;

	if(aryEventR[Y]==undefined){
		$.getJSON('/script/php/loadevent.php', {sY:Y}, function(json){
			aryEventR[Y] = {};
			$.each(json.items, function(i,item){
				var aryDate = item.date.split("/");
				var strKey  = "v"+(aryDate.join(""));
				if(strKey!="v"){
					var tmpKM   = parseInt(aryDate[1],10) - 1;
					if(aryEventR[Y][tmpKM]==undefined) aryEventR[Y][tmpKM]= {};
					var tmpObj  = new Object();
					tmpObj.eventId     = item.eventId;
					tmpObj.title       = item.title;
					tmpObj.href        = item.link;
					tmpObj.datetext    = item.datetext;
					tmpObj.description = item.description;
					tmpObj.area        = item.area;
					tmpObj.areanum     = item.areanum;
	
					aryEventR[Y][tmpKM][strKey] = tmpObj;
				}
			});
			aryEvent = aryEventR[Y][M];
			$(f).html(makeCalBody(Y,M,aryEvent));
		});
	}else{
		aryEvent = aryEventR[Y][M];
		$(f).html(makeCalBody(Y,M,aryEvent));
	}
}

function makeCalBody(Y,M,aryEvent)
{
	var retVal = "";
	var PrCal = new ConstPrCal(Y,M);

	var tdM         = (PrCal.dM<9) ? ' '+(PrCal.dM+1): PrCal.dM+1;
	var strdM       = (PrCal.dM<9) ? String("0"+(PrCal.dM+1)): String(PrCal.dM+1);
	var strCalTitle = PrCal.dY+"年"+tdM+'月';
	var strAryKeyYM = "v"+(PrCal.dY+strdM);
	var idCal, clsCal, cssPiece, schPiece, isD, tmpD;

	// put calender
	retVal += '<div id="side-calnavi">';
	retVal += '<a href="javascript:makeCal('+"'#frmcal',"+PrCal.prvY+', '+PrCal.prvM+');">&lt;</a>';
	retVal += '　　<a href="/event/index.php?sY='+(PrCal.dY)+'&amp;sM='+(PrCal.dM+1)+'">'+strCalTitle+'</a>　　';
	retVal += '<a href="javascript:makeCal('+"'#frmcal',"+PrCal.fwdY+', '+PrCal.fwdM+');">&gt;</a>';
	retVal += '</div>'+"\n";

	retVal += '<table class="m-b12p">'+"\n";

	retVal += '<tr>'+"\n";
	for(var i=0; i<7; i++){
		clsCal = "";
		if(isHoliday)  clsCal =' class="holiday"';
		else if(i==0) clsCal = ' class="red"';
		else if(i==6) clsCal = ' class="blue"';
		retVal+='<th'+clsCal+' scope="col">'+PrCal.aryW[i]+'</th>'+"\n";
	}
	retVal += '</tr>'+"\n";

	for(var i=0;i<PrCal.cCal; i++){
		retVal += '<tr>'+"\n";
		for(var k=0; k<7; k++){
			var strdD     = (PrCal.cAry[k+i*7]!='　' && PrCal.cAry[k+i*7]<10) ? "0"+PrCal.cAry[k+i*7]: PrCal.cAry[k+i*7];
			var strAryKey = strAryKeyYM+strdD;
			var isHoliday = false;
			if(aryHoliday[strAryKey]!=undefined)    isHoliday = true;
			if(PrCal.cAry[k+i*7]!='&nbsp;' && k==0) isHoliday = true;

			isD      = (PrCal.cAry[k+i*7]!='　') ? true: false;
			cssPiece = new Array();
			if(isD) cssPiece.push('sel');
			if(PrCal.dY==PrCal.nY && PrCal.dM==PrCal.nM && PrCal.dD==PrCal.cAry[k+i*7]) cssPiece.push('today');
			if(isHoliday) cssPiece.push('holiday');
			if((i+1)==PrCal.cCal) cssPiece.push('pb');
			clsCal  = (cssPiece.length>0) ? ' class="'+(cssPiece.join(" "))+'"': '';
			tmpDate = (PrCal.cAry[k+i*7]!='　') ? PrCal.cAry[k+i*7]: '&nbsp;';
			if(tmpDate!='&nbsp;'){
				tmpD = (tmpDate<10) ? "0"+tmpDate: tmpDate;
				if(aryEvent!=undefined && aryEvent[strAryKeyYM+tmpD]){
					if(aryEvent[strAryKeyYM+tmpD][0]) tmpDate = '<a href="'+(aryEvent[strAryKeyYM+tmpD][0].href)+'">'+tmpDate+'</a>';
					else tmpDate = '<a href="'+(aryEvent[strAryKeyYM+tmpD].href)+'">'+tmpDate+'</a>';
				}
			}
			retVal += '<td'+clsCal+'>'+tmpDate+'</td>'+"\n";
		}
		retVal += '</tr>'+"\n";
	}
	retVal += '</table>'+"\n";

	return retVal;
}

function ConstPrCal(Y, M)
{
	this.aryW  = new Array("日","月","火","水","木","金","土");
	this.aryLD = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var nCal   = new Date();
	this.nY    = nCal.getFullYear();
	this.nM    = nCal.getMonth();
	this.nD    = nCal.getDate();

	var dCal   = new Date();
	if(this.aryLD[M]<this.nD){
		dCal.setDate(this.aryLD[M]);
	}
	if(Y!=undefined) dCal.setFullYear(Y);
	if(M!=undefined) dCal.setMonth(M);

	this.dY    = dCal.getFullYear();
	if((this.dY%4==0 && this.dY%100!=0) || this.dY%400==0) this.aryLD[1]= 29;
	this.dM    = dCal.getMonth();

	this.dD    = (this.aryLD[this.dM]<this.nD) ? this.aryLD[this.dM]: dCal.getDate();
	this.cD    = dCal.setDate(1);
	var cW     = dCal.getDay();
	this.cCal  = Math.ceil((cW+this.aryLD[this.dM])/7);

	this.cAry = new Array((this.cCal)*7);
	for(var i=0; i<this.cAry.length; i++) this.cAry[i] = '　';
	for(var i=0; i<this.aryLD[this.dM]; i++) this.cAry[i+cW] = i+1;

	this.prvM = (this.dM-1<0)   ? 11: this.dM-1;
	this.fwdM = (this.dM+1>=12) ?  0: this.dM+1;
	this.prvY = (this.prvM==11) ? this.dY-1: this.dY;
	this.fwdY = (this.fwdM==0)  ? this.dY+1: this.dY;
}

