﻿// ============================================================================================================
// Constants
// ============================================================================================================
var intLong = $get('ctl00_InnerFooter_ctrlMediaPanel_ctrlGoogleMap_hidLong').value;
var intLat = $get('ctl00_InnerFooter_ctrlMediaPanel_ctrlGoogleMap_hidLat').value;
var intZoom = $get('ctl00_InnerFooter_ctrlMediaPanel_ctrlGoogleMap_hidZoom').value;
intZoom = intZoom * 1;
var map;
var bounds = new GLatLngBounds();


// ============================================================================================================
// Marker styles
// ============================================================================================================
var destinationIcon = new GIcon(G_DEFAULT_ICON);
destinationIcon.image = "http://media.ittiocontent.co.uk/UD_Overlay_Icons/DestinationIcon_16x16.gif";
destinationIcon.iconSize = new GSize(16,16);
destinationIcon.shadow = "";
markerOptions_Destination = { icon:destinationIcon };
var regionIcon = new GIcon(G_DEFAULT_ICON);
regionIcon.image = "http://media.ittiocontent.co.uk/UD_Overlay_Icons/RegionIcon_16x16.png";
regionIcon.iconSize = new GSize(16,16);
regionIcon.shadow = "";
markerOptions_Region = { icon: regionIcon };
var hotelsIcon = new GIcon(G_DEFAULT_ICON);
hotelsIcon.image = "http://media.ittiocontent.co.uk/UD_Overlay_Icons/ITTIO_hotel_64x64.gif";
hotelsIcon.iconSize = new GSize(16,16);
hotelsIcon.shadow = "";
markerOptions_Hotels = { icon:hotelsIcon };
var boatIcon = new GIcon(G_DEFAULT_ICON);
boatIcon.image = "http://media.ittiocontent.co.uk/UD_Overlay_Icons/ITTIO_yacht_64x64.gif";
boatIcon.iconSize = new GSize(16,16);
boatIcon.shadow = "";
markerOptions_LiveaboardBoat = { icon:boatIcon };
var routeIcon = new GIcon(G_DEFAULT_ICON);
routeIcon.image = "http://media.ittiocontent.co.uk/UD_Overlay_Icons/ITTIO_yachtmoving_64x64.gif";
routeIcon.iconSize = new GSize(16,16);
routeIcon.shadow = "";
markerOptions_LiveaboardRoute = { icon:routeIcon };


// ============================================================================================================
// Startup functions
// ============================================================================================================
function initialize() 
{
    if (GBrowserIsCompatible()) 
    {
        map = new GMap2($get("ctl00_InnerFooter_ctrlMediaPanel_ctrlGoogleMap_map"));
        map.setCenter(new GLatLng(intLat, intLong), intZoom);
        map.addControl(new GLargeMapControl());
        map.setMapType(G_SATELLITE_MAP);
        map.enableScrollWheelZoom();
        fnc_ClearOverlays();
    }
}


// ============================================================================================================
// Refresh the markers
// ============================================================================================================
function fnc_ClearOverlays()
{
    map.clearOverlays();
    //fnc_SwitchDestinations();

    if ($get('ctl00_InnerFooter_ctrlMediaPanel_ctrlGoogleMap_chkRegions').checked)
        fnc_SwitchRegions();
    else
        fnc_SwitchDestinations();
    if ($get('ctl00_InnerFooter_ctrlMediaPanel_ctrlGoogleMap_chkHotels').checked)
        fnc_SwitchHotels();
    if ($get('ctl00_InnerFooter_ctrlMediaPanel_ctrlGoogleMap_chkLiveaboards').checked)
        fnc_SwitchLiveaboards();
        
    map.setZoom(map.getBoundsZoomLevel(bounds));
    map.setCenter(bounds.getCenter());

    // Check if we have an override (override new bounds or set the zoom level
    if ($get('ctl00_InnerFooter_ctrlMediaPanel_ctrlGoogleMap_divLongLatOverride').innerHTML != '')
        fnc_ClearAndSetNewBounds();

}

function fnc_ClearAndSetNewBounds() 
{
    var strLongLatOverride = $get('ctl00_InnerFooter_ctrlMediaPanel_ctrlGoogleMap_divLongLatOverride').innerHTML;

    // Extend the center for this hotel
    var point = new GLatLng(strLongLatOverride.split("|")[0], strLongLatOverride.split("|")[1]);
    bounds = new GLatLngBounds();
    bounds.extend(point);

    map.setZoom(intZoom);
    map.setCenter(bounds.getCenter());

    // Reset the overriden long lat
    $get('ctl00_InnerFooter_ctrlMediaPanel_ctrlGoogleMap_divLongLatOverride').innerHTML = '';
}

// ============================================================================================================
// Create and return a marker object
// ============================================================================================================
function createMarker(point, html, markeroptions)
{
    var marker = new GMarker(point, markeroptions);

    GEvent.addListener(marker, "mouseover", function () 
    {
        $(".gmnoprint").css("cursor", "default");
    });

    return marker;
}


// ============================================================================================================
// Add the icons to the screen
// ============================================================================================================
function fnc_SwitchDestinations()
{
    for (i = 0; i < arrDestinations_IdName.length; i++)
    {
        var point = new GLatLng(arrDestinations_LongLat[i].split("|")[0],arrDestinations_LongLat[i].split("|")[1]);
        bounds.extend(point);
        map.addOverlay(createMarker(point,arrDestinations_LongLat[i].split('|')[2],markerOptions_Destination));                
        var label = new ELabel(point, arrDestinations_IdName[i].split('|')[1], 'GoogleMaps_Label');
        map.addOverlay(label);
    }        
}
function fnc_SwitchRegions()
{
    for (i = 0; i < arrRegions_IdName.length; i++)
    {
        var point = new GLatLng(arrRegions_LongLat[i].split("|")[0],arrRegions_LongLat[i].split("|")[1]);
        bounds.extend(point);
        map.addOverlay(createMarker(point,arrRegions_LongLat[i].split('|')[2],markerOptions_Region));                
        var label = new ELabel(point, arrRegions_IdName[i].split('|')[1], 'GoogleMaps_Label');
        map.addOverlay(label);
    }
}
function fnc_SwitchHotels()
{
    for (i = 0; i < arrHotels_IdName.length; i++)
    {
        var point = new GLatLng(arrHotels_LongLat[i].split("|")[0],arrHotels_LongLat[i].split("|")[1]);
        bounds.extend(point);
        map.addOverlay(createMarker(point,arrHotels_LongLat[i].split('|')[2], markerOptions_Hotels));
        var label = new ELabel(point, arrHotels_IdName[i].split('|')[1], 'GoogleMaps_Label');
        map.addOverlay(label);
    }     
}
function fnc_SwitchLiveaboards()
{
    for (i = 0; i < arrLiveaboardRoutes_LongLat.length; i++)
    {
        var point = new GLatLng(arrLiveaboardRoutes_LongLat[i].split("|")[0],arrLiveaboardRoutes_LongLat[i].split("|")[1]);
        bounds.extend(point);
        map.addOverlay(createMarker(point,arrLiveaboardRoutes_LongLat[i].split('|')[2], markerOptions_LiveaboardBoat));
        var label = new ELabel(point, arrLiveaboardRoutes_IdName[i].split('|')[1], 'GoogleMaps_Label');
        map.addOverlay(label);
    }
    for (i = 0; i < arrLiveaboardBoats_LongLat.length; i++)
    {
        var point = new GLatLng(arrLiveaboardBoats_LongLat[i].split("|")[0],arrLiveaboardBoats_LongLat[i].split("|")[1]);
        bounds.extend(point);
        map.addOverlay(createMarker(point,arrLiveaboardBoats_LongLat[i].split('|')[2], markerOptions_LiveaboardRoute));
        var label = new ELabel(point, arrLiveaboardBoats_IdName[i].split('|')[1], 'GoogleMaps_Label');
        map.addOverlay(label);
    }
}
