/**
 *
	±â´É ÇÔ¼ö
*/

function TCalendar(objName, panelObj, selMonthFunc, selDayFunc){
	this.objName		= objName;
	this.obj			= panelObj;
	this.monFuncName	= selMonthFunc;
	this.dayFuncName	= selDayFunc;
	this.year			= 0;
	this.month			= 0;
//	this.month			= new TMonth();		// ÇöÀç ¿ùÀÇ °´Ã¼
}

TCalendar.prototype.setMonth = function(yy, mm){
	this.year	= yy;
	this.month	= mm;

	var dtFirstDate = new Date(yy, mm-1, 1);

	this.nFirstDay = dtFirstDate.getDay();									// 1ÀÏÀÇ ¿äÀÏ(ÀÏ[0], ¿ù[1], .. , Åä[6])
	this.nLastDay = iGetDaysInMonth(yy, mm);								// ÃÑ ÀÏ¼ö
	this.nWeekCount = parseInt((this.nLastDay + this.nFirstDay) / 7) +		// ÁÖÀÇ ¼ö
					(((this.nLastDay + this.nFirstDay) % 7) > 0 ? 1 : 0);
}

TCalendar.prototype.prev = function(){
	var year	= this.year;
	var month	= this.month;

	if(month == 1){
		year	= year - 1;
		month	= 12;
	} else {
		month--;
	}
	this.setDate(year, month);
}

TCalendar.prototype.next = function(){
	var year	= this.year;
	var month	= this.month;

	if(month == 12){
		year	= year + 1;
		month	= 1;
	} else {
		month++;
	}
	this.setDate(year, month);
}

TCalendar.prototype.setDate = function(_year, _month){
	var CurrDate = new Date();

	if(_year == null || _year == "" || _year < 1900 || _year > 2100)	_year = CurrDate.getYear();
	else																_year = parseInt(_year);

	if(_month == null || _month == "" || _month < 1 || _month > 12)		_month = CurrDate.getMonth() + 1;
	else																_month = parseInt(_month);

	this.setMonth(_year, _month);

	this.makeRow(this.nWeekCount);
	this.drawDayText();

	eval(this.monFuncName + "(" + this.year + ", " + this.month + ");");
}

TCalendar.prototype.selectedDay = function(r, c){
	var day = this.obj.rows(r).cells(c).outerText;
	if(day != ""){
		eval(this.dayFuncName + "(" + this.year + ", " + this.month + ", " + day + ");");
	} 
}

TCalendar.prototype.makeRow = function(count){
	deleteRow(tblCalendar, -1, true);

	for(var i=0; i<count; i++)
		addRow(this.obj, 7, '#F6F6F6', '#F6F6F6', 'center', -1, 18, null, this.objName+".selectedDay");
}

TCalendar.prototype.drawDayText = function(){
	var nCell = this.nFirstDay;
	var nRow = 1;
	for(var i=1; i<=this.nLastDay; i++){
		if(nCell > DAY_SAT){
			nCell = DAY_SUN;
			nRow++;
		}
//		if(nRow == 1 && i < nCell) continue;

		drawText(this.obj, nRow, nCell, String(i), getColorByDay(nCell), "hand");
		nCell++;
	}
}

TCalendar.prototype.setSpecialDay = function(day) {
	for(var i=1; i<=this.nWeekCount; i++){
		var j = 0;
		for(; j<7; j++){
			if(day ==this.obj.rows(i).cells(j).outerText){
				this.obj.rows(i).cells(j).style.color		= "#F47314";
				this.obj.rows(i).cells(j).style.fontWeight	= "bold";
			}
		}
	}
}

function getColorByDay(nDay){
	return "#000000";

	switch(nDay){
		case DAY_SUN:
			return "#FF0000";
		case DAY_MON:
		case DAY_TUE:
		case DAY_WED:
		case DAY_THU:
		case DAY_FRI:
			return "#000000";
		case DAY_SAT:
			return "#0000FF";
	}
	return "#000000";
}
