
function calendarNavigation(nav,cal)
{
	this.navContainer = nav;
	this.calContainer = cal;
	this.monthData;
	this.currYear;
	this.currMonth;
	this.startMonth;
	this.backButton;
	this.forwardButton;
	this.start;
	this.end;
	this.timespan = 2;
	this.init = function()
	{
		var d = new Date();
		this.currYear = d.getFullYear();
		this.start = d.getFullYear();
		this.end = this.start + (this.timespan - 1);
		this.currMonth = d.getMonth();
		this.startMonth = this.currMonth;
		var bb = '<input type="button" id="backbtn" value="&#8249;" />';
		this.backButton = $(bb);
		var fb = '<input type="button" id="forwardbtn" value="&#8250;" />';
		this.forwardButton = $(fb);
		this.getMonthData(this.getYears());
		this.setDisplay(this.currMonth,this.currYear);
		// set up message box
		var boxstr = '<div id="calmsg">Testing one two three.</div>';
		var msgBox = $(boxstr);
		var co = this.calContainer.offset();
		var ml = co.left + 100 + 'px';
		var mt = co.top + 35 + 'px';
		var css = {
			display:'none',
			position:'absolute',
			left:ml,
			top:mt,
			padding:'4px',
			backgroundColor:'#CCCCCC',
			width:'150px',
			height:'18px',
			zIndex:100
			};
		msgBox.css(css);
		$(document.body).append(msgBox);
		// end message box setup
	}
	
	this.showMessage = function(text)
	{
		$('#calmsg').text(text);
		$('#calmsg').show();
	}
	
	this.hideMessage = function()
	{
		$('#calmsg').hide();
	}
	
	this.goTo = function(m,y)
	{
		m = m - 1
		if(typeof this.monthData[y] != 'undefined' && typeof this.monthData[y][m] != 'undefined')
		{
			this.currMonth = m;
			this.currYear = y;
			this.setDisplay(this.currMonth,this.currYear);
		}
		else
		{
			this.showMessage('Date is out of range.');
		}
	}
	
	this.setButtonAction = function()
	{
		var self = this;
		this.backButton.click(function(){
			self.goBack();
		}
		);
		this.forwardButton.click(function(){
			self.goForward();						  
		}
		);
	}
	
	this.setTimespan = function(time)
	{
		this.timespan = time;	
	}
	
	this.getYears = function()
	{
		var yrs = new Array();
		var c = 0;
		var diff = this.end - this.start;
		for(var i = this.start;i <= this.end;i++)
		{
			yrs[c] = i;
			c++;
		}
		return yrs;
	}
	
	this.getMonthData = function(y)
	{
		var m = this.currMonth;
		var mlist = [
		'January','February','March','April','May','June','July','August','September','October','November','December'			 
		];
		var months = new Object();
		var marray;
		var k = 0;
		for(var i = 0;i < y.length;i++)
		{
				mArray = new Array();
				for(var j = 0;j < mlist.length;j++)
				{
					if(j >= m)
					{
						//console.log(mlist[j] + ' ' + j);
						mArray[j] = mlist[j];
					}
					else
					{
						mArray[j] = mlist[j];
					}
				}
				months[y[i]] = mArray;
		}
		//console.log(months);
		this.monthData = months;
		
	}
	
	this.setDisplay = function(m,y)
	{
		//console.log(this.monthData[y][m]);
		this.loadCalendar((m + 1),y);
		this.navContainer.empty();
		this.setButtonAction();
		if(y == this.start && m == this.startMonth)
		{
			var label = '<span id="monthLabel">' + this.monthData[y][m] + ' ' + y +  '</span>&nbsp;&nbsp;&nbsp;&nbsp;';
			this.navContainer.append($(label));
			this.navContainer.append(this.forwardButton);
		}
		else if(y == this.end && m == (this.monthData[y].length - 1))
		{
			var label = '&nbsp;&nbsp;&nbsp;&nbsp;<span id="monthLabel">' + this.monthData[y][m] + ' ' + y +  '</span>';
			this.navContainer.append(this.backButton);
			this.navContainer.append($(label));
		}
		else
		{
			var label = '&nbsp;&nbsp;&nbsp;&nbsp;<span id="monthLabel">' + this.monthData[y][m] + ' ' + y +  '</span>&nbsp;&nbsp;&nbsp;&nbsp;';
			this.navContainer.append(this.backButton);
			this.navContainer.append($(label));
			this.navContainer.append(this.forwardButton);
		}
	}
	
	this.goBack = function()
	{
		// take care of transition from one year to another
		if(this.currMonth == 0 && this.currYear == this.end)
		{
			this.currMonth = 11;
			this.currYear--;
		}
		else if(this.currMonth == 0 && this.currYear == this.start)
		{
			this.currMonth = 11;
			this.currYear = this.end;
		}
		else
		{
			this.currMonth--;
		}
		this.setDisplay(this.currMonth,this.currYear);
	}
	
	this.goForward = function()
	{
		// take care of transition from one year to another
		if(this.currMonth == 11 && this.currYear != this.end)
		{
			this.currMonth = 0;
			this.currYear++;
		}
		else if(this.currMonth == 11 && this.currYear == this.end)
		{
			this.currMonth = 0;
			this.currYear = this.start;
		}
		else
		{
			this.currMonth++;
		}
		this.setDisplay(this.currMonth,this.currYear);
	}
	
	this.loadCalendar = function(m,y)
	{
		var self = this
		var data = {month:m,year:y}
		this.showMessage('Loading...');
		$.get(
			  'cal.php',
			  data,
			  function(rdata){;
				self.setCalDisplay(rdata);
			  },
			  'html'
		);
	}
	
	this.setCalDisplay = function(rdata)
	{
		this.calContainer.empty();
		this.hideMessage();
		this.calContainer.append($(rdata + '<br clear="all" />'));
	}
}