/*BANNER*/
var x = 0;
var timeID;
var tempoBanner;
var tempiBanner;
function ShowBanner(idBanner, tempo) {
    tempiBanner = tempo;
    for (x = 0; x < tempo.split("|").length; x++) {
        if (x == idBanner) {
            document.getElementById('testoBanner' + x).style.display = 'block';
            document.getElementById('banner' + x).style.display = 'block';
        } else {
            document.getElementById('testoBanner' + x).style.display = 'none';
            document.getElementById('banner' + x).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);
}

/*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').src = '/App_Themes/Tema1/aggiorna-carrello-on.png';
       
        document.getElementById('confermaCarrello').disabled = true;
        document.getElementById('confermaCarrello').src = '/App_Themes/Tema1/conferma-ordine-off.png';
    }
}
function elementExists(id) {
	var el = document.getElementById(id);
 
	if (el != null) {
		return true;
	}
	return false;
}

