// JavaScript Document
/*
PhyneasJavascriptCalendar
----------------------------
Description
      A javascript calendar that displays one month at a time and includes 
      controls to navigate forward and backward one month at a time
Rev   0
Date  20071130
Requires
      - A reference to PhynDom.js
*/
function PhyneasJavascriptCalendar(id, startYear, startMonth, startDay) {
  function goToNextMonth()
  {
    featuredDate.setMonth(featuredDate.getMonth()+1);
    rebuildHTML();
    return false;
  }
  this.goToNextMonth = goToNextMonth;
  function goToLastMonth()
  {
    featuredDate.setMonth(featuredDate.getMonth()-1);
    rebuildHTML();
    return false;
  }
  this.goToLastMonth = goToLastMonth;
  function rebuildHTML()
  {
    HTMLElement.removeChild(table);
    buildHTML();
  }
  this.getTable = function()
  {
    return table;
  }
  function buildHTML()
  {
    //creat a table for the calendar
    table = HTMLElement.createChildElement('table');
    //set the html id of the table element
    table.setAttribute('id', id + 'Table');
    //set the border, padding, and spacing of the table
    table.border = '0';
    table.cellPadding = '0';
    table.cellSpacing = '0';
    //set the createChildElement function of the table
    table.createChildElement = createChildElement;
    //create the table caption
    tableCaption = table.createChildElement('caption');
    //set the html id of the caption
    tableCaption.setAttribute('id', id + 'TableCaption');
    //create a text node to represent the current month the calendar is displaying
    currentMonthTextNode = tableCaption.createChildTextNode(" " + monthsFull[featuredDate.getMonth()] + " ");
    //create a text node to represent the current year the calendar is displaying
    currentYearTextNode = tableCaption.createChildTextNode(" " + featuredDate.getFullYear() + " ");
    //create a tbody, ie won't display the table without it
    table.tableBody = table.createChildElement('tbody');
    //add a row to the table that will store the headers of the days of the week
    dayHeader = table.tableBody.createChildElement("tr");
    dayHeader.setAttribute('id', id + "HeaderRow");
    //create an array of each cell in the row
    dayHeader.headerCells = new Array();
    for(var i = 0; i < 7; i++)
    {
      dayHeader.headerCells[i] = dayHeader.createChildElement('th');
      //create a text node in the cell that contains the abrev of the day of the week
      dayHeader.headerCells[i].textNode = dayHeader.headerCells[i].createChildTextNode(daysInitial[i]);
    }
    //create an array of rows for each week in the month
    weeks = new Array();
    for(var i = 0; i < 6; i++)
    {
      weeks[i] = table.tableBody.createChildElement('tr');
      weeks[i].days = new Array();
      for(var e = 0; e < 7; e++)
      {
        //add a cell for each day of each week
        weeks[i].days[e] = weeks[i].createChildElement('td');
        //add a text node to each day cell
        weeks[i].days[e].dayOfMonth = weeks[i].days[e].createChildTextNode('');
      }
    }
    //get a date object that represents the first of month currently being displayed by the calendar
    var firstOfTheMonth = new Date(featuredDate.getFullYear(), featuredDate.getMonth(), 1);
    var monthStartingPoint = firstOfTheMonth.getDay();
    weeks[0].days[monthStartingPoint].dayOfMonth.data = firstOfTheMonth.getDate();
    weeks[0].days[monthStartingPoint].dateObj = new Date();
    weeks[0].days[monthStartingPoint].dateObj.setFullYear(firstOfTheMonth.getFullYear(),firstOfTheMonth.getMonth(),firstOfTheMonth.getDate());
    for(var i = monthStartingPoint + 1; i < 7; i++)
    {
      firstOfTheMonth.setDate(firstOfTheMonth.getDate() + 1)
      weeks[0].days[i].dateObj = new Date();
      weeks[0].days[i].dateObj.setFullYear(firstOfTheMonth.getFullYear(),firstOfTheMonth.getMonth(),firstOfTheMonth.getDate());
      weeks[0].days[i].dayOfMonth.data = firstOfTheMonth.getDate();
      
    }
    for(var e = 1; e < 6; e++)
    {
      for(var i = 0; i < 7; i++)
      {
        firstOfTheMonth.setDate(firstOfTheMonth.getDate() + 1)
        if(firstOfTheMonth.getMonth() == featuredDate.getMonth())
        {
          weeks[e].days[i].dateObj = new Date();
          weeks[e].days[i].dateObj.setFullYear(firstOfTheMonth.getFullYear(),firstOfTheMonth.getMonth(),firstOfTheMonth.getDate());
          weeks[e].days[i].dayOfMonth.data = firstOfTheMonth.getDate();
        }
        else if(showOtherMonths)
        {
          weeks[e].days[i].dateObj = new Date();
          weeks[e].days[i].dateObj.setFullYear(firstOfTheMonth.getFullYear(),firstOfTheMonth.getMonth(),firstOfTheMonth.getDate());
          weeks[e].days[i].firstOfMonth.data = firstOfTheMonth.getDate();
        }
      }      
    }
    if(showOtherMonths)
    {
      firstOfTheMonth = new Date(featuredDate.getFullYear(), featuredDate.getMonth(), 1);
      for(var i = monthStartingPoint - 1; i > -1; i--)
      {
        firstOfTheMonth.setDate(firstOfTheMonth.getDate() - 1);
        weeks[0].days[i].dayOfMonth.data = firstOfTheMonth.getDate();
      }   
    }
    var lastMonth = new Date(featuredDate.getFullYear(), featuredDate.getMonth() - 1, 1);
    var nextMonth = new Date(featuredDate.getFullYear(), featuredDate.getMonth() + 1, 1);
    calendarFooter = table.tableBody.createChildElement("tr");
    calendarFooter.leftSideCell = calendarFooter.createChildElement("td");
    calendarFooter.leftSideCell.setAttribute('id', id + 'leftSideCell');
    calendarFooter.leftSideCell.colSpan = '3';
    previousMonthLink = calendarFooter.leftSideCell.createChildElement('a');
    previousMonthLink.setAttribute('id', id + 'previousMonthLink');
    previousMonthLink.setAttribute('href', '#');
    previousMonthLink.innerHTML = monthsAbrev[lastMonth.getMonth()];
    previousMonthLink.onclick = goToLastMonth;
    calendarFooter.rightSideCell = calendarFooter.createChildElement("td");
    //calendarFooter.rightSideCell.setAttribute('id', id + 'leftSideCell');
    calendarFooter.rightSideCell.id = id + 'rightSideCell';
    calendarFooter.rightSideCell.colSpan = '4';
    nextMonthLink = calendarFooter.rightSideCell.createChildElement('a');
    nextMonthLink.setAttribute('id', id + 'previousMonthLink');
    nextMonthLink.setAttribute('href', '#');
    nextMonthLink.innerHTML = monthsAbrev[nextMonth.getMonth()];
    nextMonthLink.onclick = goToNextMonth;
	
	calendarSecondFooter = table.tableBody.createChildElement("tr");
	calendarSecondFooter.setAttribute('id', id + 'SecondFooter'); 
	
	calendarSecondFooter.footCell = calendarSecondFooter.createChildElement("td");
	calendarSecondFooter.footCell.colSpan = '7';
	calendarSecondFooter.footCell.setAttribute('id', id + 'footCell');
	calendarSecondFooter.footCell.innerHTML = '<br>Click on a highlighted day to read about an event';
	
	
  }
  this.getPreviousMonthLink = function()
  {
    return previousMonthLink;
  }
  this.getNextMonthLink = function()
  {
    return nextMonthLink;
  }
  this.getHTMLElement = function()
  {
    return HTMLElement;
  }
  this.buildHTMLElement = function()
  {
    buildHTML();
  }
  this.setShowOtherMonths = function(v)
  {
    showOtherMonths = v;
  }
  this.getTable = function()
  {
    return table;
  }
  this.getWeeks = function()
  {
    return weeks;
  }
  this.getFeaturedDate = function()
  {
    return featuredDate;
  }
  
  //calendarSecondFooter = calendarFooter.createChildElement("td");
  //calendarSecondFooterText = calendarFooter.createChildElement("div");
  //calendarSecondFooterText.innerHTML = Hello;
  
  //initialize variables
  var featuredDate = new Date();
  var monthsFull = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  var monthsAbrev = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
  var daysInitial = ["S", "M", "T", "W", "T", "F", "S"];
  var showOtherMonths = true;
  var table;
  var weeks;
  var previousMonthLink;
  var nextMonthLink;
  var HTMLElement = document.createElement('div');
  HTMLElement.createChildElement = createChildElement;
  classSwapping('add', HTMLElement, 'PhyneasJavascriptCalendar');
  //if an id was passed in
  if(id)
  {
    //set initial calendar properties
    id = id;
    if(startYear)
    {
      featuredDate.setFullYear(startYear);
    }
    if(startMonth || startMonth == 0 || startMonth == "0")
    {
      featuredDate.setMonth(startMonth);
    }
    if(startDay)
    {
      featuredDate.setDate(startDay);
    }      
  }
}

