function getActive(){
	var $active = $('.slide.active');
	if ( $active.length == 0 ) {
		$active = $('.slide:first');
		$active.addClass('active');
		$active.find('.slide_info').animate({
											opacity: 1.0,
											left: '-=300'
										  }, 300);
		$active.find('.slide_heading').animate({
											opacity: 1.0,
											top: '+=200'
										  }, 300);
		$active.find('.slide_link').fadeIn(300);
		$active.find('.slide_image').fadeIn(400);
	}
	return $active;
}

function switchSlide($next){
	
	$('.control').removeClass('active');
	
	var $active = getActive();
	
	if ( $next == null ) {
		var $next =  $active.next('.slide').length ? $active.next('.slide') : $('.slide:first');
	}
	
	var $num = $('.slide').index($next);
	var $control = $('.control:eq('+$num+')');
	$control.addClass('active');
	$active.find('.slide_heading').animate({
		opacity: 0.0,
		top: '-=200'
	}, 300).delay(3000).end().find('.slide_info').animate({
		opacity: 0.0,
		left: '+=300'
	}, 300, function() {
		$active.find('.slide_image').fadeOut(400, function () {
			$active.removeClass('active');
			$active.hide();
			$next.show();
			$next.find('.slide_image').fadeIn(400, function () {
				$next.find('.slide_heading').animate({
					opacity: 1.0,
					top: '+=200'
				}, 300).end().find('.slide_info').animate({
					opacity: 1.0,
					left: '-=300'
				  }, 300, function() {
					$next.find('.slide_link').fadeIn(300);	
				  });
			});
			$next.addClass('active');
		});
	});
	$active.find('.slide_link').fadeOut(300);	
}
function startSwitch() {
	switchInit = setInterval( "switchSlide()", 12000 );	
}
function stopSwitch() {
	clearInterval(switchInit);	
}



function blogSwitch() {
	var $active = $('.blog_control.active');
	if ( $active.length == 0 ) $active = $('.blog_control:first');
	var $next =  $active.next('.blog_control').length ? $active.next('.blog_control') : $('.blog_control:first');
	$next.click();
}
function startBlog() {
	blogInit = setInterval( "blogSwitch()", 12000 );	
}
function stopBlog() {
	clearInterval(blogInit);	
}


$(document).ready( function () {
	
	var num_posts = $('#latest_posts_list .child_item').length;
	
	var $active = $('#latest_posts_list .child_item.active');
	
	if(num_posts > 1){
		
		for(i=1;i<=num_posts;i++){
			$('#blog_controls').append('<div class="blog_control"></div>');
		}
		
		$('.blog_control').click( function() {
			var num = $('.blog_control').index(this);
			var box = $('#latest_posts_list .child_item:eq('+num+')');
			
			$('.blog_control').removeClass('active');
			$(this).addClass('active');
			
			$('#latest_posts_list .child_item.active').fadeOut(300, function() {
				$(this).removeClass('active')
				$(box).fadeIn(300, function() {
					$(box).addClass('active');
				});
			});
			
		});
	
	
		$('#latest_posts_list').hoverIntent( stopBlog, startBlog);
		startBlog();
	}
	
	if ( $active.length == 0 ){
		$('#latest_posts_list .child_item:first').addClass('active');
		$('.blog_control:first').addClass('active');	
	}
	
	
	$('.control').click( function() {
		$('.control').removeClass('active');
		$(this).addClass('active');
		var $num = $('.control').index(this);
		var $next = $('.slide:eq('+$num+')');
		
		switchSlide($next);
		
	});
	$('#gallery').hoverIntent( stopSwitch, startSwitch);
	getActive();
	startSwitch();
	
});
