/**
 * AjaxCalendar.js (ajax with prototype.js)
 *
 * 작성자 : 정 혁 (jjjhyeok@naver.com)
 * 작성일 : 2010.03.23
 * 수정일 : 2010.04.16 : 언어 선택 기능 추가
 * 수정일 : 2010.04.29 : 달력 위치 자동 조정 추가 (달력이 화면을 넘어가는 경우를 대비)
 */

// 달력 호출 함수
function AjaxCal(obj, event, option) {
	ajaxcalendar = new AjaxCalendar(obj, event, option);
}

var AjaxCalendar = function(obj, event, option) {
	this.obj = $(obj);
	if (!this.obj) return;

	this.lang = 'PHP'; // 언어 선택 (ASP,PHP)

	option = (option) ? option : {};

	this.e = event;
	this.option = option;

	this.ie_ver = function() {
		var ieversion = 0;
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) 
			ieversion = new Number(RegExp.$1)
		return ieversion;
    }

	this.sdate = (option.sdate) ? option.sdate : ''; // 달력 시작일
	this.edate = (option.edate) ? option.edate : ''; // 달력 종료일
	this.format = (option.format) ? option.format : ''; // 출력 포멧
	this.offsetX = (option.offsetX != undefined) ? option.offsetX : -15;
	this.offsetY = (option.offsetY != undefined) ? option.offsetY : 15;

	this.init();
	this.show();
}

AjaxCalendar.prototype = {
	init: function() {
		var objBody = document.getElementsByTagName("body").item(0);
		this.Cal_Container = $('Cal_Container');
		if (!this.Cal_Container) {
			this.Cal_Container = document.createElement("div");
			this.Cal_Container.setAttribute('id','Cal_Container');
			with (this.Cal_Container.style) {
				position = 'absolute';
				display = 'none';
				top = '0';
				left = '0';
				zIndex = '99999';
				width = '180px';
				margin = '0px';
				textAlign = 'left';
			}
			objBody.appendChild(this.Cal_Container);
		}
	},

	show: function() {
		if (this.ie_ver > 0 && this.ie_ver < 8) this.toggleSelects('hide');

		// 이벤트 좌표 설정
		var evt = this.e || window.event;
		var x = evt.pageX || evt.clientX + document.documentElement.scrollLeft || document.body.scrollLeft;
		var y = evt.pageY || evt.clientY + document.documentElement.scrollTop || document.body.scrollTop;
		var pos = this.get_position(x, y); // 달력 위치 자동 조정해서 가져옴 (20100429 정 혁)
		this.Cal_Container.style.left = (pos.x + this.offsetX) + 'px';
		this.Cal_Container.style.top = (pos.y + this.offsetY) + 'px';

		this.Cal_Container.style.display = 'block';
		this.Cal_Container.innerHTML = '<div style="padding:5px;"><img src="/js/ajaxcalendar/ajax-loader.gif"></div>'; 
		//return;

		this.updater('/js/ajaxcalendar/ajaxcalendar.'+this.lang.toLowerCase()+'?sdate='+this.sdate+'&edate='+this.edate+'&format='+this.format);
	},

	hide: function() {
		this.toggleSelects();
		this.Cal_Container.style.display = 'none';
		this.Cal_Container.innerHTML = '';
	},

	updater: function(url) {
		var TO_Ajax = new Ajax.Updater(this.Cal_Container, url, {
			onFailure: function() { alert('데이터 로딩에 실패하였습니다.'); },
			evalScripts: true
		});
	},

	insert: function(day) {
		this.obj.value = day;
		this.hide();
	},

	toggleSelects: function(state) {
		var selects = document.getElementsByTagName("select");
		for (var i = 0; i < selects.length; i++ ) {
			selects[i].style.visibility = (state == "hide") ? 'hidden' : 'visible';
		}
	},

	// 객체 크기
	get_size: function(el) {
		return {
			width:(el.offsetWidth || parseInt(el.style.width, 10)),
			height:(el.offsetHeight || parseInt(el.style.height, 10))
		};
	},

	// 화면 크기
	get_client: function() {
		return {
			width:(document.body.clientWidth || document.documentElement.clientWidth) + document.body.scrollLeft,
			height:(document.body.clientHeight || document.documentElement.clientHeight) + document.body.scrollTop
		};
	},

	// 달력 위치 자동 조정 (달력이 화면을 넘어가는 경우를 대비) (20100429 정 혁)
	get_position: function(x, y) {
		var size = this.get_size(this.Cal_Container);
		var client = this.get_client();
		var max_x = (client.width - size.width - this.offsetX) - 1;
		var max_y = (client.height - size.height - this.offsetY) - 1;
		return {
			x:(x > max_x) ? max_x : x,
			y:(y > max_y) ? max_y - (client.height - y + this.offsetY) : y
		};
	}
}

