
/**
** FILE: chartimeslots.js
**
** DESCRIPTION: 
**	Contains javascript used by the CharTimeSlotsGui PHP library
**
*/

// One per idx
var weekDay 		= new Array();
var hourSlider 		= new Array();
var timesArray 		= new Array();
var currStartTime 	= new Array();

// Global
var id_index;
var cellDisp;
var hoursToShow = 10;
var regularityLabels;	// Array with labels for regularity
var regularityColors;	// Array with bg colors for regularity 

if (navigator.appName != "Microsoft Internet Explorer") {
	cellDisp = "table-cell";
} else { 
	cellDisp = "block";
}

function HilightDay(dayNum, idx)
{
	if (weekDay.length > 0) document.getElementById('weekday'+idx+'_'+weekDay[id_index]).style.borderBottom = '0px';
	document.getElementById('weekday'+idx+'_'+dayNum).style.borderBottom = '2px solid #FF6300';
}

function SelectDay(dayNum, idx)
{
	// Underline the current day
	HilightDay(dayNum, idx);

	// Get the table that displays the schedule
	var schedTable = document.getElementById('schedule'+idx);

	// Get the rows, one row per char	
	var chars = schedTable.tBodies[0].rows;

	var firstHourToShow = 14;		// Default slider position

	// Fill the cells of the table w. the appropriate bg and title based
	// on whether the char's timeslot info 
	for (var charIdx=0; charIdx < chars.length; charIdx++) {
		
		var row = chars[charIdx];				// Row representing char	
	
		for (var hour=0; hour<24; hour++) {			

			var cell = row.cells[hour+1];		// remember, index 0 is the char name!

			// indices in the timesArray don't exist for times that are not allocated
			// so check to see whether we have timeslot allocation for this day / hour
			var daySlots = null;
			var timeSlot = null;
			var charSlots = timesArray[idx][charIdx];
			if (charSlots) daySlots = charSlots[dayNum];
			if (daySlots) timeSlot = daySlots[hour];
			
			var regularity = 0;
			var notes = ""; 
			var cursor = "default";
			if (timeSlot) {
				regularity = timeSlot['regularity'];
				notes = timeSlot['notes'];
				if (notes) notes = " - " + notes;
				notes = regularityLabels[regularity] + notes;
				cursor = "help";
				
				if (hour < firstHourToShow) firstHourToShow = hour;
			}
			cell.title = notes;				// The note that will appear on mouseover
			cell.style.cursor = cursor;		// the cursor that will appear on mouseover
			cell.firstChild.style.background = regularityColors[regularity];
		}
		
	}
	weekDay[id_index] = dayNum;		// Set current day being viewed
	currStartTime[id_index] = firstHourToShow;
	if (hourSlider[id_index]) hourSlider[id_index].setValue(firstHourToShow);
}

function ScrollHours(tableId, startTime)
{
	var schedTable = document.getElementById(tableId);
	var rows = schedTable.rows.length;
	//alert(schedTable.prevStartTime);
	if (schedTable.prevStartTime == null) {
		schedTable.prevStartTime = startTime;
	} else {
		if (schedTable.prevStartTime == startTime) return;
	}
	// Only show and hide the deltas, don't iterate through cells whose display
	// status doesn't change to make the scrolling performance better
	var prevStartTime = schedTable.prevStartTime;
	var diff = startTime - prevStartTime;
	
	if (diff < 0) {		// slider moved to the left
		var startHourShow = startTime;
		var endHourShow = startHourShow - diff;
		var startHourHide = startTime + hoursToShow;
		var endHourHide = prevStartTime + hoursToShow;
	} else {			// slider moved to the right
		var startHourHide = prevStartTime;
		var endHourHide = prevStartTime+diff;
		var startHourShow = prevStartTime + hoursToShow;
		var endHourShow = startTime + hoursToShow;
		
	}
	// Iterate over each hour 
	for (var h=startHourShow+1; h <= endHourShow; h++) {
		for (var r=0; r < rows; r++) {
			schedTable.rows[r].cells[h].style.display = cellDisp;
		}
	}	
	for (var h=startHourHide+1; h <= endHourHide; h++) {
		for (var r=0; r < rows; r++) {
			schedTable.rows[r].cells[h].style.display = 'none';
		}
	}	
	schedTable.prevStartTime = startTime;
}

function LoadMultiSchedule(divId, frameId, toList)
{
	var div   = document.getElementById(divId);		// the container
	var frame = document.getElementById(frameId);	// the iframe
	
	if (toList.trim() == '') return;
	
	div.style.visibility='visible';
	var url   = "../svcs/getschedules.php?tolist="+escape(toList);
	// alert (url);
	frame.src = url;
}
