var scrollItems;
var scrollItems2;
var scrollPos = 0;
var scrollPos2 = 0;
var itemWidth = 0;
var timmer;

var useTime = true;

$(document).ready(function(){

    var maxHeight = 0;
    $(".capaNews").each( function()
    {
        maxHeight = Math.max( $(this).height(), maxHeight );
    }).height( maxHeight );

	scrollItems = $("#scroll div.items div.screenshots").length;
	scrollItems2 = $("#scroll2 div.items div.screenshots").length;
	itemWidth = parseInt($("div.screenshots").css("width"));
	
	$("#scroll").mouseover(function(){
		$(document).stopTime();
	}).mouseout(function(){
		startTime();
	});

	$("#scroll2").mouseover(function(){
		$(document).stopTime();
	}).mouseout(function(){
		startTime2();
	});
	
	$("#scroll div.scrollInd a").click(function()
	{
        $(document).stopTime(); useTime = false;
		idScreenShot = parseInt($(this).attr("idScreenShot"));
		scrollPos = idScreenShot;
		gotoItem(idScreenShot);
	});

	$("#scroll2 div.scrollInd a").click(function()
	{
        $(document).stopTime(); useTime = false;
		idScreenShot = parseInt($(this).attr("idScreenShot"));
		scrollPos2 = idScreenShot;
		gotoItem2(idScreenShot);
	});
	
	startTime();
	startTime2();
});

function startTime()
{
	if(useTime)
	{
		$(document).everyTime(5000, function(i) {
			moveScroll();
		});
	}
}

function startTime2()
{
	if(useTime)
	{
		$(document).everyTime(5000, function(i) {
			moveScroll2();
		});
	}
}

function moveScroll(){
	scrollPos++;
	if(scrollPos == scrollItems){
		scrollPos = 0;
	}
	gotoItem(scrollPos);
}

function moveScroll2(){
	scrollPos2++;
	if(scrollPos2 == scrollItems2){
		scrollPos2 = 0;
	}
	gotoItem2(scrollPos2);
}

function gotoItem(position){
	posLeft = position * itemWidth;
	
	$("#scroll div.items").animate({
		left : 0 - posLeft
	}, 250);
	
	$("#scroll div.scrollInd a").attr("class", "scrollControl");
	$("#scroll div.scrollInd a#ind" + position).attr("class", "scrollControlSel");
}

function gotoItem2(position){
	posLeft = position * itemWidth;
	
	$("#scroll2 div.items").animate({
		left : 0 - posLeft
	}, 250);
	
	$("#scroll2 div.scrollInd a").attr("class", "scrollControl");
	$("#scroll2 div.scrollInd a#ind" + position).attr("class", "scrollControlSel");
}


