﻿/// <reference path="../jquery/jquery.js"/>

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];
        }
    }
}

var objMap = null;  
var strImagePath = "/Images.Net/v2/bmi/DestinationGuides/Icons/";

// When page has loaded...
$(function() {

    $(".VScrollable").scrollable({
        vertical: true,
        clickable: false,
        keyboard: false,
        size: 14
    }).circular()
      .autoscroll({
          autoplay: true,
          interval: 750
      });

    $("div#DestinationGuide div#divHub_Col0 div.VScrollable").css("position", "relative").css("height", "360px").css("overflow", "hidden");
    $("div#DestinationGuide div#divHub_Col0 div.VScrollable ul.items").css("position", "absolute");

    // Tabify country/city guides
    $("#divCountry_Col1, #divCity_Col1").tabs({
        fx: { opacity: 'toggle' }
    });

    if (querySt('menu') != null) {
        // Show correct content depending on querystring
        switch (querySt('menu').toLowerCase()) {
            case 'overview':
                $("#divCity_Col1").tabs('select', 0)
                break;
            case 'fast-facts':
                $("#divCity_Col1").tabs('select', 1)
                break;
            case 'arrive':
                $("#divCity_Col1").tabs('select', 2)
                break;
            case 'see':
                $("#divCity_Col1").tabs('select', 3)
                break;
            case 'eat':
                $("#divCity_Col1").tabs('select', 4)
                break;
            case 'sleep':
                $("#divCity_Col1").tabs('select', 5)
                break;
            case 'connect':
                $("#divCity_Col1").tabs('select', 6)
                break;
            default:
                break;
        }
    }

    // Load the Virtual Earth map if user clicks a city page
    if (strBreadCrumb.split('/')[3]) {
        if (objMap == null) GetMap();
    }

    // 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");
    });

    // When item clicked on the menu...
    $("div#divCity_Col1 ul li a").click(MenuItemClick);

    // If the twitter panel is shown, then do stuff
    if ($("#divTwitterContainer").size() != 0) {
        
        //Center tweets
        setTimeout("InitialCenter()", 0.01);

        // Update twitter every 30 seconds
        setInterval("Twitter()", 30000);
    }
});

function InitialCenter() {
    // Centre the tweets
    if (!(jQuery.browser.msie && parseInt(jQuery.browser.version) < 7)) 
    {
        var height = $("#divTwitterContainer").height() - $("#twitterHeader").height();
        $("#divTwitter").css("top", ((height / 2) - ($("#divTwitter").height() / 2)) + "px");
    }    
}

function Twitter() {
    
    $.getJSON(document.location.protocol + "//" + document.location.hostname + document.location.pathname, "filter=data", function(data) {
        // If we get some data back from our method...
        if (data) {
            $(".TwitterPanel").html(data.filtered);

            // Centre the tweets
            if (!(jQuery.browser.msie && parseInt(jQuery.browser.version) < 7)) {
                var height = $("#divTwitterContainer").height() - $("#twitterHeader").height();
                $("#divTwitter").css("top", ((height / 2) - ($("#divTwitter").height() / 2)) + "px");
            }    
        }
    });
}
 

// 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;         
    
    // Load the map into the specified div
    objMap = new VEMap('divFlashMapArea');
    // Disable the mouse zooming in and out
    objMap.AttachEvent("onmousewheel", function() { return true; });  
   
    // Set default location and style for the map
    // VEMap.LoadMap(VELatLong, zoom, style, fixed, mode, showSwitch, tileBuffer, mapOptions);
    objMap.LoadMap(new VELatLong(strLat, strLon), 11, VEMapStyle.Road, false, VEMapMode.Mode2D, false, 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'/>");
    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 MenuItemClick(event) {

    var strSelectedMenuItem = $(event)[0].currentTarget.innerHTML.toLowerCase();
    var menuItemSelectedId = $($(event)[0].currentTarget).attr("href");
        
    // 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 for the selected item
        var lats = $("span[id^='lat_']", menuItemSelectedId);
        var longs = $("span[id^='lon_']", menuItemSelectedId);
        var address = $("span[id^='address_']", menuItemSelectedId);
        var addressTitles = $("h3[id$='CityTitleText']", menuItemSelectedId);
        
        // 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));         
    }
};


            

