// ==============================================
// Schedule Object
// ==============================================

var iCurrentParty = 5
var isAdmin = false
var iFacilityRowCount = 0;
var sAltColor = "blue";

function Schedule() {
	this.days = iShowDays;
	this.weekdays = "S,M,T,W,T,F,S".split(",")
	this.color = ["#eef",
				  "#ffd",
				  "#ffd",
				  "#ffd",
				  "#ffd",
				  "#ffd",
				  "#eef"
				   ]
	this.altcolor = ["#f7f7ff",
				  "#fff",
				  "#fff",
				  "#fff",
				  "#fff",
				  "#fff",
				  "#f7f7ff"
				   ]
	this.facilities = new Array();
	this.raw = ajax("ajax/GetInitialData.php");
	this.data = new Object();
	this.data["0"] = ["Key", "Status", "Party", "Changed"]
	this.parse(this.raw);
}

Schedule.prototype.parse = function(sRaw) {
	var oData = sRaw.split("\1");
	var oRaw = oData[0].split("\2");
	var oLine
	var iDays  = this.days
	for (var a=1;a<oRaw.length;a++) {
		this.facilities.push(new ScheduleItem(this, a-1, oRaw[a]))
	}
	if (oRaw.length > 1) {
		oRaw = oData[1].split("\2");
		for (var a=1;a<oRaw.length;a++) {
			oLine = oRaw[a].split("\3")
			if (oLine[0] != "key") {
				this.data[oLine[0]] = oLine;
			}
		}
	}
}

Schedule.prototype.draw = function(iYear, iMonth, iDay) {
	var sBuffer = "<table border='0' cellspacing='1'>"
	sBuffer += "<tr><td></td><td colspan='200' id='controlsec'></td></tr>"
	sBuffer += this.drawMonthHeader(iYear, iMonth, iDay)
	sBuffer += this.drawDayHeader(iYear, iMonth, iDay)
	sBuffer += this.drawFacilities(iYear, iMonth, iDay)
	// if (isAdmin) sBuffer += this.drawAdminControls()
	sBuffer += "</table>"
	setInnerHTML("chartsec", sBuffer);
}

Schedule.prototype.drawMonthHeader = function(iYear, iMonth, iDay) {
	var iDays  = this.days
	var iEnd
	var iTotal = 0
	var sBuffer = "<tr><td>&nbsp</td>"
	var sTitle = ""
	
	while (iTotal < iDays) {
		iEnd = lastday[iMonth]
		iMonthSpan = iEnd - iDay + 1
		if (iTotal + iMonthSpan > iDays) {
			iMonthSpan = iDays - iTotal
		}
		iTotal += iMonthSpan
		sMonth = pad(iMonth)
		sMonthName = arrMonths[sMonth]
		
		if (iMonthSpan > 6) {
			sMonthName = sMonthName + " " + iYear
		}
		else {
			sTitle = " title='" +  sMonthName + " " + iYear + "'"
		}
		if (iMonthSpan < 4) {
			sMonthName = sMonthName.substr(0,3)
		}
		if (iMonthSpan < 2) sMonthName = sMonthName.substr(0,1)
		sBuffer += "<td class='monthheader' colspan='" + iMonthSpan + "'" + sTitle + ">" + sMonthName + "</td>"
		iMonth++;
		if (iMonth > 12) {
			iMonth = 1;
			iYear++;
		}
		iDay = 1
	}
	
	sBuffer += "</tr>"
	return sBuffer;
	
	function pad(s) {
		s = "00" + s
		s = s.substr(s.length -2, 2)
		return s
	}
}

Schedule.prototype.drawDayHeader = function(iYear, iMonth, iDay) {
	var oDate = new Date(iYear, iMonth - 1, iDay);
	var iDOW = oDate.getDay()
	var iTempDay
	var iDays = this.days
	var sBuffer = "<tr><td>&nbsp</td>"
	for (var a=0;a<iDays;a++) {
		if (iDay > lastday[iMonth]) {
			iDay = 1
			iMonth++
			if (iMonth > 12) iMonth = 1
		}
		if (iDOW > 6) iDOW = 0;
		sBuffer += "<td class='dayheader' style='background-color:" + this.color[iDOW] + ";'>" + this.weekdays[iDOW] + "<br>" + iDay + "<br></td>"
		
		iDOW++;
		iDay++
	}
	sBuffer += "</tr>"
	return sBuffer
}

Schedule.prototype.drawFacilities = function(iYear, iMonth, iDay) {
	var oDate = new Date(iYear, iMonth - 1, iDay);
	var iDOW = oDate.getDay()
	var iTempDay
	var iDays = this.days
	var sBuffer = "";
	for (var a=0;a<this.facilities.length;a++) {
		sBuffer += this.facilities[a].draw(iDOW, iYear, iMonth, iDay)
	}
	return sBuffer
	
	function pad(s) {
		s = "00" + s
		s = s.substr(s.length -2, 2)
		return s
	}
}

Schedule.prototype.drawAdminControls = function() {
	return "";
	var sBuffer = "<tr><td colspan='" + (this.days + 1) + "' align='right'>";
	sBuffer += "<a href='JavaScript:handleSubmit()'>Submit Changes</a>"
	sBuffer += "</tr>";
	return sBuffer;
}

Schedule.prototype.prep = function() {
	var oData = this.data;
	var sReturn = ""
	for (var a in oData) {
		if (oData[a][3] == "C") {
			if (sReturn != "") sReturn += "\2";
			sReturn += oData[a][0] + "\3"
			sReturn += oData[a][1] + "\3"
			sReturn += oData[a][2]
		}
	}
	return sReturn;
	
}

// ==============================================
// Schedule Item Object
// ==============================================
function ScheduleItem(oParent, iIndex, sRaw) {
	this.index = iIndex
	this.parent = oParent;
	var oLine = sRaw.split("\3");
	this.id = oLine[0]
	this.name = oLine[1]
	this.rowCount = 0;
	this.statuses = new Object();
}

ScheduleItem.prototype.draw = function(iDOW, iYear, iMonth, iDay) {
	var iStart  = 1
	iFacilityRowCount++;
	var bUseAlt = (iFacilityRowCount % 2 > 0) ? true : false;
	this.rowCount = iFacilityRowCount;
	var sStyle = ""
	if (iFacilityRowCount % 2 > 0) sStyle = " style='background-color: " + sAltColor + "'"
	var iDays   = this.parent.days
	var sReturn = "<tr" + sStyle + "><td class='facilityname' nowrap>" + this.name + "</td>"
	var oColor  = this.parent.color
	if (bUseAlt) oColor = this.parent.altcolor
	var sColor = "";
	var iStatus = 0;
	var sTitle  = "";
	var oData   = this.parent.data;
	var sKey    = ""
	
	for (var a=0;a<iDays;a++) {
		if (iDay > lastday[iMonth]) {
			iDay = 1
			iMonth++
			if (iMonth > 12) {
				iMonth = 1
				iYear++;
			}
		}
		sKey =  this.index + ":" + iYear + ":" + iMonth + ":" + iDay
		if (oData[sKey]) {
			iStatus = oData[sKey][1];
		}
		else {
			iStatus = 0;
		}
		if (!isAdmin && String(iStatus) == "1") iStatus = "2"
		
		sTitle = "";
		if (iStatus == "2") sTitle = " title='There are no vacancies for this date'"
		sReturn += "<td id='" + sKey + "' class='openday' style='background-color:" + oColor[iDOW] + ";'>"
		sReturn += "<img onmouseup=\"handleStatusClick(this, '" + sKey + "')\""
		sReturn += " src='img/status-" + iStatus + ".png'" + sTitle + "></td>"
		iDOW++;
		iDay++;
		if (iDOW > 6) iDOW = 0;

	}
	sReturn += "</tr>";
	return sReturn;
}



