// JavaScript Document 
    var map;
    var geocoder;
	var GLOBALmarkers = [];
	var GLOBALdescriptions = [];
	var GLOBALpolygons = [];
	var GLOBALcommmarkers = [];
	var GLOBALcommdescriptions = [];
	var show_address = true,
    icon = new GIcon(),
    panel = document.getElementById("panel"),
    map, p, marker, inner_html, to_show, geocoder = null,polygon;
	
	var countyDESC = [
	['<h2 class="countyname">McHenry County</h2><div class="commlist"><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/266428,algonquin.static" class="smheads dot" type="static">Algonquin</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/256891,barringtonhills.static" class="smheads dot" type="static">Barrington Hills</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280929,bullvalley.static" class="smheads dot" type="static">Bull Valley</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/266597,cary.static" class="smheads dot" type="static">Cary</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280923,crystallake.static" class="smheads dot" type="static">Crystal Lake</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280927,foxrivergrove.static" class="smheads dot" type="static">Fox River Grove</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280919,greenwood.static" class="smheads dot" type="static">Greenwood</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280928,harvard.static" class="smheads dot" type="static">Harvard</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280915,hebron.static" class="smheads dot" type="static">Hebron</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280917,holidayhills.static" class="smheads dot" type="static">Holiday Hills</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280918,huntley.static" class="smheads dot" type="static">Huntley</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280924,johnsburg.static" class="smheads dot" type="static">Johnsburg</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280925,lakeinthehills.static" class="smheads dot" type="static">Lake in the Hills</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280920,lakemoor.static" class="smheads dot" type="static">Lakemoor</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280921,lakewood.static" class="smheads dot" type="static">Lakewood</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/540957,luxury04.static" class="smheads dot" type="static">Luxury homes still a hot buy</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280926,marengo.static" class="smheads dot" type="static">Marengo</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280914,mccullomlake.static" class="smheads dot" type="static">McCullom Lake</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280916,mchenry.static" class="smheads dot" type="static">McHenry</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280922,oakwoodhills.static" class="smheads dot" type="static">Oakwood Hills</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280931,prairiegrove.static" class="smheads dot" type="static">Prairie Grove</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280933,richmond.static" class="smheads dot" type="static">Richmond</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280930,ringwood.static" class="smheads dot" type="static">Ringwood</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/265139,springgrove.static" class="smheads dot" type="static">Spring Grove</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280932,troutvalley.static" class="smheads dot" type="static">Trout Valley</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/277360,union.static" class="smheads dot" type="static">Union</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280935,wonderlake.static" class="smheads dot" type="static">Wonder Lake</a><a href="http://searchchicago.suntimes.com/homes/communities/mchenry/280934,woodstock.static" class="smheads dot" type="static">Woodstock</a></div>']
	];
icon.iconSize = new GSize(13, 21);
//icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
//icon.shadowSize = new GSize(28, 25);
icon.iconAnchor = new GPoint(7, 22);
icon.infoWindowAnchor = new GPoint(7, 2);
icon.infoShadowAnchor = new GPoint(13, 19);


function initialize() {
	
	
      if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("map_canvas"));
      map.setCenter(new GLatLng(41.700492, -87.923348), 7);
         // map.addOverlay(new GMarker(new GLatLng(42.326221, -88.267336)));
	  map.addControl(new GLargeMapControl());
	  map.addControl(new GMapTypeControl());
	  //map.addControl(new GOverviewMapControl());
	  //map.addControl(new google.maps.LocalSearch(), new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(10,10)));
	  gLocalSearch = new GlocalSearch();
      //gLocalSearch.setSearchCompleteCallback(null, OnLocalSearch);
	  
	  //svOverlay = new GStreetviewOverlay();
	  
      }
	  

  
//draw_points('mchenry','#ffcc00',countyDESC[0][0]);
//draw_points('lake','#3399ff',"Lake");//1
//draw_points('kane','#00cc66',"Kane");//2
//draw_points('kendall','#cc6600',"Kendall");//3
//draw_points('grundy','#660000',"mchenry");//4
//draw_points('dupage','#000066',"mchenry");//5
//draw_points('kankakee','#006600',"mchenry");//6
//draw_points('cook','#cc33ff',"Cook");//7
//draw_points('will','#ffcc33',"Will");//8
//draw_points('lakein','#555555',"mchenry");//9
//draw_points('porter','#006500',"porter");//10
    }
var marker;
var overlayInstance = null;
var map;
var client;
var lastMarkerLocation;
var panorama;
var mapT;
var currentYaw = 180;
var currentPitch = 0;
var timer;
var currentZoom = 0;
function dropguy(latlng){
	map.getInfoWindow().hide();
	if (marker) map.removeOverlay(marker);
	client = new GStreetviewClient();
	var latlng = map.getCenter();
	
	var guyIcon = new GIcon(G_DEFAULT_ICON);
  guyIcon.image = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-0.png";
  guyIcon.transparent = "http://maps.google.com/intl/en_us/mapfiles/cb/man-pick.png";
  guyIcon.imageMap = [26,13, 30,14, 32,28, 27,28, 28,36, 18,35, 18,27, 16,26, 16,20, 16,14, 19,13, 22,8];
  
  guyIcon.iconSize = new GSize(49, 52);
  guyIcon.iconAnchor = new GPoint(25, 35);  // near base of guy's feet
  guyIcon.infoWindowAnchor = new GPoint(25, 5);  // top of guy's head

  marker = new GMarker(latlng, {icon: guyIcon, draggable: true});
  map.addOverlay(marker);
  lastMarkerLocation = latlng;
  GEvent.addListener(marker, "dragend", onDragEnd);
  GEvent.addListener(marker, "click", openPanoramaBubble);
  //GEvent.addListener(panorama, "yawchanged", onYawChange);
 }

function openPanoramaBubble() {
	timer = window.setInterval(spiral, 200);


  var contentNode = document.createElement('div');
  contentNode.style.textAlign = 'center';
  contentNode.style.width = '500px';
  contentNode.style.height = '300px';
  contentNode.innerHTML = 'Loading panorama';
  marker.openInfoWindow("<a href='#' onClick='dizzy();'>I'm Dizzy</a> | You can drag the picture to look up or down<br><br><div id='pano' style='width:400px;height:300px;'></div><br><br><a href='#' onClick='toggleOverlay();'>Click here to see which sections of the map have a steet view available.</a>", {maxContent: contentNode, maxTitle: "Full screen"});

  panorama = new GStreetviewPanorama(document.getElementById("pano"));
  panorama.setLocationAndPOV(marker.getLatLng(), {yaw: currentYaw, pitch: currentPitch, zoom: currentZoom});
  GEvent.addListener(panorama, "newpano", onNewLocation);
  GEvent.addListener(panorama, "yawchanged", onYawChange); 
  GEvent.addListener(panorama, "initialized", function(loc) {
          panoMetaData = loc;
          moveGuy();
		  });

  var iw = map.getInfoWindow();
  GEvent.addListener(iw, "maximizeend", function() {
    panorama.setContainer(contentNode);  
    window.setTimeout("panorama.checkResize()", 5);
  });
}

function spiral() {
  currentYaw += 2;
  panorama.panTo({yaw:currentYaw, pitch:currentPitch});
}

function dizzy(){
	clearInterval(timer);
}

function onYawChange(newYaw) {
  var GUY_NUM_ICONS = 16;
  var GUY_ANGULAR_RES = 360/GUY_NUM_ICONS;
  if (newYaw < 0) {
    newYaw += 360;
  }
  guyImageNum = Math.round(newYaw/GUY_ANGULAR_RES) % GUY_NUM_ICONS;
  guyImageUrl = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-" + guyImageNum + ".png";
  marker.setImage(guyImageUrl);
}
function moveGuy(latlng){
	currentLatLng = panoMetaData.latlng;
    marker.setLatLng(currentLatLng);
	marker.GInfoWindow.reset(currentLatLng)
    //map.panTo(currentLatLng);
}

function onNewLocation(lat, lng) {
  var latlng = new GLatLng(lat, lng);
  marker.setLatLng(currentLatLng);
}

function onDragEnd() {
  var latlng = marker.getLatLng();
  if (panorama) {
    client.getNearestPanorama(latlng, onResponse);
  }
}

function onResponse(response) {
  if (response.code != 200) {
    marker.setLatLng(lastMarkerLocation);
  } else {
    var latlng = new GLatLng(response.Location.lat, response.Location.lng);
    marker.setLatLng(latlng);
    lastMarkerLocation = latlng;
    openPanoramaBubble();
  }
}

function toggleOverlay() {
  if (!overlayInstance) {
    overlayInstance = new GStreetviewOverlay();
    map.addOverlay(overlayInstance);
  } else {
    map.removeOverlay(overlayInstance);
    overlayInstance = null;
  }
}
