$('document').ready(function(){

	var teaser = $('#teaser');
	
	var items = $('#teaser li');
	
	items.each(function(i){
		
		$(this).find('h1 a').click(function(e){
			
			slideThis(i);
			e.preventDefault();
			
		});
		
		var url = $(this).find('h1 a').attr('href');
		$(this).find('p').append(' <a href="'+url+'">Read More</a>');
		
	});
	
	var current = 0;
	
	var timer = setInterval(slider, 5000);
	
	$('#teaser a.image').first().css('display','block');
	
	$('#teaser h1').first().addClass('active');
	
	$('#teaser p').first().show();
	
	function slider(){
		
		var prev;
		
		if(current == 0){
			prev = items.length-1;
		} else {
			prev = current-1;
		}
		
		$(items[current]).find('a.image').fadeIn(1500);
		$(items[prev]).find('a.image').fadeOut(1500);
		$(items[current]).find('h1').addClass('active');
		$(items[prev]).find('h1').removeClass('active');
		$(items[prev]).find('p').slideUp(700);
		$(items[current]).find('p').slideDown(700);
		
		if(current == items.length-1){
			current = 0;
		} else {
			current++;
		}
		
	}
	
	function slideThis(i){
		
		if(i == current) return false;
		
		clearInterval(timer);
		
		var prev;
		
		if(current == 0){
			prev = items.length-1;
		} else {
			prev = current-1;
		}
		
		$(items[i]).find('a.image').fadeIn(1500);
		$(items[current]).find('a.image').fadeOut(1500);
		$(items[i]).find('h1').addClass('active');
		$(items[current]).find('h1').removeClass('active');
		$(items[current]).find('p').slideUp('fast');
		$(items[i]).find('p').slideDown('fast');
				
		current = i;
		
	}
	
});
