/*
 * 달력을 자동으로 만들어주는 Javascript 
 * 기본적으로 테이블모양이며, selectBox 로 달 선택하기와 현재 년, 월을 보여주는 것으로 되어있다.
 * 
 */

var _insertTableID, _toDayClassName, _fnCallBack; 

/*
 * yearSelectBoxID : 년도 SelectBox ID
 * monthSelectBoxID : 월 SelectBox ID
 * showYearMonthID : 년도와 월 써주는 곳 ID
 * iYear : 선택 년도. null 이면 현재년도
 * iMonth : 선택 월. null 이면 현재 달
 * calendarTableID : 달력이 표시될 table ID
 * toDayClassName : 오늘 날짜의 class 이름
 * arrayDays : 요일별로 TD 안에 들어가는 HTML
 * fnCallBack : 다 그려주고 실행하는 함수
 * 
 * 기본적으로 
 *  calChange : 달력 새로 그려주기 버튼 ID
 *	previousMonth : 전달로 가기 ID
 *	nextMonth : 다음달로 가기 ID
 *	는 ID가 정해져 있다.
 */
function createCalendar(yearSelectBoxID, monthSelectBoxID, showYearMonthID, iYear, iMonth, calendarTableID, toDayClassName, arrayDays, fnCallBack) {
	_fillCalendarSelect(yearSelectBoxID,monthSelectBoxID,showYearMonthID, arrayDays);
	_createCalendar(iYear, iMonth, calendarTableID, toDayClassName, arrayDays, fnCallBack);
}


function _fillCalendarSelect(yearID, monthID, showText, arrayDays) { 
	var thisYear = (new Date()).getFullYear();
	var thisMonth = (new Date()).getMonth();
	$("#calYear > OPTION").remove();
	$("#calMonth > OPTION").remove();
	for ( var i = thisYear - 5 ; i < thisYear + 3; i++ ) {
		var oOption = document.createElement("option");
		oOption.value = i;
		oOption.innerHTML = i;
		if ( thisYear == i ) {
			oOption.selected = true;
		}
		$("#"+yearID).append(oOption);		
	}
	for ( i = 1; i < 13; i++ ) {
		oOption = document.createElement("option");
		oOption.value= i;
		oOption.innerHTML= i;
		if ( thisMonth == i-1 ) {
			oOption.selected = true;
		}		
		$("#"+monthID).append(oOption);
	}
	
	//년도와 달이 따로 디자인 된 경우
	if ( showText.indexOf("|") != -1 ) {
		var aShowText = new Array();
		aShowText = showText.split("|");
		for ( var k = 0; k < aShowText.length; k++ ) {
			if ( k == 0 ) {
				$("#"+aShowText[k]).text(thisYear);
			} else if ( k == 1 ) {
				$("#"+aShowText[k]).text(parseInt(thisMonth)+1);
			}
		}
	} else {
		$("#"+showText).text(thisYear+"."+(parseInt(thisMonth)+1));
	}
	
	$("#calChange").bind("click", function() { _changeCalendar(yearID, monthID, showText, 0, arrayDays); } );
	$("#previousMonth").bind("click", function() { _changeCalendar(yearID, monthID, showText,-1, arrayDays); } );
	$("#nextMonth").bind("click", function() { _changeCalendar(yearID, monthID, showText,1, arrayDays); } );
	$("#calChange").css("cursor","pointer");
	$("#previousMonth").css("cursor","pointer");
	$("#nextMonth").css("cursor","pointer");
	
}

function _changeCalendar(yearID, monthID, showText, changeMonth, arrayDays) {
	var change = new Date($("#"+yearID).selectedValues(), parseInt($("#"+monthID).selectedValues())-1+changeMonth, "1");	
	$("#"+yearID+" > option[value="+change.getFullYear()+"]").attr("selected", "true");	
	$("#"+monthID+" > option[value="+(parseInt(change.getMonth())+1)+"]").attr("selected", "true");
	
	//년도와 달이 따로 디자인 된 경우
	if ( showText.indexOf("|") != -1 ) {
		var aShowText = new Array();
		aShowText = showText.split("|");
		for ( var k = 0; k < aShowText.length; k++ ) {
			if ( k == 0 ) {
				$("#"+aShowText[k]).text(change.getFullYear());
			} else if ( k == 1 ) {
				$("#"+aShowText[k]).text(parseInt(change.getMonth())+1);
			}
		}
	} else {
		$("#"+showText).text(change.getFullYear()+"."+(parseInt(change.getMonth())+1));
	}
	_createCalendar(change.getFullYear(), change.getMonth(), '', '', arrayDays, '');	
	
}

function _createCalendar(yearVal, monthVal, insertTableID, toDayClassName, arrayDays, fnCallBack) {
	// 오늘 날을 찾기 위한 select의 값 추출이 필요함
	var today = new Date();
	if ( !yearVal ) {
		yearVal = today.getFullYear();
	}
	if ( !monthVal && monthVal != 0 ) {
		monthVal = today.getMonth();
	}

	if ( insertTableID ) {
		_insertTableID = insertTableID;
	} else {
		insertTableID = _insertTableID;
	}

	if ( toDayClassName ) {
		_toDayClassName = toDayClassName;
	} else {
		toDayClassName = _toDayClassName;
	}

	if ( fnCallBack ) {
		_fnCallBack = fnCallBack;
	} else {
		fnCallBack = _fnCallBack;
	}
	
	if ( !arrayDays ) {
		arrayDays = new Array(7);
		arrayDays = ["일", "월","화","수","목","금","토"];
	}
	
	var calBox = document.createElement("table");	

	var weekBox, itemBox, dateFont;
	
	var stDate = new Date(yearVal, monthVal, 1);
	var endDate = new Date(yearVal, monthVal+1, 0);

	var stDay = stDate.getDay();
	
	var tHeadBox = document.createElement("thead");
	weekBox = document.createElement("tr");

	//제목행 만들기
	for ( var i =0 ; i < 7; i++ ) {
		itemBox = document.createElement("th");
		itemBox.innerHTML = arrayDays[i];	
		weekBox.appendChild(itemBox);	
	}	
	tHeadBox.appendChild(weekBox);
	calBox.appendChild(tHeadBox);
	
	// 1일 전의 빈공간 만들기
	weekBox = document.createElement("tr");
	for (i = 0; i < stDate.getDay(); i++)	{
		itemBox = document.createElement("td");		
		weekBox.appendChild(itemBox);
	}
	
	// 1일부터 dateBox 생성
	i = 1;
	var endWeekDate = 7 - stDate.getDay();
	
	var sw = true;
	
	var tmp = 1;
	while(sw)	{
		for(i = tmp; i <= endWeekDate; i++)	{
			itemBox = document.createElement("td");
			
			if ((yearVal == today.getFullYear()) && (monthVal == today.getMonth()) && (i == today.getDate()))	{
				if ( toDayClassName ) {
					itemBox.className = toDayClassName;
				} else {
					itemBox.className = "today";
				}
			}
			
			if (i <= endDate.getDate())	{
				itemBox.appendChild(document.createTextNode(i));				
				itemBox.id = "day_"+i;				
			}
			weekBox.appendChild(itemBox);
		}
		calBox.appendChild(weekBox);
		weekBox = document.createElement("tr");
		tmp = i;
		endWeekDate = endWeekDate + 7;
		
		if (i > endDate.getDate()) {
			sw = false;
		}
	}
	//mainFrame.appendChild(dateBox);
	$("#"+insertTableID).html(calBox.innerHTML);
	if( typeof fnCallBack == "function" ) {	
		fnCallBack();
	}
	
}

