//
// (C) Valera Shybko 2003
//

//
//
//
function DownloadImage(AImage, ASrc, X, Y) {
  
  this.showMessage = function() {
    this.element.style.posLeft = this.x;
    this.element.style.posTop = this.y;
    this.element.style.visibility = "visible";
  }

  this.hideMessage = function() {
    this.DownloadImage.element.style.visibility = "hidden";
    clearTimeout(this.DownloadImage.tmout);
  }

  this.load = function(ASrc) {
    if (ASrc) this.src = ASrc;
    this.showMessage();
    this.image.attempt = 0;
    this.image.src = this.src;
          this.element1.innerText = "...";
          this.tmout = setTimeout('document.images.'+this.image.name+'.checkFileSize()' ,this.timeout);
  }

  //
  //
  //
  this.image = AImage;
  this.src   = ASrc;

  this.showDots = true;
  this.tmout = null;
  this.timeout = 7000;

  this.x = X;
  this.y = Y;

  this.image.onload = this.hideMessage;
  this.image.onabort = this.hideMessage;
  this.image.checkFileSize = function() {
    if (this.readyState.indexOf("uninitialized") < 0) {
      // ÷òî-òî áûëî è çàêîí÷èëîñü, 
      if (this.fileSize < 0) {
        // ÎØÈÁÊÀ! íî âîçìîæíî ýòî åùå ïîñëåäñòâèÿ 
        // ïåðâîé íåóäà÷íîé ïîïûòêè.
              // äàäèì åù¸ îäèí øàíñ
              this.attempt++;
              if (this.attempt > 1) {
          alert("can't load image \n"+this.src);
          this.onload();
          return;
        }
      }
      else {
        // çàãðóçèëñÿ
        this.onload();
        return;
      }
    }

    // æä¸ì. ïîêà íè÷åãî íåáûëî. âîçìîæíî ãðóçèòñÿ...
          if (this.DownloadImage.showDots) this.DownloadImage.element1.innerText += ".";
                clearTimeout(this.DownloadImage.tmout);
    this.DownloadImage.tmout = setTimeout('document.images.'+this.name+'.checkFileSize()' ,this.DownloadImage.timeout);
  }


  document.body.insertAdjacentHTML("BeforeEnd",'<div id="'+this.image.name+'_DownloadImage" style="position: absolute; top:0; left:0; visibility: hidden; z-index: 1000; filter: Shadow(color=666666, direction=135, strength=4); padding: 5px;"><div style="background-color: infobackground; color: infotext; border: 1px solid black;"><br>&nbsp;&nbsp;Downloading&nbsp;image<span id="'+this.image.name+'_DownloadImageProcess">...</span>&nbsp;&nbsp;<br>&nbsp;</div></div>');

  this.element = document.all[this.image.name+"_DownloadImage"];
  this.element1 = document.all[this.image.name+"_DownloadImageProcess"];
  this.element.DownloadImage = this;
  this.image.DownloadImage = this;


}
//
//
//
function IMouseEvents(AElement) {


  // ®¡à ¡®âç¨ª¨ á®¡ëâ¨©
        //
  this.mouDown = function() {
    var _this = event.srcElement.IMouseEvents;
    if (_this) {
    event.cancelBubble = _this.cancelBubble
    //
    _this.mousedown(event.srcElement, event.offsetX, event.offsetY);
    //
    return false;
    }
  }
        //
  this.mouUp   = function() {
    var _this = event.srcElement.IMouseEvents;
    if (_this) {
    event.cancelBubble = _this.cancelBubble
    //
    _this.mouseup(event.srcElement, event.offsetX, event.offsetY);
    //
    return false;
    }
  }
        //
  this.mouMove = function() {
    var _this = event.srcElement.IMouseEvents;
    if (_this) {
    event.cancelBubble = _this.cancelBubble;
    //
    _this.mousemove(event.srcElement, event.offsetX, event.offsetY);
    //
    return false;
    }
  }
        //
  this.mouOut = function() {
    var _this = event.srcElement.IMouseEvents;
    if (_this) {
    event.cancelBubble = _this.cancelBubble;
    //
    _this.mouseout(event.srcElement, event.offsetX, event.offsetY);
    //
    return false;
    }
  }

        //
        // ª®­áâàãªâ®à
        //
  this.element = AElement;
  this.cancelBubble = true;

        if (this.element) {
          // ¢­¥¤àï¥¬ á¥¡ï ¢ í«¥¬¥­â
          this.element.IMouseEvents = this;
          // ¯¥à¥å¢ âë¢ ¥¬ á®¡ëâ¨ï
          this.element.onmousedown = this.mouDown;
          this.element.onmouseup   = this.mouUp;
          this.element.onmousemove = this.mouMove;
          this.element.onmouseout  = this.mouOut;
        }

  //
  //  ®¡à ¡®âç¨ª¨ á®¡ëâ¨© ¤«ï ¯à¨«®¦¥­¨ï
  //
  this.mousedown = function(ASrcElement, AOffsetX, AOffsetY) { ; }
        this.mouseup   = function(ASrcElement, AOffsetX, AOffsetY) { ; }
        this.mousemove = function(ASrcElement, AOffsetX, AOffsetY) { ; }
        this.mouseout  = function(ASrcElement, AOffsetX, AOffsetY) { ; }

}

//
//
function Point(ALeft, ATop) {
  
  this.left = ALeft;
  this.top  = ATop;

}

//
//
function Rect(ALeft, ATop, AWidth, AHeight) {
  
  this._super = Point;
  this._super(ALeft, ATop);
  this.width = AWidth;
  this.height = AHeight;

      //
      //  parseInt
      this.getTop     = function() { return Math.ceil(this.height>0 ? this.top : this.top+this.height); }
      this.getLeft    = function() { return Math.ceil(this.width>0 ? this.left : this.left+this.width); }
      this.getHeight  = function() { return Math.abs(this.height); }
      this.getWidth   = function() { return Math.abs(this.width);  }

      //
      this.setCenter = function(Ax,Ay) {
        var x,y;
        if (Ay==null) {
          // Point
          x=Ax.left; y=Ax.left;
        }
        else {
          x=Ax; y=Ay;
        }

        this.left = (x - (this.width/2)); 
        this.top = (y - (this.height/2));
      }

  //
  this.getCenter = function() {
                var point = new Point(0,0);
          point.left = (this.left + (this.width / 2));
          point.top  = (this.top + (this.height / 2));

          return point;
  }

  //
  this.getRect = function(AScale, ARect) {
    if (ARect)
      var rect = ARect;
    else
            var rect = new Rect(0,0,0,0);
          rect.left = (this.getLeft()*AScale);
          rect.top  = (this.getTop()*AScale);
          rect.width = (this.getWidth()*AScale);
          rect.height = (this.getHeight()*AScale);

          return rect;
  }

  //
  this.getCSSClip = function(AMode) {
    //
    //  AMode ãª §ë¢ ¥â ­  "¯à ¢¨«ì­ë©" ¨«¨ "¡ëáâàë©" ¬¥â®¤ ¢®§¢à â 
    //
    if (AMode) {
      var t = this.getTop();      
      var r = this.getLeft()+this.getWidth(); 
      var b = this.getTop()+this.getHeight(); 
      var l = this.getLeft();     
    }
    else {
      var t = this.top;     
      var r = this.left+this.width; 
      var b = this.top+this.height; 
      var l = this.left;      
    }
    t = t > 0 ? t : 0;  
    r = r > 0 ? r : 0;      
    b = b > 0 ? b : 0;      
    l = l > 0 ? l : 0;
    
    return "rect ("+t+" "+r+" "+b+" "+l+")";
      
  }

  this.assign = function(ARect) {
    this.top    = ARect.getTop();
    this.left   = ARect.getLeft();
    this.width  = ARect.getWidth();
    this.height = ARect.getHeight();
  }

}


//
//
//
function Rectangle(AObj, ALeft, ATop, AWidth, AHeight) {

  this._super = Rect;
  this._super(ALeft, ATop, AWidth, AHeight);

  this.element = AObj;
  this.element.Rectangle = this;

  this.elementOffset = new Point(0,0);

  this.border = "1px dotted black";
  this.setBorder = function(ABorder) { this.rectangle.style.border = ABorder; }
  this.setBackgroundColor = function(AColor) { this.rectangle.style.backgroundColor = AColor; }
  this.setColor = function(AColor) { this.rectangle.style.color = AColor; }
  
      // ¸þ÷ôðª¹ div ...
        //
      document.body.insertAdjacentHTML("BeforeEnd",'<div id="'+this.element.name+'Rectangle" style="visibility: hidden; position: absolute; top:0px; left:0px; width:1px; height:1px; filter: Alpha(opacity=100); border: '+this.border+';"></div>');
  this.rectangle = document.all[this.element.name+"Rectangle"];
  this.rectangle.element = AObj;

  // ¨ø¸þòðª¹ 
  this.paint = function() {
    
    this.getElementOffset();
          
          this.rectangle.style.posLeft   = this.getLeft() + this.elementOffset.left;
          this.rectangle.style.posTop    = this.getTop()  + this.elementOffset.top;
          this.rectangle.style.posWidth  = this.getWidth();
          this.rectangle.style.posHeight = this.getHeight();
  }

  // - ðñ¸þû¦ªýð  ÿþ÷ø¡ø  ¤ûõüõýªð
  this.getElementOffset = function() {
    var el = this.element;  this.elementOffset.left=0; this.elementOffset.top=0; 
    while (el.offsetParent != null) { 
      this.elementOffset.left += el.offsetLeft; 
      this.elementOffset.top  += el.offsetTop; 
      el = el.offsetParent; 
    }
    return this.elementOffset; // Point
  }

}

//
//
//
function OutlineRectangle(AObj) {
  
  this._super = IMouseEvents;
  this._super(AObj);

  this.rc = new Rectangle(AObj,0,0,0,0);

  AObj.style.cursor = "crosshair";

  this.mode = 0;

  //
  //  þñ¨ðñþª¢øúø ¸þñvªøù ôû  ÿ¨øûþöõýø 
  //
  this.mousedown = function(AElement, AOffsetX, AOffsetY) { 

    if (AElement.IMouseEvents.mode == 0) {
      AElement.IMouseEvents.mode = 1;

      AElement.Rectangle.left = AOffsetX-1;
      AElement.Rectangle.top  = AOffsetY-1;
      AElement.Rectangle.width   = 1;
      AElement.Rectangle.height  = 1;
      AElement.Rectangle.rectangle.style.visibility = "visible";    
      AElement.Rectangle.paint();
    }
    else {
      AElement.IMouseEvents.mouseup(AElement, AOffsetX, AOffsetY);
    }


  }

        this.mouseup   = function(AElement, AOffsetX, AOffsetY) {  
    
    AElement.IMouseEvents.mode = 0;
    AElement.Rectangle.rectangle.style.visibility = "hidden";   
    AElement.IMouseEvents.onselect(AElement);
        }

        this.mousemove = function(AElement, AOffsetX, AOffsetY) {
          
    if (AElement.IMouseEvents.mode == 1) {
      AElement.Rectangle.width   = AOffsetX - AElement.Rectangle.left-1;
      AElement.Rectangle.height  = AOffsetY - AElement.Rectangle.top-1;
      AElement.Rectangle.paint();
    }
        }

        //this.mouseout = this.mouseup;

        this.onselect = function() { ; }

} 
//
//
//
//function 

//
//
//
function MoveMagnitude(AObj) {
  
  this._super = IMouseEvents;
  this._super(AObj);

  AObj.style.cursor = "move";

  this.mode = 0;

  this.x0   = 0;
  this.y0   = 0;

  //
  //  îáðàáîò÷èêè ñîáûòèé äëÿ ïðèëîæåíèÿ
  //
  this.mousedown = function(AElement, AOffsetX, AOffsetY) { 

    if (AElement.IMouseEvents.mode == 0) {
      AElement.IMouseEvents.mode = 1;
      AElement.IMouseEvents.x0 = AOffsetX;
      AElement.IMouseEvents.y0 = AOffsetY;
    }
    else {
      AElement.IMouseEvents.mouseup(AElement, AOffsetX, AOffsetY);
    }


  }

        this.mouseup   = function(AElement, AOffsetX, AOffsetY) {  
    AElement.IMouseEvents.mode = 0;
        }

        this.mousemove = function(AElement, AOffsetX, AOffsetY) {
          
    if (AElement.IMouseEvents.mode == 1) {
      // íîâûå êîîðäèíàòû îáúåêòà
      var dx = AOffsetX - AElement.IMouseEvents.x0;
      var dy = AOffsetY - AElement.IMouseEvents.y0;

      // ?!?!?!
      //if (Math.abs(dx)>10) dx /= 2;
      //if (Math.abs(dy)>10) dy /= 2;

      AElement.IMouseEvents.x0 = AOffsetX;
      AElement.IMouseEvents.y0 = AOffsetY;

      var cn = AElement.Magnitude.sImage.Rectangle.getCenter();
      var mscale = AElement.Magnitude.scale;
      AElement.Magnitude.sImage.Rectangle.setCenter(cn.left+(dx/mscale), cn.top+(dy/mscale));
      AElement.Magnitude.magnitude(AElement.Magnitude.sImage.Rectangle);

    }
        }

        //this.mouseout = this.mouseup;

} 

//
//
//
function Magnitude(AImage, LImageSrc, AScale) {

  AImage.Magnitude = this;

  this.bigImageName = AImage.name+"_B_i_G";
  document.body.insertAdjacentHTML("BeforeEnd",'<img name="'+this.bigImageName+'" src="" border=0 style="position: absolute; top:0; left:0; visibility: hidden; border: 2px white outset;">');
  this.bigImage = document.images[this.bigImageName];

  this.downloadImage = new DownloadImage(this.bigImage,LImageSrc);

  //
  //
  this.rectangle = new OutlineRectangle(AImage);

  this.rectangle.onselect = function(AElement) {
    var _this = AElement.Magnitude;

    _this.downloadImage.x = AElement.Rectangle.rectangle.style.posLeft;
    _this.downloadImage.y = AElement.Rectangle.rectangle.style.posTop;
    _this.downloadImage.load();

    _this.magnitude(_this.rectangle.element.Rectangle);
    _this.lImage.style.visibility = "visible";
    _this.lImage.Rectangle.rectangle.style.visibility = "visible";
  }
  

  this.scale = AScale;

  this.sImage = AImage;
  this.sRect = new Rect(0,0,0,0);
  
  this.lImage = document.images[AImage.name+"_B_i_G"];


  this.lRect = new Rectangle(this.lImage,0,0,0,0);
  this.lRect.setBorder("2px outset mediumblue");
  this.lRect.setBackgroundColor("gray");
  this.lRect.setColor("black");
  this.lRect.rectangle.filters.Alpha.opacity=40;

  this.lImage.style.zIndex = this.lRect.rectangle.style.zIndex +1;
  this.lImage.Magnitude = this;

  this.scroll = false;
  this.scrollTo00 = function () {
    if (this.scroll) window.scrollTo(0,0);
  }

  //
  //
  //
  this.lRect.rectangle.onclick = function() {
    event.srcElement.element.Magnitude.hideMagnitude();
  }
  //
  this.lImage.onclick = function() {
    event.srcElement.Magnitude.hideMagnitude();
  }
  //
  //
  this.hideMagnitude = function() {

    this.lImage.style.visibility = "hidden";
    this.lImage.Rectangle.rectangle.style.visibility = "hidden";
    this.lImage.IMouseEvents.mode = 0;
    this.scrollTo00();
  }
  
  //
  //
  //
  this.magnitude = function(ARect) {
    //
    this.sRect.assign(ARect);
    this.sRect.getRect(this.scale, this.lRect);
    //
    var rectCenter = this.sImage.Rectangle.getCenter();
    //
    var ax = (rectCenter.left - this.lRect.left - (this.lRect.width/2));
    var ay = (rectCenter.top  - this.lRect.top  - (this.lRect.height/2));
    //

    var eo = this.sImage.Rectangle.getElementOffset();

    this.lImage.style.posTop  = ay + eo.top;
    this.lImage.style.posLeft = ax + eo.left;

    var clp = this.lRect.getCSSClip();
    this.lImage.style.clip = clp;

    this.lRect.top -= 3;  this.lRect.height += 6; 
    this.lRect.left -= 3; this.lRect.width += 6;  
                          
    this.lRect.paint();

  }

  this.mm = new MoveMagnitude(this.lImage);

}
