$(function() {
	// Hide all images but the first
	$('.feature#paginate > img:not(:first)').css('display', 'none');

	// Set size of container on load of first image
	$('.feature#paginate > img:first').load(function() {
		$(this).parent().css({
			height: $(this).height()
		});
	});
		
	/* Cycle function override */
	$.fn.cycle.createPagerAnchor = function(i, el, $p, els, opts) {
	    var $a = (typeof opts.pagerAnchorBuilder == 'function')
	        ? $(opts.pagerAnchorBuilder(i,el))
	        : $('<li><img name="'+(i+1)+'" src="http://www.wowwee.com/img/jqpagination/'+(i+1)+'.gif" /></li>');

	    // don't reparent if anchor is in the dom
	    if ($a.parents('body').length == 0) {
	        $a.appendTo($p);
	    }

	    $a.bind(opts.pagerEvent, function() {
	        opts.nextSlide = i;
	        var p = opts.container, timeout = p.cycleTimeout;
	        if (timeout) {
	            clearTimeout(timeout);
	            p.cycleTimeout = 0;
	        }            
	        if (typeof opts.pagerClick == 'function') {
	          opts.pagerClick(opts.nextSlide, els[opts.nextSlide]);  
	          go(els,opts,1,opts.currSlide < i);
	        }
	        
	        return false;
	    });
		if (opts.pauseOnPagerHover) {
		  $a.hover(function() { opts.container.cyclePause = 1; }, function() { opts.container.cyclePause = 0; } );
		}
			
	};
	
	$.fn.cycle.updateActivePagerLink = function(pager, currSlide) {
	    $(pager).find('img').removeClass('selected').filter("img[name='"+(currSlide+1)+"']").addClass('selected');
	};
	
	$('.feature#paginate').cycle({
		fx: 'fade', 
		next: '#pagination-next',
		prev: '#pagination-previous',
		pager: '#pagination-links',
		fit: 1,
		timeout: 0,
		before: function() {
			$(this).parent().css({
				height: $(this).height()
			});
		},
		pagerEvent:   'click'
	});
	
	$('ul#pagination-links li img').click(function() {
		$('.feature#paginate').cycle( parseInt( $(this).attr('name') - 1, 10 ) );
	});
});