
HJ.YearlyAgenda = Class.create();

HJ.YearlyAgenda.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;
       // XXX: fixme: We don't load event data here because we don't 
       // actually do anything with events.  In the future, there probably
       // needs to be a way to query which dates actually have events
	   var tempDate = new Date(date.getTime());
	   tempDate.setMonth(0);
	   tempDate.setDate(1);
	   this.calendarTitle.innerHTML = tempDate.getFullYear();
	   
	   this.startTime = new Date(tempDate.getTime());
	   var dt = new Date();
	   dt.setFullYear(tempDate.getFullYear());
	   dt.setMonth(11);
	   dt.setDate(31);
	   this.endTime = dt;

	   
	   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.days[dt.getDay()] + " " + this.monthsShort[dt.getMonth()] + " " + dt.getDate() + ", " + date.getFullYear();
			  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 y = this.date.getFullYear();
	   y++;
	   this.date.setFullYear(y);
	   this.render(this.schedules, this.date);
	},
	
	previous: function() {
	   var y = this.date.getFullYear();
	   y--;
	   this.date.setFullYear(y);
	   this.render(this.schedules, this.date);
	}
	
});

