var url = '';
var md = 0;

function startupFunc()
{
	$('#titlebar').slideDown(function() {
		$('#content').fadeIn(2000, function() {
			$('#navigation li').eq($('#startingPage').val()).addClass('currPage');
		});
	});
}

function showCoverAndTracks($clickedCol)
{
	var collection = $clickedCol.attr('class');
	$clickedCol.next('dd').slideDown(function() {
		$('.albumArt.' + collection).show('slide', function() {
			$(this).next('.trackList.' + collection).slideDown();
		});
	});
}

$(document).ready(function() {
	// Page initial load
	$('#content').hide();
	$('#titlebar').hide();
		
	var startingPage = $('#startingPage').val();
	
	var flashvars = {};
	var params = {};
	flashvars.wimpyReg = wimpyButtonReg;
	params.menu = "false";
	params.quality = "high";
	params.scale = "noscale";
	params.wmode = "opaque";
	params.allowfullscreen = "true";
	params.allowscriptaccess = "always";
	var attributes = {};
	attributes.id = "wimpyButtonBridge";
	swfobject.embedSWF("wimpy_button.swf", "wimpyButtonBridgeTarget", "1", "1", "10", 'expressInstall.swf', flashvars, params, attributes);
	
	var $initialURL = $('#navigation > ul > li > a').eq(startingPage).attr('href');
	var $pageName = $('#navigation > ul > li > a').eq(startingPage).attr('title');
	$('#titlebar h1').text($pageName);
	
	$('#content').load($initialURL, function() {
		setTimeout(startupFunc, 1500);
	});
	
	// Navigation animation
	$('#navigation > ul > li > a').click(function(e) {
		var $link = $(this);
		var $pageName = $(this).attr('title');

		$('#content').fadeOut(500, function() {
			// Turn off page indicator on left nav
			$('.currPage').removeClass('currPage');
			
			// Roll up titlebar
			$('#titlebar').slideUp(function() {
				$('#titlebar h1').text($pageName);
				$('#titlebar').slideDown(function() {
					// load data
					var $page = $link.attr('href');
					$('#content').load($page, function() {
						// Show content
						$('#content').fadeIn(500, function() {
							$link.parent().addClass('currPage');
						});
					});
				});
			});
		});
		
		e.preventDefault();
	});
	
	// Anti-spam mail links
	$('a.antiSpam').live('click', function(e) {
		var $link = $(this);
		$.post('base64_decode.php', {string: $link.attr('href')}, function(response) {
			$link.attr('href', 'mailto:' + response);
			$link.text(response);
			$link.removeClass('antiSpam');
		});
		
		e.preventDefault();
	});
	
	// Collection click
	$('#collectionList dt').live('click', function() {
		// Hide existing stuff
		var $clickedCol = $(this);

		if ($('.trackList:visible').length)
		{
			$('#collectionList dd:visible').slideUp();
			$('.trackList:visible').slideUp(function() {
				$('.albumArt:visible').hide('slide', function() {
					showCoverAndTracks($clickedCol);
				});
			});
		}
		else
			showCoverAndTracks($clickedCol);
	});
	
	// Play controller
	$('img.playControl').live('click', function() {
		var filename = $(this).next('.playSource').text();
		var currTrack = $('#currTrack').val();		
		var wimpyObj = $('#' + wimpyButtonID).get(0);
		
		// Wipe out any current play list
		$('#playList').val('');

		if ($(this).attr('src').indexOf(wimpyButtonImagePlay) >= 0)
		{
			if (currTrack != '')
				$('.playSource:contains(' + currTrack + ')').prev('.playControl').attr('src', wimpyButtonImagePlay);
			wimpyObj.js_wimpy_play(filename);
			$(this).attr('src', wimpyButtonImagePause);
			$('#currTrack').val(filename);
		}
		else
		{
			wimpyObj.js_wimpy_pause();
			$(this).attr('src', wimpyButtonImagePlay);
			$('#currTrack').val('');
		}
	});
	
	// "Play All"
	$('.playAllTracks').live('click', function(e) {
		var currTrack = $('#currTrack').val();		
		var wimpyObj = $('#' + wimpyButtonID).get(0);
		var $list = $(this).parents('ul');
		var playList = '';

		$list.find('.playSource').each(function(i) {
			playList += $(this).text() + ',';
		});
		
		// Set any currently playing track to a play button
		if (currTrack != '')
			$('.playSource:contains(' + currTrack + ')').prev('.playControl').attr('src', wimpyButtonImagePlay);
		
		$('#playList').val(playList);
		
		// Fire off first track
		var firstTrack = playList.substr(0, playList.indexOf(','));
		$('#playList').val(playList.substr(playList.indexOf(',')+1));
		wimpyObj.js_wimpy_play(firstTrack);
		$('.playSource:contains(' + firstTrack + ')').prev('.playControl')
		.attr('src', wimpyButtonImagePause);
		$('#currTrack').val(firstTrack);

		e.preventDefault();
	});
	
	// Handle external links
	$('a[href^=http]').live('click', function(e) {
		url = $(this).attr('href');
		window.open(url);
		//$('#dialog').dialog('open');
		e.preventDefault();
	});
	
	// Portrait protector
	$("#portrait > img").live('mousedown', function(e) {
		if (e.which == 3) //right click
			$('#portrait').fadeOut('slow');
		else
			md = 1;

		e.preventDefault();
	});
	$("#portrait > img").live('mouseup', function(e) {
		md = 0;
		e.preventDefault();
	});
	$("#portrait > img").live('mouseout', function() {
		if (md == 1)
			$("#portrait").fadeOut("slow");
	});
});
