// JavaScript Document


	var map; //Map being manipulated
	//var mgr; //Marker manager (currently not used)
	var ZoomLevel;
	var postalLookupCode = "22201";
	var radiusMilesLookup = "10";
	var intClientLat = "38.891553";
	var intClientLng = "-77.085048";
	
	var qualityGroupID = 0;
	var certifiedGroupID = 1;
	var NATEGroupID = 3;
	var minGroupLat = 90;
	var maxGroupLat = -90;
	var minGroupLng = 360;
	var maxGroupLng = -360;
	
	
	
	var arrAllPins = new Array();
	
	//Set up our groups of push pins
	arrAllPins[qualityGroupID] = new pushPinGroup("quality", "/maps/images/NATE_Quality_Icon.png", "/maps/images/map-shadow.png", 32, 38, 4, 33, 49, 38, 4, 33);
	arrAllPins[certifiedGroupID] = new pushPinGroup("certified", "/maps/images/NATE_Certifide_Icon.png", "/maps/images/map-shadow.png", 32, 38, 4, 33, 49, 38, 4, 33);
	arrAllPins[NATEGroupID] = new pushPinGroup("NATE", "/maps/images/map-hq-star.png", "/maps/images/map-hq-shadow.png", 24, 26, 12, 13, 37, 29, 12, 13);
		

	//Create a generic icon for the map.  This is extended functionality which is not required.
	var baseIcon = new GIcon();
	baseIcon.shadow = "/maps/images/shadow.png";
	baseIcon.iconSize = new GSize(24, 30);
	baseIcon.shadowSize = new GSize(46, 31);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);

	//********************************************
	//----- Stop page scrolling if wheel over map ----
	//********************************************
	function wheelevent(e)
	{
		if (!e) e = window.event;
		if (e.preventDefault) e.preventDefault();
		e.returnValue = false;
	}

	//********************************************
	//*
	//********************************************
	function pushPinGroup(name, image, shadow, imageWidth, imageHeight, iconAnchorX, iconAnchorY, shadowWidth, shadowHeight, shadowAnchorX, shadowAnchorY) {
		this.reqIS = null;
		this.GroupName = name;
		this.Image = image;
		this.imageWidth = imageWidth;
		this.imageHeight = imageHeight;
		this.iconAnchorX = iconAnchorX;
		this.iconAnchorY = iconAnchorY;
		this.shadowWidth = shadowWidth;
		this.shadowHeight = shadowHeight;
		this.shadowAnchorX  = shadowAnchorX;
		this.shadowAnchorY = shadowAnchorY;
		this.Shadow = shadow;
		this.markers = new Array();    //parallels arrMarkerInfo array
		this.arrMarkerInfo = new Array(); //parallels markers array
		this.displayed = false;
		this.addedToMap = false;
	}
	//********************************************
	//*
	//********************************************
	function objMarker(icon, gPoint, lat, lng, id, company, add1, add2, city, state, zip, phone, email, url, info, intCertPercent, distance, certTechs, moreInfo, specialOffer) {
		this.icon = icon;
		this.point = gPoint;
		this.lat = lat;
		this.lng = lng;
		this.displayed = true; //based on user subType checkbox status checked = displayed
		this.id = id;
		this.company = company;
		//this.subType = subType;
		this.add1 = add1;
		this.add2 = add2;
		this.city = city;
		this.state = state;
		this.zip = zip;
		this.phone = phone;
		this.email = email;
		this.url = url;
		this.info = info;
		this.intCertPercent = intCertPercent;
		this.certTechs = certTechs;
		this.distance = distance;
		this.moreInfo = moreInfo;
		this.specialOffer = specialOffer;
	}
	
	function selectMarker(sectionID, markerID){
		//alert(sectionID + " " + markerID);
		//alert(arrAllPins[sectionID].markers[markerID].name);
		//alert(arrAllPins[sectionID].markers[markerID].id);
		//arrAllPins[sectionID].markers[markerID].click();
		GEvent.trigger(arrAllPins[sectionID].markers[markerID], "click");
	}

	//********************************************
	//*
	//********************************************
	function addPointToMarkerList(arrObjAllMarkers, sectionID, sectionName, lat, lng, id, company, add1, add2, city, state, zip, phone, email, url, info, intCertPercent, distance, certTechs, moreInfo, specialOffer){
		var point = new GLatLng(lat, lng);
		var icon = new GIcon(baseIcon);
		//alert(arrObjAllMarkers);
		//alert(sectionID);
		icon.image = arrObjAllMarkers[sectionID].Image;
		icon.iconSize = new GSize(arrObjAllMarkers[sectionID].imageWidth, arrObjAllMarkers[sectionID].imageHeight);
		icon.iconAnchor = new GPoint(arrObjAllMarkers[sectionID].iconAnchorX, arrObjAllMarkers[sectionID].iconAnchorY);
		icon.shadow = arrObjAllMarkers[sectionID].Shadow;
		icon.shadowSize = new GSize(arrObjAllMarkers[sectionID].shadowWidth, arrObjAllMarkers[sectionID].shadowHeight);
		icon.infoShadowAnchor = new GPoint(arrObjAllMarkers[sectionID].shadowAnchorX, arrObjAllMarkers[sectionID].shadowAnchorY);

		//Add the marker
		var marker = new GMarker(point, {icon:icon, company:company.replace(/<(br) \/+(>)/g, " ")});
		GEvent.addListener(marker, "click", function() {				
			var strHTML1, strHTML2, strAddressOnly, strAddress;
			
            //Track the click
            trackContractorClick(id);

			strAddressOnly = add1;
			if (add2.length > 0){
				strAddressOnly += "<br />" + add2;
			}
			strAddressOnly += "<br />" + city + ", " + state + " " + zip;
			strAddress = strAddressOnly + "<br />" + phone;		

			strHTML1 = 	"<div class='mapPopup'>Ask for a NATE certified technician at...</div><div class='infoWinTitle'>" + company + "</div>" +
						"<div id='infoWinInfo'>" + 
							"<div class='infoWin'>"
						strHTML1 += "<div class='infoWinRight'>" 
					strHTML1 +=     "<div class='infoWinAddress'>" + strAddress + "</div>"
									
					if (url.length > 0){
						strHTML1 += "<div class='infoWinURL'><a href='http://" + url.replace('http://', '') + "' target='_blank'>" + url.replace('http://', '') + "</a></div>"
					}	
					strHTML1 += "<div class='infoWinTCopy'>"
									
					if (email.length > 0) {
						strHTML1 += "<a href='mailto:" + email + "'>" + email + "</a><br />"
					}
					strHTML1 +=		"</div>"
					if (distance != 0){
					strHTML1 += "<div style='clear:both;'>Distance: " + distance + " miles</div>"
					}
					strHTML1 += "</div>" //End WinRight
						if (sectionID == qualityGroupID){
							strHTML1 += "<div class='infoWinLeft'><div class='infoWinTImg'><img src='/maps/images/QC-logo.jpg' /></div></div>"
						}	
                        //"<div style='clear:both; padding-top:5px;'>" + info + "</div>" +	
						strHTML1 += "</div>" +
						"<div class='infoWinActions'>" + 
								"<a onclick='showDirectionsInfoWin();'  class='infoWinLink' style='padding-right:10px;'>Get Directions</a>"

			strHTML1 += "<div style='width:1px; padding-right:30px;  display:inline;'>&nbsp;</div>"
			strHTML1 += " Send to:&nbsp;&nbsp; <a onclick='showEmailInfoWin();' class='infoWinLink'>Email</a> " + 
								"&nbsp;&nbsp; <a onclick='showPhoneInfoWin();' class='infoWinLink'>Phone</a> " + 
							"</div>" +
						"</div>" +
						"<div id='infoWinEmail' style='display:none;'>" + 
							"We'll send this information to your email.<br />Just fill out the following field. " +
							"<form>" +
							"<div style='padding:10px 0px 20px 0px'>Email: <input type='text' id='fldWinEmail' value='' onkeypress='return noenter(event);'></div> " +
							"<div style='text-align:center;'><a onClick='sendInfoWinEmailCheck(\"" + id +"\", 0, getId(\"fldWinEmail\")); ' style='cursor:pointer;'><img src='/maps/images/btn_send.gif' title='Send' border='0' /></a>" +
							"<a onClick='hideEmailInfoWin();' style='padding-left:30px;cursor:pointer;'><img src='/maps/images/btn_back.gif' title='Return to address information' border='0' /></a></div>" +
							"</form>" +
						"</div>" +
						"<div id='infoWinPhone' style='display:none;'>" + 
							"We'll send the address and phone number to your cell phone.<br />Just fill out the following fields. " +
							"<form>" +
							"<div style='padding:10px 0px 5px 0px'>10 Digit Phone #: <input type='text' id='fldInfoWinPhoneNum' value='##########' onClick='if(this.value==\"##########\"){this.value=\"\";}' size='10' maxlength='10' onkeypress='return noenter(event);'></div> " +
							"<div style='padding:5px 0px 20px 0px'>Carrier: <select id='fldInfoWinCarrier' onkeypress='return noenter(event);' ></select></div>" +
							"<div style='text-align:center;'><a onClick='sendInfoWinPhoneCheck(\"" + id +"\", 0);'><img src='/maps/images/btn_send.gif' title='Send' border='0' style='cursor:pointer;' /></a>" +
							"<a onClick='hidePhoneInfoWin();' style='padding-left:30px;cursor:pointer;'><img src='/maps/images/btn_back.gif' title='Return to address information' border='0' /></a></div>" +
							"<div style='font-size:10px; text-align:right;padding-top:10px;color:#aaaaaa;'>Standard messaging fees will apply.</div>" + 
							"</form>" +
						"</div>" +
						"<div id='infoWinDirections' style='display:none;'>" + 									
							"<form name='frmInfoWinDir' action='/maps/directions.aspx' type='post'  target='_blank'>" +
							"<div style='padding:10px 0px 0px 0px'>" + strAddressOnly + "</div> " +
							"<input type='hidden' name='type' value='user' /> " +
							"<input type='hidden' name='strTitle' value='" + company + "' /> " +
							"<input type='hidden' name='strAdd' value='" + add1 + "' /> " +
							"<input type='hidden' name='strAdd2' value='" + add2 + "' /> " +
							"<input type='hidden' name='cityStateZip' value='" + city + " " + state + ", " + zip + "' /> " +
							"<input type='hidden' name='strPhone' value='" + phone + "' /> " +
							"<input type='hidden' name='fromLat' value='" + lat + "' /> " +
							"<input type='hidden' name='fromLng' value='" + lng + "' /> " +
							"<div style='padding:15px 0px 5px 88px'><label><input type='radio' name='radDirection' value='from' checked='checked'> From Address</label>&nbsp;&nbsp;&nbsp;&nbsp;" +
							"<label><input type='radio' name='radDirection' value='to'> To Address</label></div>" +
							"<div style='padding:5px 0px 15px 0px'>Start Address: <input type='text' name='fldUserAddress' id='fldUserAddress' value='Street, City, State Zip' onClick='if(this.value==\"Street, City, State Zip\"){this.value=\"\";}' size='30'></div> " +
							"<div style='text-align:center;'><a onClick='setCookie(\"tp_dirAdd\",getId(\"fldUserAddress\").value, null);document.frmInfoWinDir.submit();' style='cursor:pointer;'><img src='/maps/images/btn_get_directions.gif' title='Get Directions' border='0'  align='middle' /></a>" +
							"<a onClick='hideDirectionsInfoWin();' style='padding-left:30px; cursor:pointer; text-decoration:underline;'><img src='/maps/images/btn_back.gif' border='0' title='Cancel'  align='middle' /></a></div> " +
							"</form>" +
						"</div>" 
						
			
			strHTML2 = "<div class='infoWinTitle'>" + company + "</div><div class='infoWinFullCopy'>TBD [May not be used]</div>"
			
			//Create our tabs
			var infoTabs
			var intNumTabs = 1;
			
			infoTabs = [new GInfoWindowTab("Location", strHTML1)];
			if (moreInfo.length > 0) {
				infoTabs[intNumTabs] = new GInfoWindowTab("More Info", "<div class='infoMore'>" + moreInfo + "</div>");
				++intNumTabs;
			}
			if (specialOffer.length > 0) {
				infoTabs[intNumTabs] = new GInfoWindowTab("Offers", "<div class='infoSpecialOffer' >" +specialOffer + "</div>");
			}
			
			// Our info window content
			marker.openInfoWindowTabsHtml(infoTabs);				
		});
		
		//bkw note that this will put two instances of a map request specifying a member id in the array. I may need to make sure member id does not exist in list before pushing

		
		arrObjAllMarkers[sectionID].markers.push(marker);

		var strMarkerInfo = new objMarker(icon, point, lat, lng, id, company, add1, add2, city, state, zip, phone, email, url, info, intCertPercent, distance, certTechs, moreInfo, specialOffer); 
		arrObjAllMarkers[sectionID].arrMarkerInfo.push(strMarkerInfo);
	}

	//********************************************
	//*
	//********************************************
	function addMarkerGroupToMap(arrObjAllMarkers, sectionID){
		//alert('adding ' + arrObjAllMarkers[sectionID].markers.length + ' markers');
		//mgr.addMarkers(arrObjAllMarkers[sectionID].markers, 5, 17);			
		//refresh and display the markers via the manager
		//mgr.refresh(); 	
				
		for(var i=0;i< arrObjAllMarkers[sectionID].markers.length;i++){
			map.addOverlay(arrObjAllMarkers[sectionID].markers[i])		
		}
		arrObjAllMarkers[sectionID].addedToMap = true;
		
		//document.getElementById("loadStatus").innerHTML = "Loaded marker group containing " + arrObjAllMarkers[sectionID].markers.length + "  markers"
	}

	//********************************************
	//*
	//********************************************
	function centerMapOnBoundingMinMaxLatLngs(minGroupLat, maxGroupLat, minGroupLng, maxGroupLng){
		//For all the pushpins on the map get the bounding box and zoom to the mid point
		var points = new Array();
		points[0] = new GLatLng(minGroupLat, minGroupLng);
		points[1] = new GLatLng(maxGroupLat, maxGroupLng);
		
		var googBounds = new GBounds(points);
		var sw = new GLatLng(googBounds.maxY, googBounds.minX);
		var ne = new GLatLng(googBounds.minY, googBounds.maxX);

		var latLngBounds = new GLatLngBounds(sw, ne);

		map.setZoom(map.getBoundsZoomLevel(latLngBounds));
		map.setCenter(latLngBounds.getCenter());
		
	}

	//********************************************
	//*
	//********************************************
	function populateContractorList(arrObjAllMarkers){
		var strHTML = "";
		var marker;
		
		//Quality Contractors
		for(var i=0;i< arrObjAllMarkers[qualityGroupID].markers.length;i++){
			marker = arrObjAllMarkers[qualityGroupID].arrMarkerInfo[i];
			
			strHTML += "<div class='RNavAddress'><div class='title'>" + marker.company + "<br /><a href='#' onclick='selectMarker(" + qualityGroupID + ", " + i + ");return false;'>View contact info on map</a></div>";
			strHTML += marker.city + ", " + marker.state + " " + marker.zip + "<br />";
            /*
			if (marker.url.length > 0){
				strHTML += "<a href='" + marker.url + "'>" + marker.url + "</a><br />";
            }
            */
			if (marker.certTechs > 0){
			    strHTML += "NATE Certified Technicians: " + marker.certTechs + "<br />";
			}
			strHTML +="Distance: " + marker.distance + " miles </div>";
            // marker.phone + 
		}
		
		document.getElementById("qualContractors").innerHTML = strHTML;
		
		//Other Contractors
		strHTML = "";
		for(var i=0;i< arrObjAllMarkers[certifiedGroupID].markers.length;i++){
			marker = arrObjAllMarkers[certifiedGroupID].arrMarkerInfo[i];
			
			strHTML += "<div class='RNavAddress'><div class='title'>" + marker.company + "<br /><a href='#' onclick='selectMarker(" + certifiedGroupID + ", " + i + ");return false;'>View contact info on map</a></div>";
			strHTML += marker.city + ", " + marker.state + " " + marker.zip + "<br />";
            /*
			if (marker.url.length > 0){
				strHTML += "<a href='" + marker.url + "'>" + marker.url + "</a><br />";
			}
            */
			if (marker.certTechs > 0) {
			    strHTML += "NATE Certified Technicians: " + marker.certTechs + "<br />";
			}
			strHTML +="Distance: " + marker.distance + " miles </div>";
            // marker.phone + 
		}
		document.getElementById("Contractors").innerHTML = strHTML;
	}

	//********************************************
	//********************************************
	//Geocoder functions
	//********************************************
	//********************************************
	var arrTryAddresses;

	//********************************************
	//*
	//********************************************
	var arrGeocoderTryAddresses = new Array(4);
	var intGeocoderAddressCnt = 0;
	var boolGeocoderRunning = false;
	var intGeocoderLat = 0;
	var intGeocoderLng = 0;
	
	function geocodeAddress(add1, add2, city, state, zip) {
		//right now I am not using this set of geocoding functions due to the complications of waiting for google to return the result asyncronously
		
		arrGeocoderTryAddresses[0] = add1 + "," + city + " " + state + " " + zip;
		arrGeocoderTryAddresses[1] = add1 + "," + add2 + "," + city + " " + state + " " + zip;
		arrGeocoderTryAddresses[2] = add2 + "," + city + " " + state + " " + zip;
		arrGeocoderTryAddresses[3] = city + " " + state + " " + zip;
			
		intGeocoderLat = 0;
		intGeocoderLng = 0;
		boolGeocoderRunning = true;
		geocoderAddressTry(0); //kick off the geocode
		//need to wait for the geocoder to do its thing
	}
	function geocoderAddressTry(index) {
		//Get the lat and long. When done, store the lat lng values
		//alert("geocoding address: " + arrGeocoderTryAddresses[index]);
		intGeocoderAddressCnt = index;
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(arrGeocoderTryAddresses[index], geocoderAddressCallBack);
		/*geocoder.getLatLng(arrGeocoderTryAddresses[index],  function (point)
          { alert(arrGeocoderTryAddresses[index]);
		  }
);*/
	}
	function geocoderAddressCallBack(point){
		
		if (!point) {
			if (intGeocoderAddressCnt == arrGeocoderTryAddresses.length){
				//alert("Unable to map address.");
				boolGeocoderRunning = false;
			} else {
				//Try the next address
				intGeocoderAddressCnt++;
				geocoderAddressTry(intGeocoderAddressCnt);
			}
		} else {
			//Point Found
			//update the global vars
			intGeocoderLat = point.lat()
			intGeocoderLng = point.lng();

			//alert("geocoding returned  " + arrGeocoderTryAddresses[intGeocoderAddressCnt] + " (" + intGeocoderLat + ", " + intGeocoderLng + ")" );
			
			boolGeocoderRunning = false;
		}
	}
	
	

