/*  esecuzione  */
var nuovotesto='';
var Xmouse = 0;
var Ymouse = 0;
 var netscape=(navigator.appName=='Netscape');
 if (netscape){
Larghezza= window.innerWidth;
Altezza= window.innerHeight;
}
else{
  Larghezza= document.body.offsetWidth;
  Altezza= document.body.offsetHeight;
}
window.onload = mouseinit;
//document.onkeydown = keyDown;

var Xa=0;
var Ya=0;
var Xb=0;
var Yb=0;

/*
'  --- ATTENZIONE ALLA DINAMITE SOTTOSTANTE!!!
' Si usa così (sviluppo in corso, per disegnare direttamente da schermo. Molto problematico)
'  Ex "onmousedown=""Xa=Xmouse;Ya=Ymouse;"" "
'  Ex "  onmouseup= ""Xb=Xmouse;Yb=Ymouse;      disegnasquare(Xa,Ya,Xb,Yb);"" "
'  ---
*/

function disegnasquare(xa,ya,xb,yb){
var strin='<div style="position: absolute;top:'+ya+'px;left: '+xa+'px;width: '+(xb-xa)+'px;height: '+(yb-ya)+'px;background-color:red;"></div>';
document.body.innerHTML+=strin;
}





 /*  fine esecuzione  */

function outXY(){
var strin="";
strin=Xmouse+", "+Ymouse;
 window.status=strin ;
}

function keyDown() {
  var keycode = event.keyCode;
   var realkey = String.fromCharCode(event.keyCode);
//    alert("keycode: " + keycode + "\nrealkey: " + realkey);
//    alert(Xmouse+'-'+Ymouse);
 }

function openup(str,w,h) {
searchWin = window.open(str,'popup','scrollbars=no,resizable=no,width='+w+',height='+h+',status=no,location=no,toolbar=no');
}


function clipValues(obj,which){
    if (netscape) {
        var clipv = obj.clip.split("rect(")[1].split(")")[0].split(",");
        if (which=="t"){return parseInt(clipv[0]);}
        if (which=="r"){return parseInt(clipv[1]);}
        if (which=="b"){return parseInt(clipv[2]);}
        if (which=="l"){return parseInt(clipv[3]);}
    }
    else {
        clipv = obj.clip.split("rect(")[1].split(")")[0].split("px");
        if (which=="t"){return Number(clipv[0]);}
        if (which=="r"){return Number(clipv[1]);}
        if (which=="b"){return Number(clipv[2]);}
        if (which=="l"){return Number(clipv[3]);}
    }
}


function clipTo(obj,t,r,b,l){
/* vale per IE e NS */          obj.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)";
}


function clipBy(obj,t,r,b,l){
/* vale per IE e NS */  var str= "rect("+(this.clipValues(obj,'t')+t)+"px "+(this.clipValues(obj,'r')+r)+"px ";
    str+= Number(this.clipValues(obj,'b')+b)+"px "+Number(this.clipValues(obj,'l')+l)+"px)";
    obj.clip=str;
}


function alritorno(){
    if (parent.alritornowin) {
        chiudi(parent.alritornowin);
    }
    else{
        settings="width=400,height=300,top=100,left=150,scrollbars=auto,location=no,";
        settings+="directories=no,status=no,menubar=no,toolbar=no,resizable=yes";
        if (parent.varianteestiva){
            var pagina="alritornoest.htm";
            }
        else{
            pagina="alritornoinv.htm";
            }
    parent.alritornowin=window.open(pagina,"Informazioni_per_il_ritorno",settings);
    if(parent.alritornowin.focus){parent.alritornowin.focus();}
        }
 }


function mouseinit() {
    if (netscape) {document.captureEvents(Event.MOUSEMOVE);}
    document.onmousemove = processMouse;
}


function processMouse(e) {
    if (netscape) {
        Xmouse = e.pageX;
        Ymouse = e.pageY;
    }
else {
        Xmouse = event.clientX;
        Ymouse = event.clientY;
    }
}




function grigio(tono){
var str='rgb('+tono+','+tono+','+tono+')';
return str;
}


function ale(){
/* assieme a aletop, aleleft e alewidth, permette di raccogliere informazioni sul 'corpo' a partire dalla 'spalla', per aprire la finestra 'pop' al punto giusto del mouse */  this.aletop=aletop;
 this.aleleft=aleleft;
 this.alewidth=alewidth;
}


function aletop(){
    return document.body.scrollTop;
}


function aleleft(){
    return document.body.scrollLeft;
}


function alewidth(){
    return document.body.scrollWidth;
}


function alecorpo(){
/* variante corpo di ale, tiene conto della presenza di titolo e info */  this.aletop=aletopcorpo;
 this.aleleft=aleleft;
 this.alewidth=alewidth;
}


function aletopcorpo(){
    return document.body.scrollTop-(70+50);
}


function showxy(){alert(Xmouse+' - '+Ymouse);}


function showscroll(){alert(document.body.scrollTop);}


function toggleVisibility(id, mode) {
/*  Da e toglie visibilità, in prossimità del mouse. Uso della funzione: onMouseOver="toggleVisibility('CESANR','show')" onMouseOut="toggleVisibility('CESANR','hidden')" */    var NNtype = (mode == "show") ? mode : "hidden";
    var IEtype = (mode == "show") ? "visible" : "hidden";
    var WC3type = (mode == "show") ? "visible" : "hidden";
    if (document.getElementById){
        eval("document.all." + id + ".style.top = document.body.scrollTop+Ymouse + 15;");
        eval("document.all." + id + ".style.left =parent.puntaale.aleleft()+ Xmouse + 15;");
        eval("document.getElementById(id).style.visibility = \"" + WC3type + "\"");
    }
    else{
        if (document.layers){document.layers[id].visibility = NNtype;}
        else{
            if(document.all){eval("document.all." + id + ".style.visibility = \"" + IEtype + "\"");}
        }
    }
}


function sintesion(testo,id){
    nuovotesto=testo;
    if (parent.lineaon==''){
    shiftedpop();
    }
    else if (parent.lineaon==id){
        shiftedpop(Ymouse-10);
        setTimeout('viapop();',4000);
    }
}


function soloIEsintesion(testo,id){
    nuovotesto=testo;
    if (parent.lineaon==''){
    if (netscape){pop();}else{shiftedpop(window.event.offsetY-10);}
    }
    else if (parent.lineaon==id){
        if (netscape){pop();}else{shiftedpop(window.event.offsetY-10);}
        setTimeout('viapop();',4000);
    }
}


function sintesioff(){
    if (parent.lineaon==''){
        viapop();
    }
}


function primariga(str){
    return str.substr(9,str.indexOf("<br>")-9);
}


function illuminalinea(nome,numero,Cx,Cy,sintesi){
    var elem=document.getElementById('im'+nome);
    chiudifoglio();
    if (parent.lineaon==nome) {
        elem.style.backgroundColor='white';
        document.getElementById('btn').style.color='silver';
        parent.oper.resetlinea(parent.lineaon,parent.numeroon,parent.fermataon);
        parent.lineaon='';
        sintesioff();
        parent.oper.rinfresca(parent.lineaon,parent.numeroon,parent.fermataon);
        }
    else {
        if (parent.lineaon) {
            document.getElementById('im'+parent.lineaon).style.backgroundColor='white';
            parent.oper.resetlinea(parent.lineaon,parent.numeroon,parent.fermataon);
            parent.lineaon='';
        }
        document.getElementById('btn').style.color='blue';
        elem.style.backgroundColor=grigio(200);
        parent.lineaon=nome;
        parent.numeroon=numero;
        parent.oper.rinfresca(parent.lineaon,parent.numeroon,parent.fermataon);
        var scrlx=Cx-parent.oper.Larghezza/2;
        var scrly=Cy-parent.oper.Altezza/2;
        parent.oper.scrolla(scrlx,scrly);
        sintesion('Linea '+nome+': '+sintesi,nome);
    }
    setTimeout('viapop();',4000);
    if (parent.mywin) {parent.mywin.focus();}
}


function illuminafermata(nome,untesto){
  if (parent.fermataon==nome){
        if (parent.mywin) {chiudi(parent.mywin);}
  }
  else{
        resetfermata(parent.lineaon,parent.numeroon,parent.fermataon);
        parent.fermataon=nome;
        rinfresca(parent.lineaon,parent.numeroon,parent.fermataon);
        if (parent.mywin){
            parent.mywin.document.body.innerHTML= untesto;
            parent.mywin.focus();
        }
        else{
            NewWindow('stop.htm','NewWindow','470','25','auto','rightup');
            nuovotesto=untesto;
cambiatesto();
        }
  }
}


function pointedpop(x,y,testo){
    parent.pop.innerHTML=testo;
    parent.pop.style.left = x;
    parent.pop.style.top = y;
 }


function pop(){
    window.status=nuovotesto;
    parent.pop.innerHTML=nuovotesto;
    parent.pop.style.top = parent.puntaale.aletop()+5;
    parent.pop.style.left = parent.puntaale.aleleft()+5;
 }


function popup(testo){
    window.status=testo;
    pop.innerHTML=testo;
    pop.style.top = Ymouse+20;
    pop.style.left =Xmouse-40;
    }
function movepop(){
    pop.style.top = Ymouse+20;
    pop.style.left =Xmouse-40;
    }


function shiftedpop(){
    if (parent.pop){
        window.status=nuovotesto;
        parent.pop.innerHTML=nuovotesto;
        parent.pop.style.top = parent.puntaale.aletop()+Ymouse-10;
        parent.pop.style.left = parent.puntaale.aleleft()+5;
        if (parseInt(parent.pop.style.top)>parent.puntaale.aletop()+Altezza-70){
        parent.pop.style.top =parent.puntaale.aletop()+Ymouse-70 ;
        }
    }
}


function soloIEshiftedpop(){
    window.status=nuovotesto;
    parent.pop.innerHTML=nuovotesto;
    if(netscape){parent.pop.style.top = parent.puntaale.aletop()+5;}
    else    {parent.pop.style.top = parent.puntaale.aletop()+window.event.offsetY-10;}
    parent.pop.style.left = parent.puntaale.aleleft()+5;
 }


function viapop(){
        window.status='';
        parent.pop.style.top=-2000;
}


function initbus(){
    parent.pop=document.getElementById('pop');
    scrolla(600,600);
    setTimeout('viapop();',10);
}


function scrolla(aX,aY){
    var x=aX;
var y=aY;
    if (x<0)    {x=0;}
    if (y<0)    {y=0;}
    window.scrollTo(x,y);
}


function statusbar(testo){
window.status = testo;
}


function upto2(linea){
    var out='LINEA ';
    var i=eval(linea);
    switch (i){
        case 1 : out=out+' ';break;
        case 2 : out=out+' ';break;
        case 3 : out=out+' ';break;
        case 4 : out=out+' ';break;
        case 5 : out=out+' ';break;
        case 6 : out=out+' ';break;
        case 7 : out=out+' ';break;
        case 8 : out=out+' ';break;
        case 9 : out=out+' ';break;
    }
    out=out+linea;
    return out;
     }


function getfoglio(){
    if(document.getElementById('btn').style.color!='silver'){
        var out=parent.lineaon;
        var i=eval(parent.lineaon);
        switch (i){
            case 1 : out='0'+out;break;
            case 2 : out='0'+out;break;
            case 3 : out='0'+out;break;
            case 4 : out='0'+out;break;
            case 5 : out='0'+out;break;
            case 6 : out='0'+out;break;
            case 7 : out='0'+out;break;
            case 8 : out='0'+out;break;
            case 9 : out='0'+out;break;
    }
    if(parent.varianteestiva){
        out='fogli/bus'+out+'e.htm';
    }
    else{
    out='fogli/bus'+out+'.htm';
    }
    openfoglio(out);
    }
}


function popfoglio(linea){
        var i=eval(linea);
        var out=linea;
        switch (i){
            case 1 : out='0'+out;break;
            case 2 : out='0'+out;break;
            case 3 : out='0'+out;break;
            case 4 : out='0'+out;break;
            case 5 : out='0'+out;break;
            case 6 : out='0'+out;break;
            case 7 : out='0'+out;break;
            case 8 : out='0'+out;break;
            case 9 : out='0'+out;break;
    }
    if(parent.varianteestiva){
        out='fogli/bus'+out+'e.htm';
    }
    else{
    out='fogli/bus'+out+'.htm';
    }
    openfoglio(out);
}


function openfoglio(indirizzo){
    var str="left=110, top=50, toolbar=no, location=no, directories=no, status=no, ";
    str+="menubar=yes, scrollbars=yes, resizable=yes, copyhistory=no, width=700, height=600";
    foglio=window.open(indirizzo,"my_new_window",str);
    foglio.focus();
}


function chiudifoglio(){
    if (foglio) {foglio.close();}
    foglio=null;
}


function centramappa(X,Y){
    document.getElementById('btn').style.color='silver';
    if (!parent.varianteestiva) chiudifoglio();
    if (parent.lineaon) {
        document.getElementById('im'+parent.lineaon).src='dotwhite.bmp';
        parent.oper.resetlinea(parent.lineaon,parent.numeroon,parent.fermataon);
        parent.lineaon='';
    }
    parent.oper.scrolla(X,Y);
    setTimeout('viapop();',10);
    if (parent.mywin) parent.mywin.focus();
}


function cambiatesto(){
    if(parent.mywin.document.body) {
    setTimeout('parent.mywin.document.body.innerHTML=nuovotesto;parent.mywin.focus();',30);
    }
    else{
    setTimeout('cambiatesto()',100);
    }
     }


function NewWindow(mypage,myname,w,h,scroll,pos){
    if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
    else if(pos=="leftup"){
        LeftPosition=0;
        TopPosition=0;
    }
    else if(pos=="rightup"){
        LeftPosition=(screen.width)?(screen.width-w-40):100;
        TopPosition=0;
    }
    else if(pos=="rightdown"){
        LeftPosition=(screen.width)?(screen.width-w-40):100;
        TopPosition=(screen.height)?(screen.height-h-80):100;
    }
    else {LeftPosition=100;TopPosition=100}
        settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
    parent.mywin=window.open(mypage,myname,settings);
    if(parent.mywin.focus){parent.mywin.focus();}
}


function chiudi(finestra){
    if (finestra) finestra.close();
}


function rinfresca(linea,numero,fermata){
    var elem=null;
    if (linea){
        for (var i=1;i<=numero;i++){
            elem=document.getElementById(linea+'_'+i).style;
            elem.width=10;
            elem.height=10;
            elem.borderWidth=3;
        }
    }
    if (fermata) {
    elem=document.getElementById(fermata).style;
    elem.width=10;
    elem.height=10;
    elem.borderWidth=3;
    }
}


function resetlinea(linea,numero,fermata){
    if(linea){
        for (var i=1;i<=numero;i++){
            elem=document.getElementById(linea+'_'+i).style;
            elem.width=6;
            elem.height=6;
            elem.borderWidth=1;
        }
    }
}


function resetfermata(linea,numero,fermata){
if (fermata){
  document.getElementById(fermata).style.width=6;
  document.getElementById(fermata).style.borderWidth=1;
  document.getElementById(fermata).style.height=6;
 }
}


function operatore() {
this.resetfermata=resetfermata;
this.resetlinea=resetlinea;
this.rinfresca=rinfresca;
this.scrolla=scrolla;
this.Altezza=Altezza;
this.Larghezza=Larghezza;
}


function adatta(name) {
alert('da allineare');
    var elm=document.getElementById(name);
    var w=elm.width;
    var h=elm.height;
    var w0=w;
    var h0=h;
    elm.height=Altezza;
    elm.width=w * Altezza/h;
    w=elm.width;
    h=elm.height;
    if (w>Larghezza){
        elm.width=Larghezza;
        elm.height=h * Larghezza/w;
        w=elm.width;
        h=elm.height;
    }
    if (h>Altezza){
        elm.height=Altezza;
        elm.width=w * Altezza/h;
        w=elm.width;
        h=elm.height;
    }
    return (w/w0);
}


function poni(name,x,y) {
    var elm=document.getElementById(name);
    elm.style.left=String(x)+'px';
    elm.style.top=String(y)+'px';
}


function next(){
    if (idati[8]==0){
        idati[8]=1;
        document.getElementById('mio').innerHTML=idati[10];
        idati[0]=0;
muta('idati');
    }
    else{
        idati[8]=0;
        document.getElementById('mio').innerHTML=idati[9];
        idati[0]=0;
        muta('idati');
    }
}


function muta(nomedati){
    var dati=eval(nomedati);
    if (dati[0]<dati[1]){
        var n=dati[0];
        dati[0]=n+1;
        ele=document.getElementById('mio');
        npassi=dati[1];
        r1=dati[2];
        g1=dati[3];
        b1=dati[4];
        r2=dati[5];
        g2=dati[6];
        b2=dati[7];
        ratt=r1+dati[0]*(r2-r1)/npassi;
        gatt=g1+dati[0]*(g2-g1)/npassi;
        batt=b1+dati[0]*(b2-b1)/npassi;
        colatt="rgb("+ratt+","+gatt+","+batt+")";
        ele.style.color=colatt;
        setTimeout("muta('"+nomedati+"');",10);
        }
    else {
        if (dati[0]<2*dati[1]){
            var n=dati[0];
            dati[0]=n+1;
            ele=document.getElementById('mio');
            npassi=dati[1];
            r1=dati[2];
            g1=dati[3];
            b1=dati[4];
            r2=dati[5];
            g2=dati[6];
            b2=dati[7];
            ratt=r2-(dati[0]-dati[1])*(r2-r1)/npassi;
            gatt=g2-(dati[0]-dati[1])*(g2-g1)/npassi;
            batt=b2-(dati[0]-dati[1])*(b2-b1)/npassi;
            colatt="rgb("+ratt+","+gatt+","+batt+")";
            ele.style.color=colatt;
            setTimeout("muta('"+nomedati+"');",10);
        }
        else{
            next();
        }
    }
}


function ondeggia(nomedati){
    var dati=eval(nomedati);
    if (dati[0]<dati[1]){
        var n=dati[0];
        dati[0]=n+1;
        ele=document.getElementById('mio');
        npassi=dati[1];
        r1=dati[2];
        g1=dati[3];
        b1=dati[4];
        r2=dati[5];
        g2=dati[6];
        b2=dati[7];
        ratt=r1+dati[0]*(r2-r1)/npassi;
        gatt=g1+dati[0]*(g2-g1)/npassi;
        batt=b1+dati[0]*(b2-b1)/npassi;
        colatt="rgb("+ratt+","+gatt+","+batt+")";
        ele.style.color=colatt;
        setTimeout("ondeggia('"+nomedati+"');",10);
        }
    else {
        if (dati[0]<2*dati[1]){
            var n=dati[0];
            dati[0]=n+1;
            ele=document.getElementById('mio');
            npassi=dati[1];
            r1=dati[2];
            g1=dati[3];
            b1=dati[4];
            r2=dati[5];
            g2=dati[6];
            b2=dati[7];
            ratt=r2-(dati[0]-dati[1])*(r2-r1)/npassi;
            gatt=g2-(dati[0]-dati[1])*(g2-g1)/npassi;
            batt=b2-(dati[0]-dati[1])*(b2-b1)/npassi;
            colatt="rgb("+ratt+","+gatt+","+batt+")";
            ele.style.color=colatt;
            setTimeout("ondeggia('"+nomedati+"');",10);
                }
        else{
            dati[0]=0;
            setTimeout("ondeggia('"+nomedati+"');",10);
            }
    }
}

