/*

Author : 디자인쿤
Url : http://designkoon.com
Email : adm@designkoon.com

*/

var arrDate = new Array();
var arrMonth = new Array('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12');
var arrDay = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var nowDate = new Date();
var calendarLayerId = 'calendarLayer';


function _calendar(day, month, year, layerId, no, _calendarLayerId, type) {

   var html = '';
   var firstDay = new Date(year, month, 1).getDay();

   if (((year%4==0)||(year%100==0))&&(year%400==0)) arrDay[1] = 29; 
   else arrDay[1] = 28;

   html += '<table cellspacing="1" class="calendar">';
   html += '<thead class="calendarTitle"><tr><td colspan="7">';

   html += '<a href="javascript:;" onclick="arrDate[' + no + '][2] --; calendar(\'' + layerId + '\', ' + no + ');">◀</a> ';
   html += '<a href="javascript:;" onclick="arrDate[' + no + '][1] --; if (arrDate[' + no + '][1]<0) { arrDate[' + no + '][2] --; arrDate[' + no + '][1] = 11; } calendar(\'' + layerId + '\', ' + no + ');">◁</a> ';
   html += year + '.' + arrMonth[month].toUpperCase() + ' ';
   html += '<a href="javascript:;" onclick="arrDate[' + no + '][1] ++; if (arrDate[' + no + '][1]>11) { arrDate[' + no + '][2] ++; arrDate[' + no + '][1] = 0; } calendar(\'' + layerId + '\', ' + no + ');">▷</a> ';
   html += '<a href="javascript:;" onclick="arrDate[' + no + '][2] ++; calendar(\'' + layerId + '\', ' + no + ');">▶</a>';
   html += '</td></tr></thead>';

   html += '<tbody class="calendarList1"><tr><td>' + _i('일') + '</td><td>' + _i('월') + '</td><td>' + _i('화') + '</td><td>' + _i('수') + '</td><td>' + _i('목') + '</td><td>' + _i('금') + '</td><td>' + _i('토') + '</td></tr></tbody>';
   html += '<tbody class="calendarList2"><tr>';
  
   for (var i=0; i<firstDay; i++) html += '<td> '; 

   for (var i=0, incDay=1; i<arrDay[month]; i++) {
    
	var data = year + ((arrMonth[month].toUpperCase()>9)?arrMonth[month].toUpperCase():'0'+arrMonth[month].toUpperCase()) + ((incDay>9)?incDay:'0'+incDay);

    if ((arrDate[no][0]==incDay)&&(arrDate[no][1]==nowDate.getMonth())&&(arrDate[no][2]==nowDate.getFullYear()))
     html += '<td class="emp">';
    else html += '<td>';

    switch (new Date(year, month, incDay).getDay()) {

     case 0: html += '<a href="javascript:;" onclick="insertDate(\'' + layerId + '\', \'' + data + '\');" class="sun">'; break;
     case 6: html += '<a href="javascript:;" onclick="insertDate(\'' + layerId + '\', \'' + data + '\');" class="sat">'; break;
     default:
      if ((arrDate[no][0]==incDay)&&(arrDate[no][1]==nowDate.getMonth())&&(arrDate[no][2]==nowDate.getFullYear())) 
       html += '<a href="javascript:;" onclick="insertDate(\'' + layerId + '\', \'' + data + '\');" class="toDay">';
      else html += '<a href="javascript:;" onclick="insertDate(\'' + layerId + '\', \'' + data + '\');">';       
	  break;

    }

    html += incDay++;
    html += '</a>';

    if (((i+firstDay+1)%7==0)&&(incDay<arrDay[month]+1)) html += '<tr>';

   }

   var totalCell = firstDay + arrDay[month];

   for (var i=0; i<(totalCell>28?(totalCell>35?42:35):28)-totalCell; i++) html += '<td> ';

   html += '</tbody><tfoot><tr><td colspan="7"><a href="javascript:;" onclick="document.getElementById(\'' + layerId + '\').value=\'\';" class="reset">reset</a> <a href="javascript:;" onclick="calendar(\'' + layerId + '\', ' + no + ', \'hide\');" class="close">x</a></td></tr></tfoot></table>';

   return html;

}

function insertDate(layerId, value) {

   document.getElementById(layerId).value = value;
   document.getElementById(calendarLayerId + "_" + layerId).style.display = 'none';

}

function calendar(layerId, no, type) {
 
   var _calendarLayerId = calendarLayerId + '_' + layerId;
   var _objCalendarLayerId = document.getElementById(_calendarLayerId);   
   var objLayerId = document.getElementById(layerId);   

   if (type=='hide') _objCalendarLayerId.style.display = 'none';
   else {

    if (!_objCalendarLayerId) {

 	 var objCalendarLayer = document.createElement('div');

 	 objCalendarLayer.setAttribute('id', _calendarLayerId);
     objCalendarLayer.style.left = getOffsetLeft(objLayerId) + 'px';
     objCalendarLayer.style.top = ((getOffsetTop(objLayerId)+objLayerId.offsetHeight)-1) + 'px';
 	 objCalendarLayer.className = 'calendarBox';
     arrDate[no] = [nowDate.getDate(), nowDate.getMonth(), nowDate.getFullYear()];
     objCalendarLayer.innerHTML = _calendar(arrDate[no][0], arrDate[no][1], arrDate[no][2], layerId, no, _calendarLayerId);
 
	 document.body.appendChild(objCalendarLayer);

	} else {

	 _objCalendarLayerId.style.display = 'block';
     _objCalendarLayerId.innerHTML = _calendar(arrDate[no][0], arrDate[no][1], arrDate[no][2], layerId, no, _calendarLayerId);

	}


   }

}
