
// This script was modified from one provided by:  Doug Lawson (dlawson@clark.net) 
// The original was downloaded from 
// The JavaScript Source!! http://javascript.internet.com

var calStartDOW = 0;


var calendarEvents = new Array();
var homePark = "Fenway Park";


// the calendarEvents are defined in index.html and based on an xml file we read in. The calendarEvents are indexed by their date string. that is
// calendarEvents["30 Jul 2005"] = new calendarEvent(...);

// this returns the calendarEvent for a given date string
function getEvent(dStr)
{ 
  return calendarEvents[dStr];
} 

// this is just a hack to get around a pointMapper bug

function ballpark(id,team,league,address,city,state,zip,link,lat,lon,rollover)
{
  this . id = id;		// the venue eg Fenway Park
  this . team = team;
  this . league = league;
  this . address = address;
  this . city = city;
  this . state = state;
  this . zip = zip;
  this . link = link;
  this . lat = lat;
  this . lon = lon;
  this . rollover = rollover;
}

pointDataMap = new Array();
   

// this is the callback that is invoked when a date is clicked on


function calendarCB(dStr)
{
  var gE = getEvent(dStr);
  var iinf = document.getElementById("itemInfo");
  var info = "Unable to find game info.";
  if (gE != null)
    { 
     var ven = gE.loc;	// this is the venue, eg Fenway Park so need to find corresponding cooords
     var pd = pointDataMap[ven];
     if (pd != null) {
         pm_highlight(pd.league,pd.id); 
         info = "<b>" + gE.subj + "<br/>" +
         dStr + "</b><br/>" +
         gE.tm + "<br/>" +
         gE.des + "<br/>"; 	// + "Venue >><br/>";
         setVenueInfo(pd.league,pd.id);
     } else {
     var vinf = document.getElementById("venueInfo");
     vinf.innerHTML = "";
     }
  }
  iinf.innerHTML = info;
}
	

// ABBREVIATED MONTH NAMES
// months[] is used internally for (among other things) compatibility with 
// javascript Date.parse() methods.  You should only have to change it if 
// your language variable changes. 
var months = new Array( "Jan","Feb","Mar", "Apr","May","Jun", "Jul","Aug","Sep","Oct", "Nov", "Dec");

// Long month names 
// longmonths is used for calendar titles. 
// 
var longmonths = new Array( "January", "February", "March","April", "May", "June","July", "August", "September","October", "November", "December" ); 

var leapdays = new Array(31,29,31, 30,31,30, 31,31,30, 31,30,31); 
var yeardays = new Array(31,28,31, 30,31,30, 31,31,30, 31,30,31); 

// the symbols to use at the top of the calendars
var dow = new Array("S","M","T","W","T","F","S"); 

// a global date object to speed things up a little, since 
// there are lots of places where we need today's date

var myDate = new Date();
 
// thisMonth and thisYear are used as static vars for 
// newCalendars() (for persistence between calls

var thisMonth = myDate.getMonth(); 
var thisYear  = myDate.getFullYear(); 
// Now, figure out the arguments the page was loaded with 
// and what they mean

function isLeapYear( year ){
  // is it leap year ? returns a boolean
  return ( (0 == (year%4)) && ( (0 != (year%100)) || (0 == (year%400)))); 
  // ie, if the year divides by 4, but not by 100 except when it divides by
  // 400, it is leap year
} 

function isValidDayOfMonth( day, month, year) 
{ 
  // determines whether a day is valid 
  // (ie, prevents Feb 30 from being processed) 
  // ( not used but left here for other pages that use 
  // this calendar, like the time-off request form ) 
  // 
  if (day <= 0) { return false; }
  if (isLeapYear(year)) { return (day <= leapdays[month])}
  return ( day <= yeardays[month]); 
} 

function getDateStr(year,mon,mday)
{ 
  // it the date on the list? 
  // the format is a little sensitive, you
  // have to make sure that 'mon' is
  // something that Date.parse() can 
  // understand 
  var dStr = mday + " " + mon + " " + year; 
  return dStr;
} 

function calendarString(myYear, myMonth, bday)
{ 
  var homeGame = false;
  var calStr = "";
  var gE;
  // creates a string which is the code for a calendar  for myMonth myYear. 
  // get a new date for the first day of the month the user is looking at
  var calDate = new Date( myYear,  myMonth, 1, 0,0,0,0 ); 
  // how many days are in the month ? 
  var mDays = isLeapYear(myYear) ? leapdays[myMonth]: yeardays[myMonth]; 
  var i = 0; 
  // what day of the week does the month start on? 
  var wkDay = calDate.getDay(); 
  var dateBgAtt = ""; 
  var dateHref = "";
  var hrefCloseTag = ""; 
  var boldTag = "";
  var unBoldTag = ""; 
  // if the function was called with the bday argument, then the 
  // caller wants a date written in bold 
  var useBold = arguments.length >= 3 ? true: false; 
  // the header for the calendar ( month and year ) 
  calStr = "<TABLE class='calendar'>\n" + "<TR class='calendarHeading'>" + "<TD COLSPAN='5'>";
  calStr = calStr + longmonths[myMonth] + "</TD><TD COLSPAN='2'>" + myYear + "</TD></TR>\n"; 
  // the abbreviations for days of the week into the top line of the calendar
  calStr = calStr + "<TR>"; 
  for(i=0; i< 7; i++) 
	 { calStr = calStr + "<TD class='calendarDays'>" + dow[((i+calStartDOW)%7)] + "</TD>"; }
  calStr = calStr + "</TR>\n" + "<TR>";
  // start the first line with blank spaces until we get to the first day of the month
  var rowCount = 0;
  for(i=0 ; i < ((7 - calStartDOW + wkDay)%7); i++) 
	 { calStr = calStr + "<TD class='calendarElement'> &nbsp;</TD>";	 }
  // since javascript doesn't do modulus on negative numbers, 
  // add 7 to anything that might be negative
  var cmdate = i - ((7 - calStartDOW + wkDay)%7); 
  // write the weekdays
  for( i=i; cmdate < mDays ; i++) 
	 {  
		// what is the date ? 
		cmdate++; 
	   // if we have reached the end of a week, start another one
		if(0 == (i%7)){ calStr = calStr + "</TR>\n<TR>"; rowCount=rowCount+1; } 
		// if the date is an event, set it in color
                   var dateStr = getDateStr(calDate.getFullYear(),months[calDate.getMonth()],cmdate);
                   gE = getEvent(dateStr);
                   if (gE != null) { 	 
                       if (gE.loc == homePark) {
                          dateBgAtt = "class='homeElement'"; }
                         else
			     dateBgAtt = "class='eventElement'"; 
			 dateHref = "<A class='calendarEvent' href='javascript:calendarCB("  + '"' + dateStr + '"' + ");'>";
			 hrefCloseTag = "</A>"; 
                                      }
		else  
		  {	 dateBgAtt = "class='calendarElement'"; 
			 dateHref = "";
			 hrefCloseTag = "";   		 
                                       } 

		// set the days off in bold
		if( ( useBold )  && (cmdate == bday))
		  { 	 boldTag = "<STRONG><EM>"; 
			 unBoldTag = "</EM></STRONG>"; } 
		else  { 
			 boldTag = ""; 
			 unBoldTag = "";   } 
		calStr = calStr + "<TD " + dateBgAtt +  " >" + dateHref + boldTag + cmdate + unBoldTag + hrefCloseTag + "</TD>";
	 }
  while(0 != (i%7)) { 	calStr = calStr + "<TD>   </TD>"; i++; }
  calStr = calStr + "</TR>";
   calStr = calStr + "<TR class='calendarHeading'><TD align='center' colspan=7><a class= 'calendarEvent' href='javascript:previousCalendar();'>&lt;&lt;</A> | <a class= 'calendarEvent' href='javascript:nextCalendar();'> &gt;&gt;</A></TD></TR>" ;
  calStr = calStr + "</TABLE>\n"; 
  return calStr;
} 

function drawCalendar(ourYear, ourMonth, boldDay)
{
  // serves as a wrapper for writeCalendar() 
  // if you want to do anything special, such as changing the colors, 
  // or opening a special window, you can do it here before calling writeCalendar(). 
  // arguments: 
  // ourYear, ourMonth - integers, typically returned by Date.getMonth() and Date.getDate()
  // boldDay 
  var myMonth = myDate.getMonth(); 
  var myYear  = myDate.getFullYear(); 
  // strictly speaking, all the arguments are optional. 
  // if you only want this month's calendar, called drawCalendar() with no args. 
  if(arguments.length >= 1) { myYear = ourYear; } 
  if(arguments.length >= 2) { myMonth = ourMonth; } 
  var calStr = calendarString(myYear, myMonth, boldDay);
  return calStr;
} 

// more static vars for cruising through the calendars  
var lastMonth = (thisMonth + 11) % 12; 
var lmYear    = (thisMonth == 0)? thisYear -1: thisYear; 
var nextMonth = (thisMonth +1)%12; 
var nmYear    = (thisMonth == 11)? thisYear + 1: thisYear;
 
function newCalendar()
{ 
  var myMonth = thisMonth; 
  var myYear  = thisYear; 
  // it's surprising how many coders don't use modular arithmetic to 
  // do date and time. Javascript doesn't understand negative numbers in
  // positive modulus, so we add 11 instead of subtracting 1. 
  lastMonth = (myMonth + 11) % 12; 
  lmYear    = (myMonth == 0)? myYear -1: myYear; 
  nextMonth = (myMonth +1)%12; 
  nmYear    = (myMonth == 11)? myYear + 1: myYear;
  var isThisMonth = ((myMonth == myDate.getMonth())&& (lmYear==myDate.getFullYear())); 
  var calEl = document.getElementById("eventCalendar");
  var calStr = drawCalendar(myYear, myMonth, (isThisMonth ? myDate.getDate():0)); 
 // alert(calStr);
  calEl.innerHTML = calStr;

} 

function previousCalendar()
{
  thisMonth = lastMonth;
  thisYear = lmYear;
  newCalendar();
}

function nextCalendar()
{
  thisMonth = nextMonth;
  thisYear = nmYear;
  newCalendar();
}


