$(document).ready(function(){

	// Tweeter
	$('#twitter').social({
		network		: 'twitter',
		user		: 'howie_d',
		count		: 1,
		twitter		: {
			output		: '<span class="tweet"><span class="text"><a href="{tweet_url}">{text}</a></span> <span class="time"><a href="{tweet_url}">Posted {time}</a></span>'
		}
	});
	
	// Audio player
	$('#controls .pause').hide();
	
	$('#controls .play').click(function() {
		$('#audio_player').get()[0].sendEvent('PLAY', $('#playlist .current a').attr('href'));
		$('#controls .pause').show();
		$(this).hide();
		return false;
	});
	
	$('#controls .pause').click(function() {
		$('#audio_player').get()[0].sendEvent('STOP');
		$('#controls .play').show();
		$(this).hide();
		return false;
	});
	
	$('#controls .next').click(function() {
		
		var current = $('#playlist .current');
		var next = $('#playlist .current + .item');
		var nextTrack = $('#playlist .current + .item a').attr('href');
		var nextTitle = $('#playlist .current + .item a').html();
		
		$('#controls .play').hide();
		$('#controls .pause').show();
		$('#hud').text(nextTitle);
		
		$('#audio_player').get()[0].sendEvent('LOAD', nextTrack);
		$('#audio_player').get()[0].sendEvent('PLAY', nextTrack);
		
		$(current).removeClass('current');
		$(next).addClass('current');
		
		if ($(current).hasClass('last')) {
			$(current).addClass('current');
		}
		
		return false;
	});
	
	$('#controls .prev').click(function() {
		
		var current = $('#playlist .current');
		var previous = $('#playlist .current').prev();
		var prevTrack = $('#playlist .current').prev().find('a').attr('href');
		var prevTitle = $('#playlist .current').prev().find('a').html();
		
		$('#controls .play').hide();
		$('#controls .pause').show();
		$('#hud').text(prevTitle);
		
		$('#audio_player').get()[0].sendEvent('LOAD', prevTrack);
		$('#audio_player').get()[0].sendEvent('PLAY', prevTrack);

		$(current).removeClass('current');
		$(previous).addClass('current');
	
		if ($(current).hasClass('first')) {
			$(current).addClass('current');
		}
		
		return false;
	});
	
	// Render a free download link to every order detail page with these SKUs
    $("#GC.GC_order table a:contains('HD-CD-004'), #GC.GC_order table a:contains('HD-PKG-003-S'), #GC.GC_order table a:contains('HD-PKG-003-M'), #GC.GC_order table a:contains('HD-PKG-003-L'), #GC.GC_order table a:contains('HD-PKG-003-XL'), #GC.GC_order table a:contains('HD-PKG-002-S'), #GC.GC_order table a:contains('HD-PKG-002-M'), #GC.GC_order table a:contains('HD-PKG-002-L'), #GC.GC_order table a:contains('HD-PKG-002-XL'), #GC.GC_order table a:contains('HD-PKG-001-S'), #GC.GC_order table a:contains('HD-PKG-001-M'), #GC.GC_order table a:contains('HD-PKG-001-L'), #GC.GC_order table a:contains('HD-PKG-001-XL'), #GC.GC_order table a:contains('HD-CD-002')").after('<br /><span class="note">Download your purchase: <a href="http://bit.ly/uNh1Dq" class="download">&ldquo;Back To Me&rdquo; Album</a></span>');
});

