
function mouseClicked(e)
{
	//Get the pixel coordinates of where the map was clicked
	var x = e.mapX;
	var y = e.mapY;
	//alert('x'+x+', y'+y+' e.elementID='+e.elementID);
	var pixel = new VEPixel(x, y);
	var latlong = myMap.PixelToLatLong(pixel);
	
	//Round the coordinates to 5 decimal places
	var latitude = latlong.Latitude;
	var longitude = latlong.Longitude;
	if (e.rightMouseButton == true)
	{		
        myMap.FindLocations(latlong, GetResults); 
	}
	
	if (e.leftMouseButton == true)
	{		
           if (e.elementID)
            {
                var clickedShape = myMap.GetShapeByID(e.elementID);
				//alert('e.elementID='+e.elementID);
                if (clickedShape)
                {
                    if (window.ero.isVisible() && (clickedShape == _currentShape) )
                    {
                        // We clicked on the shape which has an infobox open.
                        CustomHideInfoBox(_currentShape);
                    }
                    else if (window.ero.isVisible())
                    {
                        // We clicked on a different shape. Close the infobox first,
                        // then show it for the new shape.
                        CustomHideInfoBox(_currentShape);
                        CustomShowInfoBox(clickedShape);
                    }
                    else
                    {
                        CustomShowInfoBox(clickedShape);
                    }
                }
            }
	
	}
	
} 

//////////////////////////////////////////////////
        function OnMouseDown(e)
        {
            if (e.leftMouseButton)
                _isPanning = true;
        }

        //////////////////////////////////////////////////
        function OnMouseMove(e)
        {
           if (_isPanning)
                CustomHideInfoBox(_currentShape);
        }

        //////////////////////////////////////////////////
        function OnMouseUp(e)
        {
            _isPanning = false;
        }
      	//////////////////////////////////////////////////
        function OnMouseWheel(e)
        {
            return true;
        }
		
        //////////////////////////////////////////////////
        function OnMouseOver(e)
        {
            return true;
        }

        //////////////////////////////////////////////////
        function OnStartPan(e)
        {
            if (e.leftMouseButton)
            {
                // We don't want to handle this here, because a simple mouse click on a shape,
                // could be interpreted as a "start pan" action.
                return;
            }

            CustomHideInfoBox(_currentShape);
        }

        //////////////////////////////////////////////////
        function OnStartZoom(e)
        {
            CustomHideInfoBox(_currentShape);
        }

        //////////////////////////////////////////////////
        function DisableEroHide()
        {
            _hide = window.ero.hide;
            window.ero.hide = function(a)
            {
                return;
            }      
        }
      
        //////////////////////////////////////////////////
        function EnableEroHide()
        {
            window.ero.hide = _hide;
        }

        //////////////////////////////////////////////////
        function CustomShowInfoBox(shape)
        {
            if (!shape)
                return;
              
            DisableEroHide();
            myMap.ShowInfoBox(shape);
            _currentShape = shape;
        }

        //////////////////////////////////////////////////
        function CustomHideInfoBox(shape)
        {
            if (!shape)
                return;
              
            // Before actually calling the hide method, we need to restore the ero hide function,
            EnableEroHide();
            myMap.HideInfoBox(shape);
            _currentShape = null;
        }


function GetResults(locations) {
	 //strOutput is the string info in the pushpin popup
	 var strOutput = "";
	 //precision is a string info for geocode type
	 var precision = "";
	 if (locations) {
		//Loop through results and place them in the pushpin popup
		for(var i=0;i<locations.length;i++) {
			if (locations[i].Precision == VELocationPrecision.Interpolated) {
				precision = "Interpolated";
			}
			else {
				precision = "Rooftop";
			   strOutput += "Address:  ";
			   strOutput += locations[i].Name;
			   strOutput += "<br/>Geocoded Method: " + precision;
			   strOutput += "<hr/>";
			   //var myPushpin = new VEShape(VEShapeType.Pushpin, locations[i].LatLong);
			   //myPushpin.Title = strOutput;
			   //myPushpin.SetDescription = strOutput;
			   //Get Vintage information based on Lat/Long of clicked point
			   //myMap.Clear();
			   //myMap.AddShape(myPushpin);
			}
		}
		bestPlace=locations[0];
		nameArray=locations[0].Name.split(',');
 	    document.getElementById('address').value=nameArray[0];
		onGeocodeClick()
   }
   else
	{
	   alert('No Result found.');
	}

} 


function onGeocodeClick()
{
	var address = document.getElementById("address").value;
	//alert(address);
	myMap.Clear();
	//alert('map cleared');
	StartGeocoding(address);
	return false;
}

function StartGeocoding( address )
{
   myMap.Find(null,    // what
			  address, // where
			  null,    // VEFindType (always VEFindType.Businesses)
			  null,    // VEShapeLayer (base by default)
			  null,    // start index for results (0 by default)
			  null,    // max number of results (default is 10)
			  null,    // show results? (default is true)
			  null,    // create pushpin for what results? (ignored since what is null)
			  null,    // use default disambiguation? (default is true)
			  false,    // set best map view? (default is true)
			  GeocodeCallback);  // call back function
}

 
function GeocodeCallback (shapeLayer, findResults, places, moreResults, errorMsg)
{
   // if there are no results, display any error message and return
   if(places == null)
   {
	  alert( (errorMsg == null) ? "There were no results" : errorMsg );
	  return;
   }
   var bestPlace = places[0];
   
   for(var i=0;i<bestPlace.length;i++){
   	document.write("<b>bestPlace["+i+"] is </b>=>"+bestPlace[i]+"<br>");
   }
   	
	
	   // Add pushpin to the *best* place
   var location = bestPlace.LatLong;

   name_array=bestPlace.Name.split(',');
   document.getElementById('address').value=name_array[0]+","+name_array[1];
   document.getElementById('state_ab').value=name_array[2].substring(1,3);
   document.getElementById('location').value=location.Latitude + "," + location.Longitude;
  	document.getElementById('zip_code').value=name_array[2].substring(4,9);
   document.eeForm.submit();
}

function findAddress(){
	//VEMap.Find(what, where, findType, shapeLayer, startIndex, numberOfResults, showResults, createResults, useDefaultDisambiguation, setBestMapView, callback);
	myMap.Find(null, document.getElementById('address').value ,VEFindType.Address, null,0,1,false,true,true,false,proccessAddress);
	
}
function proccessAddress(shapeLayer, results, places, moreResults, errorMsg){
	var address = places[0].Name.split(',');
	var location = places[0].LatLong;	
	document.getElementById('address').value=address[0]+","+address[1];
    document.getElementById('state_ab').value=address[2].substring(1,3);
    document.getElementById('location').value=location.Latitude + "," + location.Longitude;
	document.getElementById('zip_code').value=address[2].substring(4,9);
    document.eeForm.submit();
}

	  
function onEditClick () {	
	if(document.getElementById('user_status').value	== 'guest')
	{
		alert("You must be logged in to update this home's data");
		status=false;
	}
	else
	{
		status=true;	
	}
	return status;
}

function change_menu(menu){
	
	//non-i.e. browsers
	document.getElementById('savings').setAttribute("class", "content_off");
	document.getElementById('savings_link').setAttribute("class", "not_selected");
	document.getElementById('cost').setAttribute("class", "content_off");
	document.getElementById('cost_link').setAttribute("class", "not_selected");
	document.getElementById('use').setAttribute("class", "content_off");
	document.getElementById('use_link').setAttribute("class", "not_selected");	
	document.getElementById('emissions').setAttribute("class", "content_off");
	document.getElementById('emissions_link').setAttribute("class", "not_selected");	
	document.getElementById('home_info').setAttribute("class", "content_off");
	document.getElementById('home_info_link').setAttribute("class", "not_selected");
	
	document.getElementById(menu).setAttribute("class", "content_on");
	document.getElementById(menu+'_link').setAttribute("class", "selected");
	
	//i.e.
	document.getElementById('savings').setAttribute("className", "content_off");
	document.getElementById('savings_link').setAttribute("className", "not_selected");
	document.getElementById('cost').setAttribute("className", "content_off");
	document.getElementById('cost_link').setAttribute("className", "not_selected");
	document.getElementById('use').setAttribute("className", "content_off");
	document.getElementById('use_link').setAttribute("className", "not_selected");	
	document.getElementById('emissions').setAttribute("className", "content_off");
	document.getElementById('emissions_link').setAttribute("className", "not_selected");	
	document.getElementById('home_info').setAttribute("className", "content_off");
	document.getElementById('home_info_link').setAttribute("className", "not_selected");
	
	document.getElementById(menu).setAttribute("className", "content_on");
	document.getElementById(menu+'_link').setAttribute("className", "selected");
	
	return false;
}
