	function swapselection(elem)
{
	var active = ($F('selection') != '' && elem.id == $F('selection'));

	if (active)
		return;
		
	var isOff = (elem.className == 'listitem' || elem.className == 'listitem_active');
	
	elem.className = (isOff) ? 'listitem_on' : 'listitem';
}

function swapfilter(elem)
{
	var elem_filter = elem.id.substring(5);
	current_filter = ($F('filter') && $F('filter') == elem_filter);

	if (current_filter)
		return;

	var active = (elem.className == 'active');
	elem.className = (active) ? 'inactive' : 'active';
	if ($F("filter").length > 0 && elem.id != $F("filter"))
		elem.className = (active) ? "inactive" : "active";
}

function showmusic(sender)
{
	var id = sender.id;
	var filter = sender.parent;
	
	$('musicbox').innerHTML = '<strong>Loading...</strong><br /><img src="/images/activity/progressbar_green.gif" />'

	var url = '/service/music/get.php';
	var params = 'q=' + id + '&filter=' + filter;
	
	if (sender.page)
		params += '&page=' + sender.page;
	
	new Ajax.Request(url,
	{
		method:'post',
		parameters:params,
		onSuccess:rendersongs,
		onFailure:showFailure
	});

	$(id).className = 'listitem_active';
	$('selection').value = id;

	items = $A($(filter).childNodes);
	items.each(function(item) {
		item.className = (item.id == $F('selection')) ? 'listitem_active' : 'listitem';
	});
}

function rendersongs(t)
{
	var collection = eval(t.responseText);
	var songs = collection.songs;
	var totalsongs = parseInt(collection.total);
	var page = parseInt(collection.page);
	var totalpages = parseInt(collection.totalpages);
	var filter = $F('filter');
	
	$('musicbox').innerHTML = '';
	$('playerwindow').innerHTML = '';
	$('playerwindow').style.display = 'none';

	var html = "<table border='0' cellpadding='0' cellspacing='1'>";
	
	if (songs.length == 0)
		html += "<tr valign='middle' align='left'><td width='450'><strong>Sorry, no songs match your search criteria. Please try again.</strong></td></tr>";
	else
		html += "<tr valign='middle' align='left'><td width='200' class='header'>Title</td><td width='200' class='header'>Artist</td><td class='header' width='50'>Play</td><td class='header' width='70'>Download</td></tr>"

	songs.each(function(song) {
		html += "<tr valign='middle' align='left'>";
		html += "<td width='180'><strong>" + song.title + "</strong></td>";
		html += "<td width='200'>";
		if (song.username)
			html += "<a href='/" + song.username + "'>" + song.stagename + "</a></td>";
		else
			html += song.stagename;
		html += "</td>";
		html += "<td width='50'><a href='javascript:browseplay(" + song.music_id + ");'><img src='/images/icon/play.gif' width='20' height='20' border='0' /></a></td>";
		html += "<td width='70'><a onClick=\"javascript:alert('Thanks for downloading! Please considering making a donation by clicking on the &#34;Donate&#34; link in the navigation at the top of the page.')\" href='/service/music/getSongFile.php?uid=" + song.uid + "&file="+song.file+"'>Download</a></td>";
		html += "</tr>";
	});

	var id = $('selection').value;
	var filter = $('filter').value;

	html += "<tr valign='middle' align='left'><td colspan='4'><span style='font: 11px Verdana;'>" + totalsongs + " songs. Page " + page + " of " + totalpages + ". ";
	
	if (totalpages > 1) {
		for (x = 1; x <= totalpages; x++)
		{
			if (x == page)
				html += x + " ";
			else
				html += "<a href=\"javascript:showmusic({'id': '" + id + "', 'parent': '" + filter + "', page: " + x + "});\">" + x + "</a> ";
		}
	}
	html += "</span></td></tr>";

	html += "</table>";
	
	$('musicbox').innerHTML = html;
}

function showbox(filter)
{
	divs = $A($('browsebox').childNodes);
	divs.each(function(div) {
		if (div.id && div.className == 'list')
			div.style.display = (div.id == filter) ? 'block' : 'none';
	});
	
	divs = $A($(filter).childNodes);
	divs.each(function(div) {
		div.className = 'listitem';
	});

	$('item_genres').className = 'inactive';
	$('item_artists').className = 'inactive';
	$('item_countries').className = 'inactive';
	$('item_' + filter).className = 'active';
	
	$('selection').value = '';
	$('filter').value = filter;
	$('musicbox').innerHTML = '';
	$('playerwindow').innerHTML = '';
	$('playerwindow').style.display = 'none';
}

function browseplay(num)
{
	$('playerwindow').innerHTML = "<iframe id='" + num + "' style='margin-bottom: 1px;' src='/browse/play.php?num=" + num + "' width='450' height='110' border='0' frameborder='0' marginwidth='0' scrolling='no' marginheight='0'></iframe>";
	$('playerwindow').style.display = 'block';
}

function showFailure()
{
	alert('There was an error in your operation. Please try again.');
}

