/*MENU CATEGORIE*/
function open_closeMenuCategorie(idCategoria) {
    var contenitoreMadre;
    contenitoreMadre = document.getElementById('divContenitoreMenuCategorie')
    figlie = contenitoreMadre.getElementsByTagName('div');
    var x = 0;
    for (i = 0; i < figlie.length; i++) {
        //alert(figliePulito[i].id + ' ' + 'menuCategorie_' + x);
        //if (figlie[i].id.indexOf('banner' + x) > -1) alert('si')
        
        if (x == idCategoria) {
            if (figlie[i].id.indexOf('menuCategorie_' + x) > -1) {
                if (figlie[i].style.display == 'block') {
                    figlie[i].style.display = 'none';
                } else {
                    figlie[i].style.display = 'block';
                }
                x +=1;
            }
        } else {
            if (figlie[i].id.indexOf('menuCategorie_' + x) > -1) {
                figlie[i].style.display = 'none';
                x +=1;
            }
        }
    }
}
/*FINE MENU CATEGORIE*/

/*BANNER*/
var x = 0;
var timeID;
var tempoBanner;
var tempiBanner;
function ShowBanner(idBanner, tempo) {
    tempiBanner = tempo;
    var contenitoreMadre;
    var figlie;
    for (x = 0; x < tempo.split("|").length; x++) {
		contenitoreMadre = document.getElementById('divContenitoreBanner')
		figlie = contenitoreMadre.getElementsByTagName('div');
		j=0;
		var cl = new Array()
		for(i=0;i<figlie.length;i++){
			//alert(figlie[i].id);
			//if (figlie[i].id.indexOf('banner' + x) > -1) alert('si')
			if (x == idBanner) {
				if (figlie[i].id.indexOf('banner' + x) > -1){
					figlie[i].style.display = 'block';
					figlie[i].style.display = 'block';
				}
			} else {
				if (figlie[i].id.indexOf('banner' + x) > -1){
					figlie[i].style.display = 'none';
					figlie[i].style.display = 'none';
				}
			}
   		}
    }
    document.getElementById('SelettoreMenu').className = 'bis' + idBanner;
    tempoBanner = tempo.split("|")[idBanner];
    if (idBanner < tempo.split("|").length-1) {
        idBanner++;
    }else{
        idBanner = 0;
    }
    timerID = setTimeout('ShowBanner(' + idBanner + ',"' + tempo + '")', tempoBanner * 1000);
}        
function selectBanner(valore){
    clearTimeout(timerID);
    ShowBanner(valore,tempiBanner);
}

/*Fasce Banner dei Corsi*/
var xFasce = 0;
var timeIDFasce1;
var timeIDFasce2;
var timeIDFasce3;
var tempoBannerFasce;
var tempiBannerFasce;
function ShowBannerCorsi(idBannerFasce, nBanner, tempo) {
    for (xFasce = 0; xFasce < idBannerFasce.split("|").length; xFasce++) {
        if (xFasce == nBanner) {
            document.getElementById('Banner' + idBannerFasce.split("|")[xFasce]).style.visibility = 'visible';
            document.getElementById('Icona' + idBannerFasce.split("|")[xFasce]).style.visibility = 'visible';
            document.getElementById('Fascia' + idBannerFasce.split("|")[xFasce]).style.visibility = 'visible';
        } else {
            document.getElementById('Banner' + idBannerFasce.split("|")[xFasce]).style.visibility = 'hidden';
            document.getElementById('Icona' + idBannerFasce.split("|")[xFasce]).style.visibility = 'hidden';
            document.getElementById('Fascia' + idBannerFasce.split("|")[xFasce]).style.visibility = 'hidden';
        }
    }
    var fascia = idBannerFasce.split("|")[0].split("_")[1];;
    tempiBannerFasce = tempo.split("/")[fascia - 1].split("|")[nBanner];
    if (nBanner < idBannerFasce.split("|").length-1) {
        nBanner++;
    } else {
        nBanner = 0;
    }

    if (fascia == "1") {
        //alert(tempiBannerFasce + '<tempoCorrente totaleTempi>' + tempo + ' FasciaDadetrarre1>' + fascia + ' nBanner>' + nBanner);
        timerIDFasce1 = setTimeout('ShowBannerCorsi("' + idBannerFasce + '",' + nBanner + ',"' + tempo + '")', tempiBannerFasce * 1000);
    } else if (fascia == "2") {
        timerIDFasce2 = setTimeout('ShowBannerCorsi("' + idBannerFasce + '",' + nBanner + ',"' + tempo + '")', tempiBannerFasce * 1000);
    } else if (fascia == "3") {
        timerIDFasce3 = setTimeout('ShowBannerCorsi("' + idBannerFasce + '",' + nBanner + ',"' + tempo + '")', tempiBannerFasce * 1000);
    }

}
function stopBanner(valore) {
    if (valore == "1") {
        clearTimeout(timerIDFasce1);
    } else if (valore == "2") {
        clearTimeout(timerIDFasce2);
    } else if (valore == "3") {
        clearTimeout(timerIDFasce3);
    }
}
function startBanner(idBannerFasce, nBanner, nFascia, tempo) {
    ShowBannerCorsi(idBannerFasce, nBanner, tempo);
}
function nextBanner(idBannerFasce, nBanner, nFascia, tempo) {
    if (nFascia == "1") {
        clearTimeout(timerIDFasce1);
    } else if (nFascia == "2") {
        clearTimeout(timerIDFasce2);
    } else if (nFascia == "3") {
        clearTimeout(timerIDFasce3);
    }
    var maxValore = idBannerFasce.split("|").length
    if (nBanner + 1 > maxValore -1) nBanner = -1
    ShowBannerCorsi(idBannerFasce, nBanner+1, tempo);
}
function prevBanner(idBannerFasce, nBanner, nFascia, tempo) {
    if (nFascia == "1") {
        clearTimeout(timerIDFasce1);
    } else if (nFascia == "2") {
        clearTimeout(timerIDFasce2);
    } else if (nFascia == "3") {
        clearTimeout(timerIDFasce3);
    }
    //alert(idBannerFasce.split("|").length)
    var maxValore = idBannerFasce.split("|").length
    if (nBanner - 1 < 0) nBanner = maxValore
    ShowBannerCorsi(idBannerFasce, nBanner - 1, tempo);
}

/*Fine*/

/*GALLERIA IN SCHEDA PRODOTTO*/
function ingrandisciImmagine(){
    var divSfondo;
    divSfondo.style.width = '50px'
    divSfondo.style.height = '50px'
    divSfondo.style.border = '1px solid #000'
    document.body.appendChild(divSfondo);
}
function selezionaImmagine(elencoImmagini, immagineSelezionata, percorso){
    document.getElementById('immagineGrande').setAttribute('src',percorso + immagineSelezionata);
    document.getElementById('tagImmagineGrande').setAttribute('onClick','visualizza("' + elencoImmagini + '","' + immagineSelezionata + '",1)');
}
function IEdetect(){
   if (navigator.userAgent.toLowerCase().indexOf("msie") != -1){
     return true;
   }
}
var inizioFADE = 0
var fineFADE = 80
function fadeSfondo(elencoImmagini, immagineSelezionata, mode) {
    if (mode == 'in'){
        if (IEdetect()) {
            document.getElementById('divGalleria').style.filter = 'alpha(opacity=' + inizioFADE + ')';
        } else {
            document.getElementById('divGalleria').style.opacity = inizioFADE / 100;
        }
        inizioFADE +=10;
        if (inizioFADE < fineFADE) {
            timerFADE = setTimeout('fadeSfondo("' + elencoImmagini + '","' + immagineSelezionata + '","in")', 1);
        } else {
            clearTimeout(timerFADE);
            caricaImmagine(elencoImmagini, immagineSelezionata)
        }
    }else{
        if (IEdetect()) {
            document.getElementById('divGalleria').style.filter = 'alpha(opacity=' + inizioFADE + ')';
        } else {
            document.getElementById('divGalleria').style.opacity = inizioFADE / 100;
        }
        inizioFADE -=10;
        if (inizioFADE > 0) {
            timerFADE = setTimeout('fadeSfondo("","","out")', 1);
        } else {
            clearTimeout(timerFADE);
            eliminaDivFondo();
        }
    }
}
function visualizza(elencoImmagini, immagineSelezionata, nuovoIngresso){
    if (nuovoIngresso == 1) {
        inizioFADE = 0;
        var divSfondo = document.createElement('div');
        divSfondo.setAttribute("id", "divGalleria");
        divSfondo.setAttribute("class", "sfondoGalleria");
        document.body.appendChild(divSfondo);
        fadeSfondo(elencoImmagini, immagineSelezionata,'in');
    } else {
        caricaImmagine(elencoImmagini, immagineSelezionata)
    }
}

function caricaImmagine(elencoImmagini, immagineSelezionata) {
    var div = document.createElement('div');
    div.setAttribute("id","divGalleria2");
    div.setAttribute("class","sfondoGalleria_NoSfondo");
   
    /*tasto chiusura*/
    var tastoLink = document.createElement('a');
    var tasto = document.createElement('img');
    tastoLink.setAttribute('href','javascript:void(0)');
    tastoLink.setAttribute('onClick','chiudi(1)');
    tasto.setAttribute('src','/App_Themes/Tema1/chiudi.png');
    tasto.style.position = 'absolute';
    tasto.style.border = '0px';
    tasto.style.top = '-12px';
    tasto.style.right = '-12px';
    tastoLink.appendChild(tasto);

    /*immagine*/
    var tableInterno = document.createElement('table');
    var rowInterno = document.createElement('tr');
    var columnInterno = document.createElement('td');
    tableInterno.setAttribute("class","sfondoGalleria_immagine");
    tableInterno.setAttribute("align","center");
    
    var immagine = document.createElement("img");
    immagine.setAttribute("src","/public/catalogo/" + immagineSelezionata);
    
    var divContenitrice = document.createElement('div');
    divContenitrice.style.position = 'relative';
    divContenitrice.style.width = '100%';
    divContenitrice.style.top = '0px';    
    divContenitrice.style.right = '0px';  
    divContenitrice.appendChild(immagine);
    divContenitrice.appendChild(tastoLink);
    columnInterno.appendChild(divContenitrice);
    
    columnInterno.setAttribute('colspan','3');
    rowInterno.appendChild(columnInterno);
    tableInterno.appendChild(rowInterno);

    var rowInterno2 = document.createElement('tr');
    var columnInterno2 = document.createElement('td');
    var testo = document.createElement('label');
    var totaleImg = elencoImmagini.split('/');
    var nImgSelezionata;
    for (x=0;x<totaleImg.length;x++){
        if (totaleImg[x] == immagineSelezionata) nImgSelezionata = x
    }
    testo.innerHTML = 'immagine ' + (nImgSelezionata+1) + ' di ' + totaleImg.length;
    columnInterno2.style.paddingLeft = '2px';
    columnInterno2.setAttribute("align","left");
    columnInterno2.setAttribute('height','50');
    columnInterno2.setAttribute('valign','middle');
    columnInterno2.appendChild(testo);

    var next;
    var prev;
    switch (nImgSelezionata){
        case 0: 
        prev = '0';
        if (nImgSelezionata == totaleImg.length-1){
            next = '0';
        }else{
            next = totaleImg[nImgSelezionata+1]
        }
        break; 

        case totaleImg.length-1:
        if (nImgSelezionata == 0){
            prev = '0';
        }else{
            prev = totaleImg[nImgSelezionata-1]
        }   
        next = '0';
        break; 
        
        default:
        next = totaleImg[nImgSelezionata+1]
        prev = totaleImg[nImgSelezionata-1]
        break; 

    }

    var columnInterno3 = document.createElement('td');
    var tastoLink = document.createElement('a');
    var tasto = document.createElement('img');
    if (prev != '0') {
    tastoLink.setAttribute('href','javascript:void(0)');
    tastoLink.setAttribute('onClick','chiudi(0);visualizza("' + elencoImmagini + '","' +  prev + '")');
    tasto.setAttribute('src','/App_Themes/Tema1/prev.png');
    tasto.style.border = '0px';
    }else{
    tastoLink.setAttribute('href','javascript:void(0)');
    tasto.setAttribute('src','/App_Themes/Tema1/prevOFF.png');
    tasto.style.border = '0px';
    }
    tastoLink.appendChild(tasto);
    columnInterno3.setAttribute('width','32');
    columnInterno3.setAttribute('valign','middle');
    columnInterno3.appendChild(tastoLink);
 
    var columnInterno4 = document.createElement('td');
    var tastoLink = document.createElement('a');
    var tasto = document.createElement('img');
    if (next != '0'){
    tastoLink.setAttribute('href','javascript:void(0)');
    tastoLink.setAttribute('onClick','chiudi(0);visualizza("' + elencoImmagini + '","' +  next + '")');
    tasto.setAttribute('src','/App_Themes/Tema1/next.png');
    tasto.style.border = '0px';
    }else{
    tastoLink.setAttribute('href','javascript:void(0)');
    tasto.setAttribute('src','/App_Themes/Tema1/nextOFF.png');
    tasto.style.border = '0px';
    }
    tastoLink.appendChild(tasto);
    columnInterno4.setAttribute('width','32');
    columnInterno4.setAttribute('valign','middle');
    columnInterno4.appendChild(tastoLink);
    rowInterno2.appendChild(columnInterno2);
    rowInterno2.appendChild(columnInterno3);
    rowInterno2.appendChild(columnInterno4);

    
    tableInterno.style.background = '#fff';
    tableInterno.appendChild(rowInterno2);

    div.appendChild(tableInterno);
    
    document.body.appendChild(div);
}
function chiudi(chiusuraDefinitiva){
    if (chiusuraDefinitiva == 1){
        fadeSfondo('','', 'out')
    }

    var div = document.getElementById('divGalleria2');
    document.body.removeChild(div);
}
function eliminaDivFondo(){
    var divSfondo = document.getElementById('divGalleria');
    document.body.removeChild(divSfondo);
}

/*CARRELLO*/
function abilitaReloadCarrello(){
    if (elementExists('aggiornaCarrello') && elementExists('confermaCarrello')) {
        document.getElementById('aggiornaCarrello').disabled = false;
        document.getElementById('aggiornaCarrello').removeAttribute("src")
        document.getElementById('aggiornaCarrello').setAttribute("src","/App_Themes/Tema1/aggiorna-carrello.png")
       
        document.getElementById('confermaCarrello').disabled = true;
        document.getElementById('confermaCarrello').removeAttribute("src")
        document.getElementById('confermaCarrello').setAttribute("src", "/App_Themes/Tema1/conferma-carrello-OFF.png")
    }
}
function elementExists(id) {
	var el = document.getElementById(id);
 
	if (el != null) {
		return true;
	}
	return false;
}
function attivaPagamento(dd1, dd2) {
    var d1 = document.getElementById(dd1);
    var d2 = document.getElementById(dd2);

    d1.disabled = false;
    d2.disabled = true;
    document.getElementById('confermaPagamento').disabled = false
}
