hoverframe = "schedulerFrame";

parent.setOffsets();

// client hover function - display available times for a specific date
function showSchedulerMouseover(evt,thisdate) {
	var html       = '<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="290">';
	var innrhtml   = '';
	var type       = 'Appointment';
	var r          = 0;
	var rows       = schedulerResults.getRowCount();
	var r_date     = '';
	var r_title    = 0;
	var r_body     = 0;
	var r_time     = '';
	var r_times    = new Array();
	var t          = 0;
	var a          = 900;
	var a1         = '';
	var a2         = '';
	var a3         = '';
	var a4         = '';
	var a_start    = '';
	var a_stop     = '';
	var timeAvail  = false;
	var ampm       = '';
	var r_start    = '';

	if(arguments.length > 2) {
		type = arguments[2];
	}

	for (r; r < rows; r++) {
		r_date = schedulerResults.getField(r, 'date_start').format('yyyy-mm-dd');
		if (r_date == thisdate) {
			if (r_title == 0) { 
				html   += '<TR VALIGN="top"><TD CLASS="hover_cap" ID="hover_container_top">' + parent.hoverTopImage + '</TD></TR>';
				html   += '<TR VALIGN="top"><TD CLASS="hover_title" ID="hover_container_title">' + schedulerResults.getField(r, 'date_start').format('ddd mmm dd') + '</TD></TR>';
				r_title = 1;
			}
			if (r_body == 0) {
				html   += '<TR VALIGN="top"><TD CLASS="hover_middle" ID="hover_container_middle">';
				r_body  = 1;
			}
			innrhtml   += "<B>" + replaceNoCase(schedulerResults.getField(r, 'headline'),"case_name=","","all") + "</B><BR>" + schedulerResults.getField(r, 'time_start').format('h:nn') + "-" + schedulerResults.getField(r, 'time_stop').format('h:nn') + "<BR><BR>";

			// array of times not available
			r_start     = schedulerResults.getField(r, 'time_start');
			r_time      = r_start.format('hhhnn');
			ampm        = r_start.format('a/p');

			if (ampm != 'a') {
				r_time  = parseInt(r_time);
				r_time  = (r_time < 1200) ? r_time + 1200 : r_time;
			}
			if ((r_time / 100) != Math.round((r_time / 100))) { r_time = parseInt(r_time) + 20; }
			r_times[t]  = parseInt(r_time);
			t          += 1;
			r_times[t]  = parseInt(r_time) + 50;
			t          += 1;
			r_times[t]  = parseInt(r_time) + 100;
			t          += 1;
			r_times[t]  = parseInt(r_time) + 150;
			t          += 1;
		}
	}

	if (hasAdminRights != true) {
		innrhtml = '<B><U>Available ' + type + ' Times</U></B><BR>';
		for (a; a <= 1500; a = a + 50) {
			a1                = a;
			a2                = a + 50;
			a3                = a + 100;
			a4                = a + 150;
			timeAvail         = true;
			t                 = 0;

			for (t in r_times) {
				if (r_times[t] == a1 || r_times[t] == a2 || r_times[t] == a3 || r_times[t] == a4) {
					timeAvail = false;
				}
			}
			if (timeAvail == true) {
				a_start       = translateTime(a1);
				a_stop        = translateTime(a4 + 50);
				innrhtml    += "<B>" + a_start + " - " + a_stop + "</B><BR>";
			}
		}
	}
	
	if (r_body == 1) {
		html  += innrhtml + '</TD></TR>';
	} else {
		r_date = new Date(thisdate.split("-")[0], (thisdate.split("-")[1] - 1), thisdate.split("-")[2]);
		html  += '<TR VALIGN="top"><TD CLASS="hover_cap" ID="hover_container_top">' + parent.hoverTopImage + '</TD></TR><TR VALIGN="top"><TD CLASS="hover_title" ID="hover_container_title">' + r_date.format('ddd mmm dd') + '</TD></TR>';
		if (hasAdminRights == true) { innrhtml = '&nbsp;'; }
		html  += '<TR VALIGN="top"><TD CLASS="hover_middle" ID="hover_container_middle">' + innrhtml + '</TD></TR>';
	}

	html += '<TR VALIGN="top"><TD CLASS="hover_cap" ID="hover_container_bottom">' + parent.hoverEndImage + '</TD></TR></TABLE>'
	
	parent.showMouseover(evt,html);
}

// converts a number to a time formatted string, i.e. 900 becomes 9:00 AM
function translateTime(thistime) {
	var timehour   = (thistime / 100).toString();
	var ishalfhour = timehour.indexOf('.5');
	var timeminute = ':00';
	var timeperiod = ' AM';
	var timestring = '';

	if (ishalfhour > 0) { timehour   = timehour.replace('.5',''); timeminute = ':30'; }
	if (timehour > 12)  { timeperiod = ' PM'; timehour = timehour - 12; }

	timestring     = timehour + '' + timeminute + '' + timeperiod;
	
	return timestring;
}

// change iframe src
function changeSchedulerSrc(date) {
	// initialize variables
	var schedulerFrame = parent.document.getElementById('schedulerFrame');
	var newSrc         = '/templates/scheduler/scheduler_panel.cfm';
	var this_date      = createDateObject(date).format('yyyy-mm-dd');

	if (arguments.length >= 2) { newSrc = arguments[1]; }

	// change the src attribute of the iframe
	newSrc             = newSrc + '/date/' + this_date;
	schedulerFrame.src = newSrc;
}
