﻿// JScript File

//// 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(); } 
//    }
//}

var mapLoaded = false;

//function MapLoad()
function pageLoad()
{

// If the browser is Firefox get the version number
var ffv = 0;
var ffn = "Firefox/"
var ffp = navigator.userAgent.indexOf(ffn);
if (ffp != -1) ffv = parseFloat(navigator.userAgent.substring(ffp + ffn.length));
// If we're using Firefox 1.5 or above override the Virtual Earth drawing functions to use SVG
if (ffv >= 1.5) {
  Msn.Drawing.Graphic.CreateGraphic=function(f,b) { return new Msn.Drawing.SVGGraphic(f,b) }
}

map = new VEMap('myMap');    

      
try {
map.LoadMap(new VELatLong(54.4, -0.85), 9 ,'r', false); 
 } catch(e) {
                 //Browser doesn't support method
            } 
            
//callback when map loads
map.onLoadMap = OnMapLoad();
            
}

//map has loaded - so we can play with it
function OnMapLoad()
{
mapLoaded = true;
    
rtn = mapping.webservice.mapData.GetStartingPoints(OnGetItemsRequestComplete, OnTimeout, OnError);

}


//replace with more friendly message in time
function OnError(result) 
{
    //trap empty error
    if (result == undefined)
        {
        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)
{
alert("Error! Timed out...");
}


function OnGetItemsRequestComplete(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)
{
    alert("empty")
  }      
  
for(i = 0; i < result.length; i++)
{


 var objItem = result[i] ;


    AddPin(objItem.lat, objItem.lon, objItem.title, objItem.description, '', parseInt(objItem.grading), objItem.url, objItem.routeid);
    var myPoint = new VELatLong(objItem.lat, objItem.lon);
    points.push(myPoint);

    }
    
map.SetMapView(points); 

map.AttachEvent("onstartcontinuouspan", HidePopup);
map.AttachEvent("onstartzoom", HidePopup);

}



function AddPin(lat, lon, title, description, icon, grading, url, routeid)
{   

if (icon == '')
    {
        if (grading <= 2)
        icon ="icons/blueblob.png";

    if (grading == 3)
        icon ="icons/greenblob.png";
    
       if (grading == 4)
        icon ="icons/redblob.png";

       if (grading == 5)
        icon ="icons/blackblob.png";

    }
    
//customise the html
var html = '';

html += "<div id='popupbody'><div id='textwide'>" + description + "</div>";
html += "<a href='routes/" + url + "/route.aspx'>More details</a> ";

//only do this for IE
//if (navigator.userAgent.indexOf("MSIE") > 0)
    html += "<a onclick='LoadPoints(" + routeid + ", true);return false;' href='#'>Show on map</a>";



html += "</div>";
    
var pin = new VEPushpin(pinID, 
            new VELatLong(lat, lon), 
            icon, 
            title, 
            html,
            'pinicon24',
            'title',
            'textdescription'
            );

VEPushpin.ShowDetailOnMouseOver = false;
            VEPushpin.OnMouseOverCallback = function(x, y, title, details) 
                       {  
                       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><div id='dismiss' onclick='HidePopup();'>X</a></div></div>" + unescape(details);
                       
            }
       
map.AddPushpin(pin);
pinID++;
} 

//hides the popup
function HidePopup()
{ 
    var popupInfo = document.getElementById("popup");
    popupInfo.style.display = 'none';
}


//map has loaded - so we can play with it
function LoadPoints(routeid)
{

////points - loaded and on IE
//if (mapLoaded == true && navigator.userAgent.indexOf("MSIE") > 0)
if (mapLoaded == true)
    rtn = mapping.webservice.mapData.GetPoints(routeid, OnGetPointsRequestComplete, OnTimeout, OnError);

}

function OnGetPointsRequestComplete(result) 
{
 // alert("OnGetPointsRequestComplete");
//debugger;

//bomb out if nothing comes back - this happens sometimes
if (result == undefined)
    {
    alert("result == undefined");

    return false;
    }
    
//tell them the list is empty
if (result.length == 0)
{
   DebugPrint("empty")
  }     

points = new Array; //for the poi

//build the map at the first point
var objItem = result[0];
      
var myPoint = new VELatLong(objItem.lat, objItem.lon);

//hack to reduce the number of points for VE
var increment = 1;
if (result.length >= 200)
    increment = 2;

if (result.length >= 300)
    increment = 3;

//build array of points
for(i = 0; i < result.length; i=i+increment)
{
     var objItem = result[i]; 

     var myPoint = new VELatLong(objItem.lat, objItem.lon);
    points.push(myPoint);

    }
    
    
// delete other lines as well
map.DeleteAllPolylines();
 
//draw the pretty poly
poly = new VEPolyline('1', points);
poly.SetWidth(3);
poly.SetColor(new VEColor(255,153,0,0.57));
map.AddPolyline(poly);
 // debugger;

HidePopup();


//set the view
//    map.SetMapView(points); 

}
