$(document).ready(function() {
	function swapStories(oldone, newone) {
		oldone.hide();
		newone.fadeIn();
		$(document).stopTime();
		$(document).everyTime(5000, everTimeFunc);
	}
	function everTimeFunc() {
		var current = miniNav.find('a.current');
		var next = current.next();
		current.removeClass('current');
		current.next().addClass('current');
		if (next.length > 0) {
			swapStories(caro.find('.contentdiv.'+current.attr('class')), caro.find('.contentdiv.'+current.attr('class')).next());
			next.addClass('current');
		} else {
			swapStories(caro.find('.contentdiv.'+current.attr('class')), caro.find('.contentdiv.story1'));
			current.parent().find('a:first-child').addClass('current');
		}
	}
	var caro = $('.carousel');
	var miniNav = caro.find('.miniNav');
	miniNav.show();
	miniNav.find('a:first-child').addClass('current');
	miniNav.find('a').click( function(e) {
		e.preventDefault();
		if (!$(this).hasClass('current')) {
			miniNav.find('a.current').removeClass('current');
			var storyNo = $(this).attr('class');
			$(this).addClass('current');
			swapStories(caro.find('.contentdiv'), caro.find('.contentdiv.'+storyNo));
		}
	});
	$(document).everyTime(5000, everTimeFunc);
	
	// fixing background-image color change problem on rollover
	$('a.block:first-child').mouseover(function() {
		$(this).parent().parent().find('h4 .cbl').css('background-image', 'url('+fragment_root+'images/corner_h4_active_bottom_left.png)');
	});
	$('a.block:first-child').mouseout(function() {
		$(this).parent().parent().find('h4 .cbl').css('background-image', 'url('+fragment_root+'images/corner_h4_bottom_left.png)');
	});
	$('a.block:nth-child(2)').mouseover(function() {
		$(this).parent().parent().find('h4 .cbr').css('background-image', 'url('+fragment_root+'images/corner_h4_active_bottom_right.png)');
	});
	$('a.block:nth-child(2)').mouseout(function() {
		$(this).parent().parent().find('h4 .cbr').css('background-image', 'url('+fragment_root+'images/corner_h4_bottom_right.png)');
	});
});
