YAHOO.namespace("shillo.calendar");
YAHOO.shillo.calendar.config = {
	class_name: 'calinput',
	container: 'calDiv'
};

YAHOO.shillo.calendar.targetId = '';
YAHOO.shillo.calendar.inputId = '';
YAHOO.shillo.calendar.init = function() {
	if (!YAHOO.shillo.calendar.cal) {
		YAHOO.shillo.calendar.cal = new YAHOO.widget.Calendar(YAHOO.shillo.calendar.config.container, { mindate:new Date()} );
		YAHOO.shillo.calendar.cal.selectEvent.subscribe(YAHOO.shillo.calendar.handleSelect, YAHOO.shillo.calendar, true);
		YAHOO.shillo.calendar.cal.render();
	        var _items = YAHOO.util.Dom.getElementsByClassName(YAHOO.shillo.calendar.config.class_name);
	        if (_items.length > 0) {
	            for (var _ei = 0; _ei < _items.length; _ei++) {
	                YAHOO.util.Event.addListener(_items[_ei], 'click', YAHOO.shillo.calendar.click, YAHOO.shillo.calendar, true);                
	                YAHOO.util.Event.addListener(_items[_ei], 'mouseover', YAHOO.shillo.calendar.MouseOver, YAHOO.shillo.calendar, true);                
	                YAHOO.util.Event.addListener(_items[_ei], 'mouseout', YAHOO.shillo.calendar.MouseOut, YAHOO.shillo.calendar, true);                
	            }
	        }
        }
}

YAHOO.shillo.calendar.handleSelect = function(type,args,obj) {
	var year = args[0][0][0], month = args[0][0][1], day = args[0][0][2];
	var txtDate1 = document.getElementById(YAHOO.shillo.calendar.inputId);
	txtDate1.value = month + "/" + day + "/" + year;
	YAHOO.shillo.calendar.cal.hide();			
}

YAHOO.shillo.calendar.MouseOver = function(ev) { 
	document.body.style.cursor='pointer'; 
}

YAHOO.shillo.calendar.MouseOut = function(ev) { 
	document.body.style.cursor='default';
};

YAHOO.shillo.calendar.click = function(ev) {
	YAHOO.shillo.calendar.targetId = YAHOO.util.Event.getTarget(ev).id;
	// this removes the cal- prefix
	YAHOO.shillo.calendar.inputId = YAHOO.shillo.calendar.targetId.substr(4); 
	var aXY = YAHOO.util.Dom.getXY(YAHOO.shillo.calendar.inputId);
	if (aXY===false) {
		aXY = YAHOO.util.Dom.getXY(YAHOO.shillo.calendar.targetId);
		aXY[0] = 0;
	}
	YAHOO.shillo.calendar.cal.show();			
	YAHOO.util.Dom.setXY(YAHOO.shillo.calendar.config.container, aXY);
}

YAHOO.util.Event.onDOMReady(YAHOO.shillo.calendar.init);
