/* Filename: calendar2.0.js
 * Used with incCalendar.jsp; see notes there.
 * TODO: convert this to a class (CCalendar).
 *
 * 2009-06-01
 */


var CALENDAR = new Object();
var PopupCalendar = CALENDAR;	//official public name

document.observe("dom:loaded", function() {
	//As soon as the DOM is fully loaded...
	PopupCalendar.init();
});



CALENDAR.open = function(tb, position) {
	var cal = CALENDAR.div;
	CALENDAR.textbox = (typeof(tb)=="string") ? $(tb) : tb;

	if (CALENDAR.textbox==null) {
		alert("Invalid Calendar Target: "+tb);
		return;
	}

	if (CALENDAR.textbox) {
		CALENDAR.originalDate = null;
		CALENDAR.setCalendar( CALENDAR.textbox.value );
	} else
		CALENDAR.setCalendar();


	//Position the calendar
	if (position=="center")
		CALENDAR.center();
	else if (position=="below")
		CALENDAR.below();
	else if (position=="above")
		CALENDAR.above();
	else		//default: to the right
		CALENDAR.right();

	cal.style.display="";
	activateBackground("CALENDAR.close();");
	ActiveBackground.toForeground(cal)

};


CALENDAR.close = function() {
	var cal = CALENDAR.div;
	cal.style.display="none";
	deactivateBackground();
};

CALENDAR.setSource = function() {
	if (CALENDAR.date && CALENDAR.textbox) {
		if (CALENDAR.mode=="DMY")
			CALENDAR.textbox.value = CALENDAR.date.toDMY();
		else if(CALENDAR.mode=="MDY")
			CALENDAR.textbox.value = CALENDAR.date.toMDY();
		else	//YMD
			CALENDAR.textbox.value = CALENDAR.date.toIEEEDate();
	}
}

CALENDAR.shiftCalendar = function(years, months) {
	var datetime = new Date(CALENDAR.date);
	datetime.addYears(years);
	datetime.addMonths(months);

	CALENDAR.setCalendar(datetime);
}

CALENDAR.shiftCalendarDbl = function(years, months) {
	if (document.all) {//ie
		CALENDAR.shiftCalendar(years, months);
	}
}

//Formats: (), (date), (IEEEstring), (y,m,d)
CALENDAR.setCalendar = function(datetime, m, d) {
	if (m)					//setCalendar(y, m, d)
		datetime = new Date(datetime, (m-1), d);
	else if (!datetime)		//setCalendar()
		datetime= new Date();
	else if ( typeof(datetime)=="string" ) {
		if (CALENDAR.mode=="DMY")
			datetime = Date.parseTraditionalDate(datetime, true);
		else if(CALENDAR.mode=="MDY")
			datetime = Date.parseTraditionalDate(datetime, false);
		else	//YMD
			datetime = Date.parseIEEEDate(datetime);

		if (datetime==null)
			datetime = new Date();
	}

	var display = datetime.getMonthName() + " " + datetime.getFullYear();
	CALENDAR.display.innerHTML=display;

	CALENDAR.date = datetime;
	if (!CALENDAR.originalDate)
		CALENDAR.originalDate = datetime;
	CALENDAR.populate();
}

CALENDAR.populate = function() {//function(datetime, m, d) {
	var datetime = new Date(CALENDAR.date);	//get a copy of the date
	var original = CALENDAR.originalDate;

	var r = 1;
	var col = datetime.firstDayOfMonth().getDay();	//0-6
	var days = datetime.lastDayOfMonth().getDate(); //29-31;

	var selected = -1;
	if ( datetime.sameMonthAs(original) )
		selected = original.getDate()-1;
	

	var currentDay = -1;
	var today = new Date();
	if ( datetime.sameMonthAs(today) )
		currentDay = today.getDate()-1;
	
	var row = $("CALENDAR_row0");
	var cell = getGridCell(row, col);

	var c=col;
	for (var d=0; d<days; d++) {
		cell.innerHTML = ""+(d+1);
		
		var cls = (d==currentDay)?"calendarToday":"";
		if (d==selected) cls += " selected";
		cell.className = cls;

		c++;

		cell = getNextTag(cell);
		if (cell==null) {
			row = getNextTag(row);
			cell = getGridCell(row, 0);
			r++; c=0;
		}
	}
	//hide 6th row if unused
	$("CALENDAR_row5").style.display = (r==5||c==0)?"none":"";

	$("CALENDAR_rowCurrentMonth").style.display = (currentDay!=-1)?"none":"";
	

	//Add next month's initial days
	c = 1;
	while (cell!=null) {	
		cell.innerHTML=""+(c++);
		cell.className="calendarDiffMonth";
		cell = getNextTag(cell);
	}

	//Add last month's final days
	if (col>0) {
		datetime.addMonths(-1);
		var start = (datetime.lastDayOfMonth().getDate())-col+1;

		var cell = getGridCell( $("CALENDAR_row0"), 0);
		for (var c=0; c<col; c++) {
			cell.innerHTML=""+(c+start);
			cell.className="calendarDiffMonth";
			cell = getNextTag(cell);
		}
	}
};

CALENDAR.cellHover = function(_self, hover, isWeekend) {
	var isToday = (_self.className=="calendarToday");

	if (isToday) {
		_self.style.backgroundColor = hover ? "#D5D67A" : "";
	} else if (isWeekend) {
		_self.style.backgroundColor = hover ? "#C7D7E1" : "";
	} else {
		_self.style.backgroundColor = hover ? "#EBEBEB" : "";
	}
};
CALENDAR.cellClick = function(_self) {
	var currentMonth = (_self.className!="calendarDiffMonth");

	var day = parseInt(_self.innerHTML);
	if (!currentMonth) {
		var month = (day>15) ? -1 : +1;
		CALENDAR.date.addMonths(month);
	}
	CALENDAR.date.setDate(day);
	CALENDAR.setSource();
	CALENDAR.close();
};

CALENDAR.startDrag = function(e) {
	if (!CALENDAR.isDragging) {
		CALENDAR.isDragging = true;

		var m = getMouseXY(e);
		var cal = CALENDAR.div;
		CALENDAR.startDragMouseX = m.x;
		CALENDAR.startDragMouseY = m.y;
		CALENDAR.startDragCalX = parseInt(cal.style.left);
		CALENDAR.startDragCalY = parseInt(cal.style.top);

		//create hook & store old hook
		CALENDAR.oldOnmousemove = (document.onmousemove) ? document.onmousemove : null;
		document.onmousemove = function(event) {CALENDAR.doDrag(event); };

		CALENDAR.display.style.cursor="move";
	}
}

CALENDAR.stopDrag = function() {
	if (CALENDAR.isDragging) {
		CALENDAR.isDragging = false;
		document.onmousemove = CALENDAR.oldOnmousemove;
		CALENDAR.display.style.cursor="default";
	}
}

//Called by document.onmousemove after a call to startDrag.
CALENDAR.doDrag = function(e) {
	var m = getMouseXY(e);
	var cal = CALENDAR.div;

	var dx = m.x - CALENDAR.startDragMouseX;
	var dy = m.y - CALENDAR.startDragMouseY;

	cal.style.left = (dx + CALENDAR.startDragCalX) + "px";
	cal.style.top = (dy + CALENDAR.startDragCalY) + "px";
}

//Don't call during a drag.
CALENDAR.center = function() {
	var cal = CALENDAR.div;
	var pageWidth = document.viewport.getWidth();
	var pageHeight = document.viewport.getHeight();

	var x = ( pageWidth - parseInt(cal.getWidth()) )/2;
	var y = ( pageHeight - parseInt(cal.getHeight()) )/2;

	cal.style.left = x + "px";
	cal.style.top = (y+getPageScrollXY().y) + "px";
}

//Don't call during a drag.
CALENDAR.right = function() {
	var offset = CALENDAR.textbox.cumulativeOffset();
	var tbWidth = CALENDAR.textbox.getWidth();

	var cal = CALENDAR.div;
	var x = offset.left+tbWidth+2;
	var y = offset.top;
	cal.style.left = x + "px";
	cal.style.top =  y + "px";
}

//Don't call during a drag.
CALENDAR.below = function() {
	var offset = CALENDAR.textbox.cumulativeOffset();
	var tbHeight = CALENDAR.textbox.getHeight();

	var cal = CALENDAR.div;
	var x = offset.left;
	var y = offset.top+tbHeight+2;
	cal.style.left = x + "px";
	cal.style.top =  y + "px";
}

CALENDAR.above = function() {
	var offset = CALENDAR.textbox.cumulativeOffset();
	var cal = CALENDAR.div;
	var x = offset.left;
	var y = offset.top-cal.getHeight()-2;
	cal.style.left = x + "px";
	cal.style.top =  y + "px";
}

//Configure onmouseover, onmouseout, onclick, and page width & height.
//Call from window.onload
CALENDAR.init = function(width, height) {
	CALENDAR.div = $("CALENDAR_floatdiv");
	CALENDAR.display = $("CALENDAR_CalendarDisplay");

	var locale = CALENDAR.div.getAttribute("locale");
	if (locale!="en")
		Date.setLocale(locale);

	CALENDAR.mode = CALENDAR.div.getAttribute("mode");
	if (CALENDAR.mode==null)
		CALENDAR.mode = "YMD";

	var row = $("CALENDAR_row0");
	var cell = getGridCell(row, 0);

	var c=0;
	//while(row!=null) {
	for (var r=0; r<6;) {
		var isWeekend = (c%6==0);
		eval("cell.onmouseover = function() { CALENDAR.cellHover(this, true, "+isWeekend+") };");
		eval("cell.onmouseout = function() { CALENDAR.cellHover(this, false, "+isWeekend+"); };");
		cell.onclick = function() { CALENDAR.cellClick(this); };

		cell = getNextTag(cell);
		c++;
		if (cell==null) {
			if( (row=getNextTag(row))!=null )
				cell = getGridCell(row, 0);
			c=0;
			r++;
		}
	}
	createBackground();
}




///MORE
CALENDAR.filterKeys = function(event) {
	return filterKeys(event, '-', 48, 57);
}
