$( function ( ) { 
	loadSongs( 'ALL' );
} );

function loadSongs ( which ) { 
	switch ( which )
	{
		case 'ALL': 
			url = all_songs_filter_url;
			$( 'li.current' ).removeClass( 'current' );
			$( 'li#allsongs' ).addClass( 'current' );
			break;
		case 'DISC': 
			url = on_disc_songs_filter_url;
			$( 'li.current' ).removeClass( 'current' );
			$( 'li#ondisc' ).addClass( 'current' );
			 break;
		case 'DLC': 
			url = dlc_songs_filter_url;
			$( 'li.current' ).removeClass( 'current' );
			$( 'li#downloadable' ).addClass( 'current' );
			 break;
	}

	$( '#on-disc-songs' ).load( url );

	$( 'table.song-listing tr.song-row' ).live( 'click', loadDetailRow );
	$( 'table.song-listing tr.song-row' ).live( 'mouseover', loadDetailRowDelay );
	$( 'table.song-listing tr.song-row' ).live( 'mouseout', killDetailRowDelay );

	$( 'ul#platform-selector li a' ).live( 'click', function ( ) { 
		loadSongLeaderboards( $( this ).html( ), $( 'tr#song-detail input#song-id' ).val( ) );	
		$( 'ul#platform-selector li a' ).removeClass( 'current' );
		$( this ).addClass( 'current' );
	} );
}

function loadDetailRow ( ) { 
	$( 'tr#song-detail' ).remove( );
	$( 'tr#song-credits' ).remove( );
	$( 'tr.song-focus' ).removeClass( 'song-focus' );

	var $tr = $( this ).closest( 'tr' );
	$tr.addClass( 'song-focus' );
	var $clone = $( 'tr#song-detail-template' ).clone( );
	$clone.attr( 'id', 'song-detail' ).show( ).insertAfter( $tr ).fadeIn( 750 );

	var $credits_clone = $( 'tr#song-credits-template' ).clone( ).attr( 'id', 'song-credits' );
	$credits_clone.insertAfter( $clone ).fadeIn( 750 );

	$.getJSON( $( this ).attr( 'rel' ), null, function ( data ) { 
		$( 'tr#song-detail #song-id' ).val( data.id );
		$( 'tr#song-detail #guitar-rating' ).html( data.guitar_rating );
		$( 'tr#song-detail #bass-rating' ).html( data.bass_rating );
		$( 'tr#song-detail #drum-rating' ).html( data.drum_rating );
		$( 'tr#song-detail #vocal-rating' ).html( data.vocal_rating );
		$( 'tr#song-detail .album-pic img' ).replaceWith( data.album_art_img );
		$( 'tr#song-detail table#leaderboards' ).replaceWith( data.leaderboards );
		$( 'tr#song-detail ul#platform-selector li:first a' ).addClass( 'current' );
		$( 'tr#song-credits p#credits' ).html( data.credits );

		if ( data.ivideosongs_id )
		{
			$( 'tr#song-detail a.ivideosongs' ).attr( 'href', 'http://www.ivideosongs.com/song/rock/electric-guitar/' + data.slug + '/the-beatles/' + data.ivideosongs_id + '.aspx?utm_source=thebeatlesrockband.com&utm_medium=affiliate&utm_campaign=ivsp_rockband&ivsp=942F2347FCE34D9B8F948C3D613A3A53' );
			$( 'tr#song-detail a.ivideosongs, tr#song-detail span#span-ivideosongs' ).show( );
		}

		if ( 'DLC' == data.type )
		{
			$( 'tr#song-detail a.buydlc' ).attr( 'href', 'http://marketplace.xbox.com/en-US/games/offers/0' + data.microsoft_guid + '-0000-4000-8000-0000454108B1?cid=TheBeatlesRockBand' );
			$( 'tr#song-detail a.buydlc, tr#song-detail span#span-buydlc' ).show( );
		}
	}  );
		
	return false;
}

function loadDetailRowDelay ( ) { 
	if ( !$( this ).hasClass( 'song-focus' ) )
		$( this ).oneTime( 750, 'detail', loadDetailRow );
}

function killDetailRowDelay ( ) { 
	$( this ).stopTime( 'detail' );
}

function loadSongLeaderboards ( platform, song_id ) { 
	$( 'tr#song-detail table#leaderboards' ).load( 
		song_leaderboards_url,
		{ 
			platform: platform,
			song_id: song_id
		},
		function ( data ) { 
			$( 'tr#song-detail table#leaderboards' ).replaceWith( data );
		}
	);
}
