var contador = 0;
var contador1 = 1;
var tiempo = 0;
var tempor = null;

function carruselActivo(){
	var actual = 0;
	
	for(j=0;j<fondoArr.length;j++){
				
		var img = fondoArr[j][0];
		var imgIni = document.getElementById('imgCarrusel').src;

		indexImg = img.indexOf("/media/");
		indexImgIni = imgIni.indexOf("/media/");

		lengthImg = img.length;
		lengthImgIni = imgIni.length;

		img = img.substring(indexImg,lengthImg);
		imgIni = imgIni.substring(indexImgIni,lengthImgIni);

		if(img == imgIni){actual  =j}
		
	}
	
	if(actual<(fondoArr.length-1)){	
		nuevaImg((actual+1),document.getElementById('li'+(actual+1)).childNodes[0])
		if(actual!= 0 && actual%5==4){moveLi(1)}
	}
	else{
		parar();
		reset();	
	}
}

function nuevaImg(itm,tim){	
	document.images.imgCarrusel.src = eval('fondoArr['+itm+'][0]');
	document.images.imgCarrusel.alt = eval('fondoArr['+itm+'][4]');
	document.images.imgCarrusel.parentNode.href = eval('fondoArr['+itm+'][1]');
	for(a=0;a<tim.parentNode.parentNode.childNodes.length;a++){
		if(tim.parentNode.parentNode.childNodes[a].tagName == 'LI'){tim.parentNode.parentNode.childNodes[a].className = 'contCarruselInctive'}
	}
	tim.parentNode.className = 'contCarruselActive';
}

function reset(){
	document.getElementById('li0').className = 'contCarruselActive';
	document.getElementById('contentNum').style.left = 0;
	contador = 0;
	contador1 =1;
	fin = false;
	document.getElementById('imgCarrusel').src = fondoArr[0][0];
	document.images.imgCarrusel.alt = fondoArr[0][4];
	document.images.imgCarrusel.parentNode.href = fondoArr[0][1];
	
	if(fondoArr.length>5){document.getElementById('flechaRightCarrusel').style.display = 'block';}
	else{document.getElementById('flechaRightCarrusel').style.display = 'none';}
	document.getElementById('flechaLeftCarrusel').style.display = 'none';
}

function automat(seg){
	tiempo = parseInt(seg);
	reset();
	if (isNaN(tiempo) || tiempo <= 0)   alert("Error en el tiempo")
	else tempor = setTimeout("pasar()", tiempo)
}

function parar(){
	clearTimeout(tempor);
}

function pasar(){
	carruselActivo(1);
    	tempor = setTimeout("pasar()", tiempo);
}

function moveLi(typ){
	
	if(fondoArr.length<5){
		document.getElementById('flechaLeftCarrusel').style.display = 'none';
		document.getElementById('flechaRightCarrusel').style.display = 'none';
	}
	
	var maxLong = (fondoArr.length/5)+1;
	var newLeft = document.getElementById('contentNum').offsetLeft;
	var increm = 0;
	
	contador1 = parseInt(contador1)+parseInt(typ);
	
	if(contador1<maxLong && contador1>=1){		
		if(fondoArr.length/(5*contador1)>=1){increm = 5*27}
		else{
			increm = (fondoArr.length%5)*27;
			if(typ==1){contador1++}
			else{contador1--}
		}
	}
	
	newMaxLong= parseInt(maxLong);	
	
	
	if(contador1<newMaxLong && fondoArr.length>4){document.getElementById('flechaRightCarrusel').style.display = 'block'}
	else{document.getElementById('flechaRightCarrusel').style.display = 'none'}
	
	if(contador1>=2){document.getElementById('flechaLeftCarrusel').style.display = 'block'}
	else{document.getElementById('flechaLeftCarrusel').style.display = 'none'}
		
	increm = increm*typ;
	
	if(IE){document.getElementById('contentNum').style.left = parseInt(document.getElementById('contentNum').offsetLeft-increm);}
	else {document.getElementById('contentNum').style.left = parseInt(document.getElementById('contentNum').offsetLeft-increm)+'px';}	
}

//automat(3000);
