$(document).ready(function() {
	var count = 0;
	var cycle = 0;
	var items = $("#newsTapeItems > li")
	for (var i=1; i<items.length; i++) {
		$(items[i]).css({'display': 'none'});
	}
	if (items.length > 1) {
		setTimeout(function() { showNextStory(items, count, cycle); }, 6000);
	}
	
	$('#home_left_adverts li:not(.active)').css({'opacity': '0.0'});
	$('#home_left_adverts li.active').css({'opacity': '1.0'});
	var listSw = setInterval( "itemSwitch()", 5000 );
});

function showNextStory(items, count, cycle) {
	cycle++;
	if (cycle < 50) {
		var nextLiInd = count + 1;
		if (nextLiInd < items.length) {
			$(items[count]).fadeOut(500, function() {
				$(items[nextLiInd]).fadeIn(500);
				count++;
			});
			
		} else {
			$(items[count]).fadeOut(500, function() {
				$(items[0]).fadeIn(500);
				count = 0;
			});
			
		}
		setTimeout(function() { showNextStory(items, count, cycle); }, 6000);
	}
}

function itemSwitch() {
    var $active = $('#home_left_adverts li.active');

    if ( $active.length == 0 ) $active = $('#home_left_adverts li:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#home_left_adverts li:first');


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 500, function() {
            $active.removeClass('active last-active');
    });
}


