/*
   gtraceroute.js - (c) 2005-2007 by FreeStone Systems, Matthias Cramer <cramer@freestone.net>
   $Id: gtraceroute.js 11 2008-11-06 15:02:38Z cramer $ 
*/

/* Global Variables */

var help_status=0;
var help_counter=0;
var ired;
var iblue;
var igreen;
var iyellow;
var map;
var centerpoint;
var centerzoom;
var points = [];

var gt_debug=0;

/* Functions */

function ResetControl() {
}

function gtraceroute_init() {
    ired = new GIcon();
    ired.image = "http://www.freestone.net/mapimg/mm_20_red.png";
    ired.shadow = "http://www.freestone.net/mapimg/mm_20_shadow.png";
    ired.iconSize = new GSize(12, 20);
    ired.shadowSize = new GSize(22, 20);
    ired.iconAnchor = new GPoint(6, 20);
    ired.infoWindowAnchor = new GPoint(5, 1);

    iblue = new GIcon();
    iblue.image = "http://www.freestone.net/mapimg/mm_20_blue.png";
    iblue.shadow = "http://www.freestone.net/mapimg/mm_20_shadow.png";
    iblue.iconSize = new GSize(12, 20);
    iblue.shadowSize = new GSize(22, 20);
    iblue.iconAnchor = new GPoint(6, 20);
    iblue.infoWindowAnchor = new GPoint(5, 1);

    igreen = new GIcon();
    igreen.image = "http://www.freestone.net/mapimg/mm_20_green.png";
    igreen.shadow = "http://www.freestone.net/mapimg/mm_20_shadow.png";
    igreen.iconSize = new GSize(12, 20);
    igreen.shadowSize = new GSize(22, 20);
    igreen.iconAnchor = new GPoint(6, 20);
    igreen.infoWindowAnchor = new GPoint(5, 1);
     
    iyellow = new GIcon();
    iyellow.image = "http://www.freestone.net/mapimg/mm_20_yellow.png";
    iyellow.shadow = "http://www.freestone.net/mapimg/mm_20_shadow.png";
    iyellow.iconSize = new GSize(12, 20);
    iyellow.shadowSize = new GSize(22, 20);
    iyellow.iconAnchor = new GPoint(6, 20);
    iyellow.infoWindowAnchor = new GPoint(5, 1);
     
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    //map.addControl(new GMapTypeControl());
    // New fancy Hirarcical Control
    map.addControl(new GHierarchicalMapTypeControl());
    map.addControl(new GScaleControl());

    ResetControl.prototype = new GControl();

    ResetControl.prototype.initialize = function(map) {
      var ResetButton = document.createElement("div");
      this.setButtonStyle_(ResetButton);
      ResetButton.appendChild(document.createTextNode("Reset Map"));
      GEvent.addDomListener(ResetButton, "click", function() {
        map.setCenter(centerpoint, centerzoom);
      });
      map.getContainer().appendChild(ResetButton);
      return ResetButton;
    }
    
    ResetControl.prototype.getDefaultPosition = function() {
      return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 50));
    }
    
    // Sets the proper CSS for the given button element.
    ResetControl.prototype.setButtonStyle_ = function(button) {
      button.style.color = "#000";
      button.style.backgroundColor = "white";
      button.style.font = "small Arial";
      button.style.border = "1px solid black";
      button.style.padding = "1px 5px 1px 5px";
      button.style.marginBottom = "3px";
      button.style.textAlign = "center";
      button.style.cursor = "pointer";
    }
    
    map.addControl(new ResetControl());
    map.enableContinuousZoom();
    map.enableScrollWheelZoom();
}

function gtraceroute_map() {
      setMinViewport(points);
      // Adds Terain Selector
      map.addMapType(G_PHYSICAL_MAP);
      map.setMapType(G_HYBRID_MAP);
      var polyOptions = {geodesic:true};
      map.addOverlay(new GPolyline(points), "#7777ff", 4, 1, polyOptions);
      map.addControl(new GOverviewMapControl(),new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(0, 0)));
}

function add_point(lon, lat) {
  points.push(new GLatLng(lat, lon));
}

function gzoom_location(lat, lon) {
  map.panTo(new GLatLng(lat, lon));
  return;
}

function hide_loading(id) {
  document.getElementById(id).className=id+"_hidden";
}
 
function show_loading(id) {
  document.getElementById(id).className=id+"_show";
}

function add_marker(x,y,text,method) {

      var point=new GPoint(x,y);
      var marker=null;
      var found="";
      switch (method) {
        case 1: 
          marker=new GMarker(point, iblue);
          found="DNS LOC";
          break;
        case 2:
          marker=new GMarker(point, igreen);
          found="Regualr Expression Database";
          break;
        case 3:
          marker=new GMarker(point, iyellow);
          found="Whois";
          break;
        case 4:
          marker=new GMarker(point, ired);
          found="MaxMind GeoLite City";
          break;
        default:
          marker=new GMarker(point, ibrown);
          found="Unknown";
      }
      map.addOverlay(marker);
      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(text+"<br/>found via "+found);
      });
}   

function setMinViewport(a) {

	if (gt_debug > 0) GLog.write("Length of a : "+ a.length);
				
        //takes an array of GPoints, resizes map to display all points
	var maxX = -180;
	var minX = 180;
	var maxY = -180;
	var minY = 180;
				
	for(var i = 0; i < a.length; i++) {
	  if(a[i].lat() > maxY) maxY = a[i].lat();
	  if(a[i].lat() < minY) minY = a[i].lat();
	  if(a[i].lng() > maxX) maxX = a[i].lng();
	  if(a[i].lng() < minX) minX = a[i].lng();
	  if (gt_debug > 0) GLog.write("a["+i+"] = "+ a[i].lat() +" / " +a[i].lng());
	}
		
	var centerLng = (maxX + minX) / 2;
	var centerLat = (maxY + minY) / 2;
	
	if (gt_debug > 0) GLog.write("Center: "+centerLng+" / "+centerLat);
	
	sw = new GLatLng(minY - 0.02 , minX - 0.02);
	ne = new GLatLng(maxY + 0.02 , maxX + 0.02);

        bounds = new GLatLngBounds(sw,  ne);

        zoom = map.getBoundsZoomLevel(bounds);
		
	if (gt_debug > 0) GLog.write("Zoom : "+zoom);
                	
	//set the map to our starting location!
        centerpoint=new GLatLng(centerLat, centerLng);
        centerzoom=zoom;
        map.setCenter(centerpoint, zoom);
}

function toggle_help(name, size) {
  var trhelp=document.getElementById(name);
  
  if (help_status==0) {
    help_counter++;
  } else {
    if (help_counter == size) {
      document.getElementById(name+"_content").style.display="none";
    }
    help_counter--;
  }
  var width=help_counter+6;
  var height=(help_counter*1.2)+1.5;

  trhelp.style.width=width+"em";
  trhelp.style.height=height+"em";

  if ((help_counter < size && help_status==0) || (help_counter>0 && help_status==1)) {
    window.setTimeout("toggle_help('"+name+"', "+size+")",2);
  } else {
    help_status=(help_status+1) % 2 ;
    if (help_status==1) {
       document.getElementById(name+"_content").style.display="inline";
    } else {
      document.getElementById(name+"_content").style.display="none";
    }
  }
}

function methodchange() {
  proto = document.forms[0].proto.value;
  if (proto == "6") {
    document.getElementById("method").disabled = "disabled";
  } else {
    document.getElementById("method").disabled = "";
  }
}
