/**

* Contains methods for transforming point on sphere to
* Cartesian coordinates using various projections.
* @class
*/

jvm.Proj = {

degRad: 180 / Math.PI,
radDeg: Math.PI / 180,
radius: 6381372,

sgn: function(n){
  if (n > 0) {
    return 1;
  } else if (n < 0) {
    return -1;
  } else {
    return n;
  }
},

/**
 * Converts point on sphere to the Cartesian coordinates using Miller projection
 * @param {Number} lat Latitude in degrees
 * @param {Number} lng Longitude in degrees
 * @param {Number} c Central meridian in degrees
 */
mill: function(lat, lng, c){
  return {
    x: this.radius * (lng - c) * this.radDeg,
    y: - this.radius * Math.log(Math.tan((45 + 0.4 * lat) * this.radDeg)) / 0.8
  };
},

/**
 * Inverse function of mill()
 * Converts Cartesian coordinates to point on sphere using Miller projection
 * @param {Number} x X of point in Cartesian system as integer
 * @param {Number} y Y of point in Cartesian system as integer
 * @param {Number} c Central meridian in degrees
 */
mill_inv: function(x, y, c){
  return {
    lat: (2.5 * Math.atan(Math.exp(0.8 * y / this.radius)) - 5 * Math.PI / 8) * this.degRad,
    lng: (c * this.radDeg + x / this.radius) * this.degRad
  };
},

/**
 * Converts point on sphere to the Cartesian coordinates using Mercator projection
 * @param {Number} lat Latitude in degrees
 * @param {Number} lng Longitude in degrees
 * @param {Number} c Central meridian in degrees
 */
merc: function(lat, lng, c){
  return {
    x: this.radius * (lng - c) * this.radDeg,
    y: - this.radius * Math.log(Math.tan(Math.PI / 4 + lat * Math.PI / 360))
  };
},

/**
 * Inverse function of merc()
 * Converts Cartesian coordinates to point on sphere using Mercator projection
 * @param {Number} x X of point in Cartesian system as integer
 * @param {Number} y Y of point in Cartesian system as integer
 * @param {Number} c Central meridian in degrees
 */
merc_inv: function(x, y, c){
  return {
    lat: (2 * Math.atan(Math.exp(y / this.radius)) - Math.PI / 2) * this.degRad,
    lng: (c * this.radDeg + x / this.radius) * this.degRad
  };
},

/**
 * Converts point on sphere to the Cartesian coordinates using Albers Equal-Area Conic
 * projection
 * @see <a href="http://mathworld.wolfram.com/AlbersEqual-AreaConicProjection.html">Albers Equal-Area Conic projection</a>
 * @param {Number} lat Latitude in degrees
 * @param {Number} lng Longitude in degrees
 * @param {Number} c Central meridian in degrees
 */
aea: function(lat, lng, c){
  var fi0 = 0,
      lambda0 = c * this.radDeg,
      fi1 = 29.5 * this.radDeg,
      fi2 = 45.5 * this.radDeg,
      fi = lat * this.radDeg,
      lambda = lng * this.radDeg,
      n = (Math.sin(fi1)+Math.sin(fi2)) / 2,
      C = Math.cos(fi1)*Math.cos(fi1)+2*n*Math.sin(fi1),
      theta = n*(lambda-lambda0),
      ro = Math.sqrt(C-2*n*Math.sin(fi))/n,
      ro0 = Math.sqrt(C-2*n*Math.sin(fi0))/n;

  return {
    x: ro * Math.sin(theta) * this.radius,
    y: - (ro0 - ro * Math.cos(theta)) * this.radius
  };
},

/**
 * Converts Cartesian coordinates to the point on sphere using Albers Equal-Area Conic
 * projection
 * @see <a href="http://mathworld.wolfram.com/AlbersEqual-AreaConicProjection.html">Albers Equal-Area Conic projection</a>
 * @param {Number} x X of point in Cartesian system as integer
 * @param {Number} y Y of point in Cartesian system as integer
 * @param {Number} c Central meridian in degrees
 */
aea_inv: function(xCoord, yCoord, c){
  var x = xCoord / this.radius,
      y = yCoord / this.radius,
      fi0 = 0,
      lambda0 = c * this.radDeg,
      fi1 = 29.5 * this.radDeg,
      fi2 = 45.5 * this.radDeg,
      n = (Math.sin(fi1)+Math.sin(fi2)) / 2,
      C = Math.cos(fi1)*Math.cos(fi1)+2*n*Math.sin(fi1),
      ro0 = Math.sqrt(C-2*n*Math.sin(fi0))/n,
      ro = Math.sqrt(x*x+(ro0-y)*(ro0-y)),
      theta = Math.atan( x / (ro0 - y) );

  return {
    lat: (Math.asin((C - ro * ro * n * n) / (2 * n))) * this.degRad,
    lng: (lambda0 + theta / n) * this.degRad
  };
},

/**
 * Converts point on sphere to the Cartesian coordinates using Lambert conformal
 * conic projection
 * @see <a href="http://mathworld.wolfram.com/LambertConformalConicProjection.html">Lambert Conformal Conic Projection</a>
 * @param {Number} lat Latitude in degrees
 * @param {Number} lng Longitude in degrees
 * @param {Number} c Central meridian in degrees
 */
lcc: function(lat, lng, c){
  var fi0 = 0,
      lambda0 = c * this.radDeg,
      lambda = lng * this.radDeg,
      fi1 = 33 * this.radDeg,
      fi2 = 45 * this.radDeg,
      fi = lat * this.radDeg,
      n = Math.log( Math.cos(fi1) * (1 / Math.cos(fi2)) ) / Math.log( Math.tan( Math.PI / 4 + fi2 / 2) * (1 / Math.tan( Math.PI / 4 + fi1 / 2) ) ),
      F = ( Math.cos(fi1) * Math.pow( Math.tan( Math.PI / 4 + fi1 / 2 ), n ) ) / n,
      ro = F * Math.pow( 1 / Math.tan( Math.PI / 4 + fi / 2 ), n ),
      ro0 = F * Math.pow( 1 / Math.tan( Math.PI / 4 + fi0 / 2 ), n );

  return {
    x: ro * Math.sin( n * (lambda - lambda0) ) * this.radius,
    y: - (ro0 - ro * Math.cos( n * (lambda - lambda0) ) ) * this.radius
  };
},

/**
 * Converts Cartesian coordinates to the point on sphere using Lambert conformal conic
 * projection
 * @see <a href="http://mathworld.wolfram.com/LambertConformalConicProjection.html">Lambert Conformal Conic Projection</a>
 * @param {Number} x X of point in Cartesian system as integer
 * @param {Number} y Y of point in Cartesian system as integer
 * @param {Number} c Central meridian in degrees
 */
lcc_inv: function(xCoord, yCoord, c){
  var x = xCoord / this.radius,
      y = yCoord / this.radius,
      fi0 = 0,
      lambda0 = c * this.radDeg,
      fi1 = 33 * this.radDeg,
      fi2 = 45 * this.radDeg,
      n = Math.log( Math.cos(fi1) * (1 / Math.cos(fi2)) ) / Math.log( Math.tan( Math.PI / 4 + fi2 / 2) * (1 / Math.tan( Math.PI / 4 + fi1 / 2) ) ),
      F = ( Math.cos(fi1) * Math.pow( Math.tan( Math.PI / 4 + fi1 / 2 ), n ) ) / n,
      ro0 = F * Math.pow( 1 / Math.tan( Math.PI / 4 + fi0 / 2 ), n ),
      ro = this.sgn(n) * Math.sqrt(x*x+(ro0-y)*(ro0-y)),
      theta = Math.atan( x / (ro0 - y) );

  return {
    lat: (2 * Math.atan(Math.pow(F/ro, 1/n)) - Math.PI / 2) * this.degRad,
    lng: (lambda0 + theta / n) * this.degRad
  };
}

};