/**
 * $RCSfile: calendar.js,v $
 * ----------------------------------------------------------------------
 * Author      : byforce
 * Date        : 2008. 05. 21
 * Copyright   : Copyright (c) 2007-2008 Licome Net. All Rights Reserved.
 * Description :
 * Modify      :
 */

nalbam.Calendar = function() {};
nalbam.Calendar.prototype = {
	initialize: function(parent, object, changeMonth, changeDate)
	{
		this.parentId = parent;
		this.calendarId = parent + "_nbcal";
		this.object = object;
		this.changeMonth = changeMonth;
		this.changeDate = changeDate;

		var nbcal = document.createElement('div');
		nbcal.id = this.calendarId;
		nbcal.className = "nbcal";

		// head
		var nbcal_head = document.createElement('div');
		nbcal_head.className = "nbcal_head";

		var py = "<img src='http://files.nalbam.com/images/calendar/prevyear_blue.gif' class='nbcal_navi' onclick='"+ this.object +".prevYear()' />"
		var pm = "<img src='http://files.nalbam.com/images/calendar/prevmonth_blue.gif' class='nbcal_navi' onclick='"+ this.object +".prevMonth()' />"
		var hd = "<div onclick='"+ this.object +".thisMonth()' id='"+ this.calendarId + "_head" +"' class='nbcal_title hand'></div>"
		var nm = "<img src='http://files.nalbam.com/images/calendar/nextmonth_blue.gif' class='nbcal_navi' onclick='"+ this.object +".nextMonth()' />"
		var ny = "<img src='http://files.nalbam.com/images/calendar/nextyear_blue.gif' class='nbcal_navi' onclick='"+ this.object +".nextYear()' />"

		nbcal_head.innerHTML = py + pm + hd + nm + ny;

		// body
		var nbcal_body = document.createElement('div');
		nbcal_body.className = "nbcal_body";

		/**/
		var a = "Su,Mo,Tu,We,Th,Fr,Sa".split(",");
		for (var i=0; i<a.length; i++) {
			var nbcal_item = document.createElement('div');
			nbcal_item.className = "nbcal_week_item";
			nbcal_item.innerHTML = a[i];
			nbcal_body.appendChild(nbcal_item);
		}
		/**/

		for (var i=1; i<43; i++) {
			var nbcal_item = document.createElement('div');
			nbcal_item.id = this.calendarId + "_item_"+ i;
			if (i%7 == 1) {
				nbcal_item.className = "nbcal_item_sun";
			} else if (i%7 == 0) {
				nbcal_item.className = "nbcal_item_sat";
			} else {
				nbcal_item.className = "nbcal_item";
			}
			nbcal_body.appendChild(nbcal_item);
		}

		nbcal.appendChild(nbcal_head);
		nbcal.appendChild(nbcal_body);

		$(this.parentId).appendChild(nbcal);

		if (this.selectyear) {
			this.paintMonth(this.selectyear, this.selectmonth);
		} else {
			this.today();
		}
	},

	prevYear: function()
	{
		this.paintMonth(this.year-1, this.month);
	},

	nextYear: function()
	{
		this.paintMonth(this.year+1, this.month);
	},

	thisMonth: function()
	{
		this.paintMonth(this.year, this.month);
	},

	prevMonth: function()
	{
		this.paintMonth(this.year, this.month-1);
	},

	nextMonth: function()
	{
		this.paintMonth(this.year, this.month+1);
	},

	today: function()
	{
		var now = new Date();
		this.paintMonth(now.getFullYear(), now.getMonth());
	},

	setDate: function(ymd)
	{
		this.selectyear  = eval(ymd.substr(0,4));
		this.selectmonth = eval(ymd.substr(4,2))-1;
		this.selectday   = eval(ymd.substr(6,2));
	},

	selectDate: function(y,m,d)
	{
		this.selectyear  = y;
		this.selectmonth = m;
		this.selectday   = d;

		var ymd = y+"";
		m = m+1;
		if (m < 10) {
			ymd = ymd +"0";
		}
		ymd = ymd + m;
		if (d < 10) {
			ymd = ymd +"0";
		}
		ymd = ymd + d;

		if (this.changeDate != null)
		{
			eval(this.changeDate +"('"+ ymd +"')");
		}
	},

	paintMonth: function(y, m)
	{
		date = new Date(y, m, 1);

		this.year  = date.getFullYear();
		this.month = date.getMonth();

		$(this.calendarId +"_head").innerHTML = this.year +" "+ (this.month+1);

		var startPos = date.getDay();
		var days = this.lastday(this.year, this.month+1) + startPos;
		var day = 0;
		var btn = "";

		for (var i=0; i<startPos; i++) {
			$(this.calendarId +"_item_"+ (i+1)).update("");
		}
		for (var i=startPos; i<days; i++) {
			day++;
			btn = "<div id='"+ this.calendarId +"_item_d"+ day +"' class='nbcal_item_btn' onclick='"+ this.object +".selectDate("+ this.year +","+ this.month +","+ day +")'>"+ day +"</div>";
			$(this.calendarId +"_item_"+ (i+1)).update(btn);
		}
		for (var i=days; i<42; i++) {
			$(this.calendarId +"_item_"+ (i+1)).update("");
		}

		var ym = this.year+"";
		m = this.month+1;
		if (m < 10) {
			ym = ym +"0";
		}
		ym = ym + m;

		if (this.changeMonth != null)
		{
			eval(this.changeMonth +"('"+ ym +"')");
		}
	},

	getYear: function()
	{
		return this.year;
	},

	getYearMonth: function()
	{
		var y = this.year;
		var m = this.month+1;
		if (m < 10) {
			m = "0"+ m;
		}
		var ym = y +""+ m;

		return ym;
	},

	lastday: function(year, month)
	{
		var days;
		if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) {
			days=31;
		} else if (month==4 || month==6 || month==9 || month==11) {
			days=30;
		} else if (month==2) {
			if (((year % 4)==0) && ((year % 100)!=0) || ((year % 400)==0)) {
				days=29;
			} else {
				days=28;
			}
		}
		return days;
	}
}
