/**
 * Copyright 2002 Earth Resource Mapping Pty Ltd. This document contains unpublished source code of
 * Earth Resource Mapping Pty Ltd. This notice does not indicate any intention to publish the source
 * code contained herein.
 *
 * /ecwplugins/lib/Scripts/NCSOverlayRect.js
 *
 */


/**
 * NCSOverlayRect Class definition : a class that uses 4 layers to draw an overlay rect. The class takes 2 ecw views as
 * its Init() function, one is the overview controller and one the slave. When the mouse pointer is dragged over the controller
 * the slave's extents are updated to reflect the overview zoom box. When the slave roams or zooms, the rect in the controller
 * is updated to reflect its extents.
 */
function NCSOverlayRect () {
	this.dOverlayWorldTLX = 0.0;
	this.dOverlayWorldTLY = 0.0;
	this.dOverlayWorldBRX = 0.0;
	this.dOverlayWorldBRY = 0.0;
	
	this.ECWViewController = null;
	this.ECWViewSlave = null;

	this.vLineSrc = "/ecwplugins/lib/bitmaps/vLine.gif";
	this.hLineSrc = "/ecwplugins/lib/bitmaps/hLine.gif";
}

/**
 * If you add or delete layers to the controller view after you have created the overview class, 
 * call this function to move the rectangle layers back to the top.
 */
function NCSOverlayRect_ResetLayers() {
	var top   = this.ECWViewController.GetNumberLayers();
	var index = this.ECWViewController.GetLayerIndex("NCSOverlayRect");
	this.ECWViewController.MoveLayer(index, top-1);
}

/**
 * Set up the controller and the slave views. The controller is the view with the red overlay rect. The slave is the
 * view that gets updated with new extents when the red box is dragged around the controller.
 */
function NCSOverlayRect_Init(controllView, slaveView) {

	if (controllView == null || slaveView == null) {
		alert("NCSOverlayRect : controller or slave view cannot be null.");
	}
	else {
		this.ECWViewController = controllView;
		this.ECWViewSlave = slaveView;
	}
}

/**
 * Move the overview square on the controller to center about the location dWorldX, dWorldY, maintaining the current aspect ratio and zoom.
 */
function NCSOverlayRect_Move(dWorldX, dWorldY, bUpdateSlave) {

	var dWorldTLX, dWorldTLY, dWorldBRX, dWorldBRY;
	var halfWidthX, halfWidthY;
	
	halfWidthX = (this.dOverlayWorldBRX - this.dOverlayWorldTLX)/2.0;
	if(this.dOverlayWorldBRY > this.dOverlayWorldTLY) {
		halfWidthY = (this.dOverlayWorldBRY - this.dOverlayWorldTLY)/2.0;
	} else {
		halfWidthY = (this.dOverlayWorldTLY - this.dOverlayWorldBRY)/2.0;
	}
		  
	dWorldTLX = dWorldX - halfWidthX;
	dWorldBRX = dWorldX + halfWidthX;
	
	if(this.dOverlayWorldBRY > this.dOverlayWorldTLY) {
		dWorldTLY = dWorldY - halfWidthY;
		dWorldBRY = dWorldY + halfWidthY;
	} else {
		dWorldTLY = dWorldY + halfWidthY;
		dWorldBRY = dWorldY - halfWidthY;
	}
	
	this.SetRect(dWorldTLX, dWorldTLY, dWorldBRX, dWorldBRY, bUpdateSlave);
}

/**
 * Set the extents of the rectangle on the overlay controller. If bUpdateSlave is true, the slave view will receive an extent change event.
 */
function NCSOverlayRect_SetRect(dWorldTLX, dWorldTLY, dWorldBRX, dWorldBRY, bUpdateSlave) {
	var sParams = "";
	
	this.dOverlayWorldTLX = dWorldTLX;
	this.dOverlayWorldTLY = dWorldTLY;
	this.dOverlayWorldBRX = dWorldBRX;
	this.dOverlayWorldBRY = dWorldBRY;

	var vecObject = "";
  vecObject += "linewidth=1;color=#FF0000;fillcolor=#FFFFFF;filledpolygon1="
  vecObject += dWorldTLX + "," + dWorldTLY + "|";
  vecObject += dWorldBRX + "," + dWorldTLY + "|";
  vecObject += dWorldBRX + "," + dWorldBRY + "|";
  vecObject += dWorldTLX + "," + dWorldBRY + ";";
  this.ECWViewController.SetLayerParameter("NCSOverlayRect", vecObject);
  this.ECWViewController.SetLayerTransparency("NCSOverlayRect", "#FFFFFF", 0.5);
  /*
  var aLyr=getLayer("_overview_");
  if (aLyr) {
    showLayer("_overview_");
    var c1=this.Word2Pixel(dWorldTLX,dWorldTLY);
    var c2=this.Word2Pixel(dWorldBRX,dWorldBRY);
    var w=c2.x-c1.x;
    var h=c2.y-c1.y;
    var ovPos=getPosition("overview");
    var ovSize=getObjectSize("overview");
    var ovZPos=getPosition("_overview_");
    var ovZSize=getObjectSize("_overview_");
    resizeLayer("_overview_",w,h);           
    moveLayer("_overview_",c1.x,c1.y);
    if (Math.abs(c1.x+w)>Math.abs(ovPos.x+ovSize.width)) w-=Math.abs(Math.abs(c1.x+w)-Math.abs(ovPos.x+ovSize.width)+15);
    if (Math.abs(c1.y+h)>Math.abs(ovPos.y+ovSize.height)) h-=Math.abs(Math.abs(c1.y+h)-Math.abs(ovPos.y+ovSize.height)+22);    
    if ((c1.x<ovPos.x || c1.y<ovPos.y || c1.x<(ovPos.x+ovSize.width) || c1.y<(ovPos.y+ovSize.height)) && w>0 && h>0) { 
      if (isNav6) {
        if (c1.x<0 || c1.y<0) {
          if (c1.x<0) {
            w=w-Math.abs(c1.x);
            c1.x=0;
          }
          if (c1.y<0){
            h=h-Math.abs(c1.y);  
            c1.y=0;
          }
          //alert(w+" "+h);                             
          clipLayer("_overview_",c1.x,c1.y,c1.x+w,c1.y+h);
        }
        else {
          clipLayer("_overview_",c1.x,c1.y,c1.x+w,c1.y+h);
        }     
      }
      else {
        if (c1.x<0 || c1.y<0) {
          if (c1.x<0) {  
            c1.x=Math.abs(c1.x);
          }
          else c1.x=0;
          if (c1.y<0){  
            c1.y=Math.abs(c1.y);//(c1.y-ovPos.y);
          }
          else c1.y=0;            
          clipLayer("_overview_",c1.x,c1.y, w,h);
        }
        else clipLayer("_overview_",0,0, w,h);
      }
    }
    else {
      if (w<0 || h<0) {
        hideLayer("_overview_");
      }
    }
  }
  */
	if (bUpdateSlave) {
		this.ECWViewSlave.SetExtents(dWorldTLX, dWorldTLY, dWorldBRX, dWorldBRY);
	}
}

/**
 * Creat the overlay rect on the controller view. Init must be called before this function and the controller must already have a layer
 * in a valid coordinate sytem. This function creates 4 layers, two for horizontal lines and two for vertical lines, which are used 
 * for the overlay rect.
 */
function NCSOverlayRect_Create(dWorldTLX, dWorldTLY, dWorldBRX, dWorldBRY) {
  
  if (this.ECWViewController == null || this.ECWViewSlave == null) {
	alert("NCSOverlayRect.Create() has a null controller or slave. Use NCSOverlayRect.Init() to initialize the controlling view.");
  }
  else {
	if (this.ECWViewController.GetNumberLayers() == 0) {
		alert("You must add valid layers to the view before creating an overlay rect.");
	}
	else {
	
		// If the inputs are negative one, pick our own starting rectangle, in the center.
		if (dWorldTLX == -1 && dWorldTLY == -1 &&  dWorldBRX == -1 && dWorldBRY == -1) {
				
			dWorldTLX = this.ECWViewSlave.GetTopLeftWorldCoordinateX();
			dWorldTLY = this.ECWViewSlave.GetTopLeftWorldCoordinateY();
			dWorldBRX = this.ECWViewSlave.GetBottomRightWorldCoordinateX();
			dWorldBRY = this.ECWViewSlave.GetBottomRightWorldCoordinateY();

			var dWorldX = dWorldTLX + ((dWorldBRX - dWorldTLX) / 2.0);
			var dWorldY = dWorldTLY + ((dWorldBRY - dWorldTLY) / 2.0);

			dWorldTLX = dWorldX - (dWorldBRX - dWorldTLX) / 7.0;
			dWorldBRX = dWorldX + (dWorldBRX - dWorldTLX) / 7.0;

			if(dWorldBRY > dWorldTLY) {
			  dWorldTLY = dWorldY - (dWorldBRY - dWorldTLY) / 7.0;
			  dWorldBRY = dWorldY + (dWorldBRY - dWorldTLY) / 7.0;
			} else {
			  dWorldTLY = dWorldY + (dWorldTLY - dWorldBRY) / 7.0;
			  dWorldBRY = dWorldY - (dWorldTLY - dWorldBRY) / 7.0;
			}
		}

		// Add a simple vector layer to the view
	if (this.ECWViewController.AddLayer("simplevector", "", "NCSOverlayRect", ";") >= 0) {
            var vecObject = "";
            vecObject += "linewidth=1;color=#FF0000;fillcolor=#FFFFFF;filledpolygon1="
            vecObject += dWorldTLX + "," + dWorldTLY + "|";
            vecObject += dWorldBRX + "," + dWorldTLY + "|";
            vecObject += dWorldBRX + "," + dWorldBRY + "|";
            vecObject += dWorldTLX + "," + dWorldBRY + ";";
            this.ECWViewController.SetLayerParameter("NCSOverlayRect", vecObject);
            this.ECWViewController.SetLayerTransparency("NCSOverlayRect", "#FFFFFF", 0.5);
            /*
            var aLyr=getLayer("_overview_");
            if (aLyr) {
              var c1=this.Word2Pixel(dWorldTLX,dWorldBRY);
              var c2=this.Word2Pixel(dWorldBRX,dWorldTLY);
              moveLayer("_overview_",c1.x,c1.y);
              resizeLayer("_overview_", c2.x-c1.x,c2.y-c1.y);
            }
            */
	}
	else {
		alert(this.ECWViewController.GetLastErrorText());
        }
		this.ECWViewSlave.SetExtents(dWorldTLX, dWorldTLY, dWorldBRX, dWorldBRY);
    }
  }  
}

/**
 * Prototype the NCSOverlayRect class
 */
NCSOverlayRect.prototype.Init		   = NCSOverlayRect_Init;
NCSOverlayRect.prototype.SetRect	   = NCSOverlayRect_SetRect;
NCSOverlayRect.prototype.Move		   = NCSOverlayRect_Move;
NCSOverlayRect.prototype.Create 	   = NCSOverlayRect_Create;
NCSOverlayRect.prototype.Word2PixelCoord=function(x,y,w,h,xmin,ymin,xmax,ymax) {
  var theDifX=xmax-xmin;
  var theDifY=ymax-ymin;
  /*
  var Xpercent=(x-xmin)/theDifX;
  var Ypercent=(ymax+y)/theDifY;
  var pX=Xpercent*w;
  var pY=Ypercent*h;  
  */
  var pX=(x-xmin)/theDifX*w
  var pY=h-((y-ymin)/theDifY)*h  
  var coord=new Object();
  coord.x=Math.round(pX)
  coord.y=Math.round(pY);
  return coord;  
}
NCSOverlayRect.prototype.Word2Pixel=function(x,y) {
  var w=this.ECWViewController.GetViewWidth();
  var h=this.ECWViewController.GetViewHeight();
  var xmin = this.ECWViewController.GetTopLeftWorldCoordinateX();
  var ymin = this.ECWViewController.GetBottomRightWorldCoordinateY();
  var xmax = this.ECWViewController.GetBottomRightWorldCoordinateX();
  var ymax = this.ECWViewController.GetTopLeftWorldCoordinateY();
  return this.Word2PixelCoord(x,y,w,h,xmin,ymin,xmax,ymax);
}
NCSOverlayRect.prototype.Pixel2WordCoord=function(pX,pY,viewWidth,viewHeight,xmin,ymin,xmax,ymax){  
  var theDifX=xmax-xmin;
  var theDifY=ymax-ymin;
  var Xpercent=pX/viewWidth;
  var Ypercent=(viewHeight-pY)/viewHeight;
  var theX=xmin+(theDifX*Xpercent);
  var theY=ymin+(theDifY*Ypercent);
  return new Array(theX,theY);
}
NCSOverlayRect.prototype.Pixel2Word=function(pX,pY){
  var w=this.ECWViewController.GetViewWidth();
  var h=this.ECWViewController.GetViewHeight();
  var xmin = this.ECWViewController.GetTopLeftWorldCoordinateX();
  var ymin = this.ECWViewController.GetBottomRightWorldCoordinateY();
  var xmax = this.ECWViewController.GetBottomRightWorldCoordinateX();
  var ymax = this.ECWViewController.GetTopLeftWorldCoordinateY();
  return this.Pixel2WordCoord(pX,pY,w,h,xmin,ymin,xmax,ymax);
}
