﻿function posicionaCarrossel() {
	window.carrossel = document.getElementById("carrossel");
	window.area = document.getElementById("areaVisivel");
	window.lis = carrossel.getElementsByTagName("li");
	window.ul = document.getElementById("ulCar");	
	window.marginLi = "10"; //soma das margem + bordas entre as linhas	
	window.tamanhoUl = parseInt(parseInt(lis[0].clientWidth) + parseInt(marginLi)) * parseInt(lis.length); 
	ul.style.width = tamanhoUl + "px";
	window.liTamanho = parseInt(lis[0].clientWidth) + parseInt(marginLi);
	window.liQuant = lis.length;
	window.liShow = parseInt(area.clientWidth / liTamanho);
	window.limiteLiScroll = liQuant - liShow;
	window.over = 0;
	window.posiUltima = 0;
}

function ligaDesligaBotoes(){
	window.bt_left = document.getElementById('bt_left');
	window.bt_right = document.getElementById('bt_right');
	
	bt_left.className = "bt left_on";
	bt_right.className = "bt right_on";
	
	window.posAtual = (parseInt(area.scrollLeft) + parseInt(over));
	
	if ((posAtual <= 0) || (over <= 0)){
		bt_left.className = "bt left_off";
	}
	
	else if (posAtual >= area.scrollWidth) {
		bt_right.className = "bt right_off";
	}
}
function btscrollLeft(){    
	if ((posAtual >= 0) || (over <= 0)) {
		var start = area.scrollLeft;
		
		window.posAtual = area.scrollWidth - ((Math.floor((area.scrollWidth - area.scrollLeft) / liTamanho)) * liTamanho);
        window.over = posAtual - liTamanho;
        window.posiUltima = posAtual;
        
		window.mov = start;
		
		window.direcao = -1;
		if (window.controlador){
          clearInterval(window.controlador)
        }
		window.controlador = setInterval('moveScroll()','1');
	}
}

function btscrollRight(){
	if (posAtual != area.scrollWidth) {
		var start = area.scrollLeft;
		
		window.posAtual = area.scrollWidth - ((Math.floor((area.scrollWidth - area.scrollLeft) / liTamanho)) * liTamanho);
        window.over = posAtual + liTamanho;
        window.posiUltima = posAtual;
        
		window.mov = start;
		
		window.direcao = 1;
		if (window.controlador){
          clearInterval(window.controlador);
        }
		window.controlador = setInterval('moveScroll()','1');	
	}
}

function moveScroll(){
	if (direcao == 1 && mov < over)
	{
		mov += direcao;
		area.scrollLeft = mov;
	}
	else if (direcao == -1 && mov > over)
	{
		mov += direcao;
		area.scrollLeft = mov;
	}
	else
    {
        clearInterval(window.controlador)
        ligaDesligaBotoes();
    }
}

function ativaItem(linha, contId){
	for (var i = 0; i < lis.length; i++){
		lis[i].className = "";
		document.getElementById('cont'+i).style.display = "none";
	}
	linha.className = "on";
	document.getElementById(contId).style.display = "block";
}