﻿// JScript File
addLoadEvent(MapLoad);

function addLoadEvent(func) 
{ 
    var oldonload = window.onload; 
    if (typeof window.onload != 'function') 
        { window.onload = func; } 
    else 
        { window.onload = function() 
            { oldonload(); func(); } 
    }
}

function MapLoad()
//function pageLoad()
{

map = new VEMap('myMap');    

map.LoadMap(new VELatLong(54.40, -0.85), 9 ,'r', false); 

//callback when it loads
map.onLoadMap = OnMapLoad();

document.getElementById("loadingdata").style.display = 'block';

}

//map has loaded - so we can play with it
function OnMapLoad()
{
//todo - check ms ajax has loaded

//add points of interest
rtn = mapping.webservice.mapData.GetPOI(routeid, OnGetPOIRequestComplete, OnTimeout, OnError);
////points
rtn = mapping.webservice.mapData.GetPoints(routeid, OnGetPointsRequestComplete, OnTimeout, OnError);

}


//debig print stuff
function DebugPrint(msg)
{
if (gTestMode == true)
    alert(msg);
}

//replace with more friendly message in time
function OnError(result) 
{

    //trap empty error
    if (result == undefined)
        {
      alert("Error.\n\n");
       return false;
    }
           
    if (gTestMode == true)
        alert("Error.\n\n" + result.get_message() + "\n" + result.get_stackTrace());
    else
        alert("Sorry, but something has gone wrong.\nIf you continue to experience problems please get in touch.");
}

function OnTimeout(result)
{

//if (gTestMode == true)
//    debugger;
    
alert("Error! Something went wrong...");
}

function OnGetPOIRequestComplete(result) 
{

//bomb out if nothing comes back - this happens sometimes
if (result == undefined)
    {
    return false;
    }
    
//tell them the list is empty
if (result.length == 0)
{
    DebugPrint("no points found")
  }     
     
for(i = 0; i < result.length; i++)
{
    var icon = '';
    var objItem = result[i];
            
    AddPin(objItem.lat, objItem.lon, objItem.title, objItem.description, objItem.photoURL, objItem.photoLinkURL, objItem.mapIcon);
    } 
   //debugger;
   

   return true;
              
}

function OnGetPointsRequestComplete(resultstring) 
{
 // alert("OnGetPointsRequestComplete");

//bomb out if nothing comes back - this happens sometimes
if (resultstring == undefined)
    {
    alert("result == undefined");

    return false;
    }
    
//tell them the list is empty
if (resultstring.length == 0)
{
   DebugPrint("empty")
  }     


//hack to reduce the number of points for VE
var increment = 1;
if (resultstring.length >= 200)
    increment = 2;

if (resultstring.length >= 300)
    increment = 3;

myPoints = new Array; //reset the poi

//build array of points
for(i = 0; i < resultstring.length; i=i+increment)
{
     var objItem = resultstring[i]; 

    var myPoint = new VELatLong(objItem.lat, objItem.lon);
    myPoints.push(myPoint);

    }
    
//start of route
var objItem = myPoints[0] ;

//map.SetCenter(new VELatLong(objItem.Latitude, objItem.Longitude));

AddPin(objItem.Latitude, objItem.Longitude, "Start", "Start of the Route", "", "", "start16x16.png");

map.SetMapStyle(VEMapStyle.Hybrid);
map.SetMapView(myPoints); 

map.AttachEvent("onstartcontinuouspan", HidePopup);
map.AttachEvent("onstartzoom", HidePopup);

//draw the pretty poly
poly = new VEPolyline('1', myPoints);
poly.SetWidth(6);
poly.SetColor(new VEColor(255,153,0,0.57));
map.AddPolyline(poly);

document.getElementById("loadingdata").style.display = 'none';

}

function AddPin(lat, lon, title, description, photoUrl, photoLink, icon)
{   
var html = "<div id='popupbody'>";

//customise the html

//if (photoUrl != undefined && photoUrl != '')
if (photoLink != '')
{
    html +=  "<div id='photo'><a href='../../" + photoLink + "'><img alt='click for full size image' src='../../" + photoUrl + "' /></a></div>";
    html += "<div id='text'>" + description + "</div>";
    icon ="../../icons/camera.png";        //default icon for image
    }
else
    {
    html += "<div id='textwide'>" + description + "</div>";
    
    if (icon != undefined && icon != '')
        icon = "../../icons/" + icon;
    else
        icon = "../../icons/warning16x16.png";     //default for warning

    }

html += "</div>";


    
var pin = new VEPushpin(pinID, 
            new VELatLong(lat, lon), 
            icon, 
            title, 
            html,
            'pinicon',
            'title',
            'textdescription'
            );

VEPushpin.ShowDetailOnMouseOver = false;
            VEPushpin.OnMouseOverCallback = function(x, y, title, details) 
                       {  
                       //alert("hello");
                       var popupInfo = document.getElementById("popup");
                       popupInfo.style.display = 'block';
                      popupInfo.style.left = x + 'px';
                       popupInfo.style.top = y + 'px';
                        
                       
                       popupInfo.innerHTML = "<div id='top'><h2>" + unescape(title) + "</h2><span id='dismiss' onclick='HidePopup();'>X</a></span></div>" + unescape(details);
                       
            }
       
map.AddPushpin(pin);
pinID++;
} 

//hides the popup
function HidePopup()
{ 
    var popupInfo = document.getElementById("popup");
    popupInfo.style.display = 'none';
}
//removes highlights on selected point
function Unhighlight()
{
}
//highlights selected point
function Highlight(event)
{
//debugger;
}

