
function prepSchedule() {
	clearViewer();
	
	schedMenu="<table id='scheduleInterface' border=0 cellpadding=0 cellspacing=0>";
	schedMenu+="<tr><td id='gameDate'></td><td>&nbsp;";
//	schedMenu+="<!--<div id='jumpToDate' onMouseUp='displayCalendar()'>-->";
//	schedMenu+="<div id='jumpToDate'>Jump to date: <img src='apps/scheduleViewer/images/miniCalendar.gif' border=0  style='margin-bottom:-1px;'>";
//	schedMenu+="<div id='calendarContainer' style='position:absolute;'><!-- PRINT DYNAMIC CALENDAR FOR JUMP TO DATE --></div>";
//	schedMenu+="</div>";
	schedMenu+="</td></tr></table>";
	
	schedMenu+="<table id='jumpToWeekday' border=0 cellpadding=0 cellspacing=0><tr>";
	schedMenu+="<td id='prevWeek' class='weekJump' onMouseUp='jumpWeek(\"prevWeek\");'>&lt;&lt;</td>"; 		
	schedMenu+="<td id='monJump' class='scheduleDayJump' onMouseUp='jumpDay(1);'>Mon</td>";
	schedMenu+="<td id='tueJump' class='scheduleDayJump' onMouseUp='jumpDay(2);'>Tue</td>";
	schedMenu+="<td id='wedJump' class='scheduleDayJump' onMouseUp='jumpDay(3);'>Wed</td>";
	schedMenu+="<td id='thurJump' class='scheduleDayJump' onMouseUp='jumpDay(4);'>Thur</td>";
	schedMenu+="<td id='friJump' class='scheduleDayJump' onMouseUp='jumpDay(5);'>Fri</td>";
	schedMenu+="<td id='satJump' class='scheduleDayJump' onMouseUp='jumpDay(6);'>Sat</td>";
	schedMenu+="<td id='sunJump' class='scheduleDayJump' onMouseUp='jumpDay(7);'>Sun</td>";
	schedMenu+="<td id='nextWeek' class='weekJump' onMouseUp='jumpWeek(\"nextWeek\");'>&gt;&gt;</td>";
	
	schedMenu+="</tr></table></center>";
	schedMenu+="<div id='scheduleContainer'><!-- PRINT DYNAMIC GAME SCHEDULE FOR THE SELECTED DATE --></div>";
	$("#scheduleViewerContainer").html(schedMenu);
	scheduleDate=setScheduleDate();
	loadSchedule(scheduleDate);
}

function loadSchedule(gameDate) {
	$.post("apps/scheduleViewer/scripts/gatherData.php", 
		{ gameDate: gameDate },
		function(data){
			parseResponse(data)
		}, "json"
	);
}

function parseResponse(data) {
	// PULL DATA
	schedDate=data.date;
	games=data.games;
	jumpDays=["sunJump","monJump","tueJump","wedJump","thurJump","friJump","satJump"]

	// SET THE CLASS IN THE WEEKLY SCHEDULE FOR THE DAY
	$("#"+jumpDays[dateObj.getDay()]).addClass('scheduleCurrentDay');
	
	if(games.length==0) {
		schedTable="<b>No Games for this Date.</b>";
		$('#gameDate').html(schedDate);
		$('#scheduleContainer').html(schedTable);
		return;
	}
	
	division=games[0].division;
	
	// CREATE THE SCHEDULE TABLES
	r=0;
	schedTable="<table id='scheduleTable' border=0 cellpadding=0 cellspacing=0 width=100%><tr><td class='headerRowTeams'>"+division+"</td><td class='headerRow'>Time</td><td class='headerRow'>Field</td></tr>";
	for(i=0;i<games.length;i++) {
		game=games[i];
		if(game.division==division) {
			if( Math.floor(r/2) == (r/2)) 
				cssClass="oddRow"
			else
				cssClass="evenRow"
			schedTable+="<tr><td class='"+cssClass+"Teams'>"+game.home+" vs "
			
			// CHECK IF THE COMBINED STRING LENGTH OF THE 2 TEAMS IS ABOVE 27 CHARACTERS
			if( (game.home.length + game.away.length) >= 27 )
				schedTable+="<BR>";
			schedTable+=game.away+"</td><td class='"+cssClass+"'>"+game.time+"</td><td class='"+cssClass+"'>"+game.field+"</td></tr>";
			r++;
		} else {
			division=game.division;
			schedTable+="</table><table id='scheduleTable' border=0 cellpadding=0 cellspacing=0 width=100%><tr><td class='headerRowTeams'>"+division+"</td><td class='headerRow'>Time</td><td class='headerRow'>Field</td></tr>";
			schedTable+="<tr><td class='oddRowTeams'>"+game.home+" vs "
			// CHECK IF THE COMBINED STRING LENGTH OF THE 2 TEAMS IS ABOVE 27 CHARACTERS
			if( (game.home.length + game.away.length) >= 27 )
				schedTable+="<BR>";
			schedTable+=game.away+"</td><td class='oddRow'>"+game.time+"</td><td class='oddRow'>"+game.field+"</td></tr>";

			r=1;
		}
	}
	schedTable+="</table>";

	// PRINT THE DATA
	$('#gameDate').html(schedDate);
	$('#scheduleContainer').html(schedTable);

}	// function parseResponse

function setScheduleDate() {
	newDate=String(dateObj.getFullYear())
	if((dateObj.getMonth()+1) < 10)
		newDate+='0';
	newDate+=String(dateObj.getMonth()+1)
	if(dateObj.getDate() < 10 )
		newDate+='0';
	newDate+=String(dateObj.getDate());
	return newDate;
}

function jumpWeek(jump) {
	dateMod=0;
	if(jump=='prevWeek')
		dateMod=-604800000;
	else
		dateMod=604800000;	
	newTS=dateObj.getTime()+dateMod;
	dateObj.setTime(newTS);
	scheduleDate=setScheduleDate()
	
	loadSchedule(scheduleDate);
}

function jumpDay(jump) {
	// SET THE CLASS FOR THE CURRENT DAY
	$("#"+jumpDays[dateObj.getDay()]).removeClass('scheduleCurrentDay');
	
	if(dateObj.getDay()!=0)
		curDay=dateObj.getDay();
	else curDay=7;
	newDay=jump;
	diff=(newDay-curDay)*86400000;

	newTS=dateObj.getTime()+diff;
	dateObj.setTime(newTS);
	scheduleDate=setScheduleDate()
	
	loadSchedule(scheduleDate);
}

function displayCalendar(month) {
	if(month==null)
		month=dateObj.getMonth()
		
	monthNames=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	numDays=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	// get the first day of the month
	firstDayDate=new Date();
	firstDayDate.setFullYear(firstDayDate.getFullYear(),month,1);
	firstDay=firstDayDate.getDay()
	
	calendarTable="<table id=jumpCalendar border=0 cellpadding=0 cellspacing=0><tr>"
	calendarTable+="<td class=monthButton onMouseUp=\"switchMonth('prevMonth',"+ month +")\">&lt;&lt;</td>"
	calendarTable+="<td colspan=5 class=calendarTitle>"+ monthNames[month] +"</td>";
	calendarTable+="<td class=monthButton onMouseUp=\"switchMonth('nextMonth',"+ month +")\">&gt;&gt;</td></tr><tr>";
	calendarTable+="<td class=calendarDay>Su</td>";
	calendarTable+="<td class=calendarDay>M</td>";
	calendarTable+="<td class=calendarDay>T</td>";
	calendarTable+="<td class=calendarDay>W</td>";
	calendarTable+="<td class=calendarDay>Th</td>";
	calendarTable+="<td class=calendarDay>F</td>";
	calendarTable+="<td class=calendarDay>Sa</td></tr>";

	// position the first of the month in the appropriate day
	for(c=0;c<firstDay;c++) {
		calendarTable+="<td class=calendarCellEmpty>&nbsp;</td>";			
	}

	for(i=1;i<=numDays[month];i++) {
		// CREATE NEW ROW IF IT IS LAST DAY OF THE WEEK
		if( (c/7) == Math.floor(c/7)	)
			calendarTable+="</tr><tr>";
		calendarTable+="<td class=calendarCell>"+ i +"</td>";
		c++;
	}
	calendarTable+="</table>";
	
	// if the user has selected a month outside of the current year, stop user
	if(month<0) {
		calendarTable="<table id=jumpCalendar border=0 cellpadding=0 cellspacing=0><tr>"
		calendarTable+="<td class=monthButton onMouseUp=\"switchMonth('nextMonth',"+ month +")\"><b>BACK</b></td></tr>"
		calendarTable+="<tr><td class=calendarDay>"+month+" We are only displaying schedules for dates in "+ dateObj.getFullYear() +".</td></tr></table>";
	}
	if(month>11) {
		calendarTable="<table id=jumpCalendar border=0 cellpadding=0 cellspacing=0><tr>"
		calendarTable+="<td class=monthButton onMouseUp=\"switchMonth('prevMonth',"+ month +")\"><b>BACK</b></td></tr>"
		calendarTable+="<td class=calendarDay>We are only displaying schedules for dates in "+ dateObj.getFullYear() +".</td></tr></table>";
	}
	
	$('#calendarContainer').html(calendarTable);
}

function switchMonth(direction,month) {
	if(direction=="prevMonth")
		month--;
	else
		month++;

	displayCalendar(month)
}