//--------------------------------------------------------------
// Name: Utils.js
// Date: 06/26/05
// Desc: Handy utilities
//
// Copyright (C) 2002-5 nekotech SOFTWARE
//--------------------------------------------------------------

//--------------------------------------------------------------
// Get an object by id
//--------------------------------------------------------------
function GetObjectByID(id)
{
	return(document.getElementById(id));
}

//--------------------------------------------------------------
// Get an image
//--------------------------------------------------------------
function GetImageByName(name)
{
	return(document.images[name]);
}

//--------------------------------------------------------------
// Get a form
//--------------------------------------------------------------
function GetFormByName(name)
{
	return(document.forms[name]);
}

//--------------------------------------------------------------
// Find the first selected radio button in a form
//--------------------------------------------------------------
function FirstCheckedRadio(form, startIndex)
{
	var	i;
	var	element;
	
	for (i = startIndex; i < form.length; ++i)
	{
		element = form.elements[i];
		
		if ((element.type == "radio") &&
			(element.checked == true))
			return(i);	
	}
	
	return(-1);
}

//--------------------------------------------------------------
// Load the arguments from the URL location
//--------------------------------------------------------------
function GetArgs()
{
	var	args = new Object();
	var	query = location.search.substring(1);
	var	pairs = query.split("+");
	
	for (var i = 0; i < pairs.length; ++i)
	{
		var	pos = pairs[i].indexOf('=');
		
		if (pos == -1)
			continue;
			
		var argName = pairs[i].substring(0, pos);
		var	value = pairs[i].substring(pos + 1);
		
		args[argName] = unescape(value);
		
	}
	
	return(args);
}

//--------------------------------------------------------------
// Get the parent URL from url
//--------------------------------------------------------------
function ParentURL(url)
{
	var slashChar = "\/";
	var	slashIdx = url.lastIndexOf(slashChar);
	
	if (slashIdx == url.length)
		slashIdx = url.lastIndexOf(slashChar, url.length - 1);
		
	return(url.substring(0, slashIdx));
}

//--------------------------------------------------------------
// Find the lecture that corresponds to this lecture code, if any
//--------------------------------------------------------------
function LectureFromCode(code)
{
	var	lecture = playlist[code];
	
	return(lecture);
}

//--------------------------------------------------------------
// Find the lecture that corresponds to this segment code, if any
//--------------------------------------------------------------
function LectureFromSegment(code)
{
	var name, list;
	var	hasCode = 0;
	
	for (name in playlist)
	{
		lecture = playlist[name];
		
		if (SegmentIndexForCode(lecture, code) != -1)
		{
			hasCode = 1;
			break;
		}
	}
	
	return(hasCode ? name : "");
}

//--------------------------------------------------------------
// Return the index of the matching segment, -1 if none
//--------------------------------------------------------------
function SegmentIndexForCode(lecture, code)
{
	var	encoded = Encode(code);
	
	for (var i = 0; i < lecture.length; ++i)
	{
		if (encoded == lecture[i])
			return(i);
	}
	
	return(-1);
}

//--------------------------------------------------------------
// Return the index of the next segment, -1 if none
//--------------------------------------------------------------
function NextSegmentIndex(lecture, index)
{
	if (index < (lecture.length - 1))
		return(index + 1);
		
	return(-1);
}

//--------------------------------------------------------------
// Get the base URL of the web page w/o search information
//--------------------------------------------------------------
function BaseURL(url)
{
	var queryIdx = url.indexOf("?");

	return(url.substring(0, queryIdx));
}

//--------------------------------------------------------------
// Get location of the Playlist Root folder
//--------------------------------------------------------------
function PlaylistRootURL(url)
{
	var	scriptsDir = ParentURL(url);
	var	root = ParentURL(scriptsDir);
	
	if (document.location.protocol == "file:")
		root += "/PlaylistsLocal/";
	else
		root += "/Playlists/";
	
	return(root);
}

//--------------------------------------------------------------
// Build a URL for this lecture and index
//--------------------------------------------------------------
function BuildURL(baseURL, lecture, lectureCode, index, playMode)
{
	var	url = baseURL + "?P=" + playMode + "+L=" + lectureCode + "+S=";
	
	if (playMode == "P")
		url += Decode(lecture[index]);
	else
		url += (parseInt(index) + 1);
		
	return(url);
}

//--------------------------------------------------------------
// Show links to the previous segments
//--------------------------------------------------------------
function PreviousSegments(baseURL, lecture, lectureCode, index)
{
	var	str = "";
	
	if (index == 0)
		return("None.<br>");
	
	for (var i = 0; i < lecture.length; ++i)
	{
		if ((index == -1) ||
			(i < index))
		{
			var	newURL = BuildURL(baseURL, lecture, lectureCode, i, "P");
			str += "Segment " + (i + 1) + ": <a href=\"" + newURL + "\">";
			str += Decode(lecture[i]) + "<\/a>" + "<br>\n";
		}
	}

	return(str);
}

//--------------------------------------------------------------
// Build the link to the rm file
//--------------------------------------------------------------
function RealMediaFile(url, lecture, index)
{
	var alpha = "abcdefghijklmnopqrstuvwxyz";
	var rm = url + "/../../Participatory/" + lectureCode + "/" + lectureCode + alpha.substr(index, 1) + ".rm";	

	return(rm);
}

function RealMediaCodeFile(url, index)
{
	var rm = url + "/../../Common/";
	
	if (index == -1)
		rm += "Chirp.rm";
	else
		rm += index + ".rm";

	return(rm);
}

//--------------------------------------------------------------
// Build the player for this link
//--------------------------------------------------------------
function EmbeddedPlayer(playlistURL, lecture, index, playMode, showSlider)
{
	var	str;
	var	rm = RealMediaFile(playlistURL, lecture, index);
	
	if (playMode == "P")
	{
		var	play = "<embed name=\"EmbeddedPlayer\"" + 
//			" scriptcallbacks=\"OnPresentationClosed, OnPresentationOpened\" " +
			" src=\"" + rm + "\"" +
			" type=\"audio/x-pn-realaudio-plugin\" console=\"Clip1\"" +
			" controls=\"PlayButton\" height=\"26\" width=\"45\" autostart=\"true\">";
		var	rew = "<embed type=\"audio/x-pn-realaudio-plugin\" console=\"Clip1\"" +
			" controls=\"RWCtrl\" height=\"26\" width=\"26\" autostart=\"true\">";	
		var	vol = "<embed type=\"audio/x-pn-realaudio-plugin\" console=\"Clip1\"" +
			" controls=\"MuteVolume\" height=\"26\" width=\"88\" autostart=\"true\">";
		var	position = "<embed type=\"audio/x-pn-realaudio-plugin\" console=\"Clip1\"" +
			" controls=\"PositionField\" height=\"26\" width=\"100\" autostart=\"true\">";
		var	slider = "<embed type=\"audio/x-pn-realaudio-plugin\" console=\"Clip1\"" +
			" controls=\"PositionSlider\" height=\"26\" width=\"120\" autostart=\"true\">";
		
		str = rew + play + position + vol;
		
		if (showSlider)
			str += slider;
		
	}
	else if (playMode == "S")
	{
		var play = "<embed name=\"EmbeddedPlayer\"" + 
//			" scriptcallbacks=\"OnPresentationClosed, OnPresentationOpened\"" +
			" src=\"" + rm + "\" type=\"audio/x-pn-realaudio-plugin\" console=\"Clip1\"" +
			" controls=\"PlayButton\" height=\"26\" width=\"45\" autostart=\"true\">";
		var	rew = "<embed type=\"audio/x-pn-realaudio-plugin\" console=\"Clip1\"" +
			" controls=\"RWCtrl\" height=\"26\" width=\"26\" autostart=\"true\">";	
		var	ff = "<embed type=\"audio/x-pn-realaudio-plugin\" console=\"Clip1\"" +
			" controls=\"FFCtrl\" height=\"26\" width=\"26\" autostart=\"true\">";	
		var	vol = "<embed type=\"audio/x-pn-realaudio-plugin\" console=\"Clip1\"" +
			" controls=\"MuteVolume\" height=\"26\" width=\"88\" autostart=\"true\">";
		var	position = "<embed type=\"audio/x-pn-realaudio-plugin\" console=\"Clip1\"" +
			" controls=\"PositionField\" height=\"26\" width=\"100\" autostart=\"true\">";
		var	slider = "<embed type=\"audio/x-pn-realaudio-plugin\" console=\"Clip1\"" +
			" controls=\"PositionSlider\" height=\"26\" width=\"120\" autostart=\"true\">";

		str = play + rew + ff + slider + "&nbsp;" + position + vol;
	}
	else
		str = "Unknown play mode.  Please return to start.";
	
	return(str);
}

//--------------------------------------------------------------
// Check if <code> is the valid code for <lecture>'s <idx>th
// code.  Return <true> if valid, <false> otherwise.  Set
// the value of <statusObj> to be a valid/invalid string.
//--------------------------------------------------------------
function ValidateCodeForLecture(lecture, idx, code, statusObj)
{
	var	result = false;
	var str = "";
	
	if (idx == -1)
		return(false);
	
	if (code == Decode(lecture[idx]))
		result = true;
		
	// Setup the valid text
	if (code.length == 4)
		str = GetCodeValidation(result);
	
	HTMLWrite(statusObj, str);		
	
	return(result);
}

//--------------------------------------------------------------
// Return a formatted string for the validation
//--------------------------------------------------------------
function GetCodeValidation(isValid)
{
	var	str;
	
	if (isValid)
		str = "<font color=\"green\">Valid Code (Hit Return)<\/font>";
	else
		str = "<font color=\"red\">Invalid Code<\/font>";
		
	return(str);
}

//--------------------------------------------------------------
// Return all keys in an associative array
//--------------------------------------------------------------
function GetArrayKeys(assocArray)
{
	var	keys = new Array;
	var	key, idx = 0;
	
	for (key in assocArray)
	{
		keys[idx++] = key;
	}
	
	return(keys);
}

//--------------------------------------------------------------
// Return all keys in an associative array
//--------------------------------------------------------------
function GetArrayDescription(assocArray)
{
	var	keys = GetArrayKeys(assocArray).sort();
	var	i, count = keys.length;
	var	key;
	var	str = "";
	
	for (i = 0; i < count; ++i)
	{
		if (i > 0)
			str += ", ";
			
		str += "['" + keys[i] + "']='" + assocArray[keys[i]] + "'";
	}
	
	return(str);
}
