/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function calcGPS(n,t) {
    var GPS = {};
    GPS.abs = Math.abs(n);
    GPS.D = Math.floor(GPS.abs);
    GPS.Dfrac = GPS.abs - GPS.D;
    GPS.M = Math.floor(GPS.Dfrac * 60);
    GPS.Mfrac = GPS.Dfrac * 60 - GPS.M
    GPS.S = Math.floor(GPS.Mfrac * 60);
    GPS.Sfrac = GPS.Mfrac * 60 - GPS.S;

    if (n >= 0) {
	GPS.Dp = GPS.D;
	switch (t) {
	    case 1:
		GPS.dir = 'N';
		break;
	    case 2:
		GPS.dir = 'A';
		break;
	}
    }
    else {
	GPS.Dp = -GPS.D;
	switch (t) {
	    case 1:
		GPS.dir = 'S';
		break;
	    case 2:
		GPS.dir = 'V';
		break;
	}
    }

    return GPS;
}

function getResult(a, n)
{
    var Lat = this.calcGPS(a.x, 1);
    var Lon = this.calcGPS(a.y, 2);
    var txt;
	switch (n) {
	    case 1:
		txt = 'LAT = ' + zeroPad(Lat.Dp, 2) + ',' + zeroPad(Math.round(Lat.Dfrac * 10000), 4) +
			 ' LON = ' + zeroPad(Lon.Dp, 3) + ',' + zeroPad(Math.round(Lon.Dfrac * 10000), 4);
		break;
	    case 0:
		txt = zeroPad(Lat.Dp, 2) + '\u00B0' + zeroPad(Lat.M, 2) + ',' + zeroPad(Math.round(Lat.Mfrac * 1000), 3) +
			 '\'N, ' + zeroPad(Lon.Dp, 3) + '\u00B0' + zeroPad(Lon.M, 2) + ',' + zeroPad(Math.round(Lon.Mfrac * 1000), 3) + '\'W';
		break;
	    case 2:
		txt = 'LATi = ' + zeroPad(Lat.D, 2) + '\u00B0' + zeroPad(Lat.M, 2) + "'" + zeroPad(Lat.S, 2) + ',' + zeroPad(Math.round(Lat.Sfrac * 10), 2) + '"' + Lat.dir +
			 ' LON = ' + zeroPad(Lon.D, 3) + '\u00B0' + zeroPad(Lon.M, 2) + "'" + zeroPad(Lon.S, 2) + ',' + zeroPad(Math.round(Lon.Sfrac * 10), 2) + '"' + Lon.dir;
		break;
	}
    return txt;
}

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});


/*
  document.write("ISN93 -> WGS84<br/>");    
  document.write("X=500001, Y = 500000<br/>");
  var pos = SiteWatch.conversion.reverseLambert(500001, 500000);
  document.write(getResult(pos,0)+"<br/>");
  document.write(getResult(pos,1)+"<br/>");
  document.write(getResult(pos,2)+"<br/>");
  document.write("WGS84 -> ISN93<br/>");
  pos = SiteWatch.conversion.getLambert(pos.x, pos.y);

  document.write("X = " + pos.x + " Y = " + pos.y);
*/



