let sliderDatos = {
	contenedor: 'container-slider',
	CSSListSlider: 'listslider'
}

var mTimer;

if(document.querySelector('#container-slider')){
	btnPrevNextSlide();
	ulListSlider();
	iniSlides('next');
	//setInterval('fntExecuteSlide("next")',5000);
}

function iniSlides(s){
	clearInterval(mTimer);
	fntExecuteSlide(s);
	mTimer = setInterval('fntExecuteSlide("'+s+'")',5000);
}

//------------------------ BOTONES ATRAS, ADELANTE -------------------
function btnPrevNextSlide(){
	sContenedor = document.getElementById(sliderDatos.contenedor);
	var bPrev = document.createElement('a');
		bPrev.innerHTML = '<i class="fas fa-chevron-circle-left"></i>';
		bPrev.className = 'arrowPrev'; //bPrev.setAttribute( "href", "#" );
		bPrev.onclick = function(){
			iniSlides("prev");
		}
		sContenedor.appendChild( bPrev );
	var bNext = document.createElement('a');
		bNext.innerHTML = '<i class="fas fa-chevron-circle-right"></i>';
		bNext.className = 'arrowNext';
		bNext.onclick = function(){
			iniSlides("next");
		}
		sContenedor.appendChild( bNext );
}

//------------------------------ LIST SLIDER -------------------------
function ulListSlider(){
	let parentTarget = document.getElementById('slider');
    let elements = parentTarget.getElementsByTagName('li');
	let cnt = document.getElementById(sliderDatos.contenedor);
	
	var ul = document.createElement('ul');
		ul.className = 'listslider';
		cnt.appendChild( ul );
	
	 for (var i = 0; i < elements.length; i++) {
		 var li = document.createElement("li");
		 var a = document.createElement("a");
		 	a.setAttribute("id", i);
		 	a.onclick = function(){
				clearInterval(mTimer);
				fntExecuteSlide(this.id);
         		return false;
			}
			//ul.appendChild(li).appendChild(a);
			li.appendChild(a);
			ul.appendChild(li);
    }
}

/*if(document.querySelector('.listslider')){
   let link = document.querySelectorAll(".listslider li a");
   link.forEach(function(link) {
      link.addEventListener('click', function(e){
         e.preventDefault();
         let item = this.getAttribute('itlist');
         let arrItem = item.split("_");
         fntExecuteSlide(arrItem[1]);
         return false;
      });
    });
}*/

function fntExecuteSlide(side){
    let parentTarget = document.getElementById('slider');
    let elements = parentTarget.getElementsByTagName('li');
    let curElement, nextElement;
	
    for(var i=0; i<elements.length;i++){

        if(elements[i].style.opacity==1){
            curElement = i;
            break;
        }
    }
    if(side == 'prev' || side == 'next'){
        if(side=="prev"){
            nextElement = (curElement == 0)?elements.length -1:curElement -1;
        }else{
            nextElement = (curElement == elements.length -1)?0:curElement +1;
        }
    }else{
        nextElement = side;
        side = (curElement > nextElement)?'prev':'next';

    }
    //RESALTA LOS PUNTOS
    let elementSel = document.getElementsByClassName("listslider")[0].getElementsByTagName("a");
    elementSel[curElement].classList.remove("item-select-slid");
    elementSel[nextElement].classList.add("item-select-slid");
    elements[curElement].style.opacity=0;
    elements[curElement].style.zIndex =0;
    elements[nextElement].style.opacity=1;
    elements[nextElement].style.zIndex =1;
}