﻿// jcalendar start
// 2005. 6. 29. by guoo.net
var JCalendar_list = [];
function JCalendar(year, month, width, divObj) {
	if(!explorer && !gecko) return;
	this.index = JCalendar_list.length;
	JCalendar_list[JCalendar_list.length] = this;

	this.year = year;
	this.month = month;
	this.width = width;

	divObj.style.position = 'absolute';
	divObj.style.zIndex = 200;
	divObj.style.visibility = 'hidden';
	divObj.style.backgroundColor = '#fff';
	divObj.style.border = '1px solid #777';
	this.divCalendar = divObj;
	this.divContent = document.createElement('DIV');
	this.cselectYear;
	this.cselectMonth;
	this.args = [];

	this.create();
}
JCalendar.prototype.create = function() {
	var divYear = document.createElement('DIV');
	var divMonth = document.createElement('DIV');
	this.cselectYear = new CustomSelect('cs_jcal_year'+this.index, divYear, 50, 18, 11);
	eval('this.cselectYear.onchange = function() { JCalendar_list['+this.index+'].selectYear(this.value) };');
	this.cselectYear.create();
	for(var i=this.year-10; i<=this.year+10; i++) {
		this.cselectYear.addOption(i, i, (i==this.year));
	}
	this.cselectMonth = new CustomSelect('cs_jcal_month'+this.index, divMonth, 40, 18, 12);
	eval('this.cselectMonth.onchange = function() { JCalendar_list['+this.index+'].selectMonth(this.value) };');
	this.cselectMonth.create();
	for(var i=1; i<=12; i++) {
		this.cselectMonth.addOption(i, i, (i==this.month));
	}

	var tableWrapObj = document.createElement('TABLE');
	tableWrapObj.border = 0;
	tableWrapObj.cellPadding = 0;
	tableWrapObj.cellSpacing = 0;
	tableWrapObj.width = this.width;
	var cellWrapObj = tableWrapObj.insertRow(0).insertCell(0);
	cellWrapObj.align = 'center';

	var tableObj = document.createElement('TABLE');
	tableObj.border = 0;
	tableObj.cellPadding = 1;
	tableObj.cellSpacing = 1;
	var rowObj = tableObj.insertRow(0);
	var cellObj1 = rowObj.insertCell(0);
	cellObj1.align = 'center';
	var cellObj2 = rowObj.insertCell(1);
	cellObj2.align = 'center';
	cellObj1.appendChild(divYear);
	cellObj2.appendChild(divMonth);

	var tableObj2 = document.createElement('TABLE');
	tableObj2.width = '100%';
	tableObj2.border = 0;
	tableObj2.cellPadding = 0;
	tableObj2.cellSpacing = 0;
	tableObj2.style.backgroundColor = '#000077';
	rowObj = tableObj2.insertRow(0);
	cellObj1 = rowObj.insertCell(0);
	cellObj1.height = '18';
	cellObj1.innerHTML = '&nbsp;';
	cellObj2 = rowObj.insertCell(1);
	cellObj2.align = 'center';
	cellObj2.width = '18';
	cellObj2.style.color = '#fff';
	cellObj2.style.fontWeight = 'bold';
	cellObj2.style.cursor = 'pointer';
	cellObj2.innerHTML = 'x';
	eval("cellObj2.onclick = function() { JCalendar_list["+this.index+"].divCalendar.style.visibility = 'hidden'; }; ");

	cellWrapObj.appendChild(tableObj2);
	cellWrapObj.appendChild(tableObj);

	this.divCalendar.appendChild(tableWrapObj);
	this.divCalendar.appendChild(this.divContent);
	this.update();
}
JCalendar.prototype.update = function() {
	this.cselectYear.setValue(this.year);
	this.cselectMonth.setValue(this.month);

	var cellCount = Math.ceil((this.getLastDay() + this.getDay())/7) * 7;
	var cellWidth = Math.floor(this.width/7);
	var cellColor = '';
	var day = 1;

	var html = '';
	html += '<table width="'+this.width+'" border="0" cellpadding="0" cellspacing="1" style="background-color:#c8c8c8;">';
	for(var i=0; i<cellCount; i++) {
		if(i%7==0) cellColor = 'red';
		else if(i%7==6) cellColor = 'blue';
		else cellColor = 'black';
		
		if(i%7==0) html += '<tr>';
		html += '<td width="'+cellWidth+'" height="'+cellWidth+'" align="center" style="font-family:tahoma; font-size:'+Math.round(cellWidth*3/5)+'px; background-color:#fff;">';
		if(i>this.getDay()-1 && i<=this.getDay()+this.getLastDay()-1) {
			html += '<a onclick="JCalendar_list['+this.index+'].setValue('+this.year+', '+this.month+', '+day+', JCalendar_list['+this.index+']);" style="cursor:pointer; cursor:hand;">';
			html += '<font color="'+cellColor+'">'+day+'</font>';
			html += '</a>';
			day++;
		}else {
			html += '&nbsp;';
		}
		html += '</td>';
		if((i+1)%7==0) html += '</tr>';
	}
	html += '</table>';
	this.divContent.innerHTML = html;
}
JCalendar.prototype.setValue = function(year, month, day) {
}
JCalendar.prototype.selectYear = function(year) {
	this.year = year;
	this.update();
}
JCalendar.prototype.selectMonth = function(month) {
	this.month = month;
	this.update();
}
JCalendar.prototype.getDay = function() {
	var date = new Date(this.year, this.month-1, 1);
	return date.getDay();
}
JCalendar.prototype.getLastDay = function() {
	var lastday;
	switch(this.month) {
		case 1:
			lastday = 31;
			break;
		case 2:
			if(this.isLeapMonth(this.year)) lastday = 29;
			else lastday = 28;
			break;
		case 3:
			lastday = 31;
			break;
		case 4:
			lastday = 30;
			break;
		case 5:
			lastday = 31;
			break;
		case 6:
			lastday = 30;
			break;
		case 7:
			lastday = 31;
			break;
		case 8:
			lastday = 31;
			break;
		case 9:
			lastday = 30;
			break;
		case 10:
			lastday = 31;
			break;
		case 11:
			lastday = 30;
			break;
		case 12:
			lastday = 31;
			break;
	}
	return lastday;
}
JCalendar.prototype.isLeapMonth = function(year) {
	return ((year%4==0 && year%100!=0) || (year%400==0));
}

function JCalendar_mouseup() {
	if(event.button==1) { // 왼쪽 클릭
	}else if(event.button==2) { //오른쪽 클릭
	}else if(event.button==4) { //휠 클릭
	}
	return true;
}
var JCalendar_event = new CustomEvent('JCalendar', JCalendar_mouseup, 2);
addMouseupEvent(JCalendar_event);
// jcalendar finish
