
// Selected item
var currentPlaylistItem = null;

// Video Playlist
var videoPlaylist = Array();
videoPlaylist.push({ id: 'video_nlma', obj: { file: '../files/videos/nlma_mv.flv', image: 'images/media/video/splash_nlma_mv.jpg' } });
videoPlaylist.push({ id: 'video_nlma_1xtra', obj: { file: '../files/videos/nlma_1xtra.flv', image: 'images/media/video/splash_nlma_live1x.jpg' } });
videoPlaylist.push({ id: 'video_1xtra_interview', obj: { file: '../files/videos/1XInterview.flv', image: 'images/media/video/splash_interview_live1x.jpg' } });
videoPlaylist.push({ id: 'video_stpburg08', obj: { file: '../files/videos/stpburg08.flv', image: 'images/media/video/splash_stpburg08.jpg' } });
videoPlaylist.push({ id: 'video_nlma_rec1', obj: { file: '../files/videos/nlma_bts1.flv', image: 'images/media/video/splash_nlma_bts1.jpg' } });
videoPlaylist.push({ id: 'video_nlma_rec2', obj: { file: '../files/videos/nlma_bts2.flv', image: 'images/media/video/splash_nlma_bts2.jpg' } });

// First playlist item
var id;
var pos = location.href.lastIndexOf('play=');
if (pos > -1) id = 'video_' + location.href.substring(pos+5, location.href.length);
var currentPlaylistItem = videoPlaylist.first();
for (var i = 0; i < videoPlaylist.length; i++) {
	if (videoPlaylist[i].id == id) {
		currentPlaylistItem = videoPlaylist[i]; break;
	}
}

// Setup Flash Player
var flashvars = {
	file: currentPlaylistItem.obj.file,
	image: currentPlaylistItem.obj.image,
	overstretch: "true",
	displayheight: "270",
	height: "270",
	autostart: "false",
	repeat: "false",
	width: "480",
	frontcolor: "0xCCCCCC",
	backcolor: "0x000000",
	lightcolor: "0xb92c70",
	screencolor: "0x000000",
	showdigits: "true",
	shuffle: "true",
	javascriptid: "mpl",
	enablejs: "true",
	shuffle: "false"
};
var params = {
	allowscriptaccess: "always",
	allowfullscreen: "true",
	wmode: "opaque"
};
var attributes = {
	id: "mpl"
};
swfobject.embedSWF("flash/mediaplayer.swf", "player", "480", "270", "7", "flash/expressInstall.swf", flashvars, params, attributes);

// Load first video on page load
Event.observe(window, 'load', function() { 

	// Observe video clicks
	$$('#playlist .playlist_entry').each(function(i) {
		i.observe('click', changeVideo, false);
	});

	// Load first video
	if (currentPlaylistItem) loadVideo(currentPlaylistItem); 
		
}, false);

// Change video
function changeVideo(e) {

	// Get video id
	e.stop();
	var videoId = e.findElement('a').up('.playlist_entry').id;
	
	// Get video object
	var playlistItem = null;
	for (var i = 0; i < videoPlaylist.length; i++) {
		if (videoPlaylist[i].id == videoId) {
			playlistItem = videoPlaylist[i]; break;
		}
	}
	
	// Load
	if (playlistItem) loadVideo(playlistItem);

	// Return false
	return false;

}

// Load a video
function loadVideo(playlistItem) {

	// Only if flash is working
	if (!$('player')) {
		if (playlistItem.id != currentPlaylistItem.id) {
			loadFile(playlistItem.obj);	
			currentPlaylistItem = playlistItem;
		}
	}
	
	// Select index playlist_entry
	var entries = $$('.playlist_entry');
	entries.each(function(i){i.removeClassName('selected')});
	var currentEntry = $(playlistItem.id);
	currentEntry.addClassName('selected');
	
	// Update description
	updateVideoDescription();
	
}

// Update currently selected video description
function updateVideoDescription() {

	// Get entry of currently select item
	var currentEntry = $$('.playlist_entry.selected').reduce();

	// Set description
	var temp, el;
	temp = currentEntry.select('h4').first(); el = $('current_trackname'); if (temp) { el.update(temp.innerHTML); el.show(); } else { el.hide(); }
	temp = currentEntry.select('.artist').first(); el = $('current_artist'); if (temp) { el.update(temp.innerHTML); el.show(); } else { el.hide(); }
	temp = currentEntry.select('.description').first(); el = $('current_description'); if (temp) { el.update(temp.innerHTML); el.show(); } else { el.hide(); }
	temp = currentEntry.select('.produced_by').first(); el = $('current_produced_by'); if (temp) { el.update(temp.innerHTML); el.show(); } else { el.hide(); }

}

