var _year = 0 , _month = 0 , _day = 0;
var _hour = "00" , _min = "00";
var _time_f = 1;
var _mth_str = new Array('S','M','T','W','T','F','S'); 
var sel_obj = null;
var mf = null;
var padding_size = 0;

function init_cal (th, selected_time)
{

	//padding = padding_size;
	//if( mf == null )
	//	makeIframe(0,0);
	var x,y;
	
	if ( _year == 0 || _month == 0 || _day == 0 ) {
		cur_date = new Date();
	//	alert("cur_date: " + cur_date);
		//_year = cur_date.getYear();
		_year = cur_date.getFullYear();
		_month = cur_date.getMonth() + 1
		_day = cur_date.getDate();

		_hour = cur_date.getHours()
		_min =  cur_date.getMinutes()
	}

	if(selected_time){
		_year = selected_time.substring(0, 4);
		_month =selected_time.substring(4, 6);
		_day = selected_time.substring(6, 8);
	}
	

	//x = window.event.clientX + document.body.scrollLeft+ 10 ;
	//y = window.event.clientY + document.body.scrollTop- 50 ;
	//div_calendar = document.all('div_calendar')
	
	var calendarDiv_obj = document.getElementById('div_calendar')

	calendarDiv_obj.style.pixelTop	= 186 + "px";
	calendarDiv_obj.style.pixelLeft = 700 + "px";

	sel_obj =  th
	
	_time_f = 0;
	

	show_cal(_year, _month, _day, _hour, _min, selected_time)

}


function re_cal ( el )
{

	if (el == -2) {
		_year--;
	} else if ( el == -1) {
		if (_month == 1) {
			_year--;
			_month = 12;
		} else {
			_month--;
		}
	} else if (el == 1 ) {
		if (_month == 12) {
			_year++;
			_month = 1;
		} else {
		_month++;
		}
	} else if (el ==2 ) {
		_year++;
	} else {
		return ;
	}

	show_cal(_year, _month, _day, _hour, _min );
}

var stime


function mouse_click() {
	sel_day = window.event.srcElement.title;
	if (sel_day.length > 7) {
		if (sel_obj != null ) {
			tmp  = sel_day;
			if (_time_f ==1 ){
				tmp  += "-" + document.getElementById("cal_sel_hour").value + "" + document.getElementById("cal_sel_min").value  + "01"
			}
			sel_obj.value = tmp;
			div_calendar.style.visibility='hidden'
			//mf.style.visibility='hidden'

		}
	}

}




function get_last_day(e_year, e_month)
{
	rtval = 31;
	tp1 = e_year % 4;
	tp2 = e_year % 100;

	if (e_month == 4 || e_month == 6 || e_month == 9 || e_month == 11) {
		rtval = 30;
	} else if (e_month == 2 && tp1 != 0 ) {
		rtval = 28;
	} else if (e_month == 2 && tp1 == 0 ) {
		if (tp2 == 0 ) {
			rtval = (tp2 == 0 ? 29 : 28 );
		} else {
			rtval=29;
		}
	}
	return  rtval;
}



function show_cal(e_year, e_month, e_day, e_hour, e_min, selected_time)
{
	cur_date = new Date();
	if(selected_time){
		today_year = selected_time.substring(0, 4);
		today_month =selected_time.substring(4, 6);
		today_day = selected_time.substring(6, 8);
	}
	else{
		today_year = cur_date.getFullYear();
		today_month = cur_date.getMonth() + 1 ;
		today_day = cur_date.getDate();
	}
	
	if ( e_year == "" || e_year <= 0 || e_month == "" || e_month <= 0 || e_day == "" || e_day <= 0 ) {
		e_year = cur_date.getFullYear();
		e_month = cur_date.getMonth() + 1
		e_day = cur_date.getDate();
	} else {
		cur_date.setYear(e_year);
		cur_date.setMonth(e_month-1);
		cur_date.setDate(e_day);
	}

	
		__cur_date = new Date();
	//	alert("cur_date: " + cur_date);
		var _cur_year = __cur_date.getFullYear();
		var _cur_month = __cur_date.getMonth() + 1
		var _cur_day = __cur_date.getDate();

		var _cur_hour = __cur_date.getHours()
		var _cur_min =  __cur_date.getMinutes()
	
	
	
//	alert(e_month);
	if(e_month < 10)	print_e_month = '0' + e_month;
	else print_e_month = e_month;
	
	if(e_day < 10)	print_e_day = '0' + e_day;
	else print_e_day = e_day;
	
	str =  "<div>"
	str += "<div class=\"title\"></div>"
	str += "<!--// s : calendar //-->"
	str +=  "<div style=\"margin-bottom:10px;\" align=\"center\"  class=\"calendar_mon\"><a href=\"#\" onfocus=\"blur();\"><img onclick=\"javascript:re_cal(-1)\" src=\"http://images.joins.com/ui_joins/issuemap08/calendar_ybtn_1.gif\" border=\"0\" alt=\"ÀÌÀü ´Þ\" /></a>&nbsp;&nbsp;" + e_year + "³â " + print_e_month + "¿ù&nbsp;&nbsp;<a href=\"#\" onfocus=\"blur();\"><img onclick=\"javascript:re_cal(1)\" src=\"http://images.joins.com/ui_joins/issuemap08/calendar_ybtn_2.gif\" border=\"0\" alt=\"´ÙÀ½ ´Þ\" /></a></div>"
	str += 	"<div style=\"margin-bottom:" + padding_size + "px; background:url(http://images.joins.com/ui_joins/issuemap08/calendar_bar_tit.gif) no-repeat; width:242px; height:20px;\"></div>"
	str += 	"<div style=\"margin-bottom:15px;\"><br>"
	str += "	  <!--// s : Ä¶¸°´õ ¹Ú½º ºÎºÐ //-->"
	str += 	"  <table width=\"226\" height=\"99\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">"

	count_day = 1;

	last_day = get_last_day(e_year, e_month)
	row_cnt = 0;

	for ( week=0 ; week < 6 ; week++ ) {

		tmp_cur_date = new Date(e_year, e_month-1, count_day);
	//	alert("tmp_cur_date: " + tmp_cur_date);

		if (last_day==31 && today_day > 31){
			s_day_of_week = tmp_cur_date.getDay()-1;
		} else {
			s_day_of_week = tmp_cur_date.getDay();
		}


		row_cnt++;
		str += "<tr>"
		for (dow=0; dow <= 6 ; dow++) {
			
			if ( dow < s_day_of_week ) {
				str += "<td align=\"center\" class=\"calendar_table\">&nbsp;</td>"
			} else {
				if (count_day > last_day) {
					str += "<td align=\"center\" class=\"calendar_table\">&nbsp;</td>"
					week = 10;
				} else {
					tmp_month  = (_month < 10) ? "0" + _month : _month;
					tmp_count_day = (count_day < 10 ) ? "0" + count_day : count_day;
					if (_year == today_year && tmp_month == today_month && tmp_count_day == today_day ){
						str += "<td align=\"center\" class=\"calendar_table_select\"><a href=\"javascript:searchByExactDate( '" + _year + tmp_month + tmp_count_day +"');\">" + count_day + "</a></td>";
					}
					else if (_year >= _cur_year && tmp_month >= _cur_month && tmp_count_day > _cur_day ){
						str += "<td align=\"center\" class=\"calendar_table\">" + count_day + "</td>";
					}
					else {
						str += "<td align=\"center\" class=\"calendar_table\"><a  href=\"javascript:searchByExactDate( '" + _year + tmp_month + tmp_count_day +"');\">" + count_day + "</a></td>";
					}

				}
				count_day++;
			}


		}
		str += "</tr>"
		
	}

	if(row_cnt == 5)
		t_height = 178;
	else if(row_cnt == 6)
		t_height = 198;
	else
		t_height = 178;


	if (_time_f ==1 ){ 
		str += show_time(e_hour, e_min);
		t_height += 23;
	}


	str += 	"</table>"
	str += 	"<!--// e : Ä¶¸°´õ ¹Ú½º ºÎºÐ //-->"
	str += 	"</div>"
	str += 	"</div>"

	document.getElementById("div_calendar").innerHTML = str
	//mf.style.visibility = "visible";
	//mf.style.height = t_height -20;
	document.getElementById("div_calendar").style.visibility = "visible";

	_year = e_year;
	_month = e_month;
	_day = e_day;
	
}


//////////////////////////////////////////////////////////////////////////////////////

//str = " <div id='div_calendar' style=\"background-color:#0A1831; width:270px; height:170px;position: absolute; z-index: 99\" ";
//str += "	onSelect='return false'>";
//str += " </div> \n";
//document.write(str)
