		dojo.require("dojox.widget.Calendar");
		dojo.addOnLoad(function(){
		
		var fullAddress = document.URL.split("?")[0];
		var siteAddress = fullAddress.split("/")[4];
		
			// create a new instance of a Calendar
			var calendar = new dojox.widget.Calendar({
				onChange: function(val){
					// when the value changes, hide the popup 
					// and set a value to our input 
					dijit.popup.close(calendar);
					console.log(val, typeof val);
					this._pushChangeTo.value = dojo.date.locale.format(val, {datePattern: "yyyy-MM-dd", selector: "date"});
					
					var newCalDate = dojo.byId("CalDate").value;
					if(siteAddress == "view_daylist.php") newCalendarView('incWeek.php?date=',newCalDate);
					else if(siteAddress ==  "view_day.php") newCalendarView('view_dayaj.php?date=',newCalDate);
					else if(siteAddress == "weekplan.php") window.location.href = fullAddress+"?date="+newCalDate;
				},
				destroy: function(){
					// avoid leaving a ref in object for IE
					delete this._pushChangeTo;
					this.inherited(arguments);
				}
			});
			
			dojo.extend(dojo.NodeList, {
				// the guts of the "plugin"
				addPicker: function(args){
					// summary: A method to add a popup calendar to some input. Only inputs are
					// supported currently. 
					//
					// args: Object?
					// unused, reserved for object-hash of possible params
					//
					// example:
					// | dojo.query("input.needsCalendar").addPicker().removeClass(".needsCalendar");
					//
					this.forEach(function(n){
						
						// add an image after the input we're targeting for clickage
						var img = dojo.doc.createElement('img');
						dojo.attr(img, {
							// change this to something better:
							src:"http://2peak.com/images/timetable.gif", 
							alt:"calendar",
							style:{ cursor:"pointer" }
						})
						dojo.place(img, n, "after");
						
						// in Dojo 1.3 use this instead of above:
						// dojo.create("img", { 
						//		src:"calendar.png",
						//		alt:"celendar",
						//		style:{ cursor:"pointer" } 
						// }, n, "after");
						
						dojo.connect(img, "onclick", function(e){
							// tell popup which id to send onChange value to
							calendar._pushChangeTo = n; 
							// and open the popup below the targeted input
							dijit.popup.open({ 
								popup: calendar,
								around: img
							});
						})
						
					});
					return this; // dojo.NodeList
				}
			});
			
			// one line of code:
			dojo.query(".datePicker").addPicker();
			
		});
		
		
