//Louis' viewport getter
function getViewportSize()
{
 var size = [0, 0];

 if (typeof window.innerWidth != 'undefined')
 {
   size = [
       window.innerWidth,
       window.innerHeight
   ];
 }
 else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
   size = [
       document.documentElement.clientWidth,
       document.documentElement.clientHeight
   ];
 }
 else
 {
   size = [
       document.getElementsByTagName('body')[0].clientWidth,
       document.getElementsByTagName('body')[0].clientHeight
   ];
 }

 return size;
}

// JavaScript Document
//requires a reference to PhyneasJavascriptCalendar
var detailLink = '#';
var overDate;
function PhyneasJavascriptEventCalendar(id, startYear, startMonth, startDay)
{
  var pjc = new PhyneasJavascriptCalendar(id, startYear, startMonth, startDay);
  var HTMLElement = pjc.getHTMLElement();
  var table;
  var weeks;
  var featuredDate;
  var previousMonthLink;
  var nextMonthLink;
  var detailPopup;
  this.setShowOtherMonths = pjc.setShowOtherMonths;
  this.buildHTMLElement = buildHTML;
  this.getHTMLElement = pjc.getHTMLElement;
  this.setDetailLink = function (v)
  {
    detailLink = v;
  }
  function markDaysWithEvents()
  {
    var ajaxHandler = getHTTPRequest();
    if(ajaxHandler)
    {
      ajaxHandler.onreadystatechange = function()
      {
        if(ajaxHandler.readyState==4)
        {
          var responseText = ajaxHandler.responseText;
          var dateEvents = new Array();
          if(responseText)
          {
            eval(responseText);
            //alert(responseText);
          }
          if(dateEvents.length > 0)
          {
            if(!weeks)
            {
              weeks = pjc.getWeeks();
            }
            for(var i = 0; i < dateEvents.length; i++)
            {
              for(var e = 0; weeks[e]; e++)
              {
                for(var a = 0; weeks[e].days[a]; a++)
                {
                  if(weeks[e].days[a].dateObj)
                  {
                    if(weeks[e].days[a].dateObj.getDate() == dateEvents[i].dateObj.getDate())
                    {
                      classSwapping('add', weeks[e].days[a], 'PhyneasDateHasEvents');
                      weeks[e].days[a].onmouseover = function(e)
                      {
                        document.body.style.cursor = 'pointer';
                      	if (!e) var e = window.event;
                      	if (e.pageX || e.pageY) 	{
                      		posx = e.pageX;
                      		posy = e.pageY;
                      	}
                      	else if (e.clientX || e.clientY) 	{
                      		posx = e.clientX + document.body.scrollLeft
                      			+ document.documentElement.scrollLeft;
                      		posy = e.clientY + document.body.scrollTop
                      			+ document.documentElement.scrollTop;
                      	}
                        //overDate = this.dateObj;
                        //popItUp();
//-------------------------------------------------------------------------------------------
                        var currentDate = this.dateObj;
                        var fullYear;
                        var closePopUp;
                        var detailsPopUpHeading;
                        var detailsPopUpEvents = new Array();
                        var closePopUp;
                        var body;
                        var ajaxHandler = getHTTPRequest();
						var size = getViewportSize();
                        if(ajaxHandler)
                        {
                          ajaxHandler.onreadystatechange = function()
                          {
                            if(ajaxHandler.readyState==4)
                            {
                              var popUpVars = ajaxHandler.responseText;
                              var dateEvents = new Array();
                              if(popUpVars)
                              {
                                eval(popUpVars);
                              }
                              body = document.getElementsByTagName('body');

                              if(detailsPopUp)
                              {
                                if(detailsPopUp.parentNode)
                                {
                                    detailsPopUp.parentNode.removeChild(detailsPopUp);
                                }
                              }
                              detailsPopUp = document.createElement('div');
                              detailsPopUp.id = 'PhyneasEventPopUp';
                              classSwapping('add', detailsPopUp, 'PhyneaseEventPopUp');
                              detailsPopUp.createChildElement = createChildElement;
                              /*
                              detailsPopUp.onmousemove = function(e)
                              {
                              	if (!e) var e = window.event;
                              	if (e.pageX || e.pageY) 	{
                              		var posx = e.pageX;
                              		var posy = e.pageY;
                              	}
                              	else if (e.clientX || e.clientY) 	{
                              		posx = e.clientX + document.body.scrollLeft
                              			+ document.documentElement.scrollLeft;
                              		posy = e.clientY + document.body.scrollTop
                              			+ document.documentElement.scrollTop;
                              	}
                              	var removeIt = false;
                              	if(posx < parseInt(detailsPopUp.style.left) + 3)
                              	{
                                  removeIt = true;
                                }
                                else if(posy < parseInt(detailsPopUp.style.top) + 3)
                                {
                                  removeIt = true;
                                }
                                else if(posx > (parseInt(detailsPopUp.style.left) + detailsPopUp.clientWidth - 3))
                                {
                                  removeIt = true;
                                }
                                else if(posy > (parseInt(detailsPopUp.style.top) + detailsPopUp.clientHeight - 3))
                                {
                                  removeIt = true;
                                }
                                if(removeIt)
                                {
                                  body[0].removeChild(detailsPopUp);
                                  appended = false;
                                }
                              }
                              if(appended)
                              {
                                body[0].removeChild(detailsPopUp);
                                appended = false;
                              }
                              */
                              if(dateEvents.length > 0)
                              {
                                while(detailsPopUp.hasChildNodes())
                                {
                                  detailsPopUp.removeChild(detailsPopUp.firstChild);
                                }
                                detailsPopUpHeading = detailsPopUp.createChildElement('h6');
                                classSwapping('add', detailsPopUpHeading, 'PhyneasPopUpHeader');
                                var fullYear = new String(currentDate.getFullYear());
                                var theDate = new String(currentDate.getDate());
                                if(theDate.length == 1)
                                {
                                  theDate = '0' + theDate;
                                }
                                detailsPopUpHeading.innerHTML = (currentDate.getMonth() + 1) + '.' + theDate + '.' + fullYear.substr(2, 2);
                                for(var i = 0; i < dateEvents.length; i++)
                                {
                                  var theHour;
                                  var theMinute;
                                  var ampm;
                                  var theEndHour;
                                  var theEndMinute;
                                  var endAmpm;
                                  var startTime = '';
                                  var endTime = '';
                                  if(dateEvents[i].dateObj.includeStartTime)
                                  {
                                    theHour = dateEvents[i].dateObj.getHours();
                                    if(theHour < 12)
                                    {
                                      ampm = "am";
                                    }
                                    else
                                    {
                                      ampm = "pm"
                                    }
                                    if(theHour < 1)
                                    {
                                      theHour = 12;
                                    }
                                    else if(theHour > 12)
                                    {
                                      theHour = theHour - 12;
                                    }
                                    var theMinute = new String(dateEvents[i].dateObj.getMinutes());
                                    theMinute = theMinute.checkFor2Char();
                                    startTime = (theHour + ':' + theMinute + ' ' + ampm);
                                  }
                                  if(dateEvents[i].dateObj.endHour)
                                  {
                                    theEndHour = dateEvents[i].dateObj.endHour;
                                    if(theEndHour < 12)
                                    {
                                      endAmpm = "am";
                                    }
                                    else
                                    {
                                      endAmpm = "pm"
                                    }
                                    if(theEndHour < 1)
                                    {
                                      theEndHour = 12;
                                    }
                                    else if(theEndHour > 12)
                                    {
                                      theEndHour = theEndHour - 12;
                                    }
                                    theEndMinute = dateEvents[i].dateObj.endMinute;
                                    endTime = (theEndHour + ':' + theEndMinute + ' ' + endAmpm);
                                  }
                                  var timeDisplay = '';
                                  if(startTime)
                                  {
                                    timeDisplay = startTime;
                                    if(endTime)
                                    {
                                      timeDisplay = timeDisplay + ' - ' + endTime;
                                    }
                                  }
                                  detailsPopUpEvents[i] = detailsPopUp.createChildElement('p');
                                  if(timeDisplay)
                                  {
                                    timeDisplay = '<strong>' + timeDisplay + '</strong><br />';
                                    detailsPopUpEvents[i].innerHTML = timeDisplay + dateEvents[i];
                                  }
                                  else
                                  {
                                    detailsPopUpEvents[i].innerHTML = dateEvents[i];
                                  }

                                  classSwapping('add', detailsPopUpEvents[i], 'PhyneasPopUpEventDescription');
                                }
                                detailsPopUpEvents[detailsPopUpEvents.length - 1].style.borderWidth = '0px';
                                detailsPopUpEvents[detailsPopUpEvents.length - 1].style.paddingBottom = '0px';
                                detailsPopUp.style.position = 'absolute';
                                if((size[0]-posx) < 210)
                                {
                									detailsPopUp.style.left = size[0]- 210;
                								}
                								else
                                {
                									detailsPopUp.style.left = posx + 15;
                								}
                                detailsPopUp.style.top = posy + 12;
                                body[0].appendChild(detailsPopUp);
                                var theMonth = new String((currentDate.getMonth()+1));
                                theMonth = theMonth.checkFor2Char();
                                var theDay = new String(currentDate.getDate());
                                theDay = theDay.checkFor2Char();
                                detailsPopUp.onclick = function()
                                {
                                  document.location = detailLink + '?calendar_day=' + currentDate.getFullYear() + '-' + theMonth + '-' + theDay;
                                }
                              }
                            }
                          }
                          var theDate = new String(currentDate.getDate());
                          if(theDate.length == 1)
                          {
                            theDate = '0' + theDate;
                          }
                          var theMonth = new String(currentDate.getMonth() + 1);
                          theMonth = theMonth.checkFor2Char();
                          ajaxHandler.open("GET","behavior/javascript/PhyneasJavascriptEventCalendar/ajax_appointments.php?event_date=" + currentDate.getFullYear() + '-' + theMonth + '-' + theDate,true);
                          ajaxHandler.send(null);
                        }
//----------------------------------------------------------------------------------------
                      }
                      
                      weeks[e].days[a].onmouseout = function()
                      {
                        document.body.style.cursor = 'default';
                        if(detailsPopUp)
                        {
                          if(detailsPopUp.parentNode)
                          {
                            detailsPopUp.parentNode.removeChild(detailsPopUp);
                          }
                        }
                      }
                      
                    }
                  }
                }
              }
            }
          }
        }
      }
      var theMonth = new String(featuredDate.getMonth() + 1);
      theMonth = theMonth.checkFor2Char();
      ajaxHandler.open("GET","behavior/javascript/PhyneasJavascriptEventCalendar/ajax_appointments.php?event_date=" + featuredDate.getFullYear() + '-' + theMonth,true);
      ajaxHandler.send(null);
    }
  }
  function eventCalendarBuild()
  {
    featuredDate = pjc.getFeaturedDate();
    previousMonthLink = pjc.getPreviousMonthLink();
    previousMonthLink.onclick = goToLastMonth;
    nextMonthLink = pjc.getNextMonthLink();
    nextMonthLink.onclick = goToNextMonth;
    weeks = pjc.getWeeks();
    for(var i = 0; weeks[i]; i++)
    {
      for(var e = 0; weeks[i].days[e]; e++)
      {
        weeks[i].days[e].onmouseover = function()
        {
          document.body.style.cursor = 'pointer';
        }
        weeks[i].days[e].onmouseout = function()
        {
          document.body.style.cursor = 'default';
        }
        if(weeks[i].days[e].dateObj)
        {
          if(weeks[i].days[e].dateObj.getDate() == featuredDate.getDate() && weeks[i].days[e].dateObj.getMonth() == featuredDate.getMonth() )
          {
            classSwapping('add', weeks[i].days[e], 'PhyneasCurrentDate');
          }
          var todaysDate = new Date();
          if(weeks[i].days[e].dateObj.getDate() == todaysDate.getDate() && weeks[i].days[e].dateObj.getMonth() == todaysDate.getMonth() && weeks[i].days[e].dateObj.getFullYear() == todaysDate.getFullYear())
          {
            classSwapping('add', weeks[i].days[e], 'PhyneasTodaysDate');
          }
          var aInnerText = weeks[i].days[e].dayOfMonth.data;
          weeks[i].days[e].removeChild(weeks[i].days[e].dayOfMonth);


/*---------------------------------------------------------------------------------------
          weeks[i].days[e].dayOfMonth = weeks[i].days[e].createChildElement('a');
          var theDate = new String(weeks[i].days[e].dateObj.getDate());
          if(theDate.length == 1)
          {
            theDate = '0' + theDate;
          }
          var theMonth = new String((weeks[i].days[e].dateObj.getMonth() +1));
          theMonth = theMonth.checkFor2Char();
          weeks[i].days[e].dayOfMonth.setAttribute
          (
            'href', 
            detailLink + '?calendar_day=' + 
              weeks[i].days[e].dateObj.getFullYear() + '-' +
              theMonth + '-' +
              theDate
          );
          weeks[i].days[e].dayOfMonth.innerHTML = aInnerText;
-------------------------------------------------------------------------------*/
          var theDate = new String(weeks[i].days[e].dateObj.getDate());
          if(theDate.length == 1)
          {
            theDate = '0' + theDate;
          }
          var theMonth = new String((weeks[i].days[e].dateObj.getMonth() +1));
          theMonth = theMonth.checkFor2Char();
          weeks[i].days[e].theMonth = theMonth;
          weeks[i].days[e].theDate = theDate;
          weeks[i].days[e].onclick = function()
          {
            document.location.href = detailLink + '?calendar_day=' + this.dateObj.getFullYear() + '-' + this.theMonth + '-' + this.theDate;
          }
          weeks[i].days[e].dayOfMonth = weeks[i].days[e].createChildTextNode(aInnerText);
          /*
          weeks[i].days[e].onmouseover = function(e)
          {
          	if (!e) var e = window.event;
          	if (e.pageX || e.pageY) 	{
          		posx = e.pageX;
          		posy = e.pageY;
          	}
          	else if (e.clientX || e.clientY) 	{
          		posx = e.clientX + document.body.scrollLeft
          			+ document.documentElement.scrollLeft;
          		posy = e.clientY + document.body.scrollTop
          			+ document.documentElement.scrollTop;
          	}
            overDate = this.dateObj;
            popItUp();
          }
          weeks[i].days[e].onmouseout = function()
          {
            overDate = '';
          }
          */
        }
      }
    }
    markDaysWithEvents();
  }
  function buildHTML()
  {
    var theBod = document.getElementsByTagName('body');
    var theBod = theBod[0];
    //theBod.innerHTML = theBod.innerHTML + '1';
    pjc.buildHTMLElement();
    //theBod.innerHTML = theBod.innerHTML + '2';
    eventCalendarBuild();
    //theBod.innerHTML = theBod.innerHTML + '3';
    table = pjc.getTable();
    //theBod.innerHTML = theBod.innerHTML + '4';
    table.onmouseout = function()
    {
      if(detailsPopUp)
      {
        if(detailsPopUp.parentNode)
        {
            detailsPopUp.parentNode.removeChild(detailsPopUp);
        }
      }
    }
  }
  function goToNextMonth()
  {
    if(switchMonth)
    {
      featuredDate.setMonth(featuredDate.getMonth()+1);
      var nextMonth = new String((featuredDate.getMonth()+1));
      nextMonth = nextMonth.checkFor2Char();
      var nextMonthDay = new String(featuredDate.getDate());
      nextMonthDay = nextMonthDay.checkFor2Char();
      document.location = detailLink + '?calendar_day=' + featuredDate.getFullYear() + '-' + nextMonth + '-' + nextMonthDay;
    }
    else
    {
      pjc.goToNextMonth();
      eventCalendarBuild();
      return false;
    }
  }
  function goToLastMonth()
  {
    if(switchMonth)
    {
      featuredDate.setMonth(featuredDate.getMonth()-1);
      var nextMonth = new String((featuredDate.getMonth()+1));
      nextMonth = nextMonth.checkFor2Char();
      var nextMonthDay = new String(featuredDate.getDate());
      nextMonthDay = nextMonthDay.checkFor2Char();
      document.location = detailLink + '?calendar_day=' + featuredDate.getFullYear() + '-' + nextMonth + '-' + nextMonthDay;
    }
    else
    {
      pjc.goToLastMonth();
      eventCalendarBuild();
      return false;
    }
  }
}
var detailsPopUp;
var posx = 0;
var posy = 0;

