//the ajax xml object
var xmlhttp=null;
//the xml file that will be loaded into memory
//
//var toLoad = "/includes/js/aeCalendar.xml";
//var toLoad = "/entertainment/calendar/2007_11.xml";

var ldate = new Date();
//var toLoad =  "get_xml.php?t=event&f=/entertainment/calendar/"+ldate.getFullYear()+"_"+(ldate.getMonth()+1)+".xml";
var toLoad =  "/inc/services/get_xml?t=event&y="+ldate.getFullYear()+"&m="+(ldate.getMonth()+1);


var xmlDoc;
//the string that gets printed to the dynamic div
var strOutput = "";

var calendar_grid_div = "party_calendar";

var lBound = 0;
var uBound = 9;

var epm = 10;
var pages = 0;
var activePage = 1;
var paginate = "";

var prevDay = 0;
var prevMonth = 0;
var prevYear;


var lookHere = "/events/index.html";

var months = new Array("January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November","December");
var days = new Array("Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
daysInMonth=[31,28,31,30,31,30,31,31,30,31,30,31]

function loadCalendar() {
	new Ajax.Request(toLoad, {
		onSuccess: function(transport) {
			xmlDoc = transport.responseXML;
			processCalendar();
		}
	});	
}
function processCalendar() {
    try
    {
       var query = window.location.search.substring(1);
       var pair = query.split("=date");
       //alert('pair: '+pair);
       if(pair != '') {
	       var rawDate = pair[1].split("/");
	       //dayView(rawDate[0],rawDate[1],rawDate[2]);
	       drawCal(rawDate[1],rawDate[0]);
       } else {
           var today = new Date();
           var year = today.getFullYear();
           var month = today.getMonth()+1;
           var day = today.getDate();
           
           //dayView(year,month,day);
           drawCal(month,year,0);
       }

    } catch(e) { /* this shouldn't happen */
       var today = new Date();
       var year = today.getFullYear();
       var month = today.getMonth()+1;
       var day = today.getDate();
       
       //dayView(year,month,day);
       drawCal(month,year,0);
    }

}

function drawMonth(month,year,drawFirst) {
   document.getElementById(calendar_grid_div).innerHTML = "Loading. . .";
//   reloadXML("/inc/services/get_xml?t=event&y="+year+"&m="+month);
   drawCal(month,year,drawFirst);
}

// This function draws the calender for the given month and year
function drawCal(month,year,drawFirst) {
   //these 4 values hold the next and previous month and year with respect
   //to the provided month and year.
   var pMonth;
   var nMonth;
   var pYear = year;
   var nYear = year;
   
   //checking for calendar wrap around one year behind
   if(month != 1)
      pMonth = month-1;
   else
   {
      pMonth = 12;
      pYear--;  
   }
      
   //checking for calendar wrap around one year before
   if(month != 12)
   {
      nMonth = month;
      nMonth++;
   }
   else
   {
      nMonth=1
      nYear++;
   } 
   
   //generating the calendar header
   var calCode = "";
   calCode += '<table class="calendar" cellpadding="4" cellspacing="0">';
   calCode += '<tr><td class="noline"><a href="javascript:drawMonth('+pMonth+','+pYear+',1)">&#171;</a></td>';
   calCode += '<td class="noline" colspan="5"><strong>'+getMonth(month-1)+' '+year+'</strong></td>';
   calCode += '<td><a href="javascript:drawMonth('+nMonth+','+nYear+',0)">&#187;</a></td></tr>';
   calCode += '<tr><td class="days">Su</td><td class="days">M</td><td class="days">Tu</td><td class="days">W</td><td class="days">Th</td><td class="days">F</td><td class="days">Sa</td></tr><tr>';
   
   var daysThisMonth = getDaysInMonth(month,year);
   var weekDay = 0;
   var lastDay = getDaysInMonth(pMonth,pYear);
   
   //this loop is used to create the calendar
   for(var i = 1; i<=daysThisMonth; i++)
   {
      var currDate = new Date();
      currDate.setFullYear(year,month-1,i);
      day = currDate.getDay();
      
      //starting a new week
      if(weekDay == 7)
      {
         weekDay=0;
         calCode += '</tr><tr>';
      }
      
      //checking if the current weekDay in the loop matches with the current weekday of the month
      //if so print that day, else we print the previous month's date
      if(day==weekDay)
      {
         if(checkForEvent(year,month,currDate.getDate()))
         {
            calCode += '<td><a href="'+lookHere+'?date='+year+'/'+month+'/'+i+'">'+i+'</a></td>';
         }
         else
         {
            //calCode += '<td><a href="?date='+year+'/'+month+'/'+i+'">'+i+'</a></td>';
            calCode += '<td>'+i+'</td>';
         }
      }
      else
      {
         var dDay = weekDay;
         dDay++;
         var lastMonthDay = lastDay-(day-dDay);
         //if(checkForEvent(pYear,pMonth,lastMonthDay))
         //{
         //   calCode += '<td class="nextMonth"><a href="javascript:dayView('+pYear+','+pMonth+','+lastMonthDay+')">'+lastMonthDay+'</a></td>';
         //}
         //else
         //{
            calCode += '<td class="nextMonth">'+lastMonthDay+'</td>';
         //}
         i--;
      }
      
      //checking if it's the last day of the month, and if there is an incomplete
      //week that needs to be filled with the first j days of the next month
      if(i==daysThisMonth && weekDay!=6)
      {  
         var j = 1;
         while(weekDay!=6)
         {
            //if(checkForEvent(nYear,nMonth,j))
            //{
            //   calCode += '<td class="nextMonth"><a href="javascript:dayView('+nYear+','+nMonth+','+j+')">'+j+'</a></td>';
            //}
            //else
            //{
               calCode += '<td class="nextMonth">'+j+'</a></td>';
            //}
            weekDay++;
            j++;
         }
      }
      weekDay++;
   }
   calCode += '</table>';
   //calCode += '<br /><div class="moreLink"><NOBR><a href="/socialdatebook/31478.asp">SUBMIT YOUR EVENT &#xBB;</a></NOBR></div>';
   //calCode += '<br /><br />Sponsored by: <br /><img src="/images/graves_164px.gif">';

   $(calendar_grid_div).innerHTML = calCode;
   
}

function checkForEvent(year,month,day) {

    if(xmlDoc == null)
       return false;
         
    var startDates  = xmlDoc.getElementsByTagName("startDate");
    var endDates    = xmlDoc.getElementsByTagName("endDate");
     
    for(var i = 0; i < startDates.length; i++) {
       var syear  = startDates[i].attributes.getNamedItem("year").value;
       var smonth = startDates[i].attributes.getNamedItem("month").value;
       var sday   = startDates[i].attributes.getNamedItem("day").value;
       
       var eyear  = endDates[i].attributes.getNamedItem("year").value;
       var emonth = endDates[i].attributes.getNamedItem("month").value;
       var eday   = endDates[i].attributes.getNamedItem("day").value;
       
       var startDate = new Date();
       var endDate = new Date();
       var checkDate = new Date();
       
       startDate.setFullYear(syear,smonth-1,sday);
       endDate.setFullYear(eyear,emonth-1,eday);
       checkDate.setFullYear(year,month-1,day);
       
       if(syear == year && smonth == month && sday == day)
          return true;
       else if(eyear == year && emonth == month && eday == day)
          return true;
       else if(startDate < checkDate && checkDate < endDate)
          return true;
    }
    return false;
}

// this function returns the name of the month given a number
function getMonth(month) {   
   return months[month];
}

//this function returns the name of the weekday given a number
function getDay(day) {
   return days[day];
}

// this function retrieves the number of days in a month given an month and a year
// it also takes into account a leap year.

function getDaysInMonth(month,year) {
	if ((month-1==1)&&(year%4==0)&&((year%100!=0)||(year%400==0))) {
		return 29;
	} else {
		return daysInMonth[month-1];
	}
}

function reloadXML(url) {
//	alert('loading url: '+url);
	new Ajax.Request(url, {
		onSuccess: function(transport) {
			xmlDoc = transport.responseXML;
		}
	});	
}
