//<!--
// Multimedia Listings Javascripts
// -------------------------------
// Copyright 2005 Sherman J. Silber.  The Infertility Center of Saint Louis.  http://www.infertile.com


/* globals declarations */
var allItems = new Array();
var bgColorA = '#CCCCCC';
var bgColorB = '#99CCCC';
var itemBackgroundColor = bgColorA;  // Initial color, then it is alternated by printTextListingOfCategory() below.
var movieDIVNumber = 0; // Allows for multiple movie frames on one page.  See printVideoChoices(), printAudioChoices() below.

// Reads all the listings data into a 2d array.
function parse_ctg_X_containingUpTo_itm_Y(ctg_id, highest_itm_id) {
	allItems[ctg_id] = new Array();
	for (var i = highest_itm_id ; i >= 0 ; i--) {
		allItems[ctg_id][highest_itm_id - i] = parseItemData(ctg_id,i);  // Note that here we are putting the highest item id as the
																		 // 0th element of the array, and as item id decreases, array 
																		 // element # increases. (This is in order later to easily
																		 // print out items in descending order of their id.)
	}
}

function parseItemData(ctgX, itmY) {
	var temp = new Array();
	var basename = 'ctg' + ctgX + '_itm' + itmY + '-';
	temp['key']  = document.getElementById(basename + 'key' ).innerHTML
	temp['page'] = document.getElementById(basename + 'page').href;
	temp['imag'] = document.getElementById(basename + 'imag').href;
	temp['desc'] = document.getElementById(basename + 'desc').innerHTML;
	temp['date'] = document.getElementById(basename + 'date').innerHTML;
	temp['time'] = document.getElementById(basename + 'time').innerHTML;  // Text describing the runtime of the video.
	temp['type'] = document.getElementById(basename + 'type').innerHTML;
	temp['basename'] = basename;
	return temp;
}

/* Useful when categories correspond to columns in a table (which html demands be printed row-by-row. */ 
function lengthOfLongestCategory() {
	var biggest = 0;
	for (var c=0; c < allItems.length; c++) {
		if (allItems[c].length > biggest)  biggest = allItems[c].length;
	}
	return biggest;
}

// BEGIN IMAGE-FILLED PRINT FUNCTIONS
function printTheImageFilledRows(columnWidth) {
	var maxNumOfRows = lengthOfLongestCategory();
	for (var k=0; k < maxNumOfRows; k++) {
		printImageFilledRow(k, columnWidth);
	}
}
function printImageFilledRow(i, columnWidth) {
	document.write('<tr valign="top">');
	for (var c=0; c < allItems.length; c++) {
		document.write('<td align="left" width="' + columnWidth + '">');
		if (allItems[c][i]) {
			if (allItems[c][i].page  &&  allItems[c][i].imag) {
				document.write('<a href="' + allItems[c][i].page + '"><img src="' + allItems[c][i].imag + '" border="1"></a>');
				document.write('<br>');
			} else if (allItems[c][i].imag) {
				document.write(allItems[c][i].imag);
				document.write('<br>');
			}
			if (allItems[c][i].desc) {
				document.write('<span class=\"verdanaGray11pixel\">' + allItems[c][i].desc + '</span>');
				document.write('<br>');
			}
			if (allItems[c][i].date) {
				document.write('<span class=\"verdanaBlue11pixel\">' + allItems[c][i].date + '. </span>');
			}
			if (allItems[c][i].time) {
				document.write('<span class=\"verdanaBlue11pixel\">(' + allItems[c][i].time + ')</span>');
			}
			document.write('<br><br>');
		}
		document.write('</td>'); 
		document.write('<td></td>');
	}
	document.write('</tr>');
}
// END IMAGE-FILLED PRINT FUNCTIONS


// BEGIN TEXT-ONLY PRINT FUNCTIONS
function printTextListingOfCategory(ctg_ID, tableWidth)
{
	for (var i=0; i < allItems[ctg_ID].length; i++) {
		printTextListingRow(ctg_ID, i, tableWidth, itemBackgroundColor);
		if (itemBackgroundColor == bgColorA) {
			itemBackgroundColor = bgColorB;
		} else {
			itemBackgroundColor = bgColorA;
		}
	}
}
function printTextListingRow(ctg, itm, tableWidth, bgcolor)
{
	var col1width=68, col3width=77;
	document.write('<table width="' + tableWidth + 'border="0" cellpadding="1" cellspacing="1">');
	document.write('<tr>');
	document.write('<td width="68" align="center" valign="top" bgcolor="' + bgcolor + '" class="bodyStyleBasic">');
	if (allItems[ctg][itm].page  &&  allItems[ctg][itm].type) {
		document.write('[<a href="' + allItems[ctg][itm].page + '">play ' + allItems[ctg][itm].type + '</a>]');
	} else if (allItems[ctg][itm].page) {
		document.write('[<a href="' + allItems[ctg][itm].page + '">play</a>]');
	}
	document.write('</td>');
	document.write('<td width="' + (tableWidth - col1width - col3width - 10) + '" align="left" valign="top" bgcolor="' + bgcolor + '" class="bodyStyleBasic">');
	if (allItems[ctg][itm].desc) {
		document.write(allItems[ctg][itm].desc)
	}
	if (allItems[ctg][itm].date) {
		document.write(' ' + allItems[ctg][itm].date + '.');
	}
	document.write('</td>');
	document.write('<td width="77" align="left" valign="top" bgcolor="' + bgcolor + '" class="bodyStyleBasic">');
	if (allItems[ctg][itm].time) {
		document.write('(' + allItems[ctg][itm].time + ')');
	}
	document.write('</td>');
	document.write('</tr>');
	document.write('</table>');
	
}
// END TEXT-ONLY PRINT FUNCTIONS

// EMBEDDING A MOVIE
function embed_multimedia(handle) {
	var h264_28k = document.getElementById(handle.basename + 'h264-28k').href;
	var mpg4_28k = document.getElementById(handle.basename + 'mpg4-28k').href;
	var legacy   = document.getElementById(handle.basename + 'legacy').href;	
	var slowPath = '';
	var fastPath = '';
	var comment  = '';
	
	// Figure out which movies are available (i.e. were their paths found in the listings--which, note--has nothing
	// to do with whether that path actually points to an existant file), by comparing their extension to ".mov".
	var is_h264_listed    = (h264_28k.substr(h264_28k.length - 4) == '.mov') ? true : false;
	var is_mpg4_listed    = (mpg4_28k.substr(mpg4_28k.length - 4) == '.mov') ? true : false;
	var is_legacy_listed  = (  legacy.substr(  legacy.length - 4) == '.mov') ? true : false;
	//alert('hlisted? ' + is_h264_listed + '  mlisted? ' + is_mpg4_listed + '  llisted? ' + is_legacy_listed);
	
	// Default to best movie available.
	if (is_h264_listed) {
		slowPath = h264_28k;
		fastPath = getFastPath(slowPath);
	} else if (is_mpg4_listed) {
		slowPath = mpg4_28k;
		fastPath = getFastPath(slowPath);
	} else if (is_legacy_listed) {
		slowPath = legacy;
	}
	
	// Logic it out and adjust paths and comments if necessary, according to quicktime version.
	// ----------------------------------------------------------------------------------------
	// no-qt OR qt2 or lower
	if (quicktime.version <= 2) {
		comment = 'get_quicktime';

	// qt7 OR HIGHER
	} else if (quicktime.version >= 7) {
		comment = 'no_comment';

	// qt6
	} else if  (quicktime.version >= 6) {
		if (is_h264_listed  &&  is_mpg4_listed) { 
			comment = 'upgrade_optional';
			slowPath = mpg4_28k;
			fastPath = getFastPath(slowPath);
		} else if (is_mpg4_listed  || is_legacy_listed) { 
			comment = 'no_comment';
			if (is_mpg4_listed) {
				slowPath = mpg4_28k;
				fastPath = getFastPath(slowPath);
			} else if (is_legacy_listed) {
				slowPath = legacy;
			}
		} else if (is_h264_listed) {
			comment = 'upgrade_mandatory';
		}

	// qt3-5
	} else { 
		if (!is_legacy_listed  &&  (is_h264_listed || is_mpg4_listed)) {
			comment = 'upgrade_mandatory';
		} else {
			if (is_h264_listed || is_mpg4_listed) comment = 'upgrade_optional';
			slowPath = legacy;
		}
	}
	
	// In the case where no listing was available for the file...
	if (!is_h264_listed  &&  !is_mpg4_listed  &&  !is_legacy_listed)  comment = 'unlisted';
	
	
	// Finally, print it.
	if (handle.type == 'audio') {
		printAudioChoices(comment, legacy);
	} else if (handle.type == 'video') {
		printVideoChoices(comment, slowPath, fastPath);
	}
}

// Returns a handle which gives access to all the info associated with the item of that key. 
function lookupItem(movieKey) {
	for (var c=0; c < allItems.length; c++) {
		if (allItems[c]) {
			for (var i=0; i < allItems[c].length; i++) {
				if (movieKey == allItems[c][i].key)  return (allItems[c][i]);
			}
		}
	}
}
//-->