﻿// JScript File
var map = null;
var geocoder = null;

// UK
var defaultLongitude = -2.1533203125;
var defaultLatitude = 55.229023057406344;
var defaultZoom = 5;

var baseIcon = null;

var icons = ["images/mm_20_red.png", "images/mm_20_blue.png", "images/mm_20_white.png"];
var shadow = "images/mm_20_shadow.png";
var curMarker = null;
var infoWindowOpen = false;
var MarkerToolTip = null;
var bDoUpdate = true;

function GMapsInit(container, longi, lati, zoom)
{
    if (GBrowserIsCompatible()) {

        if (longi) {
            defaultLongitude = longi;
        }

        if (lati) {
            defaultLatitude = lati;
        }

        if (zoom) {
            defaultZoom = zoom;
        }
    
    
        map = new GMap2(document.getElementById(container));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        ResetMap();
        ClearMap();
        // shadow, icon dimensions, etc.
        baseIcon = new GIcon();
        baseIcon.image = icons[1];
        baseIcon.shadow = shadow;
        baseIcon.iconSize = new GSize(12, 20);
        baseIcon.shadowSize = new GSize(22, 20);
        baseIcon.iconAnchor = new GPoint(6, 20);
        baseIcon.infoWindowAnchor = new GPoint(5, 1);
        
        geocoder = new GClientGeocoder();
        
        GEvent.addListener(map, "infowindowopen", function() {infoWindowOpen = true;});
        GEvent.addListener(map, "infowindowclose", function() {infoWindowOpen = false;});
        
        MarkerToolTip = document.createElement("div");
        MarkerToolTip.className = "MapToolTip";
        document.getElementById(container).appendChild(MarkerToolTip);
        MarkerToolTip.style.visibility="hidden";
    }
}

function ResetMap()
{
    map.setCenter(new GLatLng(defaultLatitude, defaultLongitude), defaultZoom);
}

function importanceOrder (marker,b) 
{
    return GOverlay.getZIndex(marker.getPoint().lat()) + marker.importance*1000000;
}

function SetPushpinIcon(pushPin, icon, save)
{
    try
    {
        pushPin.setImage(icons[icon]);
        if(save)
            pushPin.prevIconNumber = icon;           
    }
    catch(e)
    {
        alert("SetPushpinIcon " + e.description );
    }
}

function createMarker(point, makePopup, importance)
{
    try
    {
        //var marker = new GMarker(point, baseIcon);//
        var marker = new GMarker(point,{icon:baseIcon,zIndexProcess:importanceOrder});
        marker.importance = importance;
        GEvent.addListener(marker,"mouseover", function() {
            ShowMarkerTooltip(marker, true);
        });        
        GEvent.addListener(marker,"mouseout", function() {
		    ShowMarkerTooltip(marker, false);
        });
        GEvent.addListener(marker,"remove", function() {
		    MarkerToolTip.style.visibility="hidden";
        });

        GEvent.addListener(marker, "click", function() {

            if (marker.userDefined_Popup) {

                if (!infoWindowOpen || marker != curMarker) {
                    var html = marker.userDefined_Title + marker.userDefined_Description;

                    var retmarker = UpdatePushpinIcons(marker.userDefined_Search);
                    if (retmarker != null) {
                        if (marker == curMarker)
                            curMarker = retmarker;
                        marker = retmarker;
                        //marker.openInfoWindowHtml(html);

                        // Get first link from description
                        if (marker.userDefined_Description) {
                            var links = marker.userDefined_Description.split(',');

                            ViewPortraitPopup(links[0], links[1]);
                            return;
                        }


                    }
                }
            }
            else {
                var retmarker = UpdatePushpinIcons(marker.userDefined_Search);
                if (retmarker != null) {
                    if (marker == curMarker)
                        curMarker = retmarker;
                    marker = retmarker;
                }
            }

            if (marker == curMarker)
                return

            //if(curMarker != null)
            //    SetPushpinIcon(curMarker, 1, true);
            SetPushpinIcon(marker, 0);

            curMarker = marker;

            var nextSearch = curMarker.userDefined_Search;

            if (markerClickMode == 1) {
                if (nextSearch != "") {
                    ResetPortraitUC((nextSearch.length > 5) ? 8 : 7, nextSearch, null);
                }
            }
            else {
                JumpToPage(curMarker.userDefined_FirstPage);
                bDoUpdate = false;
            }


        });

    }
    catch(e)
    {
        alert("Failed to create marker " + e);
    }
    return marker;
}

function AddHomePushpin(postcode, latlong)
{
    try
    {
        var marker = new GMarker(latlong);//createMarker(latlong, true, 0);//, postcode, "");
        map.addOverlay(marker);
        
        //Set the icon        
        //SetPushpinIcon(marker, 0, true);
    }
    catch(e)
    {
        alert(e.description );
    }
}

function CloneMarker(orig, importance, icon)
{
    var newMarker = createMarker(orig.getLatLng(), orig.userDefined_Popup, importance);
    newMarker.userDefined_Popup = orig.userDefined_Popup;
    newMarker.userDefined_Title = orig.userDefined_Title;
    newMarker.userDefined_Description = orig.userDefined_Description;
    newMarker.userDefined_Search = orig.userDefined_Search;
    newMarker.userDefined_FirstPage = orig.userDefined_FirstPage;
    newMarker.title = orig.userDefined_Search;
    //Set the icon        
    //SetPushpinIcon(newMarker, icon, true);
    return newMarker;
}

function AddPushpin(search, title, description, latlong, icon, popup, importance)
{
    try
    {
        var marker = createMarker(latlong, popup, importance);
        marker.userDefined_Popup = popup;
        marker.userDefined_Title = title;
        marker.userDefined_Description = description;
        marker.userDefined_Search = search;
        marker.title = search;
        map.addOverlay(marker);
        
        //Set the icon        
        SetPushpinIcon(marker, icon, true);
        return marker;
    }
    catch(e)
    {
        return null;
    }
}

function ClearMap()
{
    if(map != null)
    {
        map.clearOverlays();
        curMarker = null;
    }
}

function SetMapView(points, zoomMap)
{
    if(points.length > 0 && zoomMap == true)
    {
        var boundary = new GLatLngBounds;
        for(x in points)
        {
            boundary.extend(points[x]);
        }
        map.setZoom(map.getBoundsZoomLevel(boundary));
        map.setCenter(boundary.getCenter());
    }
}

function GetLatLongClass(lati, longi)
{
    var latLong = new GLatLng(lati, longi);
    return latLong;
}

// it can be called from an icon mousover or a side_bar mouseover
function ShowMarkerTooltip(marker, show)
{
    if(show)
    {
        MarkerToolTip.innerHTML = marker.userDefined_Search;
        var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
        var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
        var anchor=marker.getIcon().iconAnchor;
        var width=marker.getIcon().iconSize.width;
        var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width,- offset.y + point.y +anchor.y)); 
        pos.apply(MarkerToolTip);
        MarkerToolTip.style.visibility="visible";
        SetPushpinIcon(marker, 2, false);
    }
    else
    {
        SetPushpinIcon(marker, marker.prevIconNumber, true);
        MarkerToolTip.style.visibility="hidden";
    }
}

