// mapcoords.js
// functions to convert between map coordinates and image space

// Requires:
//  zoompan.js loaded and initialized in document.onload, to get zoompan.imageWidth and zoompan.imageHeight
//  rectangle.js loaded, and a Rectangle called mapExtent initialized to current map extent with proper aspect
//  ratio.


function map_pixelToGeoX(x) {
  return mapExtent.left + (x * mapExtent.width() / zoompan.imageWidth);
}
function map_geoToPixelX(x) {
  return mapExtent.width() ? (x - mapExtent.left) * zoompan.imageWidth / mapExtent.width() : null;
}

function map_pixelToGeoY(y) {
  return mapExtent.top - (y * mapExtent.height() / zoompan.imageHeight);
}
function map_geoToPixelY(y) {
  return mapExtent.height() ? (mapExtent.top - y) * zoompan.imageHeight / mapExtent.height() : null;
}

function map_pixelToGeoDistance(dist) {
  return dist * mapExtent.width() / zoompan.imageWidth;
}
function map_geoToPixelDistance(dist) {
  return mapExtent.width() ? dist * zoompan.imageWidth / mapExtent.width() : null;
}

function map_pixelToGeoRectangle(rect) {
  return new Rectangle(
    map_pixelToGeoX(rect.left), map_pixelToGeoY(rect.bottom), 
    map_pixelToGeoX(rect.right), map_pixelToGeoY(rect.top)
  );
}
function map_geoToPixelRectangle(rect) {
  return new Rectangle(
    map_geoToPixelX(rect.left), map_geoToPixelY(rect.bottom), 
    map_geoToPixelX(rect.right), map_geoToPixelY(rect.top)
  );
}

var ovImageWidth = 180;
var ovImageHeight = 90;

function ov_pixelToGeoX(x) {
  return -180 + (x * 360 / ovImageWidth);
}
function ov_geoToPixelX(x) {
  return (x + 180) * ovImageWidth / 360;
}

function ov_pixelToGeoY(y) {
  return 90 - (y * 180 / ovImageHeight);
}
function ov_geoToPixelY(y) {
  return (90 - y) * ovImageHeight / 180;
}


/**
 * These materials were developed by ESRI Canada under contract to ECO-NOVA
 * Productions.  The following license terms and disclaimers apply.
 * 
 * Unless otherwise stated, the definition of "deliverables" includes the
 * complete contents of this file.  However, "deliverables" shall not not
 * include software, data and documentation that is subject to a separate
 * license from Environmental Systems Research Institute, Inc. of USA.
 * 
 * Copyright (c) 1997-2004 ESRI Canada Limited.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this original work of authorship including all deliverables, to
 * deal with the deliverables without restriction, including without
 * limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the deliverables, and to permit
 * persons to whom the deliverables are furnished to do so, subject to the
 * following conditions:
 * 
 *  * The above copyright notice and this permission notice shall be
 *    included in all copies of the software.
 *  
 *  * The name ESRI Canada shall not be used to endorse or promote
 *    products derived from the the software without specific prior
 *    written permission.
 * 
 * To the maximum extent permitted by applicable law, in no event shall
 * ESRI Canada be liable for any special, incidental, indirect or
 * consequential damanages whatsoever (including without limitation,
 * damages for loss of business profits, business interruption, loss of
 * business information, or any other pecuniary loss) arising out of the
 * use or inability to use the software, even if caused by ESRI Canada's
 * negligence or even if ESRI Canada has been advised of the possibility
 * of such damages.
 * 
 * ESRI Canada has extended a "Limited Warranty on Services" to ECO-NOVA
 * Productions as specified in the contract.  To the maximum extent
 * permitted by applicable law, ESRI Canada and its suppliers disclaim
 * all other warranties, representations, conditions or guarantees,
 * either express or implied, including but not limited to, implied
 * warranties of durability, merchantability and fitness for a
 * particular purpose, with regard to the software, services and any
 * accompanying documentation.
**/

