jQuery(document).ready(function($) {
	// Headline
	
	var direction = 'next';
	var headlineTimer = null;
	
	$('.layer-headline').each(function() {
		var oStage = $(this);
		
		$('.layer-thumbnails li a', oStage).click(function() {
			var index = $(this).parent().parent().find('li').index($(this).parent());
			
			clearInterval(headlineTimer);
			
			$('.layer-thumbnails li.item-selected', oStage).removeClass('item-selected');
			$(this).parent().addClass('item-selected');
			
			$('.listing-images li:visible').fadeOut('fast', function() {
				$('.listing-images li:eq(' + index + ')').fadeIn('fast');
			});
			
			initHeadlineTimer();
			
			return false;
		});
		
		$('.panel-next a', oStage).click(function() {
			var oCurrentThumbnail = $('.layer-thumbnails li.item-selected', oStage);
			
			$('.layer-thumbnails li', oStage).stop(true, true);
			$('.listing-images li', oStage).stop(true, true);
			
			clearInterval(headlineTimer);
			
			if (!oCurrentThumbnail.is(':last-child')) {
				if (!oCurrentThumbnail.next().hasClass('item-onstage')) {
					$('.layer-thumbnails li.item-onstage:first-child', oStage).removeClass('item-onstage').fadeOut('fast', function() {
						oCurrentThumbnail.next().addClass('item-onstage').fadeIn('fast', function() {
							$('a', $(this)).trigger('click');
						});
					});
				}
				else $('a', oCurrentThumbnail.next()).trigger('click');
			}
			return false;
		});
		
		$('.panel-previous a', oStage).click(function() {
			var oCurrentThumbnail = $('.layer-thumbnails li.item-selected', oStage);
			
			$('.layer-thumbnails li', oStage).stop(true, true);
			$('.listing-images li', oStage).stop(true, true);
			
			clearInterval(headlineTimer);
			
			if (!oCurrentThumbnail.is(':first-child')) {
				if (!oCurrentThumbnail.prev().hasClass('item-onstage')) {
					$('.layer-thumbnails li.item-onstage:last-child', oStage).removeClass('item-onstage').fadeOut('fast', function() {
						oCurrentThumbnail.prev().addClass('item-onstage').fadeIn('fast', function() {
							$('a', $(this)).trigger('click');
						});
					});
				}
				else $('a', oCurrentThumbnail.prev()).trigger('click');
			}
			
			return false;
		});
	});
	
	function initHeadlineTimer() {
		headlineTimer = setInterval(function() {
			if ($('.layer-thumbnails li.item-selected').is(':last-child')) direction = 'previous';
			else if ($('.layer-thumbnails li.item-selected').is(':first-child')) direction = 'next';
				
			if (direction == 'next') $('.layer-headline .panel-next a').trigger('click');
			else $('.layer-headline .panel-previous a').trigger('click');
		}, 5000);
	}
	
	initHeadlineTimer();
	
	
	// Back to top
	
	$('.panel-top a').click(function() {
		$('html,body').animate({ scrollTop: 0 }, 1000);
		
		return false;
	});
	
	
	// Search bar input
	
	$('#mc_mv_FNAME').focus(function() {
		if ($(this).val() == 'name ') $(this).val('');
	}).blur(function() {
		if ($(this).val() == '') $(this).val('name ');
	});
	
	$('#mc_mv_EMAIL').focus(function() {
		if ($(this).val() == 'email address ') $(this).val('');
	}).blur(function() {
		if ($(this).val() == '') $(this).val('email address ');
	});
	
	
	// Newsletter input
	
	$('#input-keyword').focus(function() {
		if ($(this).val() == 'enter search keyword ') $(this).val('');
	}).blur(function() {
		if ($(this).val() == '') $(this).val('enter search keyword ');
	});
});

jQuery(window).load(function() {
	jQuery('.layer-headline').each(function() {
		var oStage = jQuery(this);
		var iHeight = 0;
		
		jQuery('.listing-images li', oStage).each(function() {
			iHeight = (iHeight < jQuery(this).height()) ? jQuery(this).height() : iHeight;
		});
		
		jQuery('.listing-images', oStage).height(iHeight-12);	
	});
	
	
	// Latest posts
	
	var iHeight = 0;
	jQuery('.layer-latestpostgroup0 li').each(function() {
		iHeight = (iHeight > jQuery(this).height()) ? iHeight : jQuery(this).height();
	}).height(iHeight);
	
	iHeight = 0;
	jQuery('.layer-latestpostgroup1 li').each(function() {
		iHeight = (iHeight > jQuery(this).height()) ? iHeight : jQuery(this).height();
	}).height(iHeight);
});