var gap = 50;
var vgap = 40;
var width = 430;
var delay = 75;
var duration = 1500;
var interval = 15000;

$(function(){
	
	if(! $.browser.msie)
	{
		$(".panel").css("position","relative");

		var run_cells = $("#runs .cell");
		var promises_cells = $("#promises .cell");	

		$(".panel").each(function (j){

			var cells = $(this).children(".cell");
			var cols = [];
			var index = -1;

			cells.css("position","absolute").css("width",width + "px").each(function(i)
			{
				if((i%3) == 0)
				{
					cols[++index] = [];
				}

				cols[index].push(this);

				var top = ((i % 3) * 80) + vgap + "px";
				var left = 0;
				if(i > 2)
				{
					left = (width + gap) + "px";
				}

				$(this).css("top",top);
				$(this).css("left",left);		
			});

			$(this)[0].cols = cols;
			$(this)[0].current_col = 0;

		});

		setInterval(start_anim,interval);		
	}	

});

function start_anim()
{

	$(".panel").each(function (j){

		var panel = this;
		var cols = $(this)[0].cols;

		var previous_col = $(panel)[0].current_col;
		$(panel)[0].current_col++;
		var current_col = 	$(panel)[0].current_col;

		if(cols.length > current_col)
		{
			anim_col(cols,previous_col,current_col);
		}
		else
		{	
			$(panel)[0].current_col = current_col = 0;

			if(previous_col != current_col)
			{
				anim_col(cols,previous_col,current_col);
			}
		}

	});		

}

function anim_col(cols,previous,current)
{
	var previous_cells = cols[previous];
	var current_cells = cols[current];	

	for(var i = 0 ; i < previous_cells.length ; i++)
	{
		setTimeout(anim,delay * i,previous_cells[i],width,gap,true);							
	}

	for(var i = 0 ; i < current_cells.length ; i++)
	{
		setTimeout(anim,delay * i,current_cells[i],width,gap,false);					
	}
}

function anim(obj,width,gap,previous)
{
	$(obj).animate(
			{
				left: $(obj).css("left").split("px")[0] - width - gap + "px"
			},duration,function(){
				if(previous)
				{
					$(this).css("left",width + gap + "px");					
				}
			}
		);
}
