<!-- 
// (c) 1999-2003 Live365, Inc.  All rights reserved.

var gServer = "www.live365.com";
//gServer = "fc02.corp.nanocosm.com";

var gTimeoutID=-1;                      //for testing
var gEarlyAlert=0;                      //set to 2 or 3 minutes for release, causes
                                        //the challenge to popup early before NC stops things

function ActivateWarning(fOn,player, playerframe) 
{
	if (top.frames)	{
		if (!playerframe)
			playerframe= parent.frames.Live365PlayerVolume;

		if (playerframe) {
			if (playerframe.PlayerActivateWarning) {
				playerframe.PlayerActivateWarning(fOn,player);
			}
		}
	}
}


function KillWindow() 
{
	if (gDemoWindow != null) {
		if (gDemoWindow.closed == false) {
			gDemoWindow.close();
			gDemoWindow = null;
		}
	}
}


//
// LaunchStream()
//   streamUrl - can be passed in directly or retreived from streamInfo
//   aribtron - should turn off for listening setup
//
function LaunchStream(streamUrl, arbitron, player, ncSessionKey, playerframe, bWarning, stationID, playlist)
{
	if (LaunchStream.arguments.length < 4)
		ncSessionKey="";

	if (LaunchStream.arguments.length < 6)
		bWarning=true;

	if (LaunchStream.arguments.length < 7)
		stationID='';

	if (LaunchStream.arguments.length < 8)
		playlist='';

	if (!playerframe)
		playerframe= parent.frames.Live365PlayerVolume;
	
	var stream = "";

	if (frames && streamUrl && streamUrl != ":") {
		stream = GetStreamURL(streamUrl, player, ncSessionKey, stationID, playlist);

		if (stream != "" && playerframe) {
			if (player == "MP3") 
				playerframe.document.location.replace(stream);
			else if (player == "HOST") {
				var vHost = top.GetHostApp();
				if (vHost.bPreplay)
					return;

				if (bWarning)
					playerframe.ActivateWarning(true, player, playerframe);

				vHost.PlayFn(stream);
			}
			else {
				if (bWarning)
					playerframe.ActivateWarning(true, player, playerframe);

				playerframe.Play(stream);
			}

            SetStreamTimeout();
		}
	}
}

//simple front-end to setTimeout() 

function SetStreamTimeout()
{
	var timeout=parent.gStreamTimeout;

// alert("StreamTimeout="+timeout);

    if(gTimeoutID>=0)
        window.clearTimeout(gTimeoutID);

    if(timeout>0)
        gTimeoutID=window.setTimeout('OpenTimeoutWindow();', timeout-gEarlyAlert);
}

//Opens up the timeout window which waits for the user to respond. If nobody
//responds in a few minutes, the stream is shutdown

function OpenTimeoutWindow()
{
    var url='http://'+gServer+'/mini/timeout_challenge.html';

    window.open(url,'','width=300,height=300');
}

function LaunchAltStream(addr, args, player, plrLevel, altPlaylist, stationName, site, bPlay) 
{
	var stream = GetStreamURL(addr, player);

	if (LaunchAltStream.arguments.length < 8)
		bPlay=true;

	if (top.streamType == 'ODA')
		top.frames.Live365PlayerPlaylist.CloseSession();
	
	//if (plrLevel == '')
	//	top.RotateAd(top.displayType);

	if (player == "HOST") {
		var playerframe= parent.frames.Live365PlayerVolume;
		playerframe.ActivateWarning(false, player, playerframe);
		var vHost = top.GetHostApp();
		vHost.LaunchStreamByName(stationName, site);
		return;
	}

	if (stream && stream != "" ) {
		if (parent.frames.Live365PlayerVolume) {
				
			parent.frames.Live365PlayerStreamState.document.location.replace('/cgi-bin/mini.cgi?templateid=altstream&site=' + site + '&membername=' + stationName+'&playlist='+altPlaylist);
			
			/*if (bPlay) {
				if (player == "MP3")
					parent.frames.Live365PlayerVolume.document.location.replace(stream);
				else
					parent.frames.Live365PlayerVolume.Play(stream);
			}*/
		}
	}
}


function LaunchAltStreamByID(player, plrLevel, altPlaylist, streamid, site) 
{
	if (top.streamType == 'ODA')
		top.frames.Live365PlayerPlaylist.CloseSession();	

	//if (plrLevel == '')
	//	top.RotateAd(top.displayType);

	if (streamid && streamid != "" ) {
		if (parent.frames.Live365PlayerVolume){
			parent.frames.Live365PlayerStreamState.document.location.replace('/cgi-bin/mini.cgi?templateid=altstream&site=' + site + '&stream=' + streamid+'&playlist='+altPlaylist);
		}
		else {
			//Live365PlayerVolume can be NULL if we are trying to launch a stream into a 
			//mini window that is done displaying an error message, in which case, there is no
			//PlayerVolume subframe (generated in index_tmpl.html)

			top.location.replace('/cgi-bin/mini.cgi?stream='+streamid);
		}
	}
}

function GetCookieVal(offset) 
{
	var endstr = document.cookie.indexOf(";", offset);

	if (endstr == -1)
		endstr = document.cookie.length;

	return unescape(document.cookie.substring(offset, endstr));
}


function SetCookie(name, value, expires, path, domain, secure) 
{
	document.cookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");

	return true;
}


function GetCookie(name) 
{
	var j;
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;

	while (i < clen) {
		j = i + alen;

		if (document.cookie.substring(i, j) == arg)
			return GetCookieVal(j);

		i = document.cookie.indexOf(" ", i) + 1;

		if (i == 0) 
			break; 
	}

	return null;
}


function BuyIt(action, which)
{
	var searchTerm;
	var playlistItem;
	var url = "http://" + gServer + "/cgi-bin/";

	if (action == "buy") 
		url += "storesearch.cgi";
	else if (action == "wishlist")
		url += "wishlist_add.cgi";
	else
		url += "applause.cgi";

	url += "?station_id=";
	
	if (parent) {
		url += (parent.stationID) ? parent.stationID : '0';

		if (parent.streamGenre)
			url += "&genre=" + escape(parent.streamGenre);

		if (parent.memberName)
			url += "&station=" + escape(parent.memberName);
	}
	else
		url += "0&station=0";

	if (action == "thumbs_up")
		url += "&event_type=UP";
	else if (action == "thumbs_down")
		url += "&event_type=DOWN";

	var name = GetListenerName();

	if (name != '')
		url += "&member=" + name;

	if (which == 1)
		playlistItem = playlist.one;
	else if (which == 2)
		playlistItem = playlist.two;
	else if (which == 3)
		playlistItem = playlist.three;

	TrimPlaylistItem(playlistItem);

	if (playlistItem.artist != "")
		url += "&artist=" + escape(playlistItem.artist);
	
	if (playlistItem.album != "")
		url += "&album=" + escape(playlistItem.album);
	
	if (playlistItem.title != "")
		url += "&track=" + escape(playlistItem.title);

	if (playlistItem.filename != "")
		url += "&file=" + escape(playlistItem.filename);

	if (playlistItem.url != "")
		url += "&url=" + escape(playlistItem.url);
	else if (parent && parent.ecommUrl && parent.ecommUrl != "")
		url += "&url=" + escape(parent.ecommUrl);

	if (playlistItem.time != "") {
		var arr = playlistItem.time.split(":");
		var secs = 0;
		for (var i = 0; i < arr.length; i++) {
			secs *= 60;
			secs += parseInt(arr[i], 10);
		}
		url += "&time=" + secs;
	}

	return url;
}


function WishlistIt(which)
{
	if (parent && !parent.loggedIn)
		alert("You must be logged in to use the Wishlist feature!");
	else {
		var sink = new Image();
		sink.src = BuyIt("wishlist", which);
		alert("Thank you for wishlisting this track!");
	}
}


function RateIt(up, which)
{
	if (parent && !parent.loggedIn)
		alert("You must be logged in to rate tracks!");
	else {
		var sink = new Image();
		sink.src = BuyIt(up ? "thumbs_up" : "thumbs_down", which);
		alert("Thank you for rating this track.  " + (up ? "Glad you like it!" : "Sorry it's not to your taste."));
	}
}


function Trim(s)
{
	return s.replace(/^[\s]+|[\s]+$/, "");
}


function TrimPlaylistItem(item)
{
	item.artist = Trim(item.artist);
	item.title  = Trim(item.title);
	item.album  = Trim(item.album);
	item.time   = Trim(item.time);
	item.url    = Trim(item.url);
}


function GetPlaylistItem(playlistItem, cls, na_msg)
{
	TrimPlaylistItem(playlistItem);

	if (playlistItem.artist == "ID/PSA" && playlistItem.album == "ID/PSA") {
		playlistItem.artist = "";
		playlistItem.album = "";
	}

	var hasArtist	= (playlistItem.artist != "");
	var hasTitle	= (playlistItem.title  != "");
	var hasAlbum	= (playlistItem.album  != "");
	var hasTime		= (playlistItem.time != "" && playlistItem.time != "0" && playlistItem.time != "0:0" && playlistItem.time != "0:00" && playlistItem.filename != "admin cgi");
	var hasURL		= (playlistItem.url != "");
	var url			= playlistItem.url;
	var trackname 	= "";
	
	if (playlistItem.trackType != "user") {		

		trackname += "[" + playlistItem.trackType + "] ";

		if (hasURL)
			trackname += '<a class="embeddedlink" href="' + playlistItem.url + '" target="_blank">';

		if (hasTitle)
			trackname += "<I>" + playlistItem.title + "</I>";

		if (hasURL)
			trackname += '</a>';
	} 
	else {
		if (playlistItem.filename != "" || hasTitle || hasArtist || hasAlbum) {
			if (hasTitle || hasArtist || hasAlbum) {
				if (hasArtist)
					trackname += playlistItem.artist;

				if (hasTitle) {
					if (hasArtist)
						trackname += " - ";
		
					var title = playlistItem.title.replace(/\<a /ig, "<a class='embeddedlink' ");

					trackname += "<I>" + title + "</I>";
				} 
				else if (hasArtist || hasAlbum) {
					if (hasArtist)
						trackname += " - ";

					trackname += " Unknown ";
				}
			}
			else
				trackname += playlistItem.filename + " ";
				
			if (hasTime)
				trackname += " [" + playlistItem.time + "] ";

			if (hasAlbum)
				trackname += " - " + playlistItem.album;
		} 
		else
			trackname = na_msg;
	}

	return trackname;
}


function DrawPlaylistItem(which, playlistItem, showButtons, imageDir)
{
	var showBuyButton  = showButtons;
	var showWishButton = showButtons;

	if (showButtons.indexOf('Z') != -1) { // backwards compatible
		showBuyButton  = (showButtons.indexOf('B') != -1);
		showWishButton = (showButtons.indexOf('W') != -1);
	}
	
	if (playlistItem.artist == "ID/PSA" && playlistItem.album == "ID/PSA") {
		showBuyButton  = false;
		showWishButton = false;
	}

	var fAnchor		= false;
	var cls			= "doneplaying";
	var na_msg		= "";
	var buy_img		= "buybutton-dim.gif";
	var up_img		= "thumbsup-mini-dim.gif";
	var down_img	= "thumbsdown-mini-dim.gif";
	var wish_img	= "wishlist-add-mini-dim.gif";

	if (which == 1) {
		cls			= "nowplaying";
		na_msg		= "Playlist not available";
		buy_img		= "buybutton.gif";
		up_img		= "thumbsup-mini.gif";
		down_img	= "thumbsdown-mini.gif";
		wish_img	= "wishlist-add-mini.gif";
	}

	var trackname = GetPlaylistItem(playlistItem, cls, na_msg);

	if (playlistItem.trackType == "user")
		fAnchor = (trackname != na_msg);

	var s = '<TR>';

	s += '<td valign="middle" class="' + cls + '" width="';	
	if (fAnchor && showBuyButton)
		s += '33"><a href="' + BuyIt("buy", which) +'" target="Live365_Store_Window"><img alt="Click to buy ' + playlistItem.album + '" border="0" width="28" height="13" src="' + imageDir + buy_img + '"></a>';
	else
		s += '1"><img src="http://' + gServer + '/images/dot.gif" height="13" width="1">';
	
	s += '</td><td valign="middle" class="' + cls + '">';	

	if (trackname.length > 0)
		s += trackname;
	else
		s += '<img src="http://' + gServer + '/images/dot.gif" height="1" width="1">';

	s += '</td>';

	s += '<td class="' + cls + '" align="right" nowrap width="';
	if (fAnchor && showWishButton) {
		s += '45">';
		s += '<a target="_self" href="javascript:WishlistIt(' + which + ')"><img alt="Add track to Wishlist" border="0" width="14" height="14" src="' + imageDir + wish_img + '"></a>';
		s += '<a target="_self" href="javascript:RateIt(1, ' + which + ')"><img alt="I like it, thumbs up!" border="0" hspace="3" width="12" height="15" src="' + imageDir + up_img + '"></a>';
		s += '<a target="_self" href="javascript:RateIt(0, ' + which + ')"><img alt="I dislike it, thumbs down!" border="0" width="12" height="15" src="' + imageDir + down_img + '"></a>';
	}
	else
		s += '1"><img src="http://' + gServer + '/images/dot.gif" height="15" width="1">';

	s += '</td>';
		
	s += '</TR>';

	document.write(s);
}


function DrawPlaylist(playlist, showBuy, showWish, imageDirectory)
{
	var showButtons	= "Z";
	
	if (showBuy != "N") 
		showButtons += "B";
	
	if (showWish != "N")
		showButtons += "W";
	
	var imageDir	= (imageDirectory) ? "http://" + gServer + imageDirectory  : "http://" + gServer + "/mini/images/";
	
	DrawPlaylistItem(3, playlist.three, showButtons, imageDir);
	DrawPlaylistItem(2, playlist.two,   showButtons, imageDir);
	DrawPlaylistItem(1, playlist.one,   showButtons, imageDir);
}


//
// UpdatePlaylist() - Called to refresh song playlist.  Also rotates ad.
//
function UpdatePlaylist()
{
	var currentDate = new Date();
	var now = currentDate.getTime();
	var playUrl = "http://" + gServer + "/pls/front?handler=playlist&cmd=view&handle=" + gMemberName + "&site=" + gMySite + "&time=" + now;

	top.RotateAd(top.displayType);
	
	if (parent.frames.SongInfo && parent.frames.StreamInfo1.gStreamId)
		parent.frames.SongInfo.document.location.replace(playUrl);
}


//
// RotateAd() - Called whenever user interacts with mini-window.
//
function RotateAd(displayType)
{
	var currentDate = new Date();
	var now = currentDate.getTime();

	if (parent.frames.Live365PlayerAdOrMessage1 && (displayType == "SCP" || displayType == "LIVE365"))
		parent.frames.Live365PlayerAdOrMessage1.window.location.href = "http://" + gServer + "/mini/adormessage1.html?x=" + now;		

	return true;
}


function RotateSponsor(displayType)
{
	var currentDate = new Date();
	var now = currentDate.getTime();

	if (parent.frames.Live365PlayerSponsorOrLogo && (displayType == "SCP" || displayType == "LIVE365"))
		parent.frames.Live365PlayerSponsorOrLogo.window.location.href = "http://" + gServer + "/mini/sponsor.html?x=" + now;		

	return true;
}


function RotateAdAndOpen(url)
{
	top.RotateAd(top.displayType);
	OpenURL(url);
}


function OpenURL(url)
{
	window.open(url,'LinkWindow','width=760,height=600,toolbar=yes,scrollbars=yes,location=yes,resizable=yes,status=yes,menubar=yes');
}

function ConstructStreamArgs(player)
{
	var args = new Array();
	var connector = '';
	var ret = '';

	if (parseInt(top.PreRollID) != 0)	
		args["pid"] = top.PreRollID;

	if (top.AuthType)	
		args["AuthType"] = top.AuthType;

	if (GetCookie("bitrate") != null)
		args["bitrate"] = GetCookie("bitrate");

	if (GetCookie("VisitCount") != null)
		args["VisitCount"] = GetCookie("VisitCount");

	if (player != "MP3") {
		if (GetCookie("LID") != null)
			args["lid"] = GetCookie("LID");

		if (GetCookie("SaneID")!= null)
			args["SaneID"] = GetCookie("SaneID");

		var name = GetListenerName();

		if (name != '')
			args["membername"] = name;
	}

	for (var v in args) {
		ret += connector + v + "=" + escape(args[v]);
		connector = "&";
	}

	if (player == "365") {
		if (top.nanocaster_params)
			ret += connector + top.nanocaster_params;
	}
	else if (top.play_params)
		ret += connector + top.play_params;

	return ret;
}


function GetListenerName()
{
	var name = '';

	if (GetCookie("sessionid") != null) {
		var sid = GetCookie("sessionid");
		sid = unescape(sid);
		var sidInfo = sid.split(":");
		if (sidInfo.length >= 2)
			name = sidInfo[0];
	}

	return name;
}
				
				
function GetStreamURL(addr, player, ncSessionKey, stationID, playlist)
{
	var StreamAddress;
	var connector = "?";
	var args = "";
	var escaddr = escape(addr);

	if (GetStreamURL.arguments.length < 4 || GetStreamURL.arguments[3] == "")
		stationID = '';

	if (GetStreamURL.arguments.length < 5 || GetStreamURL.arguments[4] == "")
		playlist = '';

	args += "&"+ ConstructStreamArgs(player);

	if (args.indexOf('session=') == -1 && GetStreamURL.arguments.length >= 3 && GetStreamURL.arguments[2])
		args += "session=" + escape(ncSessionKey);

	if (player == "G2" || player == "WMP")
		return "http://" + gServer + "/cgi-bin/play.m3u?addr=" + escaddr +"&" +args+ "&file=filename.m3u";
	else if (player == "365") {
		var P365Img = new Image();
		P365Img.src = "http://" + gServer +"/trkr/play.cgi";
		return "http://" + addr+ "/play?"+args;
	}
	else if (player == "S365") {
		var P365Img = new Image();
		P365Img.src = "http://" + gServer +"/trkr/play.cgi";
		return "http://" + gServer + "/cgi-bin/play.365e?lssetup=Y"; // only ever for ls-setup
	}

	if (stationID != "") {
		if (playlist != "") {
			if (args.indexOf('addr=') == -1)
				args += "&addr=" + escaddr;

			if (args.indexOf('playlist=') == -1)
				args += "&playlist=" + playlist;
		}

		return "http://" + gServer + "/cgi-bin/play.pls?stationid=" + stationID +"&"+args+"&filename.pls";;
	}

	return "http://" + gServer + "/cgi-bin/play.pls?addr=" + StreamAddress + escape(document.cookie) +"/&file=filename.pls";
}


function EMailBroadcast(stationPage)
{
	if (EMailBroadcast.arguments.length < 1 || EMailBroadcast.arguments[0] == "")	// this param handles when called from station page
		stationPage = false;

	var broadcastName;
	var mailURL;
	var to				= "";
	var subject 		= escape("Live365.com online radio station");
	var body 			= "";
	var homeURL;

	if (stationPage) {
		broadcastName 	= gBc;
		homeURL			= "http://" + gServer + "/stations/";
		if (broadcastName != "" && gSite == "..")
			homeURL += broadcastName;			
		else 
			homeURL += broadcastName += "?site=" + gSite;			
	} 
	else {
		broadcastName	= parent.frames.StreamInfo1.gMemberName;
		homeURL			= "http://" + gServer + "/cgi-bin/directory.cgi";
		if (broadcastName != "")
			homeURL += "?autostart=" + broadcastName;
		top.RotateAd(top.displayType);
	}
		
	body += escape("Check out what I'm listening to right now at " + homeURL + " . ");
	body += escape("It's an online radio station on Live365.com.  Just click on the link to listen.");
									// to, subject, and body are required fields
	mailURL = 'mailto:' + to + '?subject=' + subject + '&body=' + body;

	document.location = mailURL;
} // EMailBroadcast

// -->
