jQuery.fn.fadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle'}, speed, easing, callback);  
};

$(function() {

	$('.chart').each(function() {
		var $slideCount=$('img',this).length;
		if($slideCount>1){
			var $nav = $('<div class="nav"></div>').insertBefore(this);
		}

		$(this).cycle({
			fx:     'fade',
			speed:   300,
			timeout: 0,
			pager:   $nav,
			after:   onAfter,
			
			// callback fn that creates a thumbnail to use as pager anchor 
			pagerAnchorBuilder: function(idx, slide) { 
				return '<a href="#">' + slide.title + '</a>'; 
			}
		});
	});
	
	function onAfter(curr, next, opts) {
		var src = '&nbsp;';
		if (next.src)
			src = next.src.match(/([a-zA-Z0-9\.]+$)/)[1];
		$(curr).parent().nextAll('div.caption:first').html(src);
	}
	
	$('a', '.chart').show();
	
	
	$('.poll_footer').each(function() {
		var $embedLink=$('.embed_link',this);
		var $embed=$('.embed',this);
		var $embedClose=$('.embed_close',this);
		$embedLink.click(function() {
			$embed.fadeToggle();
			$('#embed_field').select();
		});
		$embedClose.click(function() {
			$embed.fadeToggle();
		});
	
	});
	
});

