var calendarModelMixins = {
	_sharedConstructor: function() {
		this.widget = dojo.query("#"+this.declaredClass);
		this.updateContainer = this.widget.query(".calContainer");
		this.monthControls = this.widget.query(".monthControls");
		this.prevMonthLink = this.monthControls.query(".prevMonthLink");
		this.nextMonthLink = this.monthControls.query(".nextMonthLink");
		this.monthYearContainer = this.monthControls.query(".calMonthYear");
		this.monthLabel = this.monthYearContainer.query(".month")
		this.yearLabel = this.monthYearContainer.query(".year")
		
		this.monthArray = ["January","February","March","April","May","June","July","August","September","October","November","December"];
		var classArray = this.monthYearContainer[0].className.split(" ");
		for(var c=0; c < classArray.length; c++) {
			if(classArray[c].indexOf("month") != -1) {
				this.currentMonth = Number(classArray[c].split("_")[1]);
			} else if(classArray[c].indexOf("year") != -1) {
				this.currentYear = Number(classArray[c].split("_")[1]);
			}
		};
		
		dojo.require("dojo.back");
		
		this.backState = {
			back: function(t, m, y) {
				return function() {
					t._changeView(m, y, false);
				}
			}(this, this.currentMonth, this.currentYear)
		};
		
		dojo.back.setInitialState(this.backState);
	},
	_toggle: function() {
		this.toggleButton.toggleClass('expandedToggleLink');
		// TODO: THIS NEEDS TO BE CROSS BROWSER. dojo.hasClass WAS NOT WORKING FOR THIS
		if(this.toggleButton[0].className.indexOf('expandedToggleLink') != -1) {
			this.togglePanel.style("display", "block");
			this.toggleButton[0].innerHTML = "Close Calendar";
		} else {
			this.togglePanel.style("display", "none");
			this.toggleButton[0].innerHTML = "Open Calendar";
		}
	},
	_changeView: function(month, year, addThis) {
		var moreHash = (typeof this.filterBy != 'undefined')?"-"+this.filterBy:"";
		if(typeof this.currentRequest != 'undefined') {
			this.currentRequest.cancel();
		}
		if(typeof addThis == 'undefined' || addThis == true) {
			dojo.back.addToHistory({
				changeUrl: month+"-"+year+moreHash,
				back: function(t, m, y, f) {
					return function() {
						if(typeof t.filterBy != 'undefined') {
							t._changeFilter(f, false);
						}
						t._changeView(m, y, false);
					}
				}(this, month, year, this.filterBy||undefined),
				forward: function(t, m, y, f) {
					return function() {
						if(typeof t.filterBy != 'undefined') {
							t._changeFilter(f, false);
						}
						t._changeView(m, y, false);
					}
				}(this, month, year, this.filterBy||undefined)
			});
		}
		this.currentMonth = month;
		this.currentYear = year;
		this.currentRequest = dojo.xhrGet({
			url: this._requestPath(),
			load: function(t, m, y) {
				return function(resp) {
					t._changeViewCallback(resp, m, y);
				};
			}(this, month, year)
		});
	},
		_changeViewCallback: function(resp, month, year) {
			this.updateContainer[0].innerHTML = resp;
			this._updateDate(month, year);
		},
	_prevMonth: function() {
		var newMonth = (this.currentMonth == 1)?12:this.currentMonth-1;
		var newYear = (this.currentMonth == 1)?this.currentYear-1:this.currentYear;
		this._changeView(newMonth, newYear);
	},
	_nextMonth: function() {
		var newMonth = (this.currentMonth == 12)?1:this.currentMonth+1;
		var newYear = (this.currentMonth == 12)?this.currentYear+1:this.currentYear;
		this._changeView(newMonth, newYear);
	},
	_updateDate: function(month, year) {
		var df = model_superclass.dFragment;
		if(month > 0 && month < 13) {
			this.monthLabel.removeClass(this.monthLabel[0].innerHTML.substring(0, 3));
			this.monthLabel[0].innerHTML = this.monthArray[month-1];
			this.monthLabel.addClass(this.monthLabel[0].innerHTML.substring(0, 3));
			this.currentMonth = month;
			if(typeof year != 'undefined' && year > 2000) {
				var yearString = String(year);
				this.yearLabel[0].innerHTML = "";
				for(var d=0; d < yearString.length; d++) {
					this.yearLabel[0].appendChild(df("span", {
						className: "num_"+yearString.substring(d,d+1),
						innerHTML: yearString.substring(d,d+1)
					}));
				};
				this.currentYear = year;
			}
		}
	}
}
