var Calendar = {
	comboboxYearIndex : 0, 
	comboboxMonthIndex : 0, 
	year : 0, 
	month : 0, 
	day : 0, 
	selectedDate : '', 
	calendar : null, 
	content : null, 
	callback : null, 
	
	init : function(comboboxYearIndex, comboboxMonthIndex, year, month, day) {
		this.comboboxYearIndex = parseInt(comboboxYearIndex);
		this.comboboxMonthIndex = parseInt(comboboxMonthIndex);
		this.year = parseInt(year);
		this.month = parseInt(month);
		this.day = parseInt(day);
		this.selectedDate = year + '-' + ((month<10)?'0'+month:month) + '-' + ((day<10)?'0'+day:day);
		this.calendar = $('gc_calendar');
		this.content = $('gc_calendar_date_wrap');
	}, 
	
	open : function(event) {
		this.calendar.style.left = Event.pointerX(event) + 'px';
		this.calendar.style.top = Event.pointerY(event) + 'px';
	}, 

	close : function() {
		this.calendar.style.left = '-1000px';
		this.calendar.style.top = '-1000px';
	}, 

	show : function(event, inputObj) {
		this.callback = function() { inputObj.value = this.selectedDate; };
		var dateArray = inputObj.value.split('-');
		if(dateArray.length>=2) {
			this.selectYear(dateArray[0]);
			this.selectMonth(dateArray[1]);
		}
		this.open(event);
	},

	selectYear : function(year) {
		var combobox = ComboboxManager.get(this.comboboxYearIndex);
		if(!combobox) return;

		year = parseInt(year);
		if(this.year==year) return;
		this.year = year;

		//reset select
		for(var i=0; i<combobox.select.options.length; i++) combobox.select.remove(i);
		combobox.select.options.length = 0;
		for(var i=this.year-5; i<=this.year+5; i++) {
			var option = document.createElement('option');
			option.text = i;
			option.value = i;
			option.selected = (i==year);
			combobox.select.add(option);
		}	
		//combobox갱신
		combobox.relocation();

		this.relocation();
	}, 

	selectMonth : function(month) {
		var combobox = ComboboxManager.get(this.comboboxMonthIndex);
		if(!combobox) return;

		if(month.charAt(0)=='0') month = month.substring(1);
		month = parseInt(month);

		if(this.month==month) return;
		this.month = month;

		combobox.select.value = this.month;
		combobox.sync();

		this.relocation();
	}, 

	selectDate : function(year, month, day) {
		this.year = parseInt(year);
		this.month = parseInt(month);
		this.day = parseInt(day);
		this.selectedDate = year + '-' + ((month<10)?'0'+month:month) + '-' + ((day<10)?'0'+day:day);
		this.close();

		if(this.callback) this.callback();
	}, 

	relocation : function() {
		var date = new Date();
		var currentYear = date.getFullYear();
		var currentMonth = date.getMonth() + 1;
		var currentDay = date.getDate();

		var totalDay = this.getLastDay();
		var dontcare = new Date(this.year, this.month-1, 1);
		var topCellCount = dontcare.getDay();
		var totalCellCount = Math.ceil((topCellCount + totalDay) / 7) * 7;

		var html = '';
		html += '<table id="gc_calendar_date" style="clear:both;">';
		html += '  <tr>';
		html += '    <th class="sun" scope="row">';
		html += '    <span>S</span>';
		html += '    </th>';
		html += '    <th class="mon" scope="row">';
		html += '    <span>M</span>';
		html += '    </th>';
		html += '    <th class="tue" scope="row">';
		html += '    <span>T</span>';
		html += '    </th>';
		html += '    <th class="wed" scope="row">';
		html += '    <span>W</span>';
		html += '    </th>';
		html += '    <th class="thu" scope="row">';
		html += '    <span>T</span>';
		html += '    </th>';
		html += '    <th class="fri" scope="row">';
		html += '    <span>F</span>';
		html += '    </th>';
		html += '    <th class="sat" scope="row">';
		html += '    <span>S</span>';
		html += '    </th>';
		html += '  </tr>';

		var day = 0;
		var cell = '';
		var currentClass = '';
		var onclickScript;
		for(var i=1; i<=totalCellCount; i++) {
			cell = '';
			currentClass = '';

			if(topCellCount<i && totalDay>day) {
				day++;
				cell = day;
			}
			if(currentYear==this.year && currentMonth==this.month && currentDay==day) currentClass = " current";
			if(cell) {
				cell = '<a href="javascript:Calendar.selectDate(' + this.year + ', ' + this.month + ', ' + day + ');">' + cell + '</a>';
			}//if

			switch(i%7) {
				case 1:
					html += '<tr><td class="sun' + currentClass + '">' + cell + '</td>';
					break;
				case 2:
					html += '<td class="mon' + currentClass + '">' + cell + '</td>';
					break;
				case 3:
					html += '<td class="tue' + currentClass + '">' + cell + '</td>';
					break;
				case 4:
					html += '<td class="wed' + currentClass + '">' + cell + '</td>';
					break;
				case 5:
					html += '<td class="thu' + currentClass + '">' + cell + '</td>';
					break;
				case 6:
					html += '<td class="fri' + currentClass + '">' + cell + '</td>';
					break;
				case 0:
					html += '<td class="sat' + currentClass + '">' + cell + '</td></tr>';
					break;
			}
		}//for
		html += '</table>';

		this.content.innerHTML = html;
	}, 

	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;
	}, 
	
	isLeapMonth : function(year) {
		return ((year%4==0 && year%100!=0) || (year%400==0));
	}, 
	
	mouseup : function(event) {
		this.close();
		return true;
	}
}

GlobalEvent.addEvent('mouseup', 'calendar', Calendar.mouseup.bindAsEventListener(Calendar));
