// JavaScript Document
var arrMonths=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var arrMonthLength=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var schedule=new Object();
	schedule['9/7/08']=["Greenlee"]; 
	schedule['9/8/08']=["Hello","Hi"]; 
	schedule['9/9/08']=
	["4:45 PM | Boys Varsity/JV Tennis| Plymouth | Away",'5:00 PM | Girls JV Soccer | Concord | Home','5:00 PM | Girls JV Soccer | Concord | Home','5:00 PM | Girls Freshman Volleyball | Goshen/Clay | Away',"5:30 PM | Boys/Girls Cross Country | Concord/Northridge | Home","6:00 PM | Girls Varsity/JV Volleyball | South Bend Riley | Home","6:45 PM | Girls Varsity Soccer | Concord | Home "
	]; 
	schedule['9/10/08']=["No Events Scheduled"]; 
var scheduleDefault=["No Events Scheduled "]; 

registerClassConstructor('calendar',function(){
	var self=this;
	self.rows=new Array();
	self.cols=new Array();
	self.selected=new Date();
	self.firstDay=new Date();
	self.firstDay.setDate(1);
	self.month=self.firstDay.getMonth();
	self.year=self.firstDay.getFullYear();
	self.monthBegin=self.firstDay.getDay();
	
	self.btnDecMonth=document.createElement('input');
		self.btnDecMonth.value='<';
	self.btnIncMonth=document.createElement('input');
		self.btnIncMonth.value='>';
	self.btnIncMonth.type=self.btnDecMonth.type='button';
	self.btnIncMonth.style.background=self.btnDecMonth.style.background='none';
	
	self.element.appendChild(self.titleTbl=document.createElement('table'));
		self.titleTbl.className='calendarTitle';
		self.titleTbl.appendChild(self.titleTbody=document.createElement('tbody'));
		self.titleTbody.appendChild(self.titleTr=document.createElement('tr'));
		self.titleTr.appendChild(self.titleTd1=document.createElement('td'));
		self.titleTr.appendChild(self.titleTd2=document.createElement('td'));
		self.titleTr.appendChild(self.titleTd3=document.createElement('td'));
		self.titleTd1.appendChild(self.btnDecMonth);
		self.titleTd2.appendChild(self.monthNode=document.createTextNode(arrMonths[self.month]+" "+self.year));
		self.titleTd3.appendChild(self.btnIncMonth);
	self.element.appendChild(self.dateDiv=document.createElement('div'));
	
	self.changeMonth=function(m){
		self.selected.setMonth(m);
		self.firstDay.setMonth(m);
		self.month=m;
		self.monthBegin=self.firstDay.getDay();
	}
	
	self.btnDecMonth.onclick=function(){
		self.changeMonth((self.selected.getMonth()+11)%12);
		if(self.month==11) self.selected.setFullYear(self.year=self.selected.getFullYear()-1);
		self.monthNode.nodeValue=arrMonths[self.month]+" "+self.year;
		self.buildDayTable();
	}
	
	self.btnIncMonth.onclick=function(){
		self.changeMonth((self.selected.getMonth()+1)%12);
		if(self.selected.getMonth()==0) self.selected.setFullYear(self.year=self.selected.getFullYear()+1);
		self.monthNode.nodeValue=arrMonths[self.month]+" "+self.year;
		self.buildDayTable();
	}
	
	self.buildDayTable=function(){
//		if(self.cols.length) for(var c in self.cols) self.cols[c].parentNode.removeChild(self.cols[c]);//self.cols[c].parentNode.removeChild(self.cols[c]);
		
		if(self.rows.length) for(var r in self.rows) self.rows[r].parentNode.removeChild(self.rows[r]);
		
		self.rows=new Array();
		self.cols=new Array();
		var tmp;
		var sch;
		
		for(var w=0;w<6;w++){
			self.rows.push(document.createElement('div'));
			if(isFF){
				self.rows[w].appendChild(tmp=document.createElement('div'));
				tmp.style.width=tmp.style.height="17px";
				tmp.style.padding='1px';
				tmp.style.visibility='hidden';
				tmp.appendChild(document.createTextNode(' '));
			}
			self.cols.push(new Array());
			for(var d=0;d<7;d++){
				//window.status=w+" "+d+" "+self.monthBegin;
				self.cols[w].push(document.createElement('div'));
				self.cols[w][d].style.width=self.cols[w][d].style.height="17px";
				self.cols[w][d].style.border='1px black solid';
				if(d==6) self.cols[w][d].className='saturday';
	
				if((w*7+d+1 > self.monthBegin) && (w*7+d-self.monthBegin+1<=(arrMonthLength[self.selected.getMonth()]))){

					self.cols[w][d].appendChild(tmp=document.createTextNode(Number(w*7+d+1)-self.monthBegin));
					self.cols[w][d].day=w*7+d+1-self.monthBegin;
					setEvent(self.cols[w][d],'click', function(e){
						e=e||event;
						alert((self.selected.getMonth()+1)+"/"+(getEventSource(e).day)+"\n"+((sch=schedule[(self.selected.getMonth()+1)+"/"+(getEventSource(e).day)+"/"+String(self.selected.getFullYear()).substr(2,2)])?sch:scheduleDefault).join("\n"));
					});
				}else{
					self.cols[w][d].appendChild(document.createTextNode(' '));
				}
				self.rows[w].appendChild(self.cols[w][d]);
			}
			self.dateDiv.appendChild(self.rows[w]);
		}
	}
	self.buildDayTable();
});