﻿// JScript File
var objMap = null;  
var strImagePath = null;

// Get values from querystring
function querySt(ji) 
{
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) 
    {
        ft = gy[i].split("=");
        if (ft[0] == ji) 
        {
            return ft[1];
        }
    } 
}

// When page has loaded...
$(function() { 
  
    // Set menu items to black when clicked
    $("div#DestinationGuide div#divCountryCity_Col1 input").click(function()    
    {           
        // Find what the user has clicked on
        var strSelectedValue = $(this).attr("value");
        
        // Set hidden field to selected value so we can use it for other functions
        $("#hdnMenuItemSelected").attr("value", strSelectedValue);
        
        // Set selected value to black and set all other menu items back to red    
        $("div#DestinationGuide div#divCountryCity_Col1 input").each(function() {                          
            if ($(this).attr("value") != strSelectedValue)  $(this).css('color', 'red')
            else $(this).css('color', 'black'); 
        });               
    });  
    
    // Set image path
    strImagePath = $("#" + DGUniqueID + "HdnImagePath").attr('value');

    // Load the Virtual Earth map if user clicks a city page    
    if (strBreadCrumb.split('/')[3]) { if (objMap == null) GetMap(); } 

    // On load, set the first list item to black as it is automatically selected 
    $("div#DestinationGuide div#divCountryCity_Col1 div#divCity_Col1 div:first-child input").css("marginLeft", "0px");    
    if (querySt('menu') != null && querySt('menu').length > 0 && (querySt('menu').toLowerCase() == "see" || querySt('menu').toLowerCase() == "sleep" || querySt('menu').toLowerCase() == "eat" || querySt('menu').toLowerCase() == "connect" || querySt('menu').toLowerCase() == "arrive" || querySt('menu').toLowerCase() == "fast-facts"))
    {    
        $("div#DestinationGuide div#divCountryCity_Col1 input").each(function() {                          
            if ($(this).attr("value").toLowerCase() != querySt('menu').toLowerCase().replace('-',' '))  $(this).css('color', 'red')
            else $(this).css('color', 'black'); 
        });
        
        // Change the menu selected to the one on the querystring
        $("#hdnMenuItemSelected").attr("value", querySt('menu').toLowerCase().replace('-',' '));
        
        // Plot map points
        End_Request();
    }
    else
    {
       $("div#DestinationGuide div#divCountryCity_Col1 div#divCity_Col1 div:first-child input").css("color", "black");    
    }
    
    // Highlight first menu item for country
    $("div#DestinationGuide div#divCountry_Col1 div:first-child div:first-child input").css("color", "black");   
    
    // If user clicks print...
    $("#" +DGUniqueID + "hypPrint").click(function() { window.print(); });    
    
    // Underline buttons
    jQuery.each(jQuery.browser, function(i) {        
        //Underline text on hover for input of class button 
        $("div#DestinationGuide input").hover(
           function() {
            $(this).css("text-decoration", "underline")
            if($.browser.mozilla) $(this).css("display", "table-cell");  
           },
           function() {
             $(this).css("text-decoration", "none")
           }        
        );          
    });


});

    

// Load the Virtual Earth map 
function GetMap()
{
    var hasValues = false;
    
    // If we dont have lat/lon values, then set a default set 
    if (strLat.length > 0 || strLon.length > 0)
        hasValues = true;
    else    
        strLat = strLon = 0;         
    
    // If there is no default zoom defined, then we will use a value of 12
    if (strZoom.length == 0) strZoom = 11;    
    
    // Load the map into the specified div
    objMap = new VEMap('divFlashMapArea');    
   
    // Set default location and style for the map
    // VEMap.LoadMap(VELatLong, zoom, style, fixed, mode, showSwitch, tileBuffer, mapOptions);
    objMap.LoadMap(new VELatLong(strLat, strLon), strZoom, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 0);     
        
    // Add marker where the airport is
    var airportShape = new VEShape(VEShapeType.Pushpin, new VELatLong (strLat, strLon));
    airportShape.SetCustomIcon("<img alt=''src='"+strImagePath+"plane_icon.png'/>");
    //airportShape.SetTitle("Airport");
    objMap.AddShape(airportShape);
    
    // Item 2 is the country, Item 3 is the city e.g. England Manchester
    var CityGuideLookup = strBreadCrumb.split('/')[2] + strBreadCrumb.split('/')[3];    
    
    // Find the specific city map only if we havent got lat lon values
    // VEMap.Find(what, where, findType, shapeLayer, startIndex, numberOfResults, showResults, createResults, useDefaultDisambiguation, setBestMapView, callback);
    if (!hasValues)
       objMap.Find(null,CityGuideLookup,null,null,null,1,null,null,null,true,MapFindCallBack_Event);       
}        

// Process the results from trying to locate the needed city
function MapFindCallBack_Event(VEshapeLayer, VEFindResult, VEPlace, MoreResults, Error)
{   
    // Use the first result it finds 
    if (VEPlace !=null)            
        objMap.LoadMap(VEPlace[0].LatLong, 14, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 0);
}

// When ajax call is complete... display location points
function End_Request(sender, args)
{    
    // Set links to open in new window that are in content area
    $("div#DestinationGuide div#divCity_Col1b a, div#DestinationGuide div#divCountry_Col1b a").each(function (){
        $(this).attr("target", "_blank");     
    });

    var strSelectedMenuItem = $("#hdnMenuItemSelected").attr("value").toLowerCase(); 
    
    // Depending on what the user has clicked on, set the appropriate icon   
    switch(strSelectedMenuItem)
    {        
      case 'eat':          
        $(".InformationImage").html("<img alt='' src='" + strImagePath + "icon_eat.png'/>");        
        break;
      case 'sleep':
        $(".InformationImage").html("<img alt='' src='" + strImagePath + "icon_sleep.png'/>");        
        break;
      default:     
        $(".InformationImage").html("<img alt='' src='" + strImagePath + "icon_search.png'/>");        
        break;
    }

    // Add destination points to the Virtual Earth map
    if (objMap!= null)
    { 
        // Get all lattitude and longitudes on the page
        var lats = $("span[id^='lat_']");
        var longs = $("span[id^='lon_']");
        var address = $("span[id^='address_']");
        var addressTitles = $("h3[id$='CityTitleText']");
        
        // Delete existing points on the map            
        objMap.DeleteAllShapeLayers();            
        
        // Loop through all found lat/long and put them on the map                  
        for(var i=0; i < lats.size(); i++)
        {            
            if (lats[i].className != '' && longs[i].className != '')                
            {   
                // Create shape object to add to map
                var pointsToAdd = new VEShape(VEShapeType.Pushpin, new VELatLong(lats[i].className, longs[i].className));
                
                // Set relevant icon for the live map 
                switch(strSelectedMenuItem)
                {
                    case 'eat':                        
                        pointsToAdd.SetCustomIcon("<img alt=''src='"+strImagePath+"icon_eat_small.png'/>");                                                   
                        break;
                    case 'sleep':
                        pointsToAdd.SetCustomIcon("<img alt=''src='"+strImagePath+"icon_sleep_small.png'/>");                                
                        break;
                    default:
                        pointsToAdd.SetCustomIcon("<img alt=''src='"+strImagePath+"icon_search_small.png'/>");                                
                        break;
                }                
               
                // Set the title of the map point
                pointsToAdd.SetTitle(addressTitles[i].innerHTML); 
                pointsToAdd.SetDescription(address[i].innerHTML);
                
                
                // Add point on the map                                  
                objMap.AddShape(pointsToAdd);   
             }                            
        }      
        
        // Add marker where the airport is
        var airportShape = new VEShape(VEShapeType.Pushpin, new VELatLong (strLat, strLon));
        airportShape.SetCustomIcon("<img alt=''src='"+strImagePath+"plane_icon.png'/>");
        //airportShape.SetTitle("Airport");
        objMap.AddShape(airportShape);
        
        // Pan to the first location of lat/longs
        if (lats.size() >0 && longs.size() >0)
        {
            if (lats[0].className.length > 0 && longs[0].className.length)
                objMap.PanToLatLong(new VELatLong(lats[0].className, longs[0].className)); 
        }
        
        // If you click overview, then pan back to the original location
        if (strSelectedMenuItem == "overview" && strLat != 0 && strLon != 0)
            objMap.PanToLatLong(new VELatLong (strLat, strLon));
               
               
    }
};


            

