var Calendar = {
	//기본 값 설정
	start_year:null,
	start_month:null,
	start_day:null,
	end_year:null,
	end_month:null,
	end_day:null, 

	id:null,
	return_id:null,
	year:null,
	month:null,

	end_date:0,  //각 월의 마지막 날
	create: function(id,return_id,year,month) {
		this.id=id,this.return_id=return_id,this.year=year,this.month=month;
		now = new Date();

		date = new Date(year,month-1,1);
		start_date=date.getDate();
		start_year=date.getFullYear();
		start_month=date.getMonth()+1;
		start_day=date.getDay();

		last_date = new Date(year,month,0);  //마지막 요일 구하기
		end_date = last_date.getDate();
		this.end_date=end_date;

		//기본값 세팅
		this.start_year=start_year;this.start_month=start_month;this.start_day=start_date;
		this.end_year=start_year;this.end_month=start_month;this.end_day=start_date;

		before_date = new Date(year,month-1,1);
		before_year = before_date.getFullYear();
		before_month = before_date.getMonth();

		next_date = new Date(year,month+1,1);
		next_year = next_date.getFullYear();
		next_month = next_date.getMonth();

		result="<table cellpadding='3' cellspacing='0' width='100%' bgcolor='#eeeeee'>"
		+"<tr><td width='18'><a onclick=\"Calendar.create('"+id+"','"+return_id+"',"+before_year+","+before_month+");\" style='cursor:pointer;'>◀</td><td align='center'>"+start_year+"년 "+start_month+"월</td><td align='right' width='20'><a onclick=\"Calendar.create('"+id+"','"+return_id+"',"+next_year+","+next_month+");\"  style='cursor:pointer;'>▶</a><td>"
		+"<tr></table><table cellpadding='3' cellspacing='0' width='100%' bgcolor='#eeeeee'><tr><td colspan='3'>"
		+"<table width='100%' cellpadding='3' cellspacing='1' bgcolor='#aaaaaa'>"
		+"<tr align='center' bgcolor='black'><td style='color:white;'>S</td><td style='color:white;'>M</td><td style='color:white;'>T</td><td style='color:white;'>W</td><td style='color:white;'>T</td><td style='color:white;'>F</td><td style='color:white;'>S</td></tr>";
		
		//빈셀 만들기
		for(var i=1;i<=start_day;i++) {
			if(i%7==1) result+="<tr>";
			result+="<td align='center' bgcolor='white' height='20'>&nbsp;</td>";
			if(i%7==0) result+="</tr>";
		}

		//날짜 완성
		for(var j=1;j<=end_date;j++) {
			var cell_i=j+start_day;

			if(cell_i%7==1) result+="<tr>";
			result+="<td id='day_"+j+"' height='18' style='cursor:pointer;font-family:돋움;text-align:center;";
			if(start_year==now.getYear() && start_month==(now.getMonth()+1) && j==now.getDate()) result+="border:1px solid #aaaaaa;background-color:#77BFF5";  //오늘 날짜면 bg처리
			result+="' onclick=\"Calendar.select_date('"+start_year+"-"+start_month+"-"+j+"')\" valign='top' bgcolor='white' ";
			if(cell_i%7==1) result+="style='color:red;'"; //일요일
			else if(cell_i%7==0) result+="style='color:blue;'"; //토요일
			result+=">"+j+"</td>";

			if(cell_i%7==0) result+="</tr>";
		}

		//마무리 안된 셀
		for(var i=j+start_day;;i++) {
			if(i%7==1) result+="<tr>";
			result+="<td height='18' align='center' bgcolor='white'>&nbsp;</td>";
			if(i%7==0) {
				result+="</tr>";
				break;
			}
		}

		result+="</table>"
		+"</td></tr></table>";

		$(id).update(result);
	},
	open: function(id) {
		$(id).toggle();
	},
	drag_start: function(event) {
		clickElement=Event.element(event);

		//기존의 셀렉트 영역 삭제
		if(Event.isLeftClick(event)) { //왼쪽 클릭일때만 처리
			for(var i=1;i<=this.end_date;i++) {
				$('day_'+ i).removeClassName('td_select');
			}

			Move.start_id=clickElement.id;  //시작 셀 지정

			Move.point_id=clickElement.id;  //셀포인터

			Move.moving=true;
		}
	},
	drag_move: function(event) {
		if(Move.start_id) {
			clickElement=Event.element(event);
			
			if(Move.move_id!=clickElement.id) {  //같은 셀의 영역이면 작동하지 않음.
				Move.move_id=clickElement.id;

				arr_start_id=Move.start_id.split('_');
				arr_move_id=Move.move_id.split('_');

				 //포인트를 정수변환
				var start_id=parseInt(arr_start_id[1]); 
				var move_id=parseInt(arr_move_id[1]);

				if(start_id<move_id) {
					start_cell=start_id;
					end_cell=move_id;
				}
				else {
					start_cell=move_id;
					end_cell=start_id;
				}
			
				this.start_day=start_cell;
				this.end_day=end_cell;

				//Select 효과 주기
				for(var i=start_cell;i<=end_cell;i++) {
					$('day_'+ i).addClassName('td_select');
				}

				//class 삭제
				$$('td.td_select').each(function(value,index) {
					arr_this_id=value.id.split('_');
					var this_id=parseInt(arr_this_id[1]);

					if(start_id<move_id) {
						if(start_id>this_id || move_id<this_id) $(value).removeClassName('td_select');
					}
					else {
						if(start_id<this_id || move_id>this_id) $(value).removeClassName('td_select');
					}
				});

			}
		}
	},
	drag_end: function(event) {
		clickElement=Event.element(event);
		Move.end_id=clickElement.id; //종료 셀 지정
		Move.start_id="";
		Move.moving=false;
	},
	add_schedule: function() {
		$('frm_add_schedule').start_date.value=this.start_year+"-"+this.start_month+"-"+this.start_day;
		$('frm_add_schedule').end_date.value=this.end_year+"-"+this.end_month+"-"+this.end_day;
		
		//센터로 고정후 표시
		$('div_add_schedule').style.left=(document.body.clientWidth-600)/2;
		$('div_add_schedule').style.top=(document.body.clientHeight-300)/2;
		$('div_add_schedule').show();
	},
	add_schedule_ok: function() {
		this.add_schedule_close();
	
		//일정 새로 그리기
		//this.create(this.id,this.return_id,this.year,this.month);

		//시작점 : (전체픽셀 / 7) +1 ,  끝점 : (전체픽셀/7)*일수
		
		$('main_calendar').innerHTML+="<div style='position:absolute;border:1px solid #aaaaaa;background-color:#eeeeee;width:300px;height:15px;top:300px;left:200px;text-align:center;'>일정</div>";
	},
	add_schedule_close: function() {
		//초기화 시키기
		document.frm_add_schedule.subject.value="";
		document.frm_add_schedule.contents.value="";

		$('div_add_schedule').hide();
	},
	show_menu: function(event) {
		$('menu_calendar').style.left=event.clientX;
		$('menu_calendar').style.top=event.clientY;

		$('menu_calendar').style.display='inline';
	},
	close_menu: function(event) {
		$('menu_calendar').style.display='none';
	},
	select_date: function(date) {
		$('selected_date').value=date;
		close_calendar();
	}
};
