YAHOO.util.Event.addListener(window, 'load', load_bsCalendar);
	
var panel_bsCalendar;

function load_bsCalendar() {
	
	var handleCancel = function() {
		// alert("canceling");
		this.cancel();
	};
		
	panel_bsCalendar = new YAHOO.widget.Dialog("panel_bsCalendar",   
		{
			fixedcenter: true,  
			close: true,  
			draggable: true,  
			modal: false, 
			visible: false, 
			constraintoviewport: true,
			effect: {effect:YAHOO.widget.ContainerEffect.FADE, duration:0.25}  
		}
	);  
	
	var strHTML = "the body";
	panel_bsCalendar.setHeader('Bell Schedules');
	panel_bsCalendar.setBody(strHTML);
	panel_bsCalendar.cfg.queueProperty("buttons", [ { text:"Finished", handler:handleCancel } ]);
	panel_bsCalendar.render(document.body);
}

var bsRoot;

//
//
//
var callback_BSCalDate = {
	success: function(o) {
		var html, x;
		// get the root
		bsRoot = new Function("return " + o.responseText)();
		sched = bsRoot.schedules;
		// get the element
		var el = YAHOO.util.Dom.get("objBSCalDate" + bsRoot.date);
		// stuff it with select box
		if (el) { 
			el.innerHTML = bsRoot.date + ":<br>";
			var oSelect = document.createElement("SELECT");
			el.appendChild(oSelect);
			for (x=0; x<sched.length; x++) {
				var oOption = document.createElement("OPTION");
				try {
					// firefox
					oSelect.add(oOption, null);
				}
				catch(ex) {
					// ie
					oSelect.add(oOption);
				}
				oOption.text = sched[x].name;
				oOption.value = sched[x].id;
				// .selected MUST be AFTER the option is added to select (IE bug)
				if (sched[x].id == bsRoot.current) { oOption.selected = true; }
			}
			// add a listener to the select
			//alert(bsRoot.fullDate);
			var selectDetails = new BSSelectDetails(bsRoot.fullDate);
			YAHOO.util.Event.addListener(oSelect, "change", fn_bsChange, selectDetails);
			// el.innerHTML = oSelect.innerHTML;
			// el.appendChild(oSelect);
		}
	},
	failure: function(o) {
		alert("There has been an error and that feature is not available at this time." + o.responseText);
	},
	argument: { "bob": "hi there!" }
};

//
//
//
function BSSelectDetails(iDate) {
	this.date = iDate;
}

var holdTheSelectElement;

//
//
//
var callback_BSSelectDate = {
	success: function(o) { 
		x = 1;
		//alert("wow!");
		holdTheSelectElement.style.backgroundColor = "lime";
		//alert(o.argument);
	},
	failure: function(o) {
		x = 1;
		holdTheSelectElement.style.backgroundColor = "red";
		// alert("bsselect date failure");
	},
	argument: ["Hiya!"]
};

//
//
//
function fn_bsChange(e, obj) {
	this.style.backgroundColor = "yellow";
	holdTheSelectElement = this;
	var transaction = YAHOO.util.Connect.asyncRequest('GET', 'index.asp?Action=UpdateBSAssignmentJSON&Date=' + obj.date + '&BSID=' + this.value, callback_BSSelectDate, null); 
}

//
//
//
function fn_bsChange2(obj, theDate) {
	$(obj).style.backgroundColor = "yellow";
	holdTheSelectElement = $(obj);
	var transaction = YAHOO.util.Connect.asyncRequest('GET', 'index.asp?Action=UpdateBSAssignmentJSON&Date=' + theDate + '&BSID=' + $(obj).value, callback_BSSelectDate, null); 
}

//
//
//
function fn_bsCalendarPop(dMonth, dYear) {
	
	var d = new Date();
	
	if (dMonth == null) {
		// alert("d was null");
		// d = new Date();
	} else {
		d.setFullYear(dYear, dMonth, 1);	
	}
	var html;
	var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
	var dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
	var monthLength = ["31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31"];
	var curMonth = d.getMonth();
	var curDay = d.getDay();
	var curMonthLength = monthLength[curMonth];
	// what day of week does month start on?
	var d1 = d;
	d1.setDate(1);
	var d1Day = d1.getDay();

 	html = "<table align=center class=clsBSTable><tr><th colspan=7>";
 	html += "<span class=clsClickyAction onclick='fn_bsCalendarPop(" + (d1.getMonth() - 1) + ", " + d1.getFullYear() + ");'>&lt;Previous</span> ";
 	html += monthNames[d1.getMonth()] + " " + d1.getFullYear();
 	html += " <span class=clsClickyAction onclick='fn_bsCalendarPop(" + (d1.getMonth() + 1) + ", " + d1.getFullYear() + ");'>Next&gt;</span>";
 	html += "</th></tr><tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>";
	
	var calDate = 1;
	var d2 = d;
	var d2Day;

	while (calDate <= curMonthLength) {
		d2.setDate(calDate);
		d2Day = d2.getDay();
		
		// if Sunday, or 1st day of month, start a new table row
		if (d2Day === 0 || calDate == 1) { html += "<tr>"; }
		
		// if calDate = 1, fill empty dates
		if (calDate == 1) {
			for (var x = 0; x < d2Day; x++) {
				html += "<td></td>";
			}
		}

		// the date
		var transaction = YAHOO.util.Connect.asyncRequest('GET', 'index.asp?Action=BellScheduleJSON&Date=' + (d2.getMonth() + 1) + '/' + d2.getDate() + '/' + d2.getFullYear(), callback_BSCalDate, null); 
		//fn_bsDropDown(d2);
		html += "<td id=objBSCalDate" + calDate + "></td>";

		// if Saturday or last day of month, end the table row
		if (d2Day == 6 || calDate == curMonthLength) { html += "</tr>"; }

		// go to next date
		calDate++;
	}

	panel_bsCalendar.setBody(html);
	panel_bsCalendar.show();
	
}