﻿// Functions to be called after search results are returned
//
// All rights are reserved. Reproduction or transmission in whole or in part, in 
// any form or by any means, electronic, mechanical or otherwise, is prohibited 
// without the prior written consent of the copyright owner.
// 
// Filename: ApplyResults.js
//

var resultAddress = "";
var resultPhone = "";
var resultEmail = "";
var resultId = "";
var resultLatitude = "";
var resultLongitude = "";
var multipleResultsFlag = "";
var globalState="";
var globalCity="";
var locs = new Array();


//Apply results function to put pushpins on the map if user searches for a particular location
function ApplyResults(results)
{   
    var category = 'Search';

        map.DeleteAllPushpins();
        //clear the array
        
        locs = new Array();
        
        for (i=0;i< results.length && i<SHOWRESULTNUM;i++)
        {
            var latitude = results[i].latitude;
            var longitude = results[i].longitude;
            
            var loc = new VELatLong(latitude, longitude);
            locs.push(loc);           
            CreatePushPin(latitude, longitude, results[i], i+1);           
	    }        
       
        HidePanel("locationPanel");
        //ShowPanel("eRecruitingPanel");
        buildResultPanel( results );
        //buildDrivingDirectionsPanel();
        //either set the best map view or the zoom level depending on how many results are returned
        if (locs.length > 1)
        {
            DoBestMap(locs);        
        }else
        {
            DoZoomMap(locs);
        }
}

//Create a pushpin. Populate the pop up box, set the pin image and add it to the map.
function CreatePushPin(pinLat,pinLon,result, index)
{

    var address = result.addressLine + "," + result.primaryCity + " " + result.subDivision;
    var telephone = result.phone;
    var email = result.email;

    var msPin=new VEShape(VEShapeType.Pushpin,new VELatLong(pinLat,pinLon)); 
 
    var telephoneHTML="";
    if(telephone != ""){
        telephoneHTML+='<br />';
        telephoneHTML+=telephone;
    }
    var emailHTML = "";
    if (email != "")
    {
        emailHTML += "<br/>";
        emailHTML += email;
    }
    var addressHTML="";
    if(address != ""){    
        addressHTML = address;
    }
    var isSafari = (navigator.userAgent.toLowerCase().indexOf('safari')!=-1)?true:false;
    var listLinks;
    if(isSafari)
    {
        listLinks = "<br />* <a class=\"linktext\" href=\"javascript:ZoomToStreet("+pinLat + ", " + pinLon +")\">"+L_SearchPopupZoomToStreetLevel_Text+"</a>";
    }
    else
    {
        listLinks = "<br /><br/>* <a class=\"linktext\" href=\"javascript:ZoomToStreet("+pinLat + ", " + pinLon +")\">"+L_SearchPopupZoomToStreetLevel_Text+"</a>"
                   + '<br/>* <a class="linktext" href="javascript:getDirections(0);">'+"Drive To"+'</a>'
                   + '<br/>* <a class="linktext" href="javascript:getDirections(1);">'+"Drive From"+'</a>';       
    }
    if(email != "")
    {
       listLinks += '<br/>* <a class="linktext" href="mailto:'+email+'">Email</a>';
    }   
    
    var directionHTML = GenerateDrivingDirectionPanel(address, pinLat, pinLon);
    var popupBGImage = "<div class='pushpinBG'><img src='Images/pushpinBG.png' height='100%' width='100%' alt=''/></div>";
    var padding = "<div style='background: transparent; width: 182px; height:30px'></div>";
    
    var description;
    if(isSafari)
    {
        description= '<div class="popupBoxSafari">'+ padding + result.htmlAddress + "</br>" +
        telephoneHTML + emailHTML + "</div>" + '<div class="popupBox">' + listLinks + directionHTML + "</div>";
    }
    else
    {
        description= '<div class="popupBox">'+ padding + result.htmlAddress + "</br>" +
        telephoneHTML + emailHTML + listLinks +directionHTML + "</div>";    
    }                                                         
    msPin.SetDescription(popupBGImage+description);
    msPin.SetCustomIcon("<img src=\"Images/Pins/" + PUSHPIN + index +".gif\" alt='" + index + "'/>");
    map.AddShape(msPin);
}


//Set the zoom level to predefined street level and center around the provided point
function ZoomToStreet(lat, long)
{
    if(map.GetZoomLevel() < STREETZOOMLEVEL
        &&
        map.GetMapStyle()!= VEMapStyle.Birdseye )
    {
        map.SetCenterAndZoom(new VELatLong(lat, long), STREETZOOMLEVEL);
    }
}

function HideDirectionsPopup(){
    document.getElementById("getDirections").style.display="none";
}



//Get the best map view for the array of locations
function DoBestMap(locs)
{
    map.SetMapView(locs);
    //Zoom out one more level to accommodate the pins covered by the search panel
    map.SetZoomLevel(map.GetZoomLevel() - 1);
}

//Get the zoom map view for the result
function DoZoomMap(locs)
{
    map.SetCenterAndZoom(new VELatLong(locs[0].Latitude,locs[0].Longitude), 15);
   
}

//Get the best map view for the one location
function goMapLocation(latitude,longitude)
{
    var locs = new Array;
    var loc = new VELatLong(latitude,longitude);
    locs.push(loc);

    DoBestMap(locs);
}


//Apply results function for state/city view to put pushpins on the map
function ApplyIconReference(results)
{
        var category = 'IconRef';
        var latitude='';
        var longitude='';
        
        var erAvailable='';
        var hpAvailable='';
        var otAvailable='';        
        var entityId ='';
        var state='';
        var city='';
        var enlistedRecruiting = 'Enlisted Recruiting';
        var healthProfessions = 'Health Professions';
        var officerTraining = 'Officer Training';
        
        var icon = "images/poi_search_er.gif";
               
        var locs = new Array;
       
        map.DeleteAllPushpins();
        
        for (i=0;i<results.length;i++)
        {
            globalState = results[i].state;
            globalCity = results[i].city;
            
            state = results[i].state;
            city = results[i].city;
            latitude = results[i].latitude;
            longitude = results[i].longitude;
            erAvailable = results[i].enlisted;
            hpAvailable = results[i].health;
            otAvailable = results[i].officer;
            
            //var pin =new VEShape(VEShapeType.Pushpin,new VELatLong(latitude,longitude));
            //pin.SetCustomIcon("<img src='" + icon + "'/>");

            var pin = new VEPushpin(
            i,new VELatLong(latitude,longitude),icon,category+"*"+erAvailable+"*"+hpAvailable+"*"+otAvailable+"*"+i,city+", "+state);
            map.AddPushpin(pin);
        }
        
}



//Apply results function for country view to put pushpins on the map
function ApplyCountryIconReference(results)
{
        var category = 'IconRef';
        var latitude='';
        var longitude='';
        
        var erAvailable='';
        var hpAvailable='';
        var otAvailable='';        
        var entityId ='123';
        var state='';
        var icon = "images/poi_search_er.gif";
               
        var locs = new Array;
       
        map.DeleteAllPushpins();

        for (i=0;i<results.length;i++)
        {
            
            state = results[i].iso2;
            name = results[i].name;
            latitude = results[i].latitude;
            longitude = results[i].longitude;
            erAvailable = results[i].enlisted;
            hpAvailable = results[i].health;
            otAvailable = results[i].officer;
            
            var pin = new VEPushpin(
            i,new VELatLong(latitude,longitude),icon,category+"*"+erAvailable+"*"+hpAvailable+"*"+otAvailable+"*"+i,name);
            map.AddPushpin(pin);
        }

        
}

//Apply results function for area/street view to put pushpins on the map
function ApplyAreaIconReference(results)
{
        var category = 'AreaIconRef';
        var latitude='';
        var longitude='';
        
        var erAvailable='';
        var hpAvailable='';
        var otAvailable='';        
        var entityId ='';
        var state='';
        var city='';
        var icon = null;
        var enlistedRecruiting = 'Enlisted Recruiting';
        var healthProfessions = 'Health Professions';
        var officerTraining = 'Officer Training';
               
        var entityId='';
        var htmlAddress='';
        var email= '';
        
        var locs = new Array;
       
        //clear map of all pushpins
        map.DeleteAllPushpins();
        
        //loop through results returned and put a pushpin for each location
        for (i=0;i<results.length;i++)
        {
            globalState = results[i].state;
            globalCity = results[i].state;
            
            state = results[i].state;
            city = results[i].city;
            latitude = results[i].latitude;
            longitude = results[i].longitude;
            erAvailable = results[i].enlisted;
            hpAvailable = results[i].health;
            otAvailable = results[i].officer;
            entityId = results[i].entityId;
            htmlAddress = results[i].htmlAddress;
            email = results[i].email;
            
            if( erAvailable == "True" )
            {
				icon = "images/poi_search_er.gif";
            }
            else if( hpAvailable == "True" )
            {
				icon = "images/poi_search_hp.gif";
            }
            else
            {
				icon = "images/poi_search_ot.gif";
            }
                
            var pin = new VEPushpin(
            entityId,new VELatLong(latitude,longitude),icon,category+"*"+htmlAddress+"*"+email+"*"+erAvailable+"*"+hpAvailable+"*"+otAvailable+"*"+entityId,"Drive From*Drive To*Email");
            map.AddPushpin(pin);
        }
        
}
