<!-- start scripting
// (c) 2000-2002 Live365, Inc.  All rights reserved.
// pca.js - draws, decides and stores PCA values.

var gPCABaseURL = "http://www.live365.com";
//var gPCABaseURL = "http://fc02.corp.nanocosm.com";

// Define Globals
var gLeftMargin			= 0;
var gLeftMarginImg		= '<img src="' + gPCABaseURL + '/images/dot.gif" width="' + gLeftMargin + '" height="1">';
var gBoxVertSpacing		= '<img src="' + gPCABaseURL + '/images/dot.gif" width="1" height="10">';
var gRightSideBarWidth	= "100%";
var gCellPadding		= 0;
var gFileName			= document.location.pathname;
var iLastSlash			= gFileName.lastIndexOf("/");
gFileName				= gFileName.slice(iLastSlash + 1);
var gUser 				= DetermineUser("user");			// Get user ID for UserBox
var gSessionId			= DetermineUser("sessionid");	
var gLoggedIn 			= (gUser != "");

function Random(limit) 
{
	var today = new Date();
	var num = today.getTime();
	var num = Math.round(Math.abs(Math.sin(num) * 1000000)) % limit;
	return num;
}

// THIS IS TEMPORARY TO SERVE ONLY SPONSORS UNTIL WE'RE READY TO TURN ON USER BOX FOR THE WHOLE SITE
function TempDrawSideBarRight(content1,content2,redirectURL,suppressLoginBox,suppressSponsor,site,genre,suppressCTA,adNumber)
{
	
	// handle undefined variables		
	if (TempDrawSideBarRight.arguments.length < 1 || TempDrawSideBarRight.arguments[0] == "")
		content1 = false;
	else
	if (TempDrawSideBarRight.arguments.length < 2 || TempDrawSideBarRight.arguments[1] == "")
		content2 = false;
	else
	if (TempDrawSideBarRight.arguments.length < 3 || TempDrawSideBarRight.arguments[3] == "")
		redirectURL = gPCABaseURL + "/home/";
	if (TempDrawSideBarRight.arguments.length < 6)
		site = "..";
	if (TempDrawSideBarRight.arguments.length < 7)
		genre = (content1) ? content1 : "";	
	if (TempDrawSideBarRight.arguments.length < 8 || TempDrawSideBarRight.arguments[7] == "")
		suppressCTA = false;
	if (TempDrawSideBarRight.arguments.length < 9 || TempDrawSideBarRightarRight.arguments[8] == "")
		adNumber = 100;
	if (!displayPreferredAds() &&
		 self.location.href.indexOf("cafeshops") == -1 &&
		 self.location.href.indexOf("trakhelp") == -1) {
			if (!suppressSponsor) {
				DrawSponsorBox(pageLocation,genre,adNumber);
				document.write(gBoxVertSpacing);
			}
	}	

	printBoxes(content1,content2);
}


// DrawSideBarRight() -- top level
//   draws components of sidebar on the right side of the layout.
//   content1 = optional = defines high level organization
//   content2 = optional = defines second tier organization
//   suppressLogin = optional = doesn't display the login box
//   suppressSponsor = optional = doesn't display the sponsor box
//   redirectURL = optional = if not present, will assume redirect back to the page you are currently on.
//   suppressCTA = optional = doesn't display any CTAs.. used for popup displays.
function DrawSideBarRight(content1,content2,redirectURL,suppressLoginBox,suppressSponsor,site,genre,suppressCTA,adNumber) 
{
	// handle undefined variables		
	if (DrawSideBarRight.arguments.length < 1 || DrawSideBarRight.arguments[0] == "")
		content1 = false;
	if (DrawSideBarRight.arguments.length < 2 || DrawSideBarRight.arguments[1] == "")
		content2 = false;
	if (DrawSideBarRight.arguments.length < 3 || DrawSideBarRight.arguments[2] == "")
		redirectURL = gPCABaseURL + "/home/";
	if (DrawSideBarRight.arguments.length < 6)
		site = "..";
	if (DrawSideBarRight.arguments.length < 7)
		genre = (content1) ? content1 : "";	
	if (DrawSideBarRight.arguments.length < 8 || DrawSideBarRight.arguments[7] == "")
		suppressCTA = false;
	if (DrawSideBarRight.arguments.length < 9 || DrawSideBarRight.arguments[8] == "")
		adNumber = 100;
		
	if (!suppressLoginBox) {
		DrawUserBox(redirectURL,site,genre,content1,content2);		// draw UserBox
		document.write(gBoxVertSpacing);
		//if (!gLoggedIn)
		//document.write(gBoxVertSpacing);
	}
	
	if (content1 != "home-sky") {
		DrawSearchBox()
	}
	
	if (content1 == "listen") {
		// DrawLegends is in directory.js
		// gLegend is defined in main_head.html
		
		if (gLegend.indexOf('M') != -1)
			DrawLegends("main", gUser, gRightSideBarWidth);

		if (gLegend.indexOf('L') != -1)
			DrawLegends("listeners", gUser, gRightSideBarWidth);

		if (gLegend.indexOf('R') != -1)
			DrawLegends("ratings", gUser, gRightSideBarWidth);

		if (gLegend.indexOf('A') != -1)
			DrawLegends("attributes", gUser, gRightSideBarWidth);
	}
	
	if (!suppressSponsor)
		DrawSponsorAdBox(pageLocation, genre, adNumber);
		
		
	if (!suppressCTA) {
		var maxCTAs	= 2;
		
		if (content1 == 'listen')
			maxCTAs	= 1;

		printBoxes(content1,content2,maxCTAs);
	}	
}

function DoGenreSearchPca()
{
	var url = gBaseURL + "/cgi-bin/directory.cgi";
	var val;
	
	if (document.SearchFormPca.searchdesc.value != "") {
		val = escape(document.SearchFormPca.searchdesc.value);
		val = val.replace(/[\+]/g, "%2b");

		url += '?genre=search&searchdesc=' + val;

		val = document.SearchFormPca.searchfields.value;
		if (val != "")
			url += '&searchfields=' + val;		
	}

	document.location = url;
	return false;
}


function DoGenreSearchPca()
{
	var url = gBaseURL + "/cgi-bin/directory.cgi";
	var value = GetGenreID(document.forms.SearchFormPca.genre.selectedIndex - 1);
	
	if (value) {
		if (value == "Presets" && !ReadCookie("sessionid")) {
			alert('You must be logged in to go to your list of Presets');
			document.forms.SearchFormPca.genre.selectedIndex = 0;
		   	return false;
		}

		url += '?genre=' + value;		
	}
	
	document.location = url;
	return false;
}


function DrawSearchBox() 
{		
	var s = '';

	s += '<form name="SearchFormPca" action="' + gPCABaseURL + '/cgi-bin/directory.cgi">';
	s += '<input type="hidden" name="searchgenre" value="">';
	s += '<table border="0"  width="100%" cellpadding="0" cellspacing="0">';

	document.write(s);
	DrawTitleRow("Find Your Music","2");

	s = '</table>';
	s += '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
	s += '<tr>';
	s += '<td class="informationbox-content" width="100%">';
	s += 'Browse:<br>';
	document.write(s);
	DrawNamedGenreSelections("genre", 0, "DoGenreSearchPca", -1, 1);

	s = '</td>';
	s += '</tr>';
	s += '</table>';
	s += '<img src="' + gPCABaseURL + '/images/dot.gif" width="1" height="2"><br>';
	s += '<table border="0" cellpadding="4" cellspacing="0" width="100%">';
	s += '<tr>';
	s += '<td class="pca-content" width="100%">';
	s += '<font class="informationbox-content"><b>or</b> Search:</font>&nbsp;&nbsp;(<a href="#advsearch">Advanced</a>)';
	s += '<br><img src="' + gPCABaseURL + '/images/dot.gif" width="1" height="3"><br>';
	s += '<input type="text" name="searchdesc" class="searchinput" size="16" align="absmiddle"> <input type=image src="' + gImageURL + '/navigation/images/go.gif" width="20" height="20" border="0" alt="Go" align="absmiddle"><br>';
	s += '<select name="searchfields" class="searchinput">';
	s += '<option value="TAC" selected>Artists/Tracks/CDs</option>';
	s += '<option value="A">-- Artists only</option>';
	s += '<option value="T">-- Tracks only</option>';
	s += '<option value="C">-- CDs only</option>';
	s += '<option value="">Live365 Stations</option>';
	s += '<option value="L">Locations</option>';
	s += '<option value="H">Broadcasters</option>';			
	s += '</select>';
	s += '</td>';
	s += '</tr>';	
	s += '</table>';
	s += '</form>';

	s += gBoxVertSpacing;
	document.write(s);
}


// DetermineUser()
//   call with flag for what part of the sessionid cookie to read.
function DetermineUser(flag) 
{
	var sid = ReadCookie("sessionid");  // access cookie

	if (sid) {
		if (flag == "user"){
			return sid.slice(0, sid.indexOf(':'));
		} else if (flag == "sessionid")
			return sid.slice(sid.indexOf(':') + 1);
	} else {
		return "";
	}
}


// DrawSponsorBox()
function DrawSponsorBox(sourceFile, genre, maxTiles)
{
	var numTiles = 1; // number of tiles to draw
	
	var tile;
	var firstTile = 2; // tile 1 is reserved for the banner
	var currentDate = new Date();
	var tm = currentDate.getTime();

	if (numTiles > maxTiles)
		numTiles = maxTiles;

	var i = genre.indexOf("-sky");

	if (i != -1) {
		numTiles = 1;
		firstTile = 6;
		genre = genre.substr(0, i);
	}

	document.write('<table border="0" width="100%" cellspacing="0" cellpadding="0">');	
	
	if (numTiles > 1)
		DrawTitleRow("Sponsors");
	else
		DrawTitleRow("Sponsor");

	document.write('<tr><td class="pca-content" align="center">');
	
	document.write('<table border="0" width="100%" cellspacing="0" cellpadding="4">');
	document.write('<tr>');
	document.write('<td align="center" class="pca-content">');
	for (tile = firstTile; tile < firstTile + numTiles; tile++) {
		DrawSponsor(sourceFile, genre, tile, tm);
		
		if (numTiles > 1)
		document.write('<br><img src="' + gPCABaseURL + '/images/dot.gif" width="1" height="5"><br>');	
	}
	document.write('</td>');
	document.write('</tr>');
	document.write('</table>');
	document.write('</td></tr>');
	document.write('</table>');
} // DrawSponsorBox()


// DrawTitleRow()
//   title = required
//   colspan = optional = used to span more than one column as in the user box.
function DrawTitleRow(title, colspan)
{
	if (DrawTitleRow.arguments.length >= 1)
		colspan = 'colspan="' + colspan + '"';
	else
		colspan = '';

	document.write('<tr><td ' + colspan + ' class="pca-header" width="100%">');
	document.write('<table border="0" cellpadding="0" cellspacing="0" width="100%">');
	document.write('<tr>');
		document.write('<td class="pca-header" width="4" align="left" valign="top"><img src="' + gPCABaseURL + '/images/corner-tl-dkgray.gif" width="4" height="4" border="0"></td>');
		document.write('<td class="pca-header" valign="middle" width="100%">');
		document.write(title);
		document.write('</td>');
		document.write('<td class="pca-header" width="1"><img src="/images/dot.gif" width="1" height="20" border="0"></td>');
		document.write('<td class="pca-header" width="4" align="right" valign="top"><img src="' + gPCABaseURL + '/images/corner-tr-dkgray.gif" width="4" height="4" border="0"></td>');
	document.write('</tr>');
	document.write('</table>');
	document.write('</td></tr>');
}

// DrawSubTitleRow()
//   title = required
//   colspan = optional = used to span more than one column as in the user box.
function DrawSubTitleRow(title, colspan)
{
	if (DrawSubTitleRow.arguments.length >= 1)
		colspan = 'colspan="' + colspan + '"';
	else
		colspan = '';

	document.write('<tr align="left"><td  ' + colspan + ' class="pca-content" width="' + gRightSideBarWidth + '">');
	document.write(title);
	document.write('</td></tr>');
}

// DrawByLineRow()
//   byline = required
function DrawBylineRow(byline, colspan)
{
	if (DrawBylineRow.arguments.length >= 1)
		colspan = 'colspan="' + colspan + '"';
	else
		colspan = '';

	document.write('<tr><td ' + colspan + ' class="pca-content">');
	document.write('<table border="0" width="100%" cellspacing="0" cellpadding="4">');
	document.write('<tr>');
	document.write('<td class="pca-content">');
	document.write(byline);
	document.write('</td>');
	document.write('</tr>');
	document.write('</table>');
	document.write('</td></tr>');
}


// DrawUserBox()
//   user = required
//   site = optional
//   genre = optional (used in directory, and changes buttons if on Presets directory)
//   content1 = used for home page
//   content2 = used for "down" state
function DrawUserBox(redirectURL,site,genre,content1,content2)
{
	// initialize vars
	redirectURL = escape(redirectURL);	
	
	var tmd = new Date();
	var tm = tmd.getTime();
	if (DrawUserBox.arguments.length < 2)
		site = "..";
	if (DrawUserBox.arguments.length < 3)
		genre = "";
	if (DrawUserBox.arguments.length < 4)
		content1 = "";		
	if (DrawUserBox.arguments.length < 5)
		content2 = "";
	var target = (site == "..") ? "_self" : "_blank";

	
	document.write('<table border="0"  width="100%" cellpadding="0" cellspacing="0">');
	if (!gLoggedIn) {	
		DrawTitleRow('Log In or <a href="' + gPCABaseURL + '/registration/index.live" target=' + target + '>Sign Up</a>', "2");
	} else {
		DrawTitleRow('You are logged in. <a href="' + gPCABaseURL + '/members/login.live?action=out&path=' + gPath + '">log out</a>', "2");		
	}
		
	if (!gLoggedIn) {
		document.write('<FORM NAME="LoginInForm" ACTION="' + gPCABaseURL + '/cgi-bin/login.cgi" METHOD="post">');
		document.write('<input type="hidden" name="url" value="' + redirectURL + '">');
		document.write('<input type="hidden" name="success" value="' + gPath + '">');
	

	document.write('<tr><td width="100%" align="right" class="informationbox-content">');
	
		document.write('<table border="0" width="100%" cellpadding="4" cellspacing="0">');		
		document.write('<tr>');
			document.write('<td class="pca-content"><font class="informationbox-content">Username</font><br><input type="hidden" name="site" value="' + site + '"><input type="text" name="membername" size="8" maxlength="50"></td>');
			document.write('<td class="pca-content"><font class="informationbox-content">Password</font><br><input type="password" name="password" size="8" maxlength="50"></td>');
		document.write('</tr>');
    	document.write('<tr valign="middle">');
			document.write('<td valign="middle" colspan="2" class="informationbox-content">');
    			document.write('Remember&nbsp;me:<input type="checkbox" name="rememberme" align="absmiddle" value="Y">');
				document.write('<input type="Image" src="' + gPCABaseURL + '/navigation/images/go.gif" alt="Go" width="20" height="20" border="0" align="absmiddle"><br><img src="/images/dot.gif" width="1" height="4" border="0"><br>');
				document.write('<img src="/images/dot.gif" width="1" height="1" border="0"><br>');
				document.write('<font class="footer"><a href="/members/login.live?action=forgot">Forgot your password?</A></font>');			
		document.write('</td>');
	document.write('</tr>');
	
	document.write('</table></form>');	
	document.write('</td></tr></table>');
	} else {
	document.write('<tr><td colspan="2" class="informationbox-content">');
	document.write('<table border="0"  width="100%" cellpadding="4" cellspacing="0"><tr><td class="informationbox-content">Username: ' + gUser + '</td></tr></table>');	
	document.write('</td></tr></table>');
	}
}

// Determine content
function printBoxes(content1,content2, displayLimit, displayPermanent) {
	if (printBoxes.arguments.length < 3 || printBoxes.arguments[2] == "")
		displayLimit		= 2;
//		displayLimit		= aTitle.length;		
	if (printBoxes.arguments.length < 4 || printBoxes.arguments[3] == "")
		displayPermanent	= "";
		
	var randomNumber;
	var alreadyUsed = "";
	var n;
	
	
	
	if (aTitleTop && aBodyTop) {
		document.write('<table border="0" width="100%" cellspacing="0" cellpadding="' + gCellPadding + '">');
		DrawTitleRow(aTitleTop);
		DrawBylineRow(aBodyTop);	
		document.write('</table>');
		document.write(gBoxVertSpacing);
		displayLimit = displayLimit - 1 ;
	} 	

	
	
	// if the inventory is reduced, adjust the overrequest
	if (displayLimit > aTitle.length) {
		displayLimit = aTitle.length;
	}
	
	for (i = 0; i < displayLimit; i++) {

		// don't bother randomizing CTAs if they are all going to be displayed anyway
		if (displayLimit == aTitle.length) {
			n = i;
		} else {

		// generate a random number
		randomNumber = Random(aTitle.length);

		if (i > 0) {
			while(alreadyUsed.search(randomNumber) >= 0) {
				randomNumber = Random(aTitle.length);
			}
		}
		
		// set random number to display index
		n = randomNumber;
		
		// if it's the frist time through and there is a permanent CTA, trump display index	
		if (i == 0) {
			if (displayPermanent != "")
				n = displayPermanent;
		}
		
		// add display index to already used so that it doesn't show up twice
		alreadyUsed += n;

		}
		
				
		if (aTitle[n]) {
			document.write('<table border="0" width="100%" cellspacing="0" cellpadding="' + gCellPadding + '">');
			DrawTitleRow(aTitle[n]);
			DrawBylineRow(aBody[n]);
			document.write('</table>');
			document.write(gBoxVertSpacing);
		} else {
			return false;
		}
	}
}


function DrawSponsorAdBox(page, genre, adNumber)
{
	if (!displayPreferredAds() &&
		 self.location.href.indexOf("cafeshops") == -1 &&
		 self.location.href.indexOf("trakhelp") == -1) {
		DrawSponsorBox(page, genre, adNumber);
		document.write(gBoxVertSpacing);
	}
}

// end scripting -->
