//<![CDATA[

    var iconBlue = new google.maps.Icon(); 
    iconBlue.image = 'http://www.paradisevalleyliving.com/wp-content/files/markers/spa.png';
    iconBlue.shadow = 'http://www.paradisevalleyliving.com/wp-content/files/markers/shadow_sm_sq.png';
    iconBlue.iconSize = new google.maps.Size(42, 33);
    iconBlue.shadowSize = new google.maps.Size(42, 33);
    iconBlue.iconAnchor = new google.maps.Point(30, 26);
    iconBlue.infoWindowAnchor = new google.maps.Point(10, 13);

    var iconRed = new google.maps.Icon(); 
    iconRed.image = 'http://www.paradisevalleyliving.com/wp-content/files/markers/resort.png';
    iconRed.shadow = 'http://www.paradisevalleyliving.com/wp-content/files/markers/shadow_lg_sq.png';
    iconRed.iconSize = new google.maps.Size(52, 32);
    iconRed.shadowSize = new google.maps.Size(52, 32);
    iconRed.iconAnchor = new google.maps.Point(46, 26);
    iconRed.infoWindowAnchor = new google.maps.Point(23, 12);
	
	var iconGreen = new google.maps.Icon(); 
    iconGreen.image = 'http://www.paradisevalleyliving.com/wp-content/files/markers/golf.png';
    iconGreen.shadow = 'http://www.paradisevalleyliving.com/wp-content/files/markers/mm_20_shadow.png';
    iconGreen.iconSize = new google.maps.Size(22, 28);
    iconGreen.shadowSize = new google.maps.Size(22, 20);
    iconGreen.iconAnchor = new google.maps.Point(11, 28);
    iconGreen.infoWindowAnchor = new google.maps.Point(11, 8);
	
	var iconYellow = new google.maps.Icon(); 
    iconYellow.image = 'http://www.paradisevalleyliving.com/wp-content/files/markers/el-school.png';
    iconYellow.shadow = 'http://www.paradisevalleyliving.com/wp-content/files/markers/shadow_lg_sq.png';
    iconYellow.iconSize = new google.maps.Size(52, 32);
    iconYellow.shadowSize = new google.maps.Size(52, 32);
    iconYellow.iconAnchor = new google.maps.Point(46, 26);
    iconYellow.infoWindowAnchor = new google.maps.Point(23, 12);
	
	var iconOrange = new google.maps.Icon(); 
    iconOrange.image = 'http://www.paradisevalleyliving.com/wp-content/files/markers/mid-school.png';
    iconOrange.shadow = 'http://www.paradisevalleyliving.com/wp-content/files/markers/shadow_lg_sq.png';
    iconOrange.iconSize = new google.maps.Size(52, 32);
    iconOrange.shadowSize = new google.maps.Size(52, 32);
    iconOrange.iconAnchor = new google.maps.Point(46, 26);
    iconOrange.infoWindowAnchor = new google.maps.Point(23, 12);
	
	var iconBrown = new google.maps.Icon(); 
    iconBrown.image = 'http://www.paradisevalleyliving.com/wp-content/files/markers/hi-school.png';
    iconBrown.shadow = 'http://www.paradisevalleyliving.com/wp-content/files/markers/shadow_lg_sq.png';
    iconBrown.iconSize = new google.maps.Size(52, 32);
    iconBrown.shadowSize = new google.maps.Size(52, 32);
    iconBrown.iconAnchor = new google.maps.Point(46, 26);
    iconBrown.infoWindowAnchor = new google.maps.Point(23, 12);

	var iconWhite = new google.maps.Icon(); 
    iconWhite.image = 'http://www.paradisevalleyliving.com/wp-content/files/markers/priv-school.png';
    iconWhite.shadow = 'http://www.paradisevalleyliving.com/wp-content/files/markers/shadow_lg_sq.png';
    iconWhite.iconSize = new google.maps.Size(52, 32);
    iconWhite.shadowSize = new google.maps.Size(52, 32);
    iconWhite.iconAnchor = new google.maps.Point(46, 26);
    iconWhite.infoWindowAnchor = new google.maps.Point(23, 12);

	var iconPurple = new google.maps.Icon(); 
    iconPurple.image = 'http://www.paradisevalleyliving.com/wp-content/files/markers/m-theater.png';
    iconPurple.shadow = 'http://www.paradisevalleyliving.com/wp-content/files/markers/shadow_lg_sq.png';
    iconPurple.iconSize = new google.maps.Size(52, 32);
    iconPurple.shadowSize = new google.maps.Size(52, 32);
    iconPurple.iconAnchor = new google.maps.Point(46, 26);
    iconPurple.infoWindowAnchor = new google.maps.Point(23, 12);

	var iconGray = new google.maps.Icon(); 
    iconGray.image = 'http://www.paradisevalleyliving.com/wp-content/files/markers/animal-shelter.png';
    iconGray.shadow = 'http://www.paradisevalleyliving.com/wp-content/files/markers/shadow_sm_sq.png';
    iconGray.iconSize = new google.maps.Size(42, 33);
    iconGray.shadowSize = new google.maps.Size(42, 33);
    iconGray.iconAnchor = new google.maps.Point(30, 26);
    iconGray.infoWindowAnchor = new google.maps.Point(10, 13);

    var iconDining = new google.maps.Icon(); 
    iconDining.image = 'http://www.paradisevalleyliving.com/wp-content/files/markers/dining.png';
    iconDining.shadow = 'http://www.paradisevalleyliving.com/wp-content/files/markers/shadow_lg_sq.png';
    iconDining.iconSize = new google.maps.Size(52, 32);
    iconDining.shadowSize = new google.maps.Size(52, 32);
    iconDining.iconAnchor = new google.maps.Point(46, 26);
    iconDining.infoWindowAnchor = new google.maps.Point(23, 12);
	
	var iconHospital = new google.maps.Icon(); 
    iconHospital.image = 'http://www.paradisevalleyliving.com/wp-content/files/markers/hospital.png';
    iconHospital.shadow = 'http://www.paradisevalleyliving.com/wp-content/files/markers/shadow_sm_sq.png';
    iconHospital.iconSize = new google.maps.Size(42, 33);
    iconHospital.shadowSize = new google.maps.Size(42, 33);
    iconHospital.iconAnchor = new google.maps.Point(30, 26);
    iconHospital.infoWindowAnchor = new google.maps.Point(10, 13);
	
	var iconChurch = new google.maps.Icon(); 
    iconChurch.image = 'http://www.paradisevalleyliving.com/wp-content/files/markers/church.png';
    iconChurch.shadow = 'http://www.paradisevalleyliving.com/wp-content/files/markers/shadow_sm_sq.png';
    iconChurch.iconSize = new google.maps.Size(42, 33);
    iconChurch.shadowSize = new google.maps.Size(42, 33);
    iconChurch.iconAnchor = new google.maps.Point(30, 26);
    iconChurch.infoWindowAnchor = new google.maps.Point(10, 13);
	
	var iconAirport = new google.maps.Icon(); 
    iconAirport.image = 'http://www.paradisevalleyliving.com/wp-content/files/markers/airport.png';
    iconAirport.shadow = 'http://www.paradisevalleyliving.com/wp-content/files/markers/shadow_sm_sq.png';
    iconAirport.iconSize = new google.maps.Size(42, 33);
    iconAirport.shadowSize = new google.maps.Size(42, 33);
    iconAirport.iconAnchor = new google.maps.Point(30, 26);
    iconAirport.infoWindowAnchor = new google.maps.Point(10, 13);
	
	var iconPark = new google.maps.Icon(); 
    iconPark.image = 'http://www.paradisevalleyliving.com/wp-content/files/markers/park.png';
    iconPark.shadow = 'http://www.paradisevalleyliving.com/wp-content/files/markers/shadow_lg_sq.png';
    iconPark.iconSize = new google.maps.Size(52, 32);
    iconPark.shadowSize = new google.maps.Size(52, 32);
    iconPark.iconAnchor = new google.maps.Point(46, 26);
    iconPark.infoWindowAnchor = new google.maps.Point(23, 12);

    var customIcons = [];
    customIcons["resort"] = iconRed;
	customIcons["spa"] = iconBlue;
    customIcons["golf"] = iconGreen;
	customIcons["el-school"] = iconYellow;
	customIcons["mid-school"] = iconOrange;
	customIcons["hi-school"] = iconBrown;
	customIcons["priv-school"] = iconWhite;
	customIcons["m-theater"] = iconPurple;
	customIcons["animal-shelter"] = iconGray;
	customIcons["dining"] = iconDining;
	customIcons["hospital"] = iconHospital;
	customIcons["church"] =iconChurch;
	customIcons["airport"] =iconAirport;
	customIcons["park"] =iconPark;
	
	var map;
	var bounds = new google.maps.LatLngBounds();
	var markerArray;
	var polygonArray;
	var polygonNames = new Array();
	var radiusInMiles = 2;
	var milesPerLat = 69.047;
	var milesPerLng = 53;
	var files = new Array("pv", "apachejunction", "cavecreek", "chandler", "fountainhills", "gilbert", "glendale", "goodyear", "mesa", "peoria", "rioverde", "scottsdale", "phoenix", "suncity", "surprise", "tempe");
	var cityBoxArray;


//Random background color for polygons
function genHex(){
	colors = new Array(14)
	colors[0]="0"
	colors[1]="1"
	colors[2]="2"
	colors[3]="3"
	colors[4]="4"
	colors[5]="5"
	colors[5]="6"
	colors[6]="7"
	colors[7]="8"
	colors[8]="9"
	colors[9]="a"
	colors[10]="b"
	colors[11]="c"
	colors[12]="d"
	colors[13]="e"
colors[14]="f"

digit = new Array(5)
color=""
	for (i=0;i<6;i++){
		digit[i]=colors[Math.round(Math.random()*14)]
		color = color+digit[i]
	}
  return color;
}

var highlightCircle;
var currentMarker;

function load() {
	jQuery(window).bind("unload", google.maps.Unload); // guard against leaks
      if (google.maps.BrowserIsCompatible()) {
        map = new google.maps.Map2(document.getElementById("map"));
		map.addControl(new google.maps.SmallMapControl());
        map.addControl(new google.maps.MapTypeControl());
		map.addControl(new google.maps.OverviewMapControl());
        //map.setCenter(new google.maps.LatLng(33.531092,-111.940384), 10);


		//Getting polygons for the cities
	 	polygonArray = new Array();
		var cityname = "Paradise Valley";
	    var filename = cityname.replace(/ /, "").toLowerCase();
	    google.maps.DownloadUrl("http://www.paradisevalleyliving.com/wp-content/files/xml/" + filename + "-city.xml", function(data) {
          var poly = google.maps.Xml.parse(data);
          var marker = poly.documentElement.getElementsByTagName("marker");
          var polygons = poly.documentElement.getElementsByTagName("polygon");
		  var info = poly.documentElement.getElementsByTagName("info");
		  var city = info[0].getAttribute("zip");
		  

			pline = new Array();
			for (var p = 0; p < polygons.length; p++) {		 
			 //alert("Number of lines in "+ p + " polygon : " + polygons[p].getElementsByTagName("line").length);
		  		
				for (var z = 0; z < polygons[p].getElementsByTagName("line").length; z++) {
					pline.push(new google.maps.LatLng(parseFloat(polygons[p].getElementsByTagName("line")[z].getAttribute("lat")),
                    parseFloat(polygons[p].getElementsByTagName("line")[z].getAttribute("lng"))));
				}
				
				polygon = new google.maps.Polygon(pline, "#f33f00",  1,  1, "#"+genHex(),  0.2);
				polygon.city = city;
				polygonArray.push(polygon);
				polygonNames.push(city);
				//polygon.hide();
				map.addOverlay(polygon);
				//map.setCenter(polygon.getBounds().getCenter(), map.getZoom());
				map.setCenter(polygon.getBounds().getCenter(), map.getBoundsZoomLevel(polygon.getBounds()));
	 		}
		 }); 
		
				
		//Getting marker points for the places of interest
		markerArray = new Array();

        google.maps.DownloadUrl("http://www.paradisevalleyliving.com/wp-content/files/xml/markerdata2.xml", function(data) {
          var xml = google.maps.Xml.parse(data);
          var markers = xml.documentElement.getElementsByTagName("marker");		  
		  
		  for (var i = 0; i < markers.length; i++) {
            var name = markers[i].getAttribute("name");
            var address = markers[i].getAttribute("address");
			var phone = markers[i].getAttribute("phone");
			var url = markers[i].getAttribute("url");
            var type = markers[i].getAttribute("type");
			var city = markers[i].getAttribute("city");
  		    if (city == null){ city = "n/a";}
			var link_title = markers[i].getAttribute("link_title");
			if(link_title != ''){
			  var link_title = link_title;
			}else{
			  var link_title ='Visit Web site';
			}
			
			if(url != ''){
			  var hyperlink = '<a href="' + url +'" target="_blank">' + link_title + '</a>';
			}else{
			  var hyperlink ='';
			}
			var point = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")),
                                    parseFloat(markers[i].getAttribute("lng")));
			  var marker = createMarker(point, name, address, phone, hyperlink, type, city);
			  	
			  marker.hide();		  
			  bounds.extend(point);
          }
        });
      }
}

function highlightCurrentMarker(){
      var markerPoint = currentMarker.getPoint();

      var polyPoints = Array();

      if (highlightCircle) {
        map.removeOverlay(highlightCircle);
      }

      var mapNormalProj = G_NORMAL_MAP.getProjection();
      var mapZoom = map.getZoom();
      var clickedPixel = mapNormalProj.fromLatLngToPixel(markerPoint, mapZoom);

      var polySmallRadius = 20;

      var polyNumSides = 20;
      var polySideLength = 18;

      for (var a = 0; a<(polyNumSides+1); a++) {
	    var aRad = polySideLength*a*(Math.PI/180);
	    var polyRadius = polySmallRadius; 
       	    var pixelX = clickedPixel.x + polyRadius * Math.cos(aRad);
	    var pixelY = clickedPixel.y + polyRadius * Math.sin(aRad);
	    var polyPixel = new google.maps.Point(pixelX,pixelY);
	    var polyPoint = mapNormalProj.fromPixelToLatLng(polyPixel,mapZoom);
	    polyPoints.push(polyPoint);
      }
      // Using google.maps.Polygon(points,  strokeColor?,  strokeWeight?,  strokeOpacity?,  fillColor?,  fillOpacity?)
      highlightCircle = new google.maps.Polygon(polyPoints,"#000000",2,0.0,"#FF0000",.3);
      map.addOverlay(highlightCircle);
   }


	function cityintoarray(city){
		var prepare = city.replace(/, /gi, ",");
		var cityArray = prepare.split(",");
		return cityArray;
	}

function showMarker(type) {
   var html ="";
   var items = checkCityBox();
   var bounds = new google.maps.LatLngBounds();
   //var types = checkMarkerBox();
  // types.push(type);
   //html += "<p>Array Length: " + items.length + "</p>";
   //for(var v=0; v<types.length; v++){
		
		for (var i=0; i<markerArray.length; i++) {
          for (var z=0; z<items.length; z++) {
		  	if (markerArray[i].type == type && markerArray[i].city == items[z]) {
            	markerArray[i].show();
				//Outputs City Names for testing purposes
				//html += "City: " + markerArray[i].city + "<br />";
				bounds.extend(markerArray[i].getLatLng());
          	}
		  }
       }
//	}
	//map.setCenter(bounds.getCenter(), map.getZoom());
	map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
	makeList(type);
	//testing output
	//document.getElementById("marker_list").innerHTML = html;
}



// == hides all markers of a particular category, and ensures the checkbox is cleared ==
function hideMarker(type) {
   var html ="";
   var items = checkCityBox();
   //alert(items[0]+"  - " + items[1] + " - " + items[2]);
		for (var i=0; i<markerArray.length; i++) {
	//	  for (var f=0; f<items.length; f++) {
		  	if (markerArray[i].type == type) {
				markerArray[i].hide();
				//html += "Don't Remove: ["+ markerArray[i].city +"] [" + items[f] + "]<br />";

          	}
	//	  }
    }
	document.getElementById(type+"_marker_list").innerHTML = html;
		
        // == close the info window, in case its open on a marker that we just hid
        map.closeInfoWindow();
}

//Check if a value is in array
Array.prototype.has=function(v,i){
  for (var j=0;j<this.length;j++){
	if (this[j]==v) return (!i ? true : j);
  }
	return false;
}
//Hide markers withing hidden polygon
function hideMarkerInPolygon(type) {
   var html ="";
   var items = checkCityBox();
   //alert(items[0]+"  - " + items[1] + " - " + items[2]);
		for (var i=0; i<markerArray.length; i++) {
	//	  for (var f=0; f<items.length; f++) {
		  	if (markerArray[i].type == type && !items.has(markerArray[i].city)) {
				markerArray[i].hide();
				//html += "Don't Remove: ["+ markerArray[i].city +"] [" + items[f] + "]<br />";
          	}
	//	  }
    }
	//document.getElementById(type+"_marker_list").innerHTML = "Select a city";
	
		
        // == close the info window, in case its open on a marker that we just hid
        map.closeInfoWindow();
}
 
      function boxclick(box,type) {
		markerBoxArray = checkMarkerBox();
  	    cityBoxArray = checkCityBox();
		if (box.checked == 1) {
		  if(box.name == "city"){
		    if(!polygonNames.has(type)){
				loadPolygon(type);
			}
		  	showPolygon(type);
			  for(var z=0; z<markerBoxArray.length; z++){
				showMarker(markerBoxArray[z]);
			  }
		  }else{
		    for(var x=0; x<markerBoxArray.length; x++){
				showMarker(markerBoxArray[x]);
			  }
			//makeList(type);
		  }
        } else {
          if(box.name == "city"){
		  	hidePolygon(type);
			for(var x=0; x<markerBoxArray.length; x++){
				hideMarkerInPolygon(markerBoxArray[x]);
				makeList(markerBoxArray[x]);
			  }
		  }else{
		    hideMarker(type);
			//makeList(type);
		  }
        }
		
		//document.getElementById("markers").innerHTML = "<b>Categories in Array :</b>" + markerBoxArray.length;
		//document.getElementById("polygons").innerHTML = "<b>Cities in Array :</b>" + cityBoxArray.length;
      }
  
	function checkCityBox(){
	cityBoxArray = new Array();
	var allCities = new Array();
	  var cb = document.getElementsByName("city");
		  for (var g=0; g<cb.length; g++){
		  	allCities.push(cb[g].value);
		    if (cb[g].checked == 1){
			  cityBoxArray.push(cb[g].value);
			}
		  }
	  	//If none of the cities checked - return all cities
		//if(cityBoxArray.length < 1 ){
		//  cityBoxArray = allCities;
		//}
		
	  return cityBoxArray;
	}
	
	function checkMarkerBox(){
	markerBoxArray = new Array();
	  var mb = document.getElementsByName("marker");
		  for (var g=0; g<mb.length; g++){
		    if (mb[g].checked == 1){
			  markerBoxArray.push(mb[g].value);
			}
		  }

	  return markerBoxArray;
	}
	  
	 function myclick(i) {
        //google.maps.Event.trigger(markerArray[i],"click");
		map.setCenter(markerArray[i].getLatLng(), 14);
      } 
	  
	 function makeList(type) {
        var html = "";
	  var cityBoxArray = checkCityBox();	
	  var bounds = new google.maps.LatLngBounds();
	  if(cityBoxArray.length > 0){
		html = '<ul style="height: auto; max-height: 200px; overflow: auto; font-size: 9px;">';
		for (var i=0; i<markerArray.length; i++) {
          if (markerArray[i].type == type && !markerArray[i].isHidden()) {
			html += '<li><a href="javascript:myclick(' + i + ')" onMouseOver="doHighlight('+ i +')">' + markerArray[i].name + '</a></li>';
		  }
        }
		html += '</ul>';
	  }else{ html = "<ul><li>Select a City -></li></ul>";}
		document.getElementById(type+"_marker_list").innerHTML = html;
     }

function loadPolygon(cityname){
	//for (var c = 0; c < files.length; c++){ 
	var filename = cityname.replace(/ /, "").toLowerCase();
	    google.maps.DownloadUrl("xml/" + filename + "-city.xml", function(data) {
          var poly = google.maps.Xml.parse(data);
          var marker = poly.documentElement.getElementsByTagName("marker");
          var polygons = poly.documentElement.getElementsByTagName("polygon");
		  var info = poly.documentElement.getElementsByTagName("info");
		  var city = info[0].getAttribute("zip");
		  

			pline = new Array();
			for (var p = 0; p < polygons.length; p++) {		 
			 //alert("Number of lines in "+ p + " polygon : " + polygons[p].getElementsByTagName("line").length);
		  		
				for (var z = 0; z < polygons[p].getElementsByTagName("line").length; z++) {
					pline.push(new google.maps.LatLng(parseFloat(polygons[p].getElementsByTagName("line")[z].getAttribute("lat")),
                    parseFloat(polygons[p].getElementsByTagName("line")[z].getAttribute("lng"))));
				}
				
				polygon = new google.maps.Polygon(pline, "#f33f00",  1,  1, "#"+genHex(),  0.2);
				polygon.city = city;
				polygonArray.push(polygon);
				polygonNames.push(city);
				//polygon.hide();
				map.addOverlay(polygon);
				//map.setCenter(polygon.getBounds().getCenter(), map.getZoom());
				map.setCenter(polygon.getBounds().getCenter(), map.getBoundsZoomLevel(polygon.getBounds()));
	 		}
		 }); 
		  //document.getElementById("paradisevalley-city").checked = true;
		//}
}	  

function doHighlight(i){
	google.maps.Event.trigger(markerArray[i],"mouseover");
}

    function createMarker(point, name, address, phone, hyperlink, type, city) {
     var cities = new Array();
	 cities = cityintoarray(city);
	 for(var i=0; i<cities.length; i++){
	  var marker = new google.maps.Marker(point, customIcons[type]);
      if(i==0){
	    marker.name = name;
	  }else{
	    marker.name = name + "<i>(Near "+cities[i]+")</i>";
	  }
	  marker.type = type;
	  marker.city = cities[i];
	  marker.point = point;
	  markerArray.push(marker);
	  var html = "<b>" + name + "</b> <br/>" + address + "<p>" + phone + "</p>" + "<p>" + hyperlink + "</p>";
      google.maps.Event.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html, {maxWidth:300});
      });
	   map.addOverlay(marker);
	   marker.hide();
	  }
	  google.maps.Event.addListener(marker, "mouseover", function() {
			  	currentMarker = marker;
		  		highlightCurrentMarker();	
          	  });
	  return marker;
    }
	
	
/*	*/
	function showPolygon(city) {
	    for (var i=0; i<polygonArray.length; i++) {
          if (polygonArray[i].city == city) {
			polygonArray[i].show();
			map.setCenter(polygonArray[i].getBounds().getCenter(), map.getZoom());
			//map.setCenter(polygonArray[i].getBounds().getCenter(), map.getBoundsZoomLevel(polygonArray[i].getBounds()));
          }
        }
		
    }	
	
	function hidePolygon(city) {
	    for (var i=0; i<polygonArray.length; i++) {
          if (polygonArray[i].city == city) {
            polygonArray[i].hide();
          }
        }
    }

    //]]>