var cHostName = "www.live365.com";

//
// HandleClick - takes genre id as only parameter.  Will launch directory.cgi with identical 
// parameters as existing page (using gXXX context variables), but with new genre specified.
//
function HandleClick(genreId)
{
	var url;

	url = "http://" + cHostName + "/cgi-bin/directory.cgi?genre=" + genreId;

	if (gMaxSpeed > 0 && gMaxSpeed != 256)
		url += '&maxspeed=' + gMaxSpeed;
		
	if (gOrder != "" && gOrder != "N:I:D:H:G:R:L")
		url += '&order=' + gOrder;

	if (gSort != "" && gSort != "L:D")
		url += '&sort=' + gSort;

	if (gSource != "")
		url += '&source=' + gSource;

	if (gSearchDesc != "")
		url += '&searchdesc=' + gSearchDesc;

	if (gText != "")
		url += '&text=' + gText;

	document.location.href = url;
}


function HandleClickItem(genreId)
{
	gSearchDesc = "";
	HandleClick(genreId);
}


function DrawItem(index)
{
	var isCurrent = (gGenre == gTopGenreIDs[index]);
	var id = "cell" + index;
	var c = (isCurrent) ? "selected" : "normal";

	if (navigator.platform != "Win32" && navigator.appName != "Netscape") { // Mac IE4 doesn't like CSS
		document.write('<A style="font-family: verdana,helvetica,geneva,arial,sans-serif; font-size: 12pt; font-weight: bold;');

		if (isCurrent)
			document.write('color: #003399; background: #ffffff; text-decoration: none" ');
		else
			document.write('color: #ffffff; background: #0066ff" ');

		document.write('id="' + id + '" href="javascript:HandleClickItem(\'' + gTopGenreIDs[index] + '\')">');

		document.write(gTopGenres[index]);
	}
	else {
		document.write('<A class="piano" id="' + id + '" href="javascript:HandleClickItem(\'' + gTopGenreIDs[index] + '\')">');
		document.write(gTopGenres[index]);
	}
		
	document.write('</A>');
	document.write('<BR>');

	return true;
}


function DrawTable()
{
	var i;
	var alignment = (navigator.appName == "Netscape") ? "left" : "right";

//	document.write('<FORM>');

	if (parent) {
  		for (i = 0; i < gTopGenres.length; i++)
			DrawItem(i);
	}

	document.write('<img src="http://' + cHostName + '/images/dot.gif" border="0" vspace="0" height="10"><BR>');

	DrawGenreSelector(false);

//	document.write('</FORM>');
	document.write('<BR>');

	return true;
}


function HandleGenreSelector(fAll, index)
{
	var value;

	if (fAll)
		value = gGenreIDs[index];
	else
		value = gAdditionalGenreIDs[index];

	HandleClickItem(value);
}


function DrawGenreSelector(fAll)
{
	var i;
	document.write('<SELECT NAME="fast" ONCHANGE="HandleGenreSelector(' + fAll + ', this.form.fast.selectedIndex - 1)">');
	
	if (fAll) {
		document.writeln('<OPTION VALUE="top10main" id="searchinput">Pick a genre');
		for (i = 0; i < gGenres.length; i++) 
			document.writeln('<OPTION VALUE="' + gGenreIDs[i] + '">' + gGenres[i]);
	}
	else {

		document.writeln('<OPTION VALUE="top10main">other genres');

		for (i = 0; i < gAdditionalGenres.length; i++) 
			document.writeln('<OPTION VALUE="' + gAdditionalGenreIDs[i] + '">' + gAdditionalGenres[i]);
	}

	document.writeln('</SELECT>');
}


function DrawGenreSelections()
{
	DrawNamedGenreSelections("MainCategoryBox")
}


function DrawNamedGenreSelections(boxName, prompt1, action, current, showSpecials)
{
	var i;
	
	if (!boxName)
	   boxName = "MainCategoryBox";

	if (!prompt1)
		prompt1 = "Pick a genre";

	if (!action)
		action = "setFirstKeyWord";

	if (!current)
		current = gGenreIDs[0];
	   
	document.writeln('<SELECT NAME=' + boxName + ' id="searchinput" ONCHANGE="' + action + '()">');
	document.writeln('<OPTION VALUE="" id="searchinput">' + prompt1);

	for (i = (showSpecials) ? 0 : gSpecialGenres; i < gGenres.length; i++) {	// skip special entries
		if (gGenreIDs[i] == current)
			document.writeln('<OPTION VALUE="' + gGenreIDs[i] + '" SELECTED>' + gGenres[i]);
		else
			document.writeln('<OPTION VALUE="' + gGenreIDs[i] + '">' + gGenres[i]);
	}

	document.writeln('</SELECT>');
}


//
// GetGenreIndex - Given a genre, determine its index.  Returns -1 if not found.
//
function GetGenreIndex(genre)
{
	var i;

	for (i = 0; i < gGenreIDs.length; i++) {
		if (genre == gGenreIDs[i])
			return i;
	}

	return -1;
}


//
// DrawDate - Draw current time in HH:MM [am/pm] DD MMM YY format.
//
function DrawDate()
{
	var days = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
	var months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	var t = new Date();
	var h = t.getHours();
	var ampm = (h < 12) ? "am" : "pm";
	var m = t.getMinutes();
	var extra1 = (m < 10) ? "0" : "";
	var y = t.getYear()
	var extra2 = "";

	if (y > 99) {
		y -= 2000;

		if (y < 10)
			extra2 = "0";
	}

	if (h == 0)
		h = 12;
	else if (h >= 13)
		h -= 12;

	document.write(h + ':' + extra1 + m + ampm + '&nbsp;'); // &nbsp;' + days[t.getDay()] + '&nbsp;' + t.getDate() + '&nbsp;' + months[t.getMonth()] + '&nbsp;' + extra2 + y + '&nbsp;');
}


function GetGenreName(index)
{
	if (index == 32000)
		return "Search Results";
	else if (index < 0)
		return gGenres[-index];
	else
		return gTopGenres[index];
}


function GetGenreId(index)
{
	if (index == 32000)
		return "All";
	else if (index < 0)
		return gGenreIDs[-index];
	else
		return gTopGenreIDs[index];
}


function GetGenreNameFromId(id)
{
	var i;
    var tag = escape(id);

    if (tag == "")
		tag = "All";
    else if (tag == "hip-hop")	// hack
		tag = "hip%2dhop";

	for (i = 0; i < gGenreIDs.length; i++) {
		if (tag == gGenreIDs[i])
			return gGenres[i];
	}

	return "Search Results";
}



