//classe per implementare meccanismo di zoom; by M.Corbatto Nov.06

var z=new Zoom(2.41);

function Zoom(fattore) 
{  this.clickX=0; //coordinate mouse dove è stato fatto il click 
   this.clickY=0;
   this.divObj=null; //oggetto relativo al div contenitore
   this.imgObj=null; //oggetto relativo all'img contenuta
   this.posX=0; //coordinate dell'angolo in alto/sx del div relative alla vista
   this.posY; //corrente e non alle coordinate assolute nella pagina
   this.offX; //offset del click del mouse rispetto all'img normalizzato
   this.offY; 
   this.divWidth; //largezza in pixel del div contenitore
   this.divHeight; //altezza in pixel del div contenitore
   this.fattore=fattore; //fattore di zoom da utilizzare
//metodi
   this.findPos=findPos;
   this.invertiZoom=invertiZoom;
   this.pan=pan
}

  //restituisce la posizione corrente dell'oggetto div contenitore rispetto
  //alla vista corrente
   function findPos() {
        var curleft = curtop = 0;
	var obj=this.divObj;
        if (obj.offsetParent)
	   while(1)
	   { curleft+=obj.offsetLeft;
             curtop+=obj.offsetTop;
             if (!obj.offsetParent) 
		break;
	     obj=obj.offsetParent;
           }
	else
	  if(obj.x)
	  {  curleft+=obj.x;
	     curtop+=obj.y;
	  }
        //tolgo l'attuale valore dello scroll della finestra in modo da restituire le coordinate relative alla vista corrente e non quelle assolute della pagina
	 var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
	    //Netscape compliant
	    scrOfY = window.pageYOffset;
	    scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    //DOM compliant
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    //IE6 standards compliant mode
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
	  }
/*	var theTop,theLeft;
	if (document.documentElement && document.documentElement.scrollTop)
       {  theTop = document.documentElement.scrollTop;
          theLeft = document.documentElement.scrollLeft;
       }
        else if (document.body)
       { theTop = document.body.scrollTop
         theLeft = document.body.scrollLeft
       }
       //alert(theLeft+curleft+","+theTop+curtop);
*/
//    display(curleft+","+scrOfX+","+curtop+","+scrOfY);
if (navigator.userAgent.toLowerCase().indexOf('safari') != - 1)
       return [curleft,curtop];
 return [curleft-scrOfX,curtop-scrOfY];
}

  function invertiZoom(e,obj)
  { if(this.divObj!=null)  //annullo lo zoom se e' attivo per qualche immagine
     {  this.imgObj.style.width=this.divObj.style.width;
        this.imgObj.style.left="0px";
        this.imgObj.style.top="0px";
        document.onmousemove=null;
     }
     if(obj!=this.divObj) //attivo lo zoom se era disattivo o era attivo per un'altra immagine
     {  this.imgObj=obj.firstChild;
        this.divObj=obj;
        var coords=this.findPos();
        this.posX=coords[0];
        this.posY=coords[1];
	this.divWidth=obj.offsetWidth;
	this.divHeight=obj.offsetHeight;
        this.clickX=e.clientX;
        this.clickY=e.clientY;
	this.offX=(this.fattore-1)*parseInt(this.clickX-this.posX);
	this.offY=(this.fattore-1)*parseInt(this.clickY-this.posY);

	this.imgObj.style.width=parseInt(this.divWidth)*this.fattore+"px";
        this.imgObj.style.left=-this.offX +"px";
        this.imgObj.style.top=-this.offY+"px";
        document.onmousemove=this.pan;
//       link.firstChild.nodeValue="zoom-";
    }
    else //nel caso non attivi zoom azzero i riferimenti all'ultima img
    {   this.imgObj=null;
        this.divObj=null;
    }
     return false;
   }

   function pan(e)
   { //uso z al posto di this xchè js purtroppo non riconosce this se il metodo
     //è chiamato come gestione di un evento 
     // compatibilità crossbrowser (e è null in IE)
     if (!e) var e = window.event;
     var x=e.clientX;
     var y=e.clientY;
//     display(x+","+z.posX +","+parseInt(z.posX+z.divWidth)+","+z.clickX);
//     display(y+","+z.posY +","+parseInt(z.posY+z.divHeight)+","+z.clickY);
     if (x>=z.posX && x<=parseInt(z.posX+z.divWidth) &&
         y>=z.posY && y<=parseInt(z.posY+z.divHeight))
     {  z.imgObj.style.left=-z.offX-parseInt(x-z.clickX)*z.fattore+"px";
        z.imgObj.style.top=-z.offY-parseInt(y-z.clickY)*z.fattore+"px";
     }
     return false;
   }
                                                                                
   function display(msg)
   { var debug=document.getElementById("debug");
     debug.innerHTML=msg;
   }


