// JavaScript Document

// Global Variables
var jDayWidth = "128";
var jDayHeight = "100";
var jWDayHeight = "20";
var jEventLength = 36;
var jDisplayEventWidth = "220";
var jEventClicked = false;
var jCanEditCalendar = false;
var jCanEdit_AddItem = "";
var jCanEdit_EditItem = "";
var jBeginDate = "";

var jMonth = new Array(12);
	jMonth[0] = "January";
	jMonth[1] = "February";
	jMonth[2] = "March";
	jMonth[3] = "April";
	jMonth[4] = "May";
	jMonth[5] = "June";
	jMonth[6] = "July";
	jMonth[7] = "August";
	jMonth[8] = "September";
	jMonth[9] = "October";
	jMonth[10] = "November";
	jMonth[11] = "December";
var jWday = new Array(7);
	jWday[0] = "Sunday";
	jWday[1] = "Monday";
	jWday[2] = "Tuesday";
	jWday[3] = "Wednesday";
	jWday[4] = "Thursday";
	jWday[5] = "Friday";
	jWday[6] = "Saturday";

var jDate;
var jEmptyTop, jEmptyBottom;

// Initialization Routine

function init_jcalendar() {
	jDate = new Date();
	if( jBeginDate != "" ) {
		var	y, m, d;
		y = parseInt( jBeginDate.substring( 0, 4 ), 10 );
		m = parseInt( jBeginDate.substring( 4, 6 ), 10 );
		d = parseInt( jBeginDate.substring( 6, 8 ), 10 );
		jDate.setFullYear( y, m - 1, d );
	}
	jSetupMonth();
}

function jChangeMonth( n ) {
	var m, y;

	m = jDate.getMonth();
	y = jDate.getFullYear();
	m += n;
	if( m < 0 ) {
		m = 11;
		y--;
	} else if( m > 11 ) {
		m = 0;
		y++;
	}
	jDate = new Date( y, m, 1 );
	jSetupMonth();
}

function jLastDayOfMonth( Year, Month ) {
	return( (new Date( (new Date( Year, Month + 1, 1 )) - 1 )).getDate() );
}

function jSetupMonth() {
	var calendar = document.getElementById( "jcalendar" );
	var html, i, j;
	var wday, mday, d;
	var today = new Date();

	// first, add area to display event
	html = '<div id="jDisplayEvent" class="jcalevent" style="display: none;" onclick="jCloseEvent(true);">&nbsp;</div>';

	// add title at top
	html += '<table border="0" align="center"><tr><td><form><input type="button" value="<<" onclick="jChangeMonth( -1 )" /></form></td>';
	html += '<td class="jcalmaintitle" align="center" width="250">' + jMonth[ jDate.getMonth() ] + " " + jDate.getFullYear() + '</td>';
	html += '<td><form><input type="button" value=">>" onclick="jChangeMonth( 1 )" /></form></td></tr></table>';
	if( jCanEditCalendar ) {
		html += '<table border="0" align="center"><tr><td align="center" colspan="3"><strong><em>(Click on Number to add an event to a day; Click on an event to edit the event)</em></strong></td></tr></table>';
	}
	// begin table and add days of week
	html += '<table align="center" border="2" cellpadding="2" cellspacing="0"><tr>';
	for( i = 0; i < 7; i++ ) {
		html += '<td align="center" valign="middle" height="' + jWDayHeight + '" width="' + jDayWidth
			+ '"><span class="jcaltitle">' + jWday[ i ] + '</span></td>';
	}
	html += '</tr>';
	// begin first row of calendar
	html += '<tr>';
	mday = jLastDayOfMonth( jDate.getFullYear(), jDate.getMonth() );
	d = jDate;
	d.setDate(1);
	wday = d.getDay();
	jEmptyTop = wday;
	if( wday > 0 ) {
		html += '<td class="jcalempty" align="center" valign="middle" id="jEmptyTop" colspan="' + wday + '">&nbsp;</td>';
	}
	for( i = 1; i <= mday; i++ ) {
		if( wday == 0 ) {
			html += '<tr>';
		}
		html += '<td ';
		if( (today.getDate() == i) && (today.getFullYear() == jDate.getFullYear()) && (today.getMonth() == jDate.getMonth()) ) {
			html += 'class="jcaltoday" ';
		} else {
			html += 'class="jcalday" ';
		}
		html += 'id="jDate' + i + '" align="left" valign="top" height="' 
			+ jDayHeight + '" width="' + jDayWidth + '"><p class="jcalevent"><span class="jcaltitle">';
		if( jCanEditCalendar ) {
			j = d.getMonth() + 1;
			html += '<a href="javascript:jCalGotoLink(\'' + jCanEdit_AddItem + 'caldate=' + j + '-' + i + '-' + d.getFullYear() + '\');">&nbsp;';
		}
		html += i;
		if( jCanEditCalendar ) {
			html += '&nbsp;&nbsp;</a>';
		}
		html += '</span></p></td>';
		wday++;
		if( wday >= 7 ) {
			html += '</tr>';
			wday = 0;
		}
	}
	jEmptyBottom = 0;
	if( wday > 0 ) {
		jEmptyBottom = 7 - wday;
		html += '<td class="jcalempty" align="center" valign="middle" id="jEmptyBottom" colspan="' + jEmptyBottom + '">&nbsp;</td></tr>';
	}
	html += '</table>';

	// update calendar HTML
	calendar.innerHTML = html;

	// now add small calendars
	var prevmonth = jSmallMonth( jDate.getFullYear(), jDate.getMonth() - 1, "-1" );
	var nextmonth = jSmallMonth( jDate.getFullYear(), jDate.getMonth() + 1, "1" );
	if( (jEmptyTop == 1) && (jEmptyBottom >= 3) ) {
		jEmptyTop = 0;		// force both calendars to bottom if the top area is too small
	}
	if( (jEmptyBottom == 1) && (jEmptyTop >= 3) ) {
		jEmptyBottom = 0;		// force both calendars to top if the bottom area is too small
	}
	if( (jEmptyTop == 0) || (jEmptyBottom == 0) ) {
		if( (jEmptyTop != 0) || (jEmptyBottom != 0) ) {
			if( jEmptyBottom == 0 ) {
				i = document.getElementById( "jEmptyTop" );
			} else {
				i = document.getElementById( "jEmptyBottom" );
			}
			i.innerHTML = '<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="50%" align="center">'
				+ prevmonth + '</td><td width="50%" align="center">' + nextmonth + '</td></tr></table>';
		}
	} else {
		i = document.getElementById( "jEmptyTop" );
		i.innerHTML = prevmonth;
		i = document.getElementById( "jEmptyBottom" );
		i.innerHTML = nextmonth;
	}

	// add the information
	jPopulateCalendar( jDate );
}

function jSmallMonth( year, month, change ) {
	var html, i;
	var wday, mday, d;
	
	// handle month out of range, so we do not have to do it elsewhere
	if( month < 0 ) {
		month = 11;
		year--;
	} else if( month > 11 ) {
		month = 0;
		year++;
	}
	var date = new Date( year, month, 1 );

	// begin table and add days of week
	html = '<table class="jsmallcal" align="center" border="0" cellpadding="0" cellspacing="0" onclick="jChangeMonth( ' + change + ' )">';
	html += '<tr><td align="center" colspan="7">' + jMonth[ date.getMonth() ] + " " + date.getFullYear() + '</td></tr>';
	// begin first row of calendar
	html += '<tr>';
	mday = jLastDayOfMonth( date.getFullYear(), date.getMonth() );
	wday = date.getDay();
	if( wday > 0 ) {
		html += '<td colspan="' + wday + '">&nbsp;</td>';
	}
	for( i = 1; i <= mday; i++ ) {
		if( wday == 0 ) {
			html += '<tr>';
		}
		html += '<td align="right">&nbsp;' + i + '</td>';
		wday++;
		if( wday >= 7 ) {
			html += '</tr>';
			wday = 0;
		}
	}
	if( wday > 0 ) {
		i = 7 - wday;
		html += '<td colspan="' + i + '">&nbsp;</td></tr>';
	}
	html += '</table>';
	return( html );
}

function jPopulateCalendar( ) {
	var len, date, evnt, s, day, cell, colors, html;
	// Info is in JCalendar[]
	// Each element is an array of: <date>, <startTime>, <endTime>, <title>, <info>, <link>, <room>, <color>, [ <index> ]
	len = JCalendar.length;
	date = '' + jDate.getFullYear() + jZeroPad( jDate.getMonth() + 1, 2 );
	for( i = 0; i < len; i++ ) {
		evnt = JCalendar[ i ];
		s = evnt[0];
		if( s.substr( 0, 6 ) != date ) {
			continue;
		}
		day = parseInt( s.substr( 6, 2 ), 10 );
		cell = document.getElementById( "jDate" + day );
		if( cell === null ) {
			alert( "Null Cell: Event #" + i + ", Day=" + day + ", Text=" + s.substr( 6, 2 ) );
			continue;
		}
		colors = jLookupColor( evnt[7] );
		html = '<p class="jcalevent" id="jDayP' + i + '" style="background-color:' + colors[1] + '; color:' + colors[0] 
				+ ';" onmouseover="jOpenEvent(' + i + ',false);" onmouseout="jCloseEvent(false);" onclick="';
		if( jCanEditCalendar ) {
			html += 'jCloseEvent(true);jCalGotoLink(\'' + jCanEdit_EditItem + 'calindex=' + evnt[8] + '\');';
		} else {
			html += 'jOpenEvent(' + i + ',true);';
		}
		html += '">';
		if( evnt[ 1 ] != "" ) {
			s = jFormatTime( evnt[1], false ) + ' ' + evnt[3];
		} else {
			s = evnt[ 3 ];
		}
		if( s.length > jEventLength ) {
			html += s.substr(  0, jEventLength ) + "...";
		} else {
			html += s;
		}
		html += '</p>';
		cell.innerHTML += html;
	}
}

function jChangeBgColor( evntIdx, bgcolor ) {
	var element = document.getElementById( "jDayP" + evntIdx );
	element.style.backgroundColor = bgcolor;
//	element.style.background = "url(files/news/PostItNote_Header_Green.gif)";
}

function jOpenEvent( evntIdx, isClick ) {
	var element = document.getElementById( "jDisplayEvent" );
	var evnt = JCalendar[ evntIdx ];
	var colors = jLookupColor( evnt[7] );
	var html, d, s, t, x, y;

	if( jEventClicked && !isClick ) {
		return;			// do not open another event until the clicked one is closed
	}
	jEventClicked = isClick;
	element.style.display = 'none';
	if( colors[1] != "transparent" ) {
		element.style.backgroundColor = colors[1];
	} else {
		element.style.backgroundColor = "#FFFFCC";
	}
	element.style.color = colors[0];
	// Each element is an array of: <date>, <startTime>, <endTime>, <title>, <info>, <link>, <room>, <color>, [ <index> ]
	html = '<span align="center"><b>' + evnt[3] + '</b>';
	d = evnt[0];
	html += '<br><b>' + jMonth[ parseInt( d.substr( 4, 2 ), 10 ) - 1 ] + ' ' + parseInt( d.substr( 6, 2 ), 10 )
			+ ', ' + d.substring( 0, 4 ) + '</b>';
	if( evnt[1] != "" ) {
		html += '<br>' + jFormatTime( evnt[1], true );
		// add end time, if any
		if( evnt[2] != "" ) {
			html += ' &ndash; ' + jFormatTime( evnt[2], true );
		}
	}
	html += '</span>';
	if( evnt[6] != "" ) {
		html += '<br>Room: ' + evnt[6];
	}
	html += '<br>&nbsp;<br>' + evnt[4];
	html += '<br /><div align="center">';
	if( evnt[5] != "" ) {
		html += '<a href="' + evnt[5] + '"><img src="/files/news/link.gif" height="20" alt="Click Link" /></a>&nbsp;&nbsp;&nbsp;&nbsp;';
	}
	html += '<a href="javascript:jCloseEvent(true);"><img src="/files/news/close.gif" height="20" alt="Close" /></a>';
	html += '</div>';
	element.innerHTML = html;
	element.style.position = "absolute";
	
	// get event position
	x = 0;
	y = 0;
	var p = document.getElementById( "jDayP" + evntIdx )
	y = p.offsetHeight;
	for( ; p; p = p.offsetParent ) {
		if( p.style.position != 'absolute' ) {
			x += p.offsetLeft;
			y += p.offsetTop;
		}
	}
	d = evnt[0];
	var date = new Date( parseInt( d.substr( 0, 4 ), 10 ), parseInt( d.substr( 4, 2 ), 10 ) - 1, parseInt( d.substr( 6, 2 ), 10 ) );
	if( date.getDay() >= 6 ) {
		x -= jDisplayEventWidth - jDayWidth;
	}
	element.style.left = x + 'px';
	element.style.top  = y + 'px';
	element.style.width  = jDisplayEventWidth + 'px';
	element.style.display  = "block";
}

function jCloseEvent( isClick ) {
	var element = document.getElementById( "jDisplayEvent" );
	if( jEventClicked && !isClick ) {
		return;			// do not close event until the clicked one is closed
	}
	element.style.display = 'none';
	element.innerHTML = "&nbsp;";
	jEventClicked = false;
}

function jZeroPad(num,count)
{
	var numZeropad = num + '';
	while(numZeropad.length < count) {
		numZeropad = "0" + numZeropad;
	}
	return numZeropad;
}

var jColorArray = new Array();	// color array. Items: <text_color>, <background_color>, <hightlight_color>, <color_name>
jColorArray[0] = new Array( "#000000", "transparent", "#FFFFCC", "none" );	// 0 = default
jColorArray[1] = new Array( "#000000", "#FF3333", "#FF6666", "red" );
jColorArray[2] = new Array( "#000000", "#FFCCCC", "#FFFFFF", "pink" );
jColorArray[3] = new Array( "#000000", "#FFFF33", "#FFFF99", "yellow" );
jColorArray[4] = new Array( "#000000", "#FF9999", "#FFCCCC", "rose" );
jColorArray[5] = new Array( "#000000", "#66FFFF", "#99FFFF", "blue" );
jColorArray[6] = new Array( "#FFFFFF", "#0000CC", "#0000FF", "dark blue" );
jColorArray[7] = new Array( "#000000", "#66FF66", "#99FF99", "green" );
jColorArray[8] = new Array( "#000000", "#FF99FF", "#FFCCFF", "purple" );
jColorArray[9] = new Array( "#FFFFFF", "#996600", "#CC9900", "brown" );

function jLookupColor( str ) {
	var i, len;
	len = jColorArray.length;
	for( i = 1; i< len; i++ ) {		// skip default color array
		if( jColorArray[i][3] == str ) {
			return( jColorArray[i] );
		}
	}
	return( jColorArray[0] );
}

function jCalGotoLink( url ) {
	document.location.href = url;
}

function jFormatTime( timestr, caps ) {
	var t, str, am;

	t = parseInt( timestr.substr( 0, 2 ), 10 );
	if( t < 12 ) {
		s = 'AM';
	} else {
		s = 'PM';
	}
	if( caps != true ) {
		s = s.toLowerCase();
	}
	if( t == 0 ) {
		t = 12;
	}
	if( t > 12 ) {
		t -= 12;
	}
	return( t + ':' + timestr.substr( 2, 2 ) + ' ' + s );
}