$(document).ready(function(){
	var nextElement, myTimeout;

	$("#confettiNews li").css('display','none');
	$("#confettiNews li").each(function() {
		$('a',this).wrap('<div class="tickerWrapper"></div>').after('<div class="sliderTicker">_</div>');
	});
	$('#buttonTicker').show();
	$('#play').hide();
	$("#confettiNews li a").hover(
		function(){
			$('.sliderTicker').fadeOut('fast').stop();
			clearTimeout(myTimeout);
		},
		function(){
			animateNextElement($(nextElement));
	});
	
	$('#pause').click(function(){
		$('.sliderTicker').fadeOut('fast').stop();
		clearTimeout(myTimeout);
		$(this).hide();
		$('#play').show();
	});
	
	$('#play').click(function(){
		animateNextElement($(nextElement));
		$(this).hide();
		
		$('#pause').show();
		$('#next').attr('src','http://www.greeneducationdirectory.com/images/ui/next.gif');
		$('#prev').attr('src','http://www.greeneducationdirectory.com/images/ui/prev.gif');
	});
	
	$('#prev').click(function(){
		
		$('.sliderTicker').fadeOut('fast').stop().hide();
		clearTimeout(myTimeout);
		
		$('#pause').hide();
		$('#play').show();
		
		if ($(nextElement).prev().length > 0){
			$(nextElement).css('display', 'none');
			
			nextElement = $(nextElement).prev();
			$('#next').attr('src','http://www.greeneducationdirectory.com/images/ui/next.gif');
			
		} else {
			nextElement = $("#confettiNews li:first");
			$(this).attr('src','http://www.greeneducationdirectory.com/images/ui/prev.gif');
			$('.sliderTicker').fadeOut('fast').stop();
		}
		$(nextElement).css('display', 'block');
	});
	
	$('#next').click(function(){
		$('.sliderTicker').fadeOut('fast').stop().hide();
		clearTimeout(myTimeout);

		
		$('#pause').hide();
		$('#play').show();
		if ($(nextElement).next().length > 0){
			$(nextElement).css('display', 'none');
			
			nextElement = $(nextElement).next();
			$('#prev').attr('http://www.greeneducationdirectory.com/images/ui/prev.gif');
		} else {
			nextElement = $("#confettiNews li:last");
			$(this).attr('src','http://www.greeneducationdirectory.com/images/ui/next.gif');
		}
		$('.sliderTicker').hide();
		$(nextElement).css('display', 'block');
		
	});
	
	nextElement = $("#confettiNews li:first");
	$(nextElement).css('display', 'block');
	$('.sliderTicker',nextElement).animate({ width:"0"	}, 6000, 'linear',function(){
		
		animateNextElement(nextElement);
	});

	function animateNextElement(currentElement){
		myTimeout = setTimeout(function(){
			$(currentElement).css('display', 'none');
	
			if ($(currentElement).next().length > 0){
				nextElement = $(currentElement).next();
			} else {
				nextElement = $("#confettiNews li:first");
			};
			$(nextElement).css('display', 'block');
			
			$('.sliderTicker',nextElement).css('width','100%').animate({ width:"0"}, 6000, 'linear',function(){animateNextElement(nextElement)});
		},6000);
	};
	

});
