function myZoom(smallImageContId, smallImageId, bigImageContId, bigImageId){

    this.recalculating = false;

    this.smallImageCont = _el(smallImageContId);
    this.smallImage = _el(smallImageId);
    this.bigImageCont = _el(bigImageContId);
    this.bigImage = _el(bigImageId);
    this.pup = 0;

    this.bigImageSizeX = 0;
    this.bigImageSizeY = 0;
    this.smallImageSizeX = 0;
    this.smallImageSizeY = 0;
    this.popupSizeX = 20;
    this.popupSizey = 20;
    this.positionX = 0;
    this.positionY = 0;

    this.baseuri = '';
};

myZoom.prototype.checkcoords = function (e) {
    if(e.clientX > parseInt(this.smallImageCont.offsetLeft + this.smallImageSizeX)){
        this.hiderect();
    }
    if(e.clientX < parseInt(this.smallImageCont.offsetLeft) ){
        this.hiderect();
    }
    if(e.clientY > parseInt(this.smallImageCont.offsetTop + this.smallImageSizeY)){
        this.hiderect();
    }
    if(e.clientY < parseInt(this.smallImageCont.offsetTop) ){
        this.hiderect();
    }
};

myZoom.prototype.mousemove = function (e) {
    if(ua == 'gecko'){
        e.cancelBubble=true;
        e.preventDefault();
        e.stopPropagation();
    } else  if (ua == 'msie' || ua == 'opera') {
        window.event.cancelBubble = true;
    }

    if(this.recalculating){
        return;
    }
    this.recalculating = true;

    var smallImg = this.smallImage;
    var smallX = 0;
    var smallY = 0;

    if(ua == 'gecko' || ua == 'opera'){
        var tag = smallImg;
        while (tag.tagName!="BODY") {
                smallY+=tag.offsetTop;
                smallX+=tag.offsetLeft;
                tag=tag.offsetParent;
        }
    }

    if(ua == 'msie'){
        this.positionX = event.x;
        this.positionY = event.y + document.body.scrollTop;
    } else {
        this.positionX = e.clientX - smallX;
        this.positionY = e.clientY - smallY;
    }

//     _el('helptd').innerHTML = this.positionX+":"+this.positionY;

    /* check borders */
    if((this.positionX + this.popupSizeX/2) >= this.smallImageSizeX){
        this.positionX = this.smallImageSizeX - this.popupSizeX/2;
    }

    if((this.positionY + this.popupSizeY/2) >= this.smallImageSizeY){
        this.positionY = this.smallImageSizeY - this.popupSizeY/2;
    }

    if((this.positionX - this.popupSizeX/2) <= 0){
        this.positionX = this.popupSizeX/2;
    }

    if((this.positionY - this.popupSizeY/2) <= 0){
        this.positionY = this.popupSizeY/2;
    }

    /*
    if(ua != 'msie'){
        setTimeout(createMethodReference(this, "showrect"), 10);
    } else {
        this.showrect();
    }
    */
    setTimeout(createMethodReference(this, "showrect"), 10);
};

myZoom.prototype.showrect = function () {

    this.pup.style.left =  (this.positionX - this.popupSizeX/2) + 'px';
    this.pup.style.top  =  (this.positionY - this.popupSizeY/2) + 'px';
    this.pup.style.visibility = "visible";

    perX = parseInt(this.pup.style.left)*(this.bigImageSizeX/this.smallImageSizeX);
    perY = parseInt(this.pup.style.top)*(this.bigImageSizeY/this.smallImageSizeY);

//alert(perY);

    if (perY>1960)
    {
        perY=1960;
    }
    

    this.bigImage.style.left =  (-perX) + 'px';
    this.bigImage.style.top  =  (-perY) + 'px';
    this.bigImageCont.style.display = 'block';
    this.recalculating = false;
};

myZoom.prototype.hiderect = function () {
    this.pup.style.visibility = "hidden";
    this.bigImageCont.style.display = 'none';
    this.bigImageCont.style.visibility = 'visible';
};

myZoom.prototype.initPopup = function () {
    this.pup = document.createElement("DIV");

    if(ua == 'msie') {
        this.pup.className = 'myZoomPup ie';
    } else {
        this.pup.className = 'myZoomPup';
        this.pup.style.background = 'url(' + this.baseuri + '/back.png' + ')';
    }

    this.popupSizeX = (parseInt(this.bigImageCont.style.width) - 3)/(this.bigImageSizeX/this.smallImageSizeX);
    //this.popupSizeY = (parseInt(this.bigImageCont.style.height) - 19 - 3)/(this.bigImageSizeY/this.smallImageSizeY);
    this.popupSizeY = 54.884;
    
    this.pup.style.width = this.popupSizeX + 'px';
    this.pup.style.height = this.popupSizeY + 'px';

    this.smallImageCont.appendChild(this.pup);
};

myZoom.prototype.initBigContainer = function () {
    var bigimgsrc = this.bigImage.src;

    while (this.bigImageCont.firstChild) {
        this.bigImageCont.removeChild(this.bigImageCont.firstChild);
    }
    
    if(ua == 'msie'){
             var f = document.createElement("IFRAME");
             f.style.left = '0px';
             f.style.top = '0px';
             f.style.position = 'absolute';
             f.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
             f.style.width = this.bigImageCont.style.width;
             f.style.height = this.bigImageCont.style.height;
             f.frameBorder = 0;
             this.bigImageCont.appendChild(f);
         }

    var f = document.createElement("DIV");
    f.className = 'myZoomHeader';
    f.innerHTML = "INGRANDIMENTO IMMAGINE";
    this.bigImageCont.appendChild(f);

    var ar1 = document.createElement("DIV");
    ar1.style.overflow = "hidden";
    this.bigImageCont.appendChild(ar1);

    this.bigImage = document.createElement("IMG");
//     f.id = this.bigImageId;
    this.bigImage.src = bigimgsrc;
    this.bigImage.style.position = 'relative';
    ar1.appendChild(this.bigImage);
};

myZoom.prototype.initZoom = function () {
    var elements = document.getElementsByTagName('script');
    for (var i=0; i<elements.length; i++) {
        if (elements[i].src && (elements[i].src.indexOf("myZoom.js") != -1 || elements[i].src.indexOf("packed.js") != -1)) {
            var src = elements[i].src;

            srcMode = (src.indexOf('_src') != -1) ? '_src' : '';
            src = src.substring(0, src.lastIndexOf('/'));

            this.baseuri = src;
            break;
        }
    }

    this.bigImageSizeX = this.bigImage.width;
    this.bigImageSizeY = this.bigImage.height;
    this.smallImageSizeX = this.smallImage.width;
    this.smallImageSizeY = this.smallImage.height;
    this.initBigContainer();
    this.initPopup();
    myAddEventListener(window.document, "mousemove", createMethodReference(this, "checkcoords"));
    myAddEventListener(this.smallImageCont, "mousemove", createMethodReference(this, "mousemove"));
};

