
var wmsUrl = "http://213.135.13.70/WMS/wms.asp?";
var vmapmap;
var bluemarblemap;
var cloudmap;

function formatnumber(value, decimals) {
  if (value == 0) {
    return '0';
  } else {
    usrdata=value.toString(10);
    i=usrdata.indexOf('.');
    if (i == 0) {
      return usrdata;
    } else {
      return usrdata.substr(0,i+decimals);
    }
  }
}

function makeURL(layers, maptype, bbox, ts) {
  return wmsUrl + "Service=WMS&WMS=" + maptype+ "&VERSION=1.1.0&REQUEST=GetMap&BBOX=" + bbox + "&srs=EPSG:4326&WIDTH=" + ts + "&HEIGHT=" + ts + "&LAYERS=" + layers + "&STYLES=,,,,,,,,&BGCOLOR=0xCEFFFF&FORMAT=image/jpeg&TRANSPARENT=True&WRAPDATELINE=TRUE";
}

function vmapGetTileURL(a,zoom) {
  var layers = encodeURIComponent("Bathymetry,Countries,Topography,Hillshading,Builtup+areas,Coastlines,Waterbodies,Inundated,Rivers,Streams,Railroads,Highways,Roads,Trails,Borders,Cities,Settlements,Spot+elevations,Airports,Ocean+features");
  var maptype = "WorldMap";
  var ts = vmapmap.getTileSize();
  var ul = vmapmap.getProjection().fromPixelToLatLng(new GPoint(a.x * ts, (a.y + 1) * ts), zoom, true);
  var lr = vmapmap.getProjection().fromPixelToLatLng(new GPoint((a.x + 1) * ts, a.y * ts), zoom, true);
  var bbox = ul.x + "," + ul.y + "," + lr.x + "," + lr.y;
  var url = makeURL(layers, maptype, bbox, ts);
  return url;
}

function blueMarbleGetTileURL(a,zoom) {
  var layers = encodeURIComponent("Countries,Earth Image,Borders");
  var maptype = "reliefWorld";
  var ts = bluemarblemap.getTileSize();
  var ul = bluemarblemap.getProjection().fromPixelToLatLng(new GPoint(a.x * ts, (a.y + 1) * ts), zoom, true);
  var lr = bluemarblemap.getProjection().fromPixelToLatLng(new GPoint((a.x + 1) * ts, a.y * ts), zoom, true);
  var bbox = ul.x + "," + ul.y + "," + lr.x + "," + lr.y;
  return makeURL(layers, maptype, bbox, ts);
}

function cloudGetTileURL(a,zoom) {
  var layers = encodeURIComponent("Layer1");
  var maptype = "cloudMap";
  var ts = cloudmap.getTileSize();
  var ul = cloudmap.getProjection().fromPixelToLatLng(new GPoint(a.x * ts, (a.y + 1) * ts), zoom, true);
  var lr = cloudmap.getProjection().fromPixelToLatLng(new GPoint((a.x + 1) * ts, a.y * ts), zoom, true);
  var bbox = ul.x + "," + ul.y + "," + lr.x + "," + lr.y;
  return makeURL(layers, maptype, bbox, ts);
}

function getCopyrightCollection(rights) {
  var copyright =  new GCopyright(1, new GLatLngBounds(new GLatLng(-90, -180), new GLatLng(90, 180)), 2, " ");
  var collection =  new GCopyrightCollection(rights);
  collection.addCopyright(copyright);
  return collection;
}

function addCustomMaps(karte) {
  var vmapCollection = getCopyrightCollection("&copy; <a href=\"http://www.nga.mil/portal/site/nga01/\">NIMA</a>");
  // vmapCollection.addCopyright(new GCopyright(1, new GLatLngBounds(new GLatLng(-90, -180), new GLatLng(90, 180)), 2, " "));
  var vmapLayer = new GTileLayer(vmapCollection, 2, 11);
  vmapLayer.getTileUrl = vmapGetTileURL;
  vmapmap = new GMapType([vmapLayer], new GMercatorProjection(12), "Land", {errorMessage:"No data available"});
  karte.addMapType(vmapmap);
 
  var blueMarbleCollection = getCopyrightCollection("&copy; <a href=\"http://earthobservatory.nasa.gov/\">The Blue Marble Project</a>");
  var blueMarbleLayer = new GTileLayer(blueMarbleCollection, 2, 11);
  blueMarbleLayer.getTileUrl = blueMarbleGetTileURL;
  bluemarblemap = new GMapType([blueMarbleLayer], new GMercatorProjection(12), "Relief", {errorMessage:"No data available"});
  karte.addMapType(bluemarblemap);
 

  var cloudCollection = getCopyrightCollection("&copy; 2009 Naval Research Laboratory");
  var cloudLayer = new GTileLayer(cloudCollection, 2, 11);
  cloudLayer.getTileUrl = cloudGetTileURL;
  cloudmap = new GMapType([cloudLayer], new GMercatorProjection(12), "Cloud", {errorMessage:"No data available"});
  karte.addMapType(cloudmap);

  var seaCollection = getCopyrightCollection("&copy; 2009 <a href=\"http://www.openseamap.org\">OpenSeaMap</a>, <a href=\"http://www.openstreetmap.org\">OpenStreetMap</a>, Lizenz: <a href=\"http://creativecommons.org/licenses/by-sa/2.0/\">CC-by-SA</a>");
  var seaLayer = new GTileLayer(seaCollection, 1, 17, {tileUrlTemplate: 'http://tile.openstreetmap.org/{Z}/{X}/{Y}.png'});
  seaOverlay = new GTileLayer(seaCollection, null, null, {tileUrlTemplate: 'http://tiles.openseamap.org/seamark/{Z}/{X}/{Y}.png', isPng:true, opacity: 1.0});
  seamap = new GMapType([seaLayer, seaOverlay], G_NORMAL_MAP.getProjection(), "OSeaM", {errorMessage:"No data available"}); 
  karte.addMapType(seamap);
}

function topframe() {
  return window.top.frames['bodyName'];
}

function makeImage(symbol) {
  var uri = "http://www.positionsreport.de/php/";
  var symbolimg = document.createElement("img");
  var symbolNumber = symbol.charCodeAt(1);
  if (symbolNumber < 10) {
    symbolNumber = "00" + symbolNumber;
  } else if (symbolNumber < 100) {
    symbolNumber = "0" + symbolNumber;
  }
  symbolNumber = symbolNumber + ".gif";
  if (symbol.charAt(0) == "/") {
    uri = uri + "pri/";
  } else {
    uri = uri + "alt/";
  }
  symbolimg.src = uri + symbolNumber;
  return symbolimg;
}

function makeOverlay(symbol) {
  var overlayDiv = document.createElement("div");
  overlayDiv.style.position = "absolute";
  overlayDiv.style.textAlign = "center";
  overlayDiv.style.verticalAlign= "middle";
  overlayDiv.style.top = "1px";
  overlayDiv.style.left = "1px";
  overlayDiv.style.width = "20px";
  overlayDiv.style.height = "20px";
  if (symbol.charAt(0) != "\\") {
    if (symbol.charCodeAt(1) == 48) {
      overlayDiv.innerHTML = "<span class=\"gmap_icon_ovl_blk\">" + symbol.charAt(0) + "</span>";
    } else {
      overlayDiv.innerHTML = "<span class=\"gmap_icon_ovl\">" + symbol.charAt(0) + "</span>";
    }
  }
  return overlayDiv;
}

function makeSymDiv(symbol, callsign, farbe) {
  var symdiv = document.createElement("div");
  symdiv.id = callsign;
  symdiv.style.position = "absolute";
  symdiv.style.top = "0px";
  symdiv.style.left = "15px";
  symdiv.style.width = "20px";
  symdiv.style.height = "20px";
  if (farbe == "colRed") {
    symdiv.style.backgroundColor = "#FF0000";
  } else if (farbe == "colWhite") {
    symdiv.style.backgroundColor = "#FFFFFF";
  }
  symdiv.appendChild(makeImage(symbol));
  if (symbol.charAt(0) != "/") {
    symdiv.appendChild(makeOverlay(symbol));
  }
  return symdiv;
}

function makeCallDiv(callsign) {
  var txtdiv = document.createElement("div");
  txtdiv.style.position = "absolute";
  txtdiv.style.top =  "20px";
  txtdiv.style.left = "0px";
  txtdiv.style.width = "50px";
  txtdiv.style.height = "10px";
  txtdiv.innerHTML = "<span class=\"gmap_icon_text\">" + callsign + "</span>";
  return txtdiv;
}

function positionMarker(point, symbol, callsign, farbe) {
  this.point_ = point;
  this.symbol_ = symbol;
  this.call_ = callsign;
  this.farbe_ = farbe;
}

positionMarker.prototype = new GOverlay();

positionMarker.prototype.getCallSign = function() {
  return this.call_;
}

positionMarker.prototype.getPosition = function() {
  return this.point_;
}

positionMarker.prototype.initialize = function(map) {
  var div = document.createElement("div");
  div.style.position = "absolute";
  div.style.width = "50px";
  div.style.height = "30px";
  
  symbolDiv = makeSymDiv(this.symbol_, this.call_, this.farbe_);
  div.appendChild(symbolDiv);
  div.appendChild(makeCallDiv(this.call_));

  map.getPane(G_MAP_MARKER_PANE).appendChild(div);

  this.point = this.point_
  this.clickable = symbolDiv;
  this.map_ = map;
  this.div_ = div;
}

positionMarker.prototype.setPoint = function(point) {
  this.point_ = point;
  this.redraw(true);
}

positionMarker.prototype.remove = function() {
  while (this.div_.childNodes[0]) {
    this.div_.removeChild(this.div_.childNodes[0]);
  }
  this.div_.parentNode.removeChild(this.div_);
}

positionMarker.prototype.copy = function() {
  return new positions(this.point_, this.symbol_, this.call_);
}

positionMarker.prototype.redraw = function(force) {
  // We only need to redraw if the coordinate system has changed
  if (!force) return;
  
  var koords = this.map_.fromLatLngToDivPixel(this.point_);
  this.div_.style.left = (koords.x - 20) + "px";
  this.div_.style.top = (koords.y - 10) + "px";
}

function trackControl() {
  var last_ = false;
  var now_ = false;
  var startdate_;
  var enddate_;
  clickLast_ = function(element) {
    divlast = document.getElementById("lastIDinner");
    divnow = document.getElementById("nowIDinner");
    txt = document.getElementById("textNowDiv");
    if (last_) {
      last_ = false;
      divlast.style.borderTopColor =  "#ffffff"; 
      divlast.style.borderLeftColor = "#ffffff"; 
      divlast.style.borderRightColor = "#b0b0b0";
      divlast.style.borderBottomColor = "#b0b0b0";
      divlast.style.fontWeight = "normal";
    } else {
      last_ = true;
      divlast.style.borderTopColor =  "#b0b0b0"; 
      divlast.style.borderLeftColor = "#b0b0b0"; 
      divlast.style.borderRightColor = "#ffffff";
      divlast.style.borderBottomColor = "#ffffff";
      divlast.style.fontWeight = "bold";
      if (now_) {
	now_ = false;
	txt.style.visibility = "hidden";
	divnow.style.borderTopColor =  "#ffffff"; 
	divnow.style.borderLeftColor = "#ffffff"; 
	divnow.style.borderRightColor = "#b0b0b0";
	divnow.style.borderBottomColor = "#b0b0b0";
	divnow.style.fontWeight = "normal";
	startdate_ = '';
	enddate_ = '';
      }
    }
    loadMap();
  }
  clickTrack_ = function(element) {
    divlast = document.getElementById("lastIDinner");
    divnow = document.getElementById("nowIDinner");
    txt = document.getElementById("textNowDiv");
    if (now_) {
      now_ = false;
      txt.style.visibility = "hidden";
      divnow.style.borderTopColor =  "#ffffff"; 
      divnow.style.borderLeftColor = "#ffffff"; 
      divnow.style.borderRightColor = "#b0b0b0";
      divnow.style.borderBottomColor = "#b0b0b0";
      divnow.style.fontWeight = "normal";
    } else {
      now_ = true;
      txt.style.visibility = "visible";
      divnow.style.borderTopColor =  "#b0b0b0"; 
      divnow.style.borderLeftColor = "#b0b0b0"; 
      divnow.style.borderRightColor = "#ffffff";
      divnow.style.borderBottomColor = "#ffffff";
      divnow.style.fontWeight = "bold";
      startdate_ = encodeURIComponent(getCurrentDate());
      enddate_ = '';
      if (last_) {
	last_ = false;
	divlast.style.borderTopColor =  "#ffffff"; 
	divlast.style.borderLeftColor = "#ffffff"; 
	divlast.style.borderRightColor = "#b0b0b0";
	divlast.style.borderBottomColor = "#b0b0b0";
	divlast.style.fontWeight = "normal";
      }
    }
    loadMap();
  }
  this.reset = function() {
    divlast = document.getElementById("lastIDinner");
    divnow = document.getElementById("nowIDinner");
    txt = document.getElementById("textNowDiv");
    last_ = false;
    divlast.style.borderTopColor =  "#ffffff"; 
    divlast.style.borderLeftColor = "#ffffff"; 
    divlast.style.borderRightColor = "#b0b0b0";
    divlast.style.borderBottomColor = "#b0b0b0";
    divlast.style.fontWeight = "normal";
    now_ = false;
    txt.style.visibility = "hidden";
    divnow.style.borderTopColor =  "#ffffff"; 
    divnow.style.borderLeftColor = "#ffffff"; 
    divnow.style.borderRightColor = "#b0b0b0";
    divnow.style.borderBottomColor = "#b0b0b0";
    divnow.style.fontWeight = "normal";
  }
  this.setLast = function() {
    divlast = document.getElementById("lastIDinner");
    last_ = true;
    divlast.style.borderTopColor =  "#b0b0b0"; 
    divlast.style.borderLeftColor = "#b0b0b0"; 
    divlast.style.borderRightColor = "#ffffff";
    divlast.style.borderBottomColor = "#ffffff";
    divlast.style.fontWeight = "bold";
  }
  this.setNow  = function() {
    divnow = document.getElementById("nowIDinner");
    txt = document.getElementById("textNowDiv");
    now_ = true;
    txt.style.visibility = "visible";
    divnow.style.borderTopColor =  "#b0b0b0"; 
    divnow.style.borderLeftColor = "#b0b0b0"; 
    divnow.style.borderRightColor = "#ffffff";
    divnow.style.borderBottomColor = "#ffffff";
    divnow.style.fontWeight = "bold";
  }
  this.getNow = function() {
    return now_;
  }
  this.getLast = function() {
    return last_;
  }
  this.startdate = function() {
    return startdate_;
  }
  this.enddate = function() {
    return enddate_;
  }
}

trackControl.prototype = new GControl();

trackControl.prototype.initialize = function(map) {
  this.div_ = document.createElement("div");
  this.div_.id = "TrackControl";
 
  var lastDiv = document.createElement("div");
  lastDiv.id = "lastID";
  var lastDivInner = document.createElement("div"); 
  lastDivInner.id = "lastIDinner";
  lastDivInner.innerHTML = "last 24 hr";
  lastDiv.appendChild(lastDivInner);
  GEvent.addDomListener(lastDivInner, "click", clickLast_);

  var nowDiv = document.createElement("div");
  nowDiv.id = "nowID";
  var nowDivInner = document.createElement("div");
  nowDivInner.id = "nowIDinner";
  nowDivInner.innerHTML = "track from now";
  nowDiv.appendChild(nowDivInner);
  GEvent.addDomListener(nowDivInner, "click", clickTrack_);
  var txtDiv = document.createElement("div");
  txtDiv.id = "textNowDiv";
  txtDiv.innerHTML = "This map refreshes every 2 minutes!";
  txtDiv.style.visibility = "hidden";

  this.txt_ = txtDiv;
  this.divlast_ = lastDivInner;
  this.divnow_ = nowDivInner;

  this.div_.appendChild(txtDiv);
  this.div_.appendChild(lastDiv);
  this.div_.appendChild(nowDiv);
  
  map.getContainer().appendChild(this.div_);

  return this.div_;
}

trackControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(80,6));
}

function ShowMousePositionControl() {
}

ShowMousePositionControl.prototype = new GControl();

ShowMousePositionControl.prototype.initialize = function(map) {
  this.div_ = document.createElement("div");
  this.div_.id = "PositionsZeiger";
  GEvent.addListener(map, "mousemove", this.UpdatePosition_);
  map.getContainer().appendChild(this.div_);
  this.div_.style.border = "none";
  this.div_.style.textAlign = "center";
  this.div_.style.width = "110px";
  this.div_.style.height = "25px"
  this.div_.style.backgroundColor = "#110EA9";
  return this.div_;
}

ShowMousePositionControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(5,45));
}

ShowMousePositionControl.prototype.UpdatePosition_ = function(latlong) {
  var xmouse = latlong.lng();
  var ymouse = latlong.lat();
  if (xmouse < 0) {
    directionLon = 'W';
    xmouse0 = -xmouse;
  } else {
    directionLon = 'E';
    xmouse0 = xmouse;
  }
  if (ymouse < 0) {
    directionLat = 'S';
    ymouse0 = -ymouse;
  } else {
    directionLat = 'N';
    ymouse0 = ymouse;
  }
  xmouse1 = (xmouse0 - Math.floor(xmouse0)) * 60;
  ymouse1 = (ymouse0 - Math.floor(ymouse0)) * 60;
  xmouse2 = (xmouse1 - Math.floor(xmouse1)) * 60;
  ymouse2 = (ymouse1 - Math.floor(ymouse1)) * 60;
  if (!isNaN(xmouse) && !isNaN(ymouse)) {
    var text = '<span class="postext">';
    text = text + '&nbsp;Lat: ' + formatnumber(ymouse0,0) + '°'+formatnumber(ymouse1,0)+'\''+formatnumber(ymouse2,0)+ '\'\' ' + directionLat;
    text = text + '<br>&nbsp;Lon: ' + formatnumber(xmouse0,0)+'°'+formatnumber(xmouse1,0)+'\''+formatnumber(xmouse2,0) + '\'\' ' + directionLon;
    text = text + '</span>';
    document.getElementById("PositionsZeiger").innerHTML = text;
  }
}
