
HJ.WeeklyAgenda = Class.create();

HJ.WeeklyAgenda.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;
       }

	   this.startTime = new Date(this.date.getTime());
	   
	   var day = date.getDay();
	   
	   this.startTime.setDate(this.startTime.getDate() - day);
	   
	   this.endTime = new Date(this.startTime.getTime());
	   this.endTime.setDate(this.endTime.getDate() + 7);
	   
	   var dt = new Date(this.startTime.getTime());
	   dt.setDate(dt.getDate() + 6);
	   
	   this.calendarTitle.innerHTML = "Week of "+this.months[this.startTime.getMonth()] + " " + this.startTime.getDate() + ", " + this.startTime.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());
	   for (var j=0; j<7; j++) {
	      var dt2 = new Date(dt1.getTime());
	      dt2.setDate(dt2.getDate() + 1);
	      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 t = this.date;
	   t.setDate(t.getDate() + 7);
	   this.render(this.schedules, t);
	},
	
	previous: function() {
	   var t = this.date;
	   t.setDate(t.getDate() - 7);
	   this.render(this.schedules, t);
	}
	
});

