dojo.require("dojo.date.common");
dojo.require("dojo.date.locale");

dojo.declare("com.mgh.Events",null, {
	constructor : function(config) {
		this.queryParams = config;
		this.months = ['January','February','March','April','May','June','July','August','September','October','November','December']; 
		this.dayNames = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
		this.date;
		this.daysInMonth;
		this.leapYear;
		this.firstDay;
		this.lastDay;
		this.weeks;
		this.sundays;
		this.days;
		this.currentWeek;
		this.numberOfWeeks;
		this.xmlDoc;
		this.eventsByDay;
		this.reloadCounter = 0;
		this.dayCount;
		this.getXmlString();
		
		this.urlRoot = "http://www.massgeneral.org/";
		this.url = "news/event.aspx?id=";
	},
	
	reset : function(){
		//this.queryParams.today is a constant. It is used to know what is todays day.
		//if the days go beyond that, i.e. from january 31 to february 28 or 9
		//this.queryParams.today remains the same, so when we go back to january, 31 is highlighted
		//correctly		
		var tempDate = new Date(this.queryParams.year, this.queryParams.month, 1);
		var tempDaysInMonth = dojo.date.getDaysInMonth(tempDate);
		if (this.queryParams.day > tempDaysInMonth){
			this.queryParams.day = tempDaysInMonth;	
		} else {
			this.queryParams.day = this.queryParams.today;	
		}
		this.date = new Date(this.queryParams.year,this.queryParams.month,this.queryParams.day);
		this.daysInMonth = dojo.date.getDaysInMonth(this.date);
		this.leapYear = dojo.date.isLeapYear(this.date);
		this.firstDay = new Date (this.queryParams.year,this.queryParams.month,1);
		this.lastDay = new Date (this.queryParams.year,this.queryParams.month,this.daysInMonth);
		this.weeks = new Array();
		//populated in weeksBeginEnd
		this.sundays = new Array();
		this.days = new Array();
		this.dayCount = this.queryParams.day - 1;
		this.weeksBeginEnd();
		this.queryParams.currentWeek = this.whatWeekIsIt();
		this.currentWeek = this.queryParams.currentWeek;		
		//this.messages();
	},
	
	/******************************************* Data Functions ***********************************************/
	updateQuery : function()
	{
		var m = this.queryParams;
		this.query = m.href + '?az=' + m.az + '&age=' + m.age + '&cat=' + m.cat +  '&subcat=' + m.subcat + '&type=' + m.type + '&query=' + m.query + 
		'&month=' + (m.month + 1) + '&audience=' + m.audience + '&eventType=' + m.eventType + '&mediaType=' + m.mediaType + '&newsType=' + m.newsType + 
		'&year=' + m.year + '&topic=' + m.topic;
		//alert(this.query);
	}, 
	
	getXmlString : function()
	{
		this.updateQuery();
		dojo.xhrGet({
			url: this.query,
			handleAs: "text",
			load: function(response, ioArgs){
				//outside function listening for the data to be ready
				calendarDataReady(response);			
				return response;
			},
			error: function(response, ioArgs){
				alert("An error occurred, with response: " + response);
				return response;
			}
		});		
	},
		
	loadXmlString : function(txt) 
	{
		var Doc;
		var parser;
		try //Internet Explorer
		{
			Doc = new ActiveXObject("Microsoft.XMLDOM");
			Doc.async = "false";
			Doc.loadXML(txt);
			return(Doc); 
		}
		catch(e)
		{
			try //Firefox, Mozilla, Opera, etc.
			{
				parser = new DOMParser();
				Doc = parser.parseFromString(txt,"text/xml");
				return(Doc);
			}
			catch(e) {console.log(e.message)}
		}
		return(null);
	},
	
	dataReady : function (response){
		this.xmlDoc = this.loadXmlString(response);
		this.handleXml();
		this.reset();
	},		
	
	handleXml : function (){
		var date = new Date();
		var days = dojo.date.getDaysInMonth(date);
		this.eventsByDay = new Array();
		var nodes = this.xmlDoc.getElementsByTagName("METADATA");
		var node;
		var itemDate;
		var itemDay;
		var itemIds = '';
		
		for (var i = 0; i< days; i ++){
			for (var x = 0; x < nodes.length; x++){
				node = nodes[x];
				itemDate = this.getNodeData(node,'Date').split('/');
				itemDay = itemDate[1];
				
				if (itemDay == i+1){
					itemIds += x + ','
				}
			}
			
			this.eventsByDay[i] = itemIds.substring(0,itemIds.length-1);
			itemIds = '';
		}
	},
	
	getNodeData : function (node,attName){
		var data = '';
		var field = '';
		var regEx;
		try {
			for (var i=0; i<node.childNodes.length; i++){
				if (node.childNodes[i].nodeName != '#text'){
					if (node.childNodes[i].getAttribute("NAME") == attName){
						data = node.childNodes[i].childNodes[0].nodeValue;
						break;
					}
				}
			}
		} catch (e) {
			console.log(e);
		}
		return data;
	},
	
	getDaysEvents : function (list,day){
		var ids = list.split(',');
		var id;
		var nodes = this.xmlDoc.getElementsByTagName("NAME");
		var title = '';
		for (var i = 0; i < ids.length; i++) {
			id = parseInt(ids[i]);
			title += '<a href="#" onclick="calendarDetailView(' + parseInt(day-1) + ')">' + nodes[id].childNodes[0].nodeValue.split('|')[0] + '</a><br><br>';
		}
		return title;
	},
	
	sortDaysEventsByTime : function (day){
		var i;
		var AM = new Array();
		var midnight = new Array();
		var PM = new Array();
		var noon = new Array();
		var startTimes = this.xmlDoc.getElementsByTagName("METADATA");
		var daysEvents = this.eventsByDay[day].split(',');
		var hour = '';
		var time = '';
		var hourStandard = '';
		var meridian = '';
		var stamp = '';
		var sorted = '';
		var sortedIds = new Array();
		try {
			for (i = 0;i < daysEvents.length; i++){
				stamp = this.getNodeData(startTimes[parseInt(daysEvents[i])],'StartTime');
				hour = stamp.split(':')[0];
				mins = stamp.split(':')[1];
				meridian = stamp.substring((stamp.length-2),stamp.length);
				hour.length == 1 ? hour = '0' + hour : hour = hour;
				hourStandard = hour + ':' + mins + '|' + daysEvents[i];
				if (meridian == 'AM'){
					hour == '12' ? midnight.push(hourStandard) : AM.push(hourStandard);
				} else {
					hour == '12' ? noon.push(hourStandard) : PM.push(hourStandard); 				
				}
			}
			midnight.sort();
			AM.sort();
			noon.sort();
			PM.sort();
			
			//combine all elements and extract just the ids
			if (midnight.length > 0) { sorted += midnight.join(',') + ',' }
			if (AM.length > 0) 		 { sorted += AM.join(',') + ',' }
			if (noon.length > 0)	 { sorted += noon.join(',') +  ',' }
			if (PM.length > 0) 		 { sorted += PM.join(',') }		
			
			for (i = 0; i< daysEvents.length; i++){
				sortedIds[i] = parseInt(sorted.split(',')[i].split('|')[1]);
			}
		} catch (e) {
			console.log(e);
		}
		return sortedIds;
	},
			
	/******************************************* Calendar Functions ***********************************************/	
	updateCalendar : function (){
		
	},
	
	calendarDays : function (){
					
	},
	
	highLightToday : function (){
		var todayLayer = 'day' + this.queryParams.day;
		try {
			dojo.byId(todayLayer).className = 'today';
		} catch (e) {
			
		}		
	},
	
	/******************************************* Month Functions ***********************************************/
	nextMonth : function (){
		var currentMonth = this.queryParams.month;
		var next;
		if(currentMonth == 11){
			next = 0;
			this.queryParams.year += 1;	
		} else {
			next = currentMonth + 1;
		}		
		this.queryParams.month = next;
		this.getXmlString();
	},
	
	previousMonth : function (){
		var currentMonth = this.queryParams.month;
		var previousMonth;
		if(currentMonth == 0){
			previousMonth = 11;
			this.queryParams.year -= 1;	
		} else {
			previousMonth = currentMonth - 1;
		}		
		this.queryParams.month = previousMonth;
		this.getXmlString();
	},	
	
	monthView : function (){
		var output = '<table width="938" cellspacing="0" cellpadding="0" id="calendar">';
		output += '<tr><th width="134" scope="col">Sun</th><th width="134" scope="col">Mon</th><th width="134" scope="col">Tue</th><th width="134" scope="col">Wed</th><th width="134" scope="col">Thur</th><th width="134" scope="col">Fri</th><th width="134" scope="col">Sat</th></tr>';
		output += this.buildMonth();
		output += '</table>';
		return output;
	},
	
	buildMonth : function (){
		var month = '';
		if (this.sundays[0] == 0){
			month += this.firstWeek();
		} 
		for (var i = 1; i < this.sundays.length; i++){
			month += this.newWeek(this.sundays[i]);
		}
		dojo.byId('descriptions').style.display = 'none';
		return month;
	},
	
	monthPagingMessage : function (){
		dojo.byId(this.queryParams.monthLayer).innerHTML = 'Events for the month of : <span class="notBold">' + this.months[this.queryParams.month] + ' ' + this.date.getFullYear() + '</span>';
		dojo.byId(this.queryParams.monthLayerBottom).innerHTML = 'Events for the month of <span class="notBold">: ' + this.months[this.queryParams.month] + ' ' + this.date.getFullYear() + '</span>';		
	},
	
	/******************************************* Week Functions ***********************************************/
	weekView : function (week){
		var output = '<table width="938" border="1" cellspacing="0" cellpadding="0" id="calendar">';
		output += '<tr><th width="134" scope="col">Sun</th><th width="134" scope="col">Mon</th><th width="134" scope="col">Tue</th><th width="134" scope="col">Wed</th><th width="134" scope="col">Thur</th><th width="134" scope="col">Fri</th><th width="134" scope="col">Sat</th></tr>';
		if (week == 0){
			output += this.firstWeek();
		} else {
			output += this.newWeek(this.sundays[week]);	
		}
		output += '</table>';
		return output;
	},
	
whatWeekIsIt : function (){		
		var todayDate = this.date.getDate();
		var today = this.date.getDay();
		var month = this.months[this.date.getMonth()];
		var year = this.date.getFullYear();	
		var weekBegins = this.days[todayDate-1]-today;
		weekBegins < this.days[0] ? weekBegins = this.days[0] : weekBegins = weekBegins;
		var weekEnds = this.days[todayDate-1]+(6-today);
		weekEnds > this.days[this.days.length-1] ? weekEnds = this.days[this.days.length-1] : weekEnds = weekEnds;
		var beginDay = this.dayNames[this.weeks[weekBegins-1]];
		var endDay = this.dayNames[this.weeks[weekEnds-1]];
		var whatWeek = 0;
		
		for (var i = 0; i < this.sundays.length; i++){
			//console.log(this.sundays[i] + ' ' + weekBegins + ' ' + weekEnds);
			//this.sundays takes into account each sunday of the month. when the first of the first week
			//of the month is 0, this means it is the first week but it but did not start on sunday
			//if (this.sundays[i] == 0){
			//	break;
			//} else {
				if ((this.sundays[i] >= weekBegins) && (this.sundays[i] <= weekEnds)){
					whatWeek = i;
					break;
				}
			//}
		}
		return whatWeek;
		//console.log('Week of ' + beginDay + ', ' + month + ' ' + weekBegins + ' to ' + endDay + ', ' + month + ' ' + weekEnds + ' ' + year);		
	},

	weeksBeginEnd : function () {
		var beginDay = this.firstDay.getDay();
		var endDay = this.lastDay.getDay();
		var days = new Array();
		var counter = beginDay;
		var weekCounter = 0;
		var day = 0;
		
		if (beginDay != 0) {
			this.sundays[0] = 0;
			weekCounter ++;
		}		
		
		for (var i = 0; i < this.daysInMonth; i++){
			if (counter > 6){
				counter = 0;
			}
									
			if (counter == 0){
				this.sundays[weekCounter] = i+1;
				weekCounter ++;				
			}
			
			day = counter;
			counter ++;
			days[i] = day;
			this.days[i] = i+1;
		}
		this.numberOfWeeks = weekCounter;
		this.weeks = days;
		//console.log(this.sundays);
		//console.log(this.weeks);
		//console.log(this.days);		
	},		
	
	firstWeek : function (){
		var week = '<tr>';
		var counter = 0;
		for (var i=0; i < 7; i++){
			if (i < this.firstDay.getDay()){
				week += '<td class="inactive">&nbsp;</td>';
			} else {
				if (this.eventsByDay[counter]){
					week += '<td class="active" id="day' + (counter + 1) + '"><div class="date">' + (this.firstDay.getDate() + counter) + '</div>';					
					week += '<div class="events">' + this.getDaysEvents(this.eventsByDay[counter],(this.firstDay.getDate() + counter)) + '</div></td>';
				} else {
					week += '<td class="inactive" id="day' + (counter + 1) + '"><div class="date">' + (this.firstDay.getDate() + counter) + '</div>';
					week += '<div class="events"></div></td>';
				}
				counter ++;
			}
		}
		week += '</tr>';
		return week;
	},	

	newWeek : function (start) {		
		//starts on sunday
		var week = '';
		week += '<tr>';
		var end = start + 7;
		var emptyCounter = 0;
		var dayCount = 0;
		var dayIndex = 0;
		for (var i = start; i < end; i++){
			if (i > this.daysInMonth){
				week += '<td class="inactive">&nbsp;</td>';
				emptyCounter ++;
			} else {
				dayIndex = i - 1;
				if (this.eventsByDay[dayIndex]){
					week += '<td class="active" id="day' + this.days[dayIndex] +  '"><div class="date">' + this.days[dayIndex] + '</div>';					
					week += '<div class="events">' + this.getDaysEvents(this.eventsByDay[dayIndex],this.days[dayIndex]) + '</div></td>';
				}
				else {
					week += '<td class="inactive" id="day' + this.days[dayIndex] +  '"><div class="date">' + this.days[dayIndex] + '</div>';
					week += '<div class="events"></div></td>';
				}				
			}
		}
		week += '</tr>';
		dojo.byId('descriptions').style.display = 'none';
		return week;
	},	
	
	nextWeek : function (){
		var week = this.currentWeek + 1;
		if (week < this.sundays.length){
			//console.log(week + ' ' + this.sundays.length + ' ' + this.currentWeek);
			this.currentWeek ++;	
			this.updateWeekView();		
		}
	},
	
	previousWeek : function (){
		var week = this.currentWeek - 1;
		if (week >= 0){
			//console.log(week + ' ' + this.sundays.length + ' ' + this.currentWeek);
			this.currentWeek --;	
			this.updateWeekView();		
		}
	},

	weekPagingMessage : function (){
		console.log(this.sundays[this.currentWeek]);
		var todayDate = this.sundays[this.currentWeek];
		var today = this.weeks[todayDate];
		var month = this.months[this.date.getMonth()];
		var year = this.date.getFullYear();	
		var weekBegins = this.days[todayDate]-today;
		weekBegins < this.days[0] ? weekBegins = this.days[0] : weekBegins = weekBegins;
		var weekEnds = this.days[todayDate]+(6-today);
		weekEnds > this.days[this.days.length-1] ? weekEnds = this.days[this.days.length-1] : weekEnds = weekEnds;
		var beginDay = this.dayNames[this.weeks[weekBegins-1]];
		var endDay = this.dayNames[this.weeks[weekEnds-1]];
	
		var output = 'Events for the week of : ';
		
		output += '<span class="notBold">'
		output += beginDay + ', ' + month + ' ' + weekBegins + ' to ' + endDay + ', ' + month + ' ' + weekEnds + ' ' + year
		
		output += '</span>';
		dojo.byId(this.queryParams.monthLayer).innerHTML = output;
		dojo.byId(this.queryParams.monthLayerBottom).innerHTML = output;			
	},			
	
	/******************************************* Day Functions ***********************************************/	

	newDay : function(){
		try {
			var eventsToday;
			var upperLimit;
			var names;
			var metaTags;
			var physicianTableClass = 'physicianTable';
			var description = '';
			var name = '';
			var location = '';
			var start = '';
			var end = '';
			var eventId;
			var text = '';
			var jtg1Flag = 0;

			//var currentDay = new Date(this.queryParams.year,this.queryParams.month,this.dayCount+1);
			
			var css = ''
			var output = '<div id="resultsGroup" class="leftMargin">';
			//output += '<div id="dayFormat">Events for : <span id="dayName">'+ dojo.date.locale.format(currentDay, {selector:'date', formatLength:'full'}) + '</span></div>';
			
			if (this.eventsByDay[this.dayCount]){
				
				eventsToday = this.sortDaysEventsByTime(this.dayCount);
				upperLimit = eventsToday.length;
				names = this.xmlDoc.getElementsByTagName('NAME');
				metaTags = this.xmlDoc.getElementsByTagName('METADATA');
				
				for (var i=0; i < upperLimit; i++){
					
					current = eventsToday[i];
					name = names[current].childNodes[0].nodeValue.split('|')[0];
					location = this.getNodeData(metaTags[current],'Location');
					description = this.getNodeData(metaTags[current],'Description');

					//change 1/14/2010 lowercase AM and PM					
					//start = this.getNodeData(metaTags[current],'StartTime');
					//end = this.getNodeData(metaTags[current],'EndTime');
					start = this.getNodeData(metaTags[current],'StartTime').replace('AM','am').replace('PM','pm');
					end = this.getNodeData(metaTags[current],'EndTime').replace('AM','am').replace('PM','pm');				

					eventId = this.getNodeData(metaTags[current],'EventID');
					
					if (i == upperLimit-1){
						physicianTableClass = 'physicianTableLast';
					}
					
					//text += '<span class="conditionDescription">';
					//text += '<table width="700" border="0" cellspacing="0" cellpadding="0" class="' + physicianTableClass + '">';
					text += '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
					text += '<tr>';
					text += '<td>';
					text +='<b>' + start + ' - ' + end + '</b><br>';
					//change 1/14/2010 do not display if there is no location
					//text +='<b>Location : </b>' + location+ '<br>';
					text += location == '' || location == null ? '' : '<b>Location : </b>' + location+ '<br>';
					text +='' + description + '<br>';

					text +='</td>';
					text += '</tr>';
					text += '</table>';
					//text += '</span>';
										
					//space not at the top 
					if (jtg1Flag == 1)
					{
						output += '<BR>';
					}
					jtg1Flag = 1;

					output += '<div><a href="' + this.urlRoot + this.url + eventId + '">' + name + '</a>' + text + '</div>';
					text = '';
				}
				dojo.byId('descriptions').style.display = 'block';	
				//console.log(output);							
			} else {
				output += '<p class="noResults">No Results Found</p>';
				dojo.byId('descriptions').style.display = 'none';
			}		
					
			output += '<br></div>';				
		} catch (e) {
			console.log(e);
		}		
		console.log(output);
		return output;
	},		
	


	newDay2 : function(){
		try {
			var eventsToday;
			var upperLimit;
			var names;
			var metaTags;
			var physicianTableClass = 'physicianTable';
			var description = '';
			var name = '';
			var location = '';
			var start = '';
			var end = '';
			var eventId;
			var text = '';
			//var currentDay = new Date(this.queryParams.year,this.queryParams.month,this.dayCount+1);
			
			var css = ''
			var output = '<div id="resultsGroup">';
			//output += '<div id="dayFormat">Events for : <span id="dayName">'+ dojo.date.locale.format(currentDay, {selector:'date', formatLength:'full'}) + '</span></div>';
			
			if (this.eventsByDay[this.dayCount]){
				
				output += '<ul id="resultsList">';	

				eventsToday = this.sortDaysEventsByTime(this.dayCount);
				upperLimit = eventsToday.length;
				names = this.xmlDoc.getElementsByTagName('NAME');
				metaTags = this.xmlDoc.getElementsByTagName('METADATA');
				
				for (var i=0; i < upperLimit; i++){
					
					current = eventsToday[i];
					name = names[current].childNodes[0].nodeValue.split('|')[0];
					location = this.getNodeData(metaTags[current],'Location');
					description = this.getNodeData(metaTags[current],'Description');
					start = this.getNodeData(metaTags[current],'StartTime');
					end = this.getNodeData(metaTags[current],'EndTime');
					eventId = this.getNodeData(metaTags[current],'EventID');
					
					if (i == upperLimit-1){
						physicianTableClass = 'physicianTableLast';
					}
					
					//text += '<span class="conditionDescription">';
					//text += '<table width="700" border="0" cellspacing="0" cellpadding="0" class="' + physicianTableClass + '">';
					text += '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
					text += '<tr>';
					text += '<td>';
					text +='<b>' + start + ' - ' + end + '</b><br>';
					text +='<b>Location : </b>' + location+ '<br>';
					text +='' + description + '<br>';

					text +='</td>';
					text += '</tr>';
					text += '</table>';
					//text += '</span>';
										
					output += '<li><a href="' + this.urlRoot + this.url + eventId + '">' + name + '</a>' + text + '</li>';
					text = '';
				}
				output += '</ul>';
				dojo.byId('descriptions').style.display = 'block';	
				//console.log(output);							
			} else {
				output += '<p class="noResults">No Results Found</p>';
				dojo.byId('descriptions').style.display = 'none';
			}		
					
			output += '</div>';				
		} catch (e) {
			console.log(e);
		}		
		return output;
	},	
	
	whatDayIsIt : function (){
		
	},
	
	dayView : function (){
		return this.newDay();
	},
	
	nextDay : function (){
		if (this.dayCount < this.daysInMonth){
			this.dayCount ++;
		} else {
			this.dayCount = this.daysInMonth;
		}
		this.updateDayView();
	},
	
	previousDay : function (){
		if (this.dayCount > 0){
			this.dayCount --;
		} else {
			this.dayCount = 0;
		}
		this.updateDayView();		
	},
	
	dayPagingMessage : function (){
		var currentDay = new Date(this.queryParams.year,this.queryParams.month,this.dayCount+1);
		dojo.byId(this.queryParams.monthLayer).innerHTML = 'Events for : <span class="notBold">' + dojo.date.locale.format(currentDay, {selector:'date', formatLength:'full'}) + '</span>';
	},
	
	/******************************************* Paging Functions ***********************************************/
	createPaging : function (){
		var output = '<div class="resultNumber" id="' + this.queryParams.monthLayer + '">' + this.months[this.queryParams.month] + ' ' + this.date.getFullYear() + '</div>';
		output += '<ul>';
		output += '<li class="prev" id="previousDisabled">Previous</li>';
        output += '<li class="prev" id="previousEnabled"><a href="#" id="previousMonth">Previous</a></li>';			
        output += '<li class="next" id="nextEnabled"><a href="#" id="nextMonth">Next</a></li>';
        output += '<li class="next" id="nextDisabled">Next</li>';
        output += '</ul>';
		dojo.byId(this.queryParams.pagingLayer).innerHTML = output;
	},
	
	createBottomPaging : function (){
		var output = '<div class="resultNumber" id="' + this.queryParams.monthLayerBottom + '">' + this.months[this.queryParams.month] + ' ' + this.date.getFullYear() + '</div>';
		output += '<ul>';
		output += '<li class="prev" id="previousDisabledBottom">Previous</li>';
        output += '<li class="prev" id="previousEnabledBottom"><a href="#" id="previousMonthBottom">Previous</a></li>';			
        output += '<li class="next" id="nextEnabledBottom"><a href="#" id="nextMonthBottom">Next</a></li>';
        output += '<li class="next" id="nextDisabledBottom">Next</li>';
        output += '</ul>';
		dojo.byId(this.queryParams.bottomPagingLayer).innerHTML = output;
	},	
		
	updatePaging : function (){
		if (this.queryParams.viewMode == 'month'){
			if((this.queryParams.month > this.queryParams.initialMonth) && (this.queryParams.year >= this.queryParams.initialYear) || (this.queryParams.month < this.queryParams.initialMonth) && (this.queryParams.year >= this.queryParams.initialYear)){
				dojo.byId('previousEnabled').style.display = 'block';
				dojo.byId('previousDisabled').style.display = 'none';
				dojo.byId('previousEnabledBottom').style.display = 'block';
				dojo.byId('previousDisabledBottom').style.display = 'none';
				dojo.byId('nextEnabled').style.display = 'block';
				dojo.byId('nextEnabledBottom').style.display = 'block';
				dojo.byId('nextDisabled').style.display = 'none';		
				dojo.byId('nextDisabledBottom').style.display = 'none';
			}
			else {
				dojo.byId('previousEnabled').style.display = 'none';
				dojo.byId('previousDisabled').style.display = 'block';	
				dojo.byId('previousEnabledBottom').style.display = 'none';
				dojo.byId('previousDisabledBottom').style.display = 'block';
				dojo.byId('nextEnabled').style.display = 'block';
				dojo.byId('nextEnabledBottom').style.display = 'block';
				dojo.byId('nextDisabled').style.display = 'none';
				dojo.byId('nextDisabledBottom').style.display = 'none';				
			}
			this.monthPagingMessage();
		} else if (this.queryParams.viewMode == 'week') {
			if((this.currentWeek >= 0) && (this.currentWeek < this.weeks.length)){
				//console.log('fall within: ' + this.currentWeek)
				dojo.byId('previousEnabled').style.display = 'block';
				dojo.byId('nextEnabled').style.display = 'block';
				dojo.byId('previousDisabled').style.display = 'none';
				dojo.byId('nextDisabled').style.display = 'none';
				
				dojo.byId('previousEnabledBottom').style.display = 'block';
				dojo.byId('nextEnabledBottom').style.display = 'block';
				dojo.byId('previousDisabledBottom').style.display = 'none';
				dojo.byId('nextDisabledBottom').style.display = 'none';
				
				if (this.currentWeek == 0){
					//console.log('first week: ' + this.currentWeek)
					dojo.byId('previousEnabled').style.display = 'none';
					dojo.byId('nextEnabled').style.display = 'block';
					dojo.byId('previousDisabled').style.display = 'block';
					dojo.byId('nextDisabled').style.display = 'none';
					
					dojo.byId('previousEnabledBottom').style.display = 'none';
					dojo.byId('nextEnabledBottom').style.display = 'block';
					dojo.byId('previousDisabledBottom').style.display = 'block';
					dojo.byId('nextDisabledBottom').style.display = 'none';			
				} 
				if (this.currentWeek + 1 == this.sundays.length) {
					//console.log('last week: ' + this.currentWeek)
					dojo.byId('previousEnabled').style.display = 'block';
					dojo.byId('nextEnabled').style.display = 'none';
					dojo.byId('previousDisabled').style.display = 'none';
					dojo.byId('nextDisabled').style.display = 'block';
					
					dojo.byId('previousEnabledBottom').style.display = 'block';
					dojo.byId('nextEnabledBottom').style.display = 'none';
					dojo.byId('previousDisabledBottom').style.display = 'none';
					dojo.byId('nextDisabledBottom').style.display = 'block';					
				}							
			}
			this.weekPagingMessage();	
		} else {
			//console.log('day : ' + this.dayCount)
			if((this.dayCount >= 0) && (this.dayCount <= this.daysInMonth)){
				dojo.byId('previousEnabled').style.display = 'block';
				dojo.byId('nextEnabled').style.display = 'block';
				dojo.byId('previousDisabled').style.display = 'none';
				dojo.byId('nextDisabled').style.display = 'none';
				
				dojo.byId('previousEnabledBottom').style.display = 'block';
				dojo.byId('nextEnabledBottom').style.display = 'block';
				dojo.byId('previousDisabledBottom').style.display = 'none';
				dojo.byId('nextDisabledBottom').style.display = 'none';
				
				if (this.dayCount == 1){
					//console.log('first week: ' + this.currentWeek)
					dojo.byId('previousEnabled').style.display = 'none';
					dojo.byId('nextEnabled').style.display = 'block';
					dojo.byId('previousDisabled').style.display = 'block';
					dojo.byId('nextDisabled').style.display = 'none';
					
					dojo.byId('previousEnabledBottom').style.display = 'none';
					dojo.byId('nextEnabledBottom').style.display = 'block';
					dojo.byId('previousDisabledBottom').style.display = 'block';
					dojo.byId('nextDisabledBottom').style.display = 'none';			
				} 
				if (this.dayCount == this.daysInMonth-1) {
					//console.log('last week: ' + this.currentWeek)
					dojo.byId('previousEnabled').style.display = 'block';
					dojo.byId('nextEnabled').style.display = 'none';
					dojo.byId('previousDisabled').style.display = 'none';
					dojo.byId('nextDisabled').style.display = 'block';
					
					dojo.byId('previousEnabledBottom').style.display = 'block';
					dojo.byId('nextEnabledBottom').style.display = 'none';
					dojo.byId('previousDisabledBottom').style.display = 'none';
					dojo.byId('nextDisabledBottom').style.display = 'block';					
				}							
			}
			this.dayPagingMessage();			
		}		
	},
	
	createAlphaList : function (){
		var list = this.queryParams.tabs;
		var css = 'test';
		var output = '<ul>';
		for (var i = 0; i < list.length; i++)
		{
			list[i].toLowerCase() == this.queryParams.viewMode ? css = 'active' : css = 'test';
			output += '<li id="' + list[i].toLowerCase() + 'Tab" class="' + css + '">';
			output += '<a href="#" onclick="switchView(\'' + list[i].toLowerCase() + '\')">';
			output += list[i];
			output += '</a>';
			output += '</li>';
		}
		output += '</ul>';
		dojo.byId('alphaList').innerHTML = output;
		//return output;
	},	
	
	resetHighlights : function (){
		var nodes = dojo.query('li', 'alphaList');
		for (var i=0; i < nodes.length; i++){
			nodes[i].className = 'test';	
		}
	},	
	
	defaultMonthView : function (){
		this.queryParams.viewMode = 'month';
		this.createAlphaList();
		dojo.byId(this.queryParams.displayLayer).innerHTML = this.monthView();
		this.createPaging();
		this.createBottomPaging();
		this.updatePaging();
		this.highLightToday();
	},
	
	updateMonthView : function (){
		this.queryParams.viewMode = 'month';
		dojo.byId(this.queryParams.displayLayer).innerHTML = this.monthView();
		this.updatePaging();
		this.highLightToday();
	},
	
	defaultWeekView : function (){
		this.queryParams.viewMode = 'week';
		this.createAlphaList();
		dojo.byId(this.queryParams.displayLayer).innerHTML = this.weekView(this.whatWeekIsIt());
		this.createPaging();
		this.createBottomPaging();
		this.updatePaging();
		this.highLightToday();	
	},
	
	updateWeekView : function (){
		this.queryParams.viewMode = 'week';
		dojo.byId(this.queryParams.displayLayer).innerHTML = this.weekView(this.currentWeek);
		this.updatePaging();
		this.highLightToday();
	},

	defaultDayView : function (){
		this.queryParams.viewMode = 'day';
		this.createAlphaList();
		dojo.byId(this.queryParams.displayLayer).innerHTML = this.dayView();
		this.createPaging();
		this.createBottomPaging();
		this.updatePaging();	
	},
	
	updateDayView : function (){
		this.queryParams.viewMode = 'day';
		dojo.byId(this.queryParams.displayLayer).innerHTML = this.dayView();
		this.updatePaging();
	},
	
	toggleDescriptions : function (){
		try {
			var nodes = dojo.query('table', 'resultsGroup');
			for (var i=0; i < nodes.length; i++){
				if (nodes[i].style.display == ''){
					nodes[i].style.display = 'block';
				}
				//(i + ' ' + nodes[i].style.display.toString());
				if (nodes[i].style.display == 'block'){
					nodes[i].style.display = 'none';				
				} else {
					nodes[i].style.display = 'block';
				}
			}
		} catch (e) {
			console.log(e);
		}
	},	
		
	messages : function (){
		var formatDate = dojo.date.locale.format(this.date, {datePattern: "MM/dd/yyyy", selector: "date"});
		console.log('format: ' + formatDate);
		console.log('first day: ' + this.firstDay.toUTCString());
		console.log('days in month ' + this.daysInMonth.toString());		
		console.log('leap year : ' + this.leapYear.toString());
		console.log('day of week : ' + this.date.getDay());
		console.log('day of month : ' + this.date.getDate());
		console.log('month : ' + this.date.getMonth());
		console.log('day sequence : ' + this.weeks.toString());
		console.log('number of weeks : ' + this.numberOfWeeks.toString());
		console.log('sundays : ' + this.sundays.toString());
		//console.log('second week : ' + this.monthView());
	}
});
