	var playlist=new Array();
	var currentPlaylistPage=1;
	var playlistPageCount=0;
	var videoCount=0;
	var rotateInterval=null;
	var playerWidth=300;

	function resetPlaylist() {
		playlist=new Array();
	}

	function addPlaylistEntry(hash, title, artist, album, duration) {
		title=unescape(title);
		artist=unescape(artist);
		album=unescape(album);
		obj={};
		obj.hash=hash;
		obj.title=title;
		obj.artist=artist;
		obj.album=album;
		obj.duration=duration;
		playlist.push(obj);
	}

	function renderPlaylist(title) {		
		$('div#relixav div.avcontent-audio div.page-nav ul').empty();
		$('div#relixav div.avcontent-audio div.tracklist').empty();
		
		var pageLink;

		$('#playlist-title').html(unescape(title));

		$('div#relixav div.avcontent-audio div.page-nav ul').append($('<li><a href="#" id="audio-nav-left" class="page-nav-arrow-left">&lt;</a></li>'));
		$('a#audio-nav-left').click(function(){
			playlistNavigateLeft();
		});
				
		var count=0;
		var pageCount=1;
		var list=$('<ul id="page-1"></ul>');
		var list;
		var duration;
		var entryTitle;
		var entryLength=45;
		
		for (i in playlist) {
			if (count==5) {
				$('div#relixav div.avcontent-audio div.tracklist').append(list);
				pageLink=$('<li><a href="#" id="audio-page-link-'+pageCount+'" class="page">'+pageCount+'</a></li>');
				pageCount++;
				list=$('<ul id="page-'+pageCount+'" style="display: none;"></ul>');
				$('div#relixav div.avcontent-audio div.page-nav ul').append(pageLink);
				count=0;
			}
			entry=playlist[i];
			
			duration=String(entry.duration%60);

			if (duration.length<2) duration='0'+duration;
			
			duration=String(Math.floor(entry.duration/60))+':'+duration;
			
			entryTitle=entry.artist+': '+entry.title;
			
			if (entryTitle.length>entryLength) {
				entryTitle=entryTitle.substr(0, 45)+'...';
			}
			
			listItem=$('<li id="track-'+entry.hash+'"><a href="#">'+entryTitle+' <span>'+duration+'</span></a></li>');
			list.append(listItem);
			count++;
		}
		
		$('div#relixav div.avcontent-audio div.tracklist').append(list);
		$('div#relixav div.avcontent-audio div.page-nav ul').append($('<li><a href="#" id="audio-page-link-'+pageCount+'" class="page">'+pageCount+'</a></li>'));

		$('div#relixav div.avcontent-audio div.page-nav ul').append($('<li><a href="#" id="audio-nav-right" class="page-nav-arrow-right">&gt;</a></li>'));
		$('a#audio-nav-right').click(function(){
			playlistNavigateRight();
		});

		if (pageCount>1) {
			$('div#relixav div.avcontent-audio div.page-nav ul').css('display', 'block');
		} else {
			$('div#relixav div.avcontent-audio div.page-nav ul').css('display', 'none');
		}

		$('div#relixav div.avcontent-audio div.page-nav ul a.page').click(function(){
			showPlaylistPage(this.id.replace('audio-page-link-', ''));
		});
		
		$('div#relixav div.avcontent-audio div.tracklist li').click(function(){
			var hash=this.id.replace('track-', '');
			$('#audio-player').flash(function(){this.playTrack(hash);});
		});
		
		playlistPageCount=pageCount;
		showPlaylistPage(1);
		
	}
	
	function playlistNavigateRight() {
		showPlaylistPage(currentPlaylistPage+1);
	}
	
	function playlistNavigateLeft() {
		showPlaylistPage(currentPlaylistPage-1);
	}
	
	function showPlaylistPage(page) {
		page=Number(page);
		if (page>playlistPageCount) page=playlistPageCount;
		if (page<1) page=1;
		
		$('div#relixav div.avcontent-audio div.tracklist ul').css('display', 'none');
		$('ul#page-'+String(page)).css('display', '');
				
		$('div#relixav div.avcontent-audio div.page-nav ul a').removeClass('chosen');
		$('a#audio-page-link-'+page).addClass('chosen');
		
		currentPlaylistPage=page;
	}
	
	function playingTrack(hash) {
		// Highlight the track
		$('div.tracklist li').removeClass('selected');
		$('div.tracklist li#track-'+hash).addClass('selected');
		
		// Switch to the track's page
		var trackPage=Number($('div.tracklist li#track-'+hash).parent().attr('id').replace('page-', ''));
		if (currentPlaylistPage!=trackPage) showPlaylistPage(trackPage);
	}

	function setupAV(isPoppedOut) {
	
		var params={
				swf: '/site/flash/audio.swf',
				width: playerWidth,
				height: 70,
				name: 'audioplayer',
				id: 'audioplayer',
				params: {
					allowScriptAccess: "always"
				}
			}
			
		if (isPoppedOut) params.flashvars={popout: true};
			
		if ($.flash.hasVersion(9.1)) {
			$('#audio-player').flash(params);
			fixExternalInterface(document.getElementById('audioplayer'), 'doPopOut');
			fixExternalInterface(document.getElementById('audioplayer'), 'playTrack');
				
//			var flashVars={};
//			if (isPoppedOut) flashVars.popout=true;
		
//			swfobject.embedSWF("/site/flash/audio.swf", "audio-player", playerWidth, "70", "9.0.0", flashVars, {allowScriptAccess: true});
		
		}
		
		$('div#relixav div.avcontent-video ul a.page').each(function(){
			$(this).click(function(){
				showAVSpot($(this).html());
			});
		});
		
		videoCount=$('div#relixav div.avcontent-video ul a.page').length;
	
		$('div#relixav div.avcontent-video ul li.page-nav-arrow-left a').click(function(){
			showAVSpot(currentAVSpot-1);
		});
	
		$('div#relixav div.avcontent-video ul li.page-nav-arrow-right a').click(function(){
			showAVSpot(currentAVSpot+1);
		});
	
		$('div#relixav div.sub-controls a.video-link').click(function(){
			switchMode('video');
			return false;
		});
	
		$('div#relixav div.sub-controls a.audio-link').click(function(){
			switchMode('audio');
			return false;
		});
		
		$('a#pop-out-link').click(function(){
			popOutPlayer();
			return false;							   
		});
		
	}

	function fixExternalInterface(Obj, method) {
		Obj[method] = (function(method) {
									return function() {
											this.CallFunction( '<invoke name="' + method + '" returntype="javascript">' +
															  __flash__argumentsToXML(arguments, 0) + '</invoke>');
										}; // dangling semi-colon for IE 6
									})(method); // force re-closure to prevent IE memory leaks
		}


	function switchMode(m) {
		if (m!='video' && m!='audio') return;
		mode=m;

		if (mode=='video') {
			$('div.avcontent-audio').css('display', 'none');
			$('div.avcontent-video').css('display', 'block');
			showAVSpot(1);
			rotateInterval=setInterval(function(){showAVSpot(currentAVSpot+1, true);}, 5000);
		} else {
			if (rotateInterval) clearInterval(rotateInterval);
			$('div.avcontent-video').css('display', 'none');
			$('div.avcontent-audio').css('display', 'block');
		}
	
	}

	function showAVSpot(i, keepInterval) {
		
		if (currentAVSpot!=-1) {
			$('div#relixav div#'+mode+'-spot-'+currentAVSpot).css('display', 'none');		
		}
		
		if (i<1) i+=videoCount;
		if (i>videoCount) i-=videoCount;

		$('div#relixav div.avcontent-video div.page-nav ul a').removeClass('chosen');
		$('li#v-page-'+i+' a').addClass('chosen');

		$('div#relixav div#'+mode+'-spot-'+i).css('display', 'block');
		currentAVSpot=i;
		if (rotateInterval && !keepInterval) clearInterval(rotateInterval);
	}

	function popOutPlayer() {
		// Spawn a pop-up containing the player
		var settings='width=300,height=270,toolbar=0,scrollbars=0,status=0,resizable=0,menubar=0';
		var id='playerpopout';
		var url='/audio/player/';

		$('#audio-player').flash(function(){
			this.doPopOut();
		});
	
		window.open(url, id, settings);
	}

	var mode='video';
	var currentAVSpot=-1;
