﻿// JScript File
	  // addAddressToMap() is called when the geocoder returns an
    // answer.  It adds a marker to the map with an open info window
    // showing the nicely formatted version of the address and the country code.
    function addAddressToMap(response,additionalHTML) {
     // map.clearOverlays();
      if (!response || response.Status.code != 200) 
      {
      if(response.Status.code=="620"){
				//alert("Bad address" + address);
      //alert(address);
      //alert(response.context);
			//	geocoder.getLocations(address,function(Response){Response.context = response.context ; addAddressToMap(Response, additionalHTML, address);});
      }
      //else
       // alert("Sorry, we were unable to geocode that address.\r\n Status Code:" + response.Status.code);
      } 
      else {
      
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
				//alert(place.address);
			switch(response.context)
						{
						case "RetailersAddress":
              var marker = createMarker(point, additionalHTML,"bulb");
							retailersItems.push(marker);
							break;    
						case "EventsAddress":
              var marker = createMarker(point, additionalHTML,"events");
              marker.hide();
							eventsItems.push(marker);
							break;
						case "RecycleAddress":
              var marker = createMarker(point, additionalHTML,"recycle");
              marker.hide();
							recycleItems.push(marker);
							break;
						case "BulbAddress":
              var marker = createMarker(point, additionalHTML,"retailers");
              marker.hide();
							bulbItems.push(marker);
							break;						
						default:
						}
			
			map.addOverlay(marker);
			//alert(response.context);
			//return marker;
			/*
							marker = new GMarker(point);
							map.addOverlay(marker);
			//        marker.openInfoWindowHtml(place.address + '<br>' +
			//          '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
						//add listener;
						 GEvent.addListener(marker, "click", function() {
											marker.openInfoWindowHtml(place.address + '<br>' +
								'<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
				});
			*/
      }
    }
    
  function addGeoPointToMap(x,y,addressType,additionalHTML)
  {
       point = new GLatLng(x, y);
				//alert(place.address);
			switch(addressType)
						{
						case "RetailersAddress":
              var marker = createMarker(point, additionalHTML,"bulb");
              marker.hide();
							retailersItems.push(marker);
							break;    
						case "EventsAddress":
              var marker = createMarker(point, additionalHTML,"events");
              marker.hide();
							eventsItems.push(marker);
							break;
						case "RecycleAddress":
              var marker = createMarker(point, additionalHTML,"recycle");
              marker.hide();
							recycleItems.push(marker);
							break;
						case "BulbAddress":
              var marker = createMarker(point, additionalHTML,"retailers");
              marker.hide();
							bulbItems.push(marker);
							break;						
						default:
						}
			
			map.addOverlay(marker);
  
  }
  
  function SetCenterPoint(response, defaultZoom)
  {
   if (!response || response.Status.code != 200) 
   {
   }
   else
   {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		
		map.setCenter(point, defaultZoom);
	 }
  }
  
	function CreateIcon(iconName) {
      if (iconName !="") {
        var Icon = new GIcon();
        Icon.image = "images/icons/" + iconName + ".png";
        Icon.iconAnchor = new GPoint(16, 16);
        Icon.infoWindowAnchor = new GPoint(16, 0);
        Icon.iconSize = new GSize(40, 38);
       // Icon.shadow = "images/icons/shadow-"  + iconName + ".png";
        //Icon.shadowSize = new GSize(40, 38);
        //iconName = icon;
      }
       markerOptions = { icon:Icon };
      return markerOptions;
    }
	
	function createMarker(point,html,IconName) {
	  var marker = new GMarker(point,CreateIcon(IconName));
	  GEvent.addListener(marker, "click", function() {

	  var infoHtml = html + '</p><p class="directions">Get directions:<a href="http://maps.google.com/?q=to+' + marker.getLatLng().lat() + '+' + marker.getLatLng().lng() +'&iwloc=ddw1&om=1&f=d" target=_blank>To here</a></p></div>';
	  
	    marker.openInfoWindowHtml(infoHtml);
	  });
	  
	  return marker;
	}

function HideAll(){
	
	arrayAction(retailersItems,"hide");
	arrayAction(eventsItems,"hide");
	arrayAction(recycleItems,"hide");
	arrayAction(bulbItems,"hide");
	map.closeInfoWindow();
	document.getElementById('liRetailers').className ="";
	document.getElementById('liEvents').className ="";
	document.getElementById('liRecycle').className ="";
	document.getElementById('liBulb').className ="";
	
	document.getElementById('imgRetailers').src="images/one.gif";
	document.getElementById('imgEvents').src ="images/two.gif";
	document.getElementById('imgRecycle').src ="images/three.gif";
	document.getElementById('imgBulb').src ="images/four.gif";
}

function ShowAll(){
	//alert('test');
	arrayAction(retailersItems,"show");
	arrayAction(eventsItems,"show");
	arrayAction(recycleItems,"show");
	arrayAction(bulbItems,"show");
}

function ShowEvents(){
	HideAll();
	arrayAction(eventsItems,"show");
	document.getElementById('liEvents').className ="active";
		document.getElementById('imgEvents').src ="images/two_over.gif";
}

function ShowRetailers(){
//alert('hide all');
	HideAll();
	arrayAction(retailersItems,"show");
	document.getElementById('liRetailers').className ="active";
	document.getElementById('imgRetailers').src="images/one_over.gif";
}

function ShowRecycleItems(){
	HideAll();
	arrayAction(recycleItems,"show");
	document.getElementById('liRecycle').className ="active";
	document.getElementById('imgRecycle').src ="images/three_over.gif";
}

function ShowBulbItems(){
	HideAll();
	arrayAction(bulbItems,"show");
	document.getElementById('liBulb').className ="active";
	document.getElementById('imgBulb').src ="images/four_over.gif";
}

function arrayAction(array,action)
{
	if(array.length > 0)
	{
		//alert(purchaseItems.length);
		for (var i=0;i<array.length;i++)
		{
			var marker = array[i];
			if(action=="hide")
				marker.hide();
			else{
				marker.show();
				/*
				//move center point on the first marker
				if(i==0){
				      point = new GLatLng(marker.getLatLng().lat(),  marker.getLatLng().lng());
				      //alert(point);
							//map.panTo(point);
							map.setCenter(point, initialZoom);
				}
				*/
			}
		}
	}
}



//Unused Functions
    // showLocation() is called when you click on the Search button
    // in the form.  It geocodes the address entered into the form
    // and adds a marker to the map at that location.
    function showLocation() {
      var address = document.forms[0].q.value;
      geocoder.getLocations(address, addAddressToMap);
    }
        function getRandomPoint() {
      var lat = 37.44 + (Math.random() - 0.5)*1.5;
      var lng = -122.14 + (Math.random() - 0.5)*2.0;
      return new GLatLng(Math.round(lat*10)/10, Math.round(lng*10)/10);
    }
    
    	function AddIcon(letter)
	{
        var baseIcon = new GIcon();
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(20, 34);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
        baseIcon.infoShadowAnchor = new GPoint(18, 25);
		
				var letteredIcon = new GIcon(baseIcon);
				letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

        // Set up our GMarkerOptions object
        markerOptions = { icon:letteredIcon };
		return markerOptions
	}
	
		function Addmarks(n)
	{
		for (var i = 0; i < n; i++) {
		
		if(i <=4){
		var marker = createMarker(getRandomPoint(),"Puchase Location:" + i,"P");
			purchaseItems.push(marker);
		}
		else if(i > 4 && i <=9)
		{
		var marker = createMarker(getRandomPoint(),"Local Event:" + i,"E");
			eventItems.push(marker);
		}
		else if (i >9 && i <=14){
		var marker = createMarker(getRandomPoint(),"Dispose of Bulb Location:" + i,"D");
			disposeItems.push(marker);
			}
		else if (i > 14){
		var marker = createMarker(getRandomPoint(),"Case back Location:" + i,"C");
			getcashItems.push(marker);
		}
		map.addOverlay(marker);			
		}
	}
		/*
	function createMarker(point, html) {
  // Create a lettered icon for this point using our icon class from above
  //var icon = new GIcon(baseIcon);
  var marker = new GMarker(point);

  // Show this marker's index in the info window when it is clicked
  //var html = "Marker <b>" + letter + "</b>";
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
  });
   return marker;
}
*/