$(document).ready(function(){
	// add stream links next to every "audio" link
	$('.sermon_table a[@href$=.mp3]')
		.parents('td').after('<td><a class="stream-link">escuchar</a></td>');
	// change the "audio" and "listen" links to "download" links
	$(".sermon_table a:contains('audio')").text('descargar');
	$(".sermon_table a:contains('listen')").text('descargar');
	// get rid of any legacy .php download links
	$(".sermon_table a[@href$=.php]").parents('td').remove();
	
	// the big click function for stream links
	$('.stream-link').click(function(){
		// what is this?
		var $thisclick = jQuery(this);
		// get the date, author, title, series
		var series = $thisclick.parents('table').prev().text();
		var date = $thisclick.parents('tr').children('td:eq(0)').text();
		var author = $thisclick.parents('tr').children('td:eq(1)').text();
		var title = $thisclick.parents('tr').children('td:eq(2)').text();
		// build the message info:
		var meta = '<h1>'+title+'</h1><ul><li>'+author+'</li><li>'+date+'</li><li>Series: '+series+'</li></ul>';
		// if its the "not i but christ" table
		if ( $thisclick.parents('table').hasClass('nibc') ) {
			var meta = '<h1>'+author+'</h1><ul><li>'+date+'</li><li>Jason Henderson</li><li>Series: '+series+'</li></ul>';	
		}
		// get the mp3 reference
		var mp3 = 
			$thisclick.parents('tr').children().children().children('a[@href$=.mp3]').attr('href');
		// sanitize paths for flash, should work for up to four levels deep of folder nesting
		mp3 = mp3.replace('/', '%2F');
		mp3 = mp3.replace('/', '%2F');
		mp3 = mp3.replace('/', '%2F');
		mp3 = mp3.replace('/', '%2F');
		// build the embed code
		var embed = '<div id="audio-player"><object width="290" height="24" id="audioplayer1" data="http://marketstreetfellowship.com/flash/audioplayer.swf" type="application/x-shockwave-flash"><param value="http://marketstreetfellowship.com/flash/audioplayer.swf" name="movie"/><param value="playerID=1&amp;bg=0xf8f8f8&amp;leftbg=0xeeeeee&amp;lefticon=0x666666&amp;rightbg=0xcccccc&amp;rightbghover=0x999999&amp;righticon=0x666666&amp;righticonhover=0xffffff&amp;text=0x666666&amp;slider=0x666666&amp;track=0xffffff&amp;border=0x666666&amp;loader=0x9FFFB8&amp;autostart=yes&amp;loop=yes&amp;soundFile='+mp3+'" name="FlashVars"/><param value="high" name="quality"/><param value="false" name="menu"/><param value="#FFFFFF" name="bgcolor"/></object></div>';
		// add the overlay and insert html
		$('<div id="overlay"></div><div id="insert" class="rounded"><a id="closeout"></a>'+meta+embed+'</div>').prependTo('body');
		// grab + calculate some dimensions
		var dheight = jQuery(document).height();
		var dwidth = jQuery(document).width();
		var wheight = jQuery(window).height();
		var divheight = jQuery('#insert').height() + 120;
		var inserttop = ((wheight / 2) - (divheight / 2)) + jQuery(window).scrollTop();
		var insertleft = (dwidth / 2) - 205;
		// turn the screen grey and bring in the player div
		$('#overlay').css({
			opacity: 0,
			height: dheight,
			width: '100%'
		}).animate({opacity: .6}, 400, 'swing', function(){
			$('#insert').fadeIn().css({
				top: inserttop,
				left: insertleft
			});
		});
		// fade out and remove inset div and overlay when close button clicked
		jQuery('#closeout').click(function(){
			jQuery('#insert').fadeOut(300, function(){
				jQuery('#overlay').animate({opacity: 0}, 300, function(){
					jQuery('#overlay, #throbber, #insert').remove();
				});
			});
		});
	});
	
	// zebra stripe the tables, and add hover color
	$('.sermon_table tr:even').addClass('alt');
	$('.sermon_table tr').mouseover(function(){
		$(this).addClass('hover');
	}).mouseout(function(){
		$(this).removeClass('hover');
	});
	
	var add_cels, this_row_count, max_rows = 0;
	jQuery('.sermon_table tr').each(function(){
		this_row_count = jQuery('td', this).length;
		if ( this_row_count > max_rows ) max_rows = this_row_count;
	});
	//jQuery('h1').after(' '+max_rows);
	jQuery('.sermon_table tr').each(function(){
		this_row_count = jQuery('td', this).length;
		if ( this_row_count < max_rows ) {
			add_cells = max_rows - this_row_count;
		} else {
			add_cells = 0;
		}
		if ( add_cells == 2 ) {
			jQuery(this).append('<td></td><td></td>');
		}
		if ( add_cells == 1 ) {
			jQuery(this).append('<td></td>');
		}
	});
	
	var table_width = jQuery('.sermon_table tr:first').width() - 3;
	jQuery('.sermon_table').prev('h2').css('width', table_width+'px');
});