$(document).ready(function(){

	// Auto-populate form fields with their titles
	$('input.populate').each(function()
	{
		var e = $(this);
		if (!e.val()) { e.val(e.attr('title')); }
		e.focus(function()
		{
			if (e.attr('title') == e.val()) { e.val(''); }
		}).blur(function()
		{
			if (!e.val()) { e.val(e.attr('title')); }
		});
	});
	
	// Simple animation for #top links
	$("a[href=#top]").click(function(){
		$("html, body").animate(
			{ scrollTop:0 }, 500
		);
		return false;
	});
				
	// Make external links open in new windows	
	$('a').each(function(){
		if (this.href.match('^https?://(?!'+document.location.hostname+')')) {
			this.target = '_blank';
		}
	});
	
	// Add Analytics tracking to PDF
	$('a[href$=".pdf"]').click(function(){
		_gaq.push(['_trackEvent', 'PDF', $(this).attr("href")]);
	});
	
	// Add Analytics tracking to MP3
	$('a[href$=".mp3"]').click(function(){
		_gaq.push(['_trackEvent', 'MP3', $(this).attr("href")]);
	});	
	
	// Add Analytics tracking to M3U
	$('a[href$=".m3u"]').click(function(){
		_gaq.push(['_trackEvent', 'M3U', $(this).attr("href")]);
	});	

	// Add Analytics tracking to PLS
	$('a[href$=".pls"]').click(function(){
		_gaq.push(['_trackEvent', 'PLS', $(this).attr("href")]);
	});

	// Add overall program tracking
	$('table.program-archives a').click(function(){
		var program_title = $('h1.program-title').text();
		_gaq.push(['_trackEvent', 'Program Download', program_title]);
	});
	
	// IE border fix for secondary nav
	$('ul.secondary-nav li:last-child a').css('border', 'none');
		
	// Jump menus
	$('form.jump input:submit').hide();
	$('form.jump select').change(function()
	{
		if($(this).val() != '')
		{
			if( $(this).parents('form').attr('target') == '_blank' )
			{
				window.open($(this).val(), 'window_' + Math.floor(Math.random()*101));
			}
			else
			{
				window.location = $(this).val();
			}
		}
	});
	
	// FB Share
	$('a.fbshare').click(function()
	{
		window.open($(this).attr('href'), 'fbshare', 'toolbar=0,status=0,resizable=1,width=626,height=436');
		return false;
	});
		
	
	// Schedule display
	if($('table.schedule').length > 0)
	{
		$('h2.schedule, table.schedule').hide();
		$('ul#schedule').show();
		if(window.location.hash)
		{
			$('table' + window.location.hash).show();
			$("html, body").animate({ scrollTop:0 }, 1);
		}
		else
		{
			$('table.schedule.current-day').show();
		}
		var current_day = $('table.schedule:visible').attr('id');			
		$('ul#schedule a[href=#' + current_day + ']').parent('li').addClass('current');
		
		$('ul#schedule a').click(function()
		{
			target_day = $(this).attr('href');
			$('ul#schedule li').removeClass('current');
			$(this).parent('li').addClass('current');
			$('table.schedule:visible').hide();
			$('table' + target_day).show();
			
			return false;
		});
	}
	
	// Pledge Images
	if($('div#pledge_images img').length > 1)
	{
		$('div#pledge_images a').show();
		$('div#pledge_images div').cycle({speed: 500, timeout: 0, next: '#next', prev: '#prev'});
	}
	
	
});
