function plotLongLat(lng, lat, zoomLevel, mapType) {
  map.setMapType(mapType);
  map.zoomTo(zoomLevel);
  map.recenterOrPanToLatLng(new GPoint(lng, lat));
}


// 'drawPolygon()' from Joe Murphy - http://www.mailbag.com/users/joe_/
var d2r = Math.PI/180; // degrees to radians
var r2d = 180/Math.PI; // radians to degrees

function SwitchOffPoligons(){
	for (var i = 0; i < Poligons.length; i++) {
		map.removeOverlay(Poligons[i])
    } 
}

function resizeMap( markers ) {
    // Create new bounds object
    var bounds = new GLatLngBounds();
    // Loop through the points, extending the bounds as necessary
    for (var i=0; i<markers.length; i++) {
		      bounds.extend(markers[i].getPoint());
    }
    // Find the centre of the new bounds
    var lat = bounds.getSouthWest().lat() + ((bounds.getNorthEast().lat()- bounds.getSouthWest().lat()) / 2);
    var lon = bounds.getSouthWest().lng() + ((bounds.getNorthEast().lng()- bounds.getSouthWest().lng()) / 2);
    // Get the bounds zoom level
    var zoom = map.getBoundsZoomLevel(bounds);
    // Change the map to the new bounds values
    map.setCenter(new GLatLng(lat, lon), zoom);
}
//draw circle
function drawPolygon(lng, lat, PGsides, PGradius, PGcolor, PGwidth, PGtrans, Pline) {
/* parameters:
  'PGsides' - number of sides of polygon (use 36 for circle)
  'PGradius' - radius in miles of polygon vertices
  'PGcolor' - polygon color, hex
  'PGwidth' - line width in pixels
  'PGtrans' - transparency 0..1
*/
	  var PGstart = (PGsides%2 == 1) ? 2/PGsides : 1/PGsides;
	  PGstart = (0.5 - PGstart) * Math.PI;
	  var PGlat = (PGradius/3963)*r2d; // using 3963 miles as earth's radius
	  var PGlng = PGlat/Math.cos(lat*d2r);
	  var PGpoints = [];
	  for (var i=-1; i < PGsides; i++) {
	    var theta = 2*i*Math.PI/PGsides + PGstart;
	    PGx = lng + (PGlng * Math.cos(theta));
	    PGy = lat + (PGlat * Math.sin(theta));
	    PGpoints.push(new GPoint(PGx,PGy));
	    }
	  
	  if(Pline>0)
	       var poly = new GPolygon(PGpoints, PGcolor, PGwidth, PGtrans,PGcolor,0.1);
	     else 
	       var poly = new GPolyline(PGpoints, PGcolor, PGwidth, PGtrans);
	  
	  map.addOverlay(poly);
	  return poly;
  }

function drawSector(lat,lng,r,azimuth,width,PGcolor,Pline) {
	
	var centerPoint = new GLatLng(lat,lng);	
	var PRlat = (r/3963) * r2d; // using 3963 miles as earth's radius
 	var PRlng = PRlat/Math.cos(lat*d2r);
	var PGpoints = [];
	
	PGpoints.push(centerPoint);

	with (Math) {	  
	    lat1 = lat + (PRlat * cos( d2r * (azimuth  - width/2 )));
	    lon1 = lng + (PRlng * sin( d2r * (azimuth  - width/2 )));
	    PGpoints.push( new GLatLng(lat1,lon1));
	    
	    lat2 = lat + (PRlat * cos( d2r * (azimuth  + width/2 )));
	    lon2 = lng + (PRlng * sin( d2r * (azimuth  + width/2 )));

	    var theta = 0;
	    var gamma = d2r * (azimuth  + width/2 );
		
		for (var a = 1; theta < gamma ; a++ ) {
			 theta = d2r * (azimuth  - width/2 +a);
			 PGlon = lng + (PRlng * sin( theta ));
		     PGlat = lat + (PRlat * cos( theta ));
		     		
			PGpoints.push(new GLatLng(PGlat,PGlon));
		}
	    
	    PGpoints.push( new GLatLng(lat2,lon2));
	    PGpoints.push(centerPoint);
	}   
	    
	 if(Pline>0)
	       var poly = new GPolygon(PGpoints, PGcolor, 1, 1,PGcolor,0.1);
	 else 
	       var poly = new GPolyline(PGpoints, PGcolor, 1, 1);
	    
	    map.addOverlay(poly);  
	  
	  return poly;	
}

function initMap(lng, lat, zoom, mapType) {
  mapDIV.innerHTML = "";

  map = new GMap(document.getElementById("map"));
  map.setMapType(mapType);
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  map.addControl(new GScaleControl());
  map.centerAndZoom(new GPoint(lng, lat), zoom);

 }

function drawCoveragePoint(lat, lon, radius, coverageLevel){
		var color="ffffff";
		if (coverageLevel==1) color=fairLevelColor;
		if (coverageLevel==2) color=goodLevelColor;
		if (coverageLevel==3) color=greatLevelColor;
		return drawPolygon(lon, lat, 36, radius, color, 7, 0.5);

}

function drawInfoPointOnMap(lat, lon, info, icon){

	var point = new GLatLng(lat,lon);
	var pointMarker = new GMarker(point, icon)
	//alert(typeof icon);
	if(typeof icon=="undefined") map.addOverlay(pointMarker);
	else map.addOverlay(pointMarker, icon);
	  GEvent.addListener(pointMarker, "click", function() {
	    pointMarker.openInfoWindowHtml(info);
	  })
}

function windowWidth() {
  if (window.innerWidth) return window.innerWidth;
  if (document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientWidth;
  return document.body.offsetWidth;
  }

function windowHeight() {
  if (window.innerHeight) return window.innerHeight;
  if (document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight;
  return document.body.clientHeight;
  }



var resizeLevel = 0; // prevent recursion in IE
var resizeW;
var resizeH;

function resizeWin(event, isInit, w, h) {
  resizeLevel++;
  if (resizeLevel == 1) {
    var scrnW = windowWidth();
    var scrnH = windowHeight();
    if (isInit || (scrnW != resizeW) || (scrnH != resizeH)) {
      resizeW = scrnW;
      resizeH = scrnH;
      mapDIV.style.width = w + "px";
      mapDIV.style.height = h + "px";
      if (!isInit) {
        showView(-1);
        map.onResize();
        }
      mapPos = jt_getOffsetXY(mapDIV);
      }
    }
  resizeLevel--;
  }

function getAutoZoom(distance){
		var autoZoom =Math.round(2+Math.LOG10E *4.0*Math.log(parseFloat(distance)));
		if(autoZoom<3)  autoZoom=3;
		else if(autoZoom>17 ) autoZoom=17;
		return autoZoom;
}

function zoomMap() {
	map.zoomTo(foSelected(zoomPD));
}

function mapLatLong() {
  map.recenterOrPanToLatLng(new GPoint(fldLng.value, fldLat.value));
  }

function setCrosshair() {
  // center crosshair in map, allowing for width/height of 'crosshair.gif'
  jt_moveTo(crosshairDIV, mapPos.x + Math.round(mapDIV.offsetWidth/2) - 8, mapPos.y + Math.round(mapDIV.offsetHeight/2) - 8);
  }



/**
 * jt_utils.js - Misc. JavaScript utility functions
 *
 * @version 16 Feb 2006
 * @author  Joseph Oster, wingo.com, Copyright (c) 2005-2006
 */

/* GENERIC FUNCTIONS */
String.prototype.trim = function () {
  return this.replace(/^\s+/g, '').replace(/\s+$/g, '');
  }

function jt_ShowHideElm(elm, showIt) {
  if (elm) elm.style.visibility = (showIt) ? "visible" : "hidden";
  }



/********** BEGIN: screen handling **********/
function jt_Point(x, y) {
  // returns a "Point" object with '.x' and '.y' properties
  this.x = x;
  this.y = y;
  }

function jt_getOffsetXY(obj, findID) {
  // returns an object with both '.x' and '.y' offsets of 'obj' relative to 'findID' (or page if 'findID' doesn't exist)
  // usage: "var point = jt_getOffsetXY(obj); var left=point.x; var top=point.y;"
  var xPos = obj.offsetLeft;
  var yPos = obj.offsetTop;
  var parent = obj.offsetParent;
  if (typeof findID == 'undefined') findID = '!@#$%^&*()';
  while ((parent != null) && (parent.id != findID)) {
    xPos += parent.offsetLeft;
    yPos += parent.offsetTop;
    parent = parent.offsetParent;
    }
  return new jt_Point(xPos, yPos);
  }

function jt_moveTo(obj, x, y) {
  // moves 'obj' to x/y coordinates
  obj.style.left = x + "px";
  obj.style.top = y + "px";
  }

/********** END: screen handling **********/
// also see: http://www.mattkruse.com/javascript/anchorposition/source.html

