
HJ.MonthlyAgenda = Class.create();

HJ.MonthlyAgenda.prototype = Object.extend(new HJ.Agenda(), {

	initialize: function(calendarTitle, calendarArea, calendarHeader, allDayEvents) {
	    this.calendarTitle = calendarTitle;
		this.calendarArea = calendarArea;
	    this.calendarHeader = calendarHeader;
	    this.allDayEvents = allDayEvents;
	    
	    this.table = document.createElement("table");
	    this.table.onclick = this.tableClicked.bindAsEventListener(this);
	},

	cleanup: function() {
	   this.table.onclick = null;
	   this.table.style.display = "none";
	   this.calendarArea.appendChild(this.table);
	},
	
	render: function(schedules, date) {
        app.showBusy();
	   this.schedules = schedules;
	   this.date = date;
       if (!app.schedule.loadEventData(this.date)) {
       	// fetching data from the backend, do not render
       	return false;
       }

	   var tempDate = new Date(date.getTime());
	   tempDate.setDate(1);
	   tempDate.setHours(0,0,0,0);
	   this.calendarTitle.innerHTML = this.months[tempDate.getMonth()] + " " + tempDate.getFullYear();
	   
	   this.startTime = tempDate;
	   
	   var e;
	   var dt; 
	   var i = 28;
	   var m = this.date.getMonth();
	   dt = new Date(this.startTime.getTime());
	   while(true) {
	      dt.setDate(i);
	      if (dt.getMonth() != m) {
	         break;
	      }
	      i++;
	   }
	   if (i > 31) {
	      i = 31;
	   }
	   this.endTime = new Date(this.startTime.getTime());
	   this.endTime.setDate(i);
	   
	   this.calendarTitle.innerHTML = this.months[date.getMonth()] + " " + date.getFullYear();

	   
	   removeAll(this.calendarArea);
	   removeAll(this.calendarHeader);
	   this.calendarArea.className = 'calDayAgenda';
	   this.allDayEvents.style.display = "none";
	   
	   var tbl = this.table;
	   //tbl.style.width = (this.calendarArea.offsetWidth - 23) + "px";
	   removeAll(tbl);
	   tbl.cellSpacing = "2";
	   this.calendarArea.appendChild(tbl);
	   var tbody = document.createElement('tbody');
	   tbl.appendChild(tbody);
	   
	   var dt1 = new Date(this.startTime.getTime());
	   while(dt1.getTime() <= this.endTime.getTime()) {
	     var dt2 = new Date(dt1.getTime());
	     dt2.setDate(dt2.getDate() + 1);
	     
	     var t1 = dt1.getTime();
	     var t2 = dt2.getTime();
	   
	     var eventList = this.getVisibleEventsList(dt1, dt1, dt2, true);
	   
		 for (var i=0; i<eventList.length; i++) {
			  var o = eventList[i];
		      
			  var tr = document.createElement("tr");
			  tbody.appendChild(tr);
		      
			  var td = document.createElement("td");
			  var dt = o.actualDate;
			  if (dt == null) {
			     var s = parseInt(o.event["startDT"]);
			     dt = new Date(s);
			  }
			  td.innerHTML = this.daysLong[dt.getDay()] + " " + this.monthsShort[dt.getMonth()] + " " + dt.getDate();
			  td.style.width = "20%";
			  tr.appendChild(td);
		      
			  td = this.getTimeCell(o.schedule, o.event);
			  td.style.width = "20%";
			  tr.appendChild(td);
		      
			  td = this.getAgendaEventCell(o.schedule, o.scindex, o.event, o.eventindex, o.actualDate);
			  td.style.width = "60%";
			  tr.appendChild(td);
		 }
		 dt1 = dt2;
	   }
        app.endBusy();
	},
	
	next: function() {
	   var m = this.date.getMonth();
	   var y = this.date.getFullYear();
	   m += 1;
	   if (m > 11) {
	     m = 0;
	     y++;
	   }
	   this.date.setFullYear(y);
	   this.date.setMonth(m);
	   this.render(this.schedules, this.date);
	},
	
	previous: function() {
	   var m = this.date.getMonth();
	   var y = this.date.getFullYear();
	   m -= 1;
	   if (m < 0) {
	     m = 11;
	     y--;
	   }
	   this.date.setFullYear(y);
	   this.date.setMonth(m);
	   this.render(this.schedules, this.date);
	}
	
});

