// init long word
$.meddle = 'meddlesome ';
for( var i=0;i<9;++i) $.meddle += $.meddle;


function track_song( song ) {
	$.get( 'track.php', {plays: song} );
	return true;
}
function cart_total(result) {
	
	var text;
	
	if( result == 1) text = "1 item";
	else text = result + " items";

	$('span#cart-total').html( text );
}
function valid_email( email ) {
	
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	return filter.test(email);
}

// DOCUMENT READY
$(document).ready( function() {

	$('#cart-notice a').addClass('blue');
	
	if( window.location.pathname.indexOf( 'checkout.php' ) < 0 ) {

		// load cart if it contains items
		$.get( 'cart-ajax.php', function(items) {
			
			if( items > 0 ) $('div#cart-notice').show();
			cart_total( items );
		});
	}
	
	// Local copy of jQuery selectors, for performance.
	var	my_jPlayer = $("#jquery_jplayer"),
		my_trackName = $(".track-name"),
		my_playState = $(".play-state"),
		my_extraPlayInfo = $(".extra-play-info");

	// Some options
	var	opt_play_first = false, // If true, will attempt to auto-play the default track on page loads. No effect on mobile devices, like iOS.
		opt_auto_play = true, // If true, when a track is selected, it will auto-play.
		opt_text_playing = "Now playing", // Text when playing
		opt_text_selected = "Track selected"; // Text when not playing


	// Change the time format
	$.jPlayer.timeFormat.padMin = false;
	$.jPlayer.timeFormat.padSec = false;
	$.jPlayer.timeFormat.sepMin = " min ";
	$.jPlayer.timeFormat.sepSec = " sec";

	// Initialize the play state text
	my_playState.text(opt_text_selected);

	// Instance jPlayer
	my_jPlayer.jPlayer({
		ready: function () {
			//$("#index_feature .track-default").click();
		},
		timeupdate: function(event) {
			my_extraPlayInfo.text(parseInt(event.jPlayer.status.currentPercentAbsolute, 10) + "%");
		},
		play: function(event) {
			my_playState.text(opt_text_playing);
		},
		pause: function(event) {
			my_playState.text(opt_text_selected);
		},
		ended: function(event) {
			my_playState.text(opt_text_selected);
		
			// play next track after old is done
			var nxt = $('.stop').closest('tr').next().find('.play:first');
			if( nxt.length == 0 ) nxt = $('.stop').closest('tr').next().next().find('.play:first');
			$(".stop").click();
			nxt.click();			
		},
		swfPath: "js/",
		cssSelectorAncestor: "#index_feature",
		supplied: "mp3",
		wmode: "window"
	});

	// Create click handlers for the different tracks
	$(".play").live( 'click', function(e) {

		$.post('play.php', { 'dl': $(this).attr('href')});

		$('.stop').html('play')
		.css( {
			'background-color': '#fff',
			'color': '#000'
		}).attr( 'class', 'play' );;		
		
		//my_trackName.text($(this).text());
		my_jPlayer.jPlayer("setMedia", {
			mp3: $(this).attr("href")
		});

		my_jPlayer.jPlayer("play");

		$(this).blur();
		$(this).attr( 'class', 'stop' );		
		$(this).html( 'stop' );
		$(this).css( {
			'background-color': '#ff0000',
			'color': '#fff'
		});	
		
		return false;
	});

	// Create click handlers for the different tracks
	$(".stop").live( 'click', function(e) {

		$.post('play.php', { 'dl': $(this).attr('href')});
		
		//my_trackName.text($(this).text());

		my_jPlayer.jPlayer("stop");

		$(this).blur();
		$(this).attr( 'class', 'play' );
		if( $(this).attr('name').length > 0 ) $(this).html( $(this).attr('name') );
		else $(this).html( 'play' );
		$(this).css( {
			'background-color': '#fff',
			'color': '#000'
		});
		
		return false;
	});	
	
	// Create click handlers for the different tracks
	$(".dl").click( function(e) {
		$.post('play.php', { 'dl': $(this).attr('href')});
	});

	
	// HIDE ALL PRELOADED ALL IMAGES
	$('img.preloaded-bg, img.preloaded' ).hide();

	// BACKEND - ADDING NEW SHOWS
	$('a#addshow').click( function() {
		
		var pass = $('#pass').val();
		$('a#addshow').next('h1').remove();
		
		if( pass == 'willy' ) {

			$('form#add_form').submit();
		} else {
			$('a#addshow').after("<h1 class='big black'> no no no</h1>");
		}
	});		
});
