var loading = '<br/><br/>Loading...<br/><span style="color:#a59e98;">';
var loading2 = '</span>';
var map = null;
var tcpage = false;
var layer = null;
var clusterOptions = null;

function loadmapnew(tcpagebool, contid, tagid, personid)
{
    $.ajax({
            url: '/System/MapPoints.ashx?TagId=' + tagid + '&ContinentId=' + contid + '&PersonId=' + personid + '&date=' + new Date(),
            type: 'GET',
            dataType: 'xml',
            error: function(){
                              alert('Error loading XML document');
                             },
            success: function (xml){
                                    tcpage = tcpagebool;
                                    map = new VEMap('mapArea');
                                    map.HideDashboard();
                                    map.AttachEvent('onmouseover', hover);
                                    map.AttachEvent('onendzoom', zoom);
                                    map.AttachEvent('onclick', clickPoint);
                                    map.LoadMap(new VELatLong(20, 0), 1, 's', false, VEMapMode.Mode2D, true);
                                    map.ClearInfoBoxStyles();
                                    var mapPoints = new Array();
                                    var shapes = new Array();
                                    layer = new VEShapeLayer();
                                    $('mappoint', xml).each(function(){
                                                                        var icon = new VECustomIconSpecification();
                                                                        icon.Image = '/Images/MapPoint.gif';
                                                                        icon.ImageOffset = new VEPixel(0, -14);
                                                                        icon.TextContent=' ';
                                                                        var point1 = new VELatLong($('lat', this).text(), $('long', this).text());
                                                                        mapPoints.push(point1);
                                                                        var shape1 = new VEShape(VEShapeType.Pushpin, point1);
                                                                        shape1.SetCustomIcon(icon);
                                                                        shape1.SetTitle(' ');
                                                                        shape1.SetDescription(loading + $('guid', this).text() + loading2);
                                                                        layer.AddShape(shape1);
                                                                      });
                                    map.AddShapeLayer(layer);

                                    clusterOptions = new VEClusteringOptions();
                                    var icon = new VECustomIconSpecification();
                                    icon.Image = '/Images/MapPointCluster.gif';
                                    icon.ImageOffset = new VEPixel(0, -14);
                                    icon.TextContent=' ';
                                    clusterOptions.Icon = icon;
                                    layer.SetClusteringConfiguration(VEClusteringType.Grid, clusterOptions);
                                    
                                    map.SetMapView(mapPoints);
                                    if(map.GetZoomLevel()>3){map.SetZoomLevel(3);}
                                    }             
           });
}

function hover(e)
{
    if (e.elementID)
    {
        if (map.GetShapeByID(e.elementID).GetDescription().indexOf(loading) > -1)
        {
            var id = map.GetShapeByID(e.elementID).GetDescription().replace(loading, '').replace(loading2,'');
            
            $.ajax({
                    url: '/System/MapHover.ashx?id=' + id + '&tcpage=' + tcpage + '&date=' + new Date(),
                    type: 'GET',
                    dataType: 'html',
                    success: function (html){
                                            map.GetShapeByID(e.elementID).SetDescription(html);
                                            map.HideInfoBox(map.GetShapeByID(e.elementID));
                                            map.ShowInfoBox(map.GetShapeByID(e.elementID));
                                            }
                    });
        }
    }  
}
function zoom(e)
{
    if(map.GetZoomLevel()>13)
    { layer.SetClusteringConfiguration(VEClusteringType.None, clusterOptions); }
    else
    { layer.SetClusteringConfiguration(VEClusteringType.Grid, clusterOptions); }
}
function clickPoint(e)
{
    if (e.elementID)
    {
        if(map.GetShapeByID(e.elementID).GetTitle().indexOf('locations near here') > -1)
        {
            map.SetCenterAndZoom(map.GetShapeByID(e.elementID).GetPoints()[0], map.GetZoomLevel()+2);  
        }
        else 
        {
            window.location = FindURL(map.GetShapeByID(e.elementID).GetDescription());  
        }
    }
}
function zoomToArea(latitude, longitude, zoomLevel)
{
    map.SetCenterAndZoom(new VELatLong(latitude, longitude), zoomLevel);
}

function FindURL(html)
{
    return html.split('<a href="')[2].split('"')[0]
}
