/**
 *	달력에 대한 정보를 제공한다.
 *	한달의 시작과 끝날을 구할 수 있고, 시작일의 요일을 구할 수 있다.
 *	요일은 숫자로 표현되며,, 0~6 (일,월,화,...,토)를 나타낸다.
 *
 *	내부변수중 this.sentDate는 3차원 배열로 1차원은 연도, 2차원은 월 3차원은 일을 나타내며
 *	메일을 발송한 날이면 true, 아니면 false로 나타낸다. 특히 1차원인 연도는 2003년을 0으로
 *	설정한다.
 *
 *	내부변수중 this.sendingDate는 발송예약된 날으로 설정하며 3차원 배열로 this.sentDate와 
 *	설정내용이 같다.
 *
 *	내부변수중 this.weekday는 특정요일을 지정한다. 함수 dayExpression에서 특정한 요일의 날짜를
 *	특정하게 표현할 때 사용한다.
 *
 *	내부변수중 this.callback은 달력의 날짜를 클릭했을때 링크되는 페이지 혹은 함수를 설정한다.
 *
 *	@author	human SNS
 *	@version	1.0
 */

function Calendar(year, month)
{
	this.year = year;
	this.month = month;
	this.sentDate = null; 
	this.sentDateTitle = null;
	this.sendingDate = null;
	this.weekday = null;
	
	this.callbackSent    = null;
	//this.callbackSentTitle    = null;
	//this.callbackSentTitleover = null;
	//this.callbackSentTitleout = null;
	this.callbackSending = null;
	this.callbackWeekday = null;

	this.selectedDay = null;

	var todayTemp = new Date();
	
	//alert (new Date());
	
	this.today = new Date(todayTemp.getYear(), todayTemp.getMonth(), todayTemp.getDate());
	//this.today = todayTemp;

	this.base  = new Date(2003, 5, 1);   // 2003-06-01  (06 -> 5)
}

Calendar.prototype.setSentDate = function(arrSentDate)
{
	this.sentDate = arrSentDate;
}

Calendar.prototype.setSentDateTitle = function(arrSentDateTitle)
{
	this.sentDateTitle = arrSentDateTitle;
}

Calendar.prototype.setSendingDate = function(arrSendingDate)
{
	this.sendingDate = arrSendingDate;
}

Calendar.prototype.setWeekday = function(weekday)
{
	this.weekday = weekday;
}

Calendar.prototype.setCallbackSent = function(callback)
{
	this.callbackSent = callback;
}
/*
Calendar.prototype.setCallbackSentTitleover = function(callback)
{
	this.callbackSentTitleover = callback;
}

Calendar.prototype.setCallbackSentTitleout = function(callback)
{
	this.callbackSentTitleout = callback;
}
*/

Calendar.prototype.setCallbackSending = function(callback)
{
	this.callbackSending = callback;
}

Calendar.prototype.setCallbackWeekday = function(callback)
{
	this.callbackWeekday = callback;
}

Calendar.prototype.setSelectedDay = function(selectedDay)
{
	this.selectedDay = selectedDay;
}


Calendar.prototype.maximumDay = function()
{
	switch(this.month)
	{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12: return 31;
	}

	if ( this.month == 2 )
	{
		if ( (((this.year % 4) == 0) && ((this.year % 100) != 0)) || ((this.year % 400) == 0) ) return 29;
		return 28;
	}
	
	return 30;
}

Calendar.prototype.minimumDay = function()
{
	return 1;
}

Calendar.prototype.startWeekDay = function()
{
	var firstDay = new Date(this.year, this.month - 1, 1);
	var diff = firstDay - this.base;
	
	diff = (((((diff / 1000) / 60) / 60) / 24) % 7);
	if ( diff < 0 ) diff += 7;
	
	return diff;
}

Calendar.prototype.movePrevMonth = function()
{
	this.month--;
	
	if ( this.month <= 0 )
	{
		this.month = 12;
		this.year--;
	}
}

Calendar.prototype.moveNextMonth = function()
{
	this.month++;
	
	if ( this.month > 12 )
	{
		this.month = 1;
		this.year++;
	}
}

Calendar.prototype.movePrevYear = function()
{
	this.year--;
	
}

Calendar.prototype.moveNextYear = function()
{
	this.year++;
	
}

Calendar.prototype.dayExpression = function(year, month, day)
{
	var	htmlCode = "";
	var checkDay = new Date(year, month - 1, day);
	
	htmlCode += day;
	
	if ( this.sentDate != null && 
	     this.sentDate[year-2003] != null && 
	     this.sentDate[year-2003][month-1] != null )
	{
		var strMonth = (month < 10) ? "0" + month : month;
		var strDay = (day < 10) ? "0" + day : day;
		
	    if ( this.sentDate[year-2003][month-1].indexOf(strDay + ":") != -1 )
		{
			var returnVar = "" + year + strMonth + strDay;
			var ArrayYM = this.sentDate[year-2003][month-1].split(":");
			var ArrayTitle = this.sentDateTitle[year-2003][month-1].split( ":");
			var returnTitle = "";
			if(ArrayYM.length > 0) 
				for( i =0 ; i < ArrayYM.length ; i++)
					if(ArrayYM[i] == strDay) returnTitle = ArrayTitle[i];
			
			htmlCode = "<font color='#2929d6'><b>" + day + "</b></font>";
			if ( this.callbackSent != null ){ 
				//htmlCode = "<a href=\"" + this.callbackSent + "('" + returnVar + "')\" title=\"" + returnVar + "\" onmouseover=\"" + this.callbackSentTitleover + "('" + returnVar + "')\" onmouseout=\"" + this.callbackSentTitleout + "('" + returnVar + "')\">" + htmlCode + "</a>";
				htmlCode = "<a href=\"" + this.callbackSent + "('" + returnVar + "')\" title=\"" + returnTitle + "\" >" + htmlCode + "</a>";
		    }
			return htmlCode;
		}
	}
	
	if ( this.sendingDate != null && 
	     this.sendingDate[year-2003] != null && 
	     this.sendingDate[year-2003][month-1] != null )
	{
		var strMonth = (month < 10) ? "0" + month : month;
		var strDay = (day < 10) ? "0" + day : day;
		
	     if ( this.sendingDate[year-2003][month-1].indexOf(strDay + ":") != -1 )
		{
			var returnVar = "" + year + strMonth + strDay;

			htmlCode = "<font color='blue'>" + day + "</font>";
			if ( this.callbackSending != null ) htmlCode = "<a href=\"" + this.callbackSending + "('" + returnVar + "')\">" + htmlCode + "</a>";		

			return htmlCode;
		}	
	}
	
	if ( this.weekday != null && this.weekday == checkDay.getDay() )
	{
		var todayTemp = new Date(year, month - 1, day);
		
		if ( (this.today - todayTemp) > 0 ) htmlCode = "<font color='gray'>" + day + "</font>";
		else
		{
			var strMonth = (month < 10) ? "0" + month : month;
			var strDay = (day < 10) ? "0" + day : day;
			var returnVar = "" + year + strMonth + strDay;
			
			if ( this.selectedDay != null && this.selectedDay == returnVar ) htmlCode = "<font color='red'>";
			else
				htmlCode = "<font color='#2929d6'>"
	
			htmlCode += day + "</font>";
			if ( this.callbackWeekday != null ) htmlCode = "<a href=\"" + this.callbackWeekday + "('" + returnVar + "')\">" + htmlCode + "</a>";
		}
		
		return htmlCode;
	}
	
	return  day ;
}
