/*******2009.07.22 ¿ìÁØÈ£******
**ºÒ·¯¿Ã ÆäÀÌÁö¿¡¼­
**<link rel="stylesheet" type="text/css" href="js/cal_css/calendar.css">
**<script src="/js/calendar.js"></script>
**	cal = new calendar("#diary #bg");
**	cal.display();
**$(function(){
**	$("#preMonth").click(function(){
**		cal.movePreMonth();
**	});
**	$("#nextMonth").click(function(){
**		cal.moveNextMonth();
**	});
**});
**/
week_array = new Array("ÀÏ","¿ù","È­","¼ö","¸ñ","±Ý","Åä");
function calendar(parent){
	this.defineDate =defineDate;
	nowDateObj = new Date();
	this.nowYear = nowDateObj.getFullYear();
	this.nowMonth= nowDateObj.getMonth();
	this.nowDate = nowDateObj.getDate();
	this.defineDate(this.nowYear,this.nowMonth,this.nowDate);
	this.calHtml = "";
	this.header = "";
	this.body= "";
	$(parent).prepend("<div id='calendarObj'></div>");

	this.basic = basicMake;
	this.thead = makeThead;
	this.last = lastMake;
	this.display = displayCal;
	this.tbody = makeTbody;
	this.moveMonth =moveMonth;
	this.movePreMonth =movePreMonth;
	this.moveNextMonth=moveNextMonth;
	this.displayYearMonth= displayYearMonth;


	this.moveMonth();
	this.basic();
	this.thead();
	$("#calendarObj").append(this.header);
	$("#calendarObj").append(this.calHtml);
	$("#calendarObj #calendar").append("<tbody></tbody>")

}
function defineDate(year,month,date){
	dateObj = new Date(year,month,date);
	this.year		= dateObj.getFullYear(); //ÇöÀç ³âµµ
	this.nowYear = this.year;
	this.nowMonth = dateObj.getMonth();
	this.nowDate = dateObj.getDate();
	this.month   = dateObj.getMonth()+1; //ÇöÀç ´Þ
	dateObj.setDate("1")
	this.start_day   = dateObj.getDay(); //ÇöÀç´Þ 1ÀÏÀÇ ¿äÀÏ

	dateObj.setMonth(this.month);
	dateObj.setDate("0");
	this.last_date = dateObj.getDate(); //ÇöÀç´Þ ¸¶Áö¸· ³¯Â¥
	this.weekPer = "0"; //ÀÏÁÖÀÏÀÇ ´ÜÀ§ 0ºÎÅÍ ½ÃÀÛ.
	this.week = "6"; //ÀÏÁÖÀÏÀÇ ´ÜÀ§ 0ºÎÅÍ ½ÃÀÛ.
	this.displayDate = "1";//³¯Â¥
}
function moveMonth(){
	this.header += "<div id='moveMonth'>";
	this.header += "	<div id='preMonth' class='floatLeft'>ÀÌÀü´Þ</div>";
	this.header += "	<div id='cal_title' class='floatLeft'><p id='yearText'>"+this.year+"</p><p id='monthText'>"+this.month+"</p></div>";
	this.header += "	<div id='nextMonth' class='floatLeft'>´ÙÀ½´Þ</div>";
	this.header += "</div>";
}

function basicMake(){
	this.calHtml += "<table  id=calendar class='clear'> ";
}
function lastMake(){
	this.calHtml += "</table>";
}

function makeThead(){
	this.calHtml +="<thead>";
	this.calHtml +="<tr>"
	for(i=0;i<=6;i++){
		this.calHtml +="<th id='day_"+i+"'>"+week_array[i]+"</th>"
	}
	this.calHtml +="</tr>"
	this.calHtml +="</thead>";
	return this.callHtml
}

function makeTbody(){
	this.body ="";
	this.body +="<tr>";
	for(i= this.displayDate;i<=this.last_date;i++){
		if(this.weekPer < this.start_day){
			this.body +="<td>&nbsp;</td>";
			i=0;
		}else{
			//Åä¿äÀÏ Å¬·¡½ºÁöÁ¤
			if((this.weekPer+1)%7==0)css = "class='saturday'";
			//ÀÏ¿äÀÏ Å¬·¡½ºÁöÁ¤
			else if((this.weekPer+1)%7==1)css = "class='sunday'";
			else css="";
			this.body +="<td "+css+">"+i+"</td>";		

			/*¸¶Áö¸· ³¯Â¥ÈÄ ³²Àº °ø°£ Ã¤¿ì±â*/
			if(i == this.last_date){
				space_cell = (this.weekPer+1)%7;
				if(space_cell==0)break;
				for(j=space_cell;j<7;j++){
					this.body +="<td>&nbsp;</td>";
				}
			}
			/*¸¶Áö¸· ³¯Â¥ÈÄ ³²Àº °ø°£ Ã¤¿ì±â ³¡*/
		}
		if((this.weekPer+1) % 7 ==0 ){
			this.body +="</tr><tr>";
		};
		this.weekPer++;
	}
	this.body +="</tr>";
}
function displayCal(){
	this.tbody();
	this.last();
	$("#calendarObj tbody").html(this.body);

};
/*ÀÌÀü´Þ ¹öÆ°*/
function movePreMonth(){
	this.defineDate(this.nowYear,(this.nowMonth-1),this.nowDate);
	this.display();
	this.displayYearMonth();
}

/*´ÙÀ½ ¹öÆ°*/
function moveNextMonth(){
	this.defineDate(this.nowYear,(this.nowMonth+1),this.nowDate);
	this.display();
	this.displayYearMonth();
}

//*º¸¿©Áú ´ÞÀ» ³ëÃâÇÑ´Ù*/
function displayYearMonth(){
	$("#calendarObj #cal_title").html("<p id='yearText'>"+this.year+"</p><p id='monthText'>"+this.month+"</p>");
}


