// SETTINGS
// timers
var slideInTime = 3000;
var showWaitTime = 6000;
// limiter
var limit = 0;

$(document).ready(function(){

	// homepage carousel
	if( $('.mainStory') )
	{
		// hide all and add to array
		var panels = new Array();
		var i = 0;
		$('.main_story').each(function() {
			panels[i] = $(this);
			$(this).css('position', 'absolute');
			$(this).css('height', '300px');
			$(this).width('625px');
			if( i != 0) $(this).width(0);
			i++;
		});

		// loop through and repeat
		if($('.main_story').length > 1)
		{
			panels[0].show();
			setTimeout(function(){
				cycle_panels(panels, 1);
			}, showWaitTime); // page load
		}
		else if( $('.main_story').length == 1)
		{
			// only show first one
			$('.main_story').show();
		}
		// do nothing none to show
	}
});

function cycle_panels(panels, id)
{
	limit++;
	if(limit > 1000) {
		return false;
	}
	
	if( panels[id] )
	{
		//console.log(panels[id]);

		// show new panel
			panels[id].css('z-index', 2);
			panels[id].animate({
				opacity: 1,
				right: 0,
				width: '610px'
			}, slideInTime, function() {
				setTimeout(function(){

					panels[id].css('z-index', 1);

					if( panels[id-1]) {
						panels[id-1].hide();
						panels[id-1].width(0);
					} else {
						panels[panels.length-1].hide();
						panels[panels.length-1].width(0);
					}

					cycle_panels(panels, (id+1));

				}, showWaitTime);
			});
	}
	else
	{
		// reset
		cycle_panels(panels, 0);
	}
}