//Se selezionata carica la canzone, il titolo, cambia il colore e avvia la funzione autofw
window.onload = function autostart() {
      var sel = document.forms['menu'].selettore;
      var idx = sel.selectedIndex;
      document.getElementById('mediaplayer').filename = sel.options[idx].value;
      document.mepl.src = sel.options[idx].value;
      document.getElementById('scroller').innerHTML = sel.options[idx].text;
      document.stat.timertot.value = 'load';
      document.stat.timerora.value = 'load';
      document.getElementById('trackbar').style.width = '0%';
      if(sel.options[idx].value==''){
      sel.options[idx].style.color='#ffffff';    
      }else {
      sel.options[idx].style.color='#800000';    
      }
      var playerStatus = document.mediaplayer.OpenState;
      if (playerStatus==5){
      var TimerID = setInterval('autofw();',1000); //se mediaplayer pronto imposta il tempo di esecuzione di autofw
      var StatID = setTimeout('calc();',4000); //avvia statistiche dopo 4 secondi
      numb();//avvia calcolo numero canzone
      }
      }
      
//Caricamento manuale da playlist canzone, titolo, colore e avvia funzione autofw                   
function cambia() {
      var sel = document.forms['menu'].selettore;
      var idx = sel.selectedIndex;
      document.getElementById('mediaplayer').filename = sel.options[idx].value;
      document.mepl.src = sel.options[idx].value;
      document.getElementById('scroller').innerHTML = sel.options[idx].text; 
      document.stat.timertot.value = 'load';
      document.stat.timerora.value = 'load';
      document.getElementById('trackbar').style.width = '0%';
      sel.options[idx].style.color='#800000';       
      var playerStatus = document.mediaplayer.OpenState;
      if (playerStatus==5){
      clearInterval(TimerID);
      clearTimeout(StatID);
      var TimerID = setInterval('autofw();',1000); //se mediaplayer pronto imposta il tempo di esecuzione di autofw
      var StatID = setTimeout('calc();',4000); //avvia statistiche dopo 4 secondi
      numb();//avvia calcolo numero canzone
      }
      }
 
//Passa automaticamente alla successiva 
 function autofw(){
    var isnow = Math.floor(document.mediaplayer.currentPosition);
    var isfull = Math.floor(document.mediaplayer.duration); 
    if ((isnow==isfull) && (isfull!=0)){   
    next();
    }
    }
    
    
//Calcolo tempo totale
function calc(){
    clearTimeout(TimerTKB);//elimino eventuali precedenti statistiche tkb
    var isfull = Math.floor(document.mediaplayer.duration); 
    var tmin = parseInt(isfull/60);
    var tsec = Math.round(((((isfull/60)-tmin)*100)/10)*6);
    if (tsec>60){(tmin=(tmin+1));(tsec=tsec-60)}else{(tmin=tmin);(tsec=tsec)}
    if (tmin<10){tmin='0'+tmin}else{tmin=tmin}
    if (tsec<10){tsec='0'+tsec}else{tsec=tsec}
    document.stat.timertot.value = tmin+':'+tsec;
    var TimerTKB = setInterval('tkb();',1000);  //avvia le statistiche ogni secondo
    }
    
    
//Calcolo trackbar e tempo attuale
function tkb(){ 
    var isnow = Math.floor(document.mediaplayer.currentPosition);
    var isfull = Math.floor(document.mediaplayer.duration);      
    var timeperc = (Math.round(((isnow*100)/isfull)*100))/100;
    document.getElementById('trackbar').style.width = timeperc+'%';
    
    var tm = parseInt(isnow/60);
    var ts = Math.round(((((isnow/60)-tm)*100)/10)*6);
    if (ts>60){(tm=(tm+1));(ts=ts-60)}else{(tm=tm);(ts=ts)}
    if (tm<10){tm='0'+tm}else{tm=tm}
    if (ts<10){ts='0'+ts}else{ts=ts}
    document.stat.timerora.value = tm+':'+ts;
    } 
        
        
 //Calcolo numero canzoni
 function numb(){
    var sel = document.forms['menu'].selettore;
    var n =  sel.selectedIndex+1; 
    var ntot = sel.options.length;
    
    if (sel.options[0].value==''){
    document.stat.nsong.value = (n-1)+' / ' +(ntot-1);
    }else{   
    document.stat.nsong.value = n+' / '+ntot;
    }  
    }     
    
    
//Play
function play(){
      var sel = document.forms['menu'].selettore;
      var idx = sel.selectedIndex;  
      var brano = sel.options[idx].text;   
      var scrl = document.getElementById('scroller');
      document.mediaplayer.Play();
      document.mepl.play();
      scrl.setAttribute("scrollAmount", 1);
      document.getElementById('scroller').innerHTML = brano;  
      }     
  
  
//Pausa
function pause(){
      var sel = document.forms['menu'].selettore;
      var idx = sel.selectedIndex;  
      var brano = sel.options[idx].text;
      var scrl = document.getElementById('scroller');
      document.mediaplayer.Pause();
      document.mepl.Pause();
      scrl.setAttribute("scrollAmount", 0);
      document.getElementById('scroller').innerHTML = '...::IN PAUSA::...'; 
      }
  
  
//Stop
function stop(){
      var sel = document.forms['menu'].selettore;
      var idx = sel.selectedIndex;  
      var brano = sel.options[idx].text;
      var scrl = document.getElementById('scroller');
      document.mediaplayer.Stop();
      document.mepl.Stop();                                  
      document.mediaplayer.currentPosition = '0';
      scrl.setAttribute("scrollAmount", 0);
      document.getElementById('scroller').innerHTML = '...::INTERROTTO::...'; 
}
    
    
//Canzone successiva
function next(){
    var sel = document.forms['menu'].selettore;
    var numop = sel.options.length;
    var idx = sel.selectedIndex; 
    // seleziona la prossima se l' option selezionata al momento non e' l' ultima
    // altrimenti torna alla prima opzione 
    if(idx==numop-1){sel.selectedIndex=0;}
    else{sel.selectedIndex=++idx;}
    document.getElementById('mediaplayer').filename = sel.options[sel.selectedIndex].value;
    document.mepl.src = sel.options[sel.selectedIndex].value;
    document.getElementById('scroller').innerHTML = sel.options[sel.selectedIndex].text;
    document.stat.timertot.value = 'load';
    document.stat.timerora.value = 'load';
    document.getElementById('trackbar').style.width = '0%';
    sel.options[idx].style.color='#800000';                
           
    //Controlla lo status del mediaplayer     
    var playerStatus = document.mediaplayer.OpenState;
    if (playerStatus==5){
    clearInterval(TimerID);
    clearTimeout(StatID);
    var TimerID = setInterval('autofw();',1000); //se mediaplayer pronto imposta il tempo di esecuzione di autofw
    var StatID = setTimeout('calc();',4000); //avvia statistiche dopo 4 secondi
    numb();//avvia calcolo numero canzone
    }
    }

//Canzone precedente
function back(){
    var sel = document.forms['menu'].selettore;
    var numop = sel.options.length;
    var idx = sel.selectedIndex; 
    // seleziona la precedente se l' option selezionata al momento non e la prima
    // altrimenti ferma
    if(idx==0){sel.selectedIndex=numop-1;}
    else{sel.selectedIndex=--idx;}
    document.getElementById('mediaplayer').filename = sel.options[sel.selectedIndex].value;
    document.mepl.src = sel.options[sel.selectedIndex].value;
    document.getElementById('scroller').innerHTML = sel.options[sel.selectedIndex].text;
    document.stat.timertot.value = 'load';
    document.stat.timerora.value = 'load';
    document.getElementById('trackbar').style.width = '0%';
    sel.options[idx].style.color='#800000';       
        
    //Controlla lo status del mediaplayer     
    var playerStatus = document.mediaplayer.OpenState;
    if (playerStatus==5){
    clearInterval(TimerID);
    clearTimeout(StatID);
    var TimerID = setInterval('autofw();',1000); //se mediaplayer pronto imposta il tempo di esecuzione di autofw
    var StatID = setTimeout('calc();',4000); //avvia statistiche dopo 4 secondi
    numb();//avvia calcolo numero canzone
    }
    }



//Alza il volume
function volsu(){
     var vol = document.getElementById('mediaplayer').volume;
     if(vol<0){
     document.getElementById('mediaplayer').volume = vol +200; 
     }
     }


//Abbassa il volume
function volgiu(){
     var vol = document.getElementById('mediaplayer').volume;
     if (vol>-2400){
     document.getElementById('mediaplayer').volume = vol -200;
  
     }
     }
     
     
//Visualizza volume +
function visvols(){
      var vol = document.getElementById('mediaplayer').volume;
      document.getElementById('vols').title = (vol/100)+12;
}     


//Visualizza volume -
function visvolg(){
      var vol = document.getElementById('mediaplayer').volume;
      document.getElementById('volg').title = (vol/100)+12;
}  

     
     
//Mute on/off
function mute(){
     var soundstate = document.getElementById('mute').title;
     if (soundstate == 'Sound Off'){  
       document.getElementById('mute').style.backgroundImage = 'url("../immagini/tasti/muteon.gif")';
       document.getElementById('mediaplayer').volume = -10000; 
       document.getElementById('mute').title = 'Sound On';
       } 
    else{
       document.getElementById('mute').style.backgroundImage = 'url("../immagini/tasti/mute.gif")';
       document.getElementById('mediaplayer').volume = 0; 
       document.getElementById('mute').title = 'Sound Off';
    }
    }
    

// Apri/Chiudi la playlist
function apri(){
      if(document.getElementById('menu').style.display == 'none'){
      document.getElementById('menu').style.display = 'inline';
      document.getElementById('aprimi').innerHTML = '--- CHIUDI PLAYLIST ---';
    }else{
      document.getElementById('menu').style.display = 'none';
      document.getElementById('aprimi').innerHTML = '--- APRI PLAYLIST ---';
    }
 
  }

//Blocco segnalazione errori
window.onerror = function stoperror(msg, url, linenumber){ 
      return true; 
      } 
