// ----------------------------------------
var activeBtn = 0;
var intervalID;
var overBorder = false;

$(document).ready(function()
{
	var visibleWidth = (btnWidth + btnPad) * visibleCount - btnPad;
	$("#scrollContainer").css("width", visibleWidth);
	$("#btnFrame").css("width", visibleWidth);
	$("#btnContainer").css("width", (btnWidth + btnPad) * btnCount * 2);
	$(".btn").css("margin-right", btnPad);
	
	for(i = 1; i <= btnCount; i++)
	{
		$("#btn"+i).attr("linkNum", i-1);
		$("#btn"+i).attr("btnNum", i);
		var newBtn = $("#btn"+i).clone(true).attr("id", "btn"+(i+btnCount)).appendTo("#btnContainer");
	}
	
	$(".btn > a > img").hover(
	function()
	{
		mouseOver(this);
		
	}, function()
	{
		mouseOut(this);
	}
	);
	
	$("#btnBorder").click(function(){
		$("#borderA").attr("href", $("#btn"+(activeBtn+1)+" > a").attr("href"));
	});
	
	$("#btnBorder").hover(
	function()
	{
		overBorder = true;
		mouseOver($("#btn"+(activeBtn+1)+" > a > img"));
	}, function()
	{
		overBorder = false;
		mouseOut($("#btn"+(activeBtn+1)+" > a > img"));
	}
	);
	
	$("#btnPrev").click(function(){
		clearInterval(intervalID);
		moveBtns(true);
		startLoop();
	});
	
	$("#btnNext").click(function(){
		clearInterval(intervalID);
		moveBtns();
		startLoop();
	});
	
	//reseteamos scroll y cargamos SWF #0
	attachSWF(btnSWF[0]);
	$("#btnFrame").scrollTo(0, 0, {axis:'x'});
	
	// mostramos el contenedor
	$("#scrollContainer").css("visibility", "visible");
	
	startLoop();
});

function startLoop()
{
	intervalID = setInterval("moveBtns()", intervalTime * 1000);
}

function moveBtns(reverse)
{
	if(overBorder) mouseOut($("#btn"+(activeBtn+1)+" > a > img"));
	
	if(reverse) activeBtn--;
	else activeBtn++;
	if(activeBtn > btnCount)
	{
		$("#btnFrame").scrollTo(0, 0, {axis:'x'});
		activeBtn = 1;
	}
	if(activeBtn < 0)
	{
		$("#btnFrame").scrollTo((btnWidth + btnPad) * btnCount, 0, {axis:'x'});
		activeBtn = btnCount - 1;
	}
	
	if(overBorder) mouseOver($("#btn"+(activeBtn+1)+" > a > img"));
	
	attachSWF(btnSWF[(activeBtn == btnCount) ? 0 : activeBtn]);
	
	var endX = (btnWidth + btnPad) * activeBtn;
	$("#btnFrame").scrollTo(endX, scrollTime * 1000, {axis:'x', easing: 'easeOutCubic'});
	
}

function attachSWF(swf)
{
	// atacheamos swf
	swfobject.embedSWF(swf, "SWFo", 539, 236, "9.0.0", "", false, {wmode:"transparent"});
}

function mouseOver(obj)
{
	$(obj).attr("src", $(obj).attr("src").substring(0, $(obj).attr("src").length - 4) + "_over.jpg");
}

function mouseOut(obj)
{
	$(obj).attr("src", $(obj).attr("src").substring(0, $(obj).attr("src").length - 9) + ".jpg");
}