/**
 * Copyright 2008 ERDAS, Inc. This document contains unpublished source code of
 * ERDAS, Inc. 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);

	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);
		}
		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;


