$(document).ready(function() {

	// Add body class
	$('body').addClass('js');

	// Slideshow of images
	$article = $('.article div');
		
	$('.article').each(function(i){
		var $$, $div, $pImageFirst, $pImages, $wrapper;
		
		$$				= $(this);
		$div			= $$.children('div');
		$pImageAll		= $div.children('p:has(img)').addClass('cycle-image');
		$pImageFirst	= $pImageAll.eq(0);
		$pImages		= $pImageFirst.nextUntil('p:not(.cycle-image)').add($pImageFirst);
		
		$pImages.wrapAll('<div />').parent().addClass('cycling');
		if($('.cycling', this).children('.cycle-image').size() > 1) {
			$('.cycling', this).after('<div class="viewmore"><a href="#" class="left">Back</a><a href="#" class="right">Forward</a></div>');
		}
		
	});
		
	$('.article .cycling').each(function() {
		var $this = $(this);
		//$this.closest('[id]').attr('id') + ' .viewmore .left';
		$this.cycle({
			 next: $this.parent().children('.viewmore').find('.left'),
			 prev: $this.parent().children('.viewmore').find('.right'),
			 timeout:0,
			 after: onAfter,
			 before: onBefore
		});

	});
	
	$('.viewmore .left, .viewmore .right').click(function() {
		return false;
	});
	
	function onBefore(curr, next, opts, fwd) {
	}
	function onAfter(curr, next, opts, fwd){
		var $ht = $(this).height();
		//set the container's height to that of the current slide
		if($(this).parent().height() > $(this).height()) {
			$(this).parent().animate({ height: $ht }, 500);
		} else {
			$(this).parent().css('height', $ht);
		}
	}
	
});


