﻿// 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(centrelat, centrelon), zoomlevel ,'h', 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 = maps.webservice.mapData.GetAreas(regionid, 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)
{
    debugprint("empty")
  }      

resultscopy = result;       //make a copy so we can reload stuff

for(i = 0; i < result.length; i++)
{
    var objItem = result[i] ;
    var rtn = DrawArea(objItem.title,objItem.points, objItem.areaid, false)
    
    //and a pin for the area
    rtn = AddPin(objItem.lat, objItem.lon, objItem.title, objItem.description, objItem.areaid)
    
}
    
//map.SetMapView(points); 

map.AttachEvent("onstartcontinuouspan", HidePopup);
map.AttachEvent("onstartzoom", HidePopup);

}



function AddPin(lat, lon, title, description, areaid)
{   

var icon ="icons/area16x16.png";

//customise the html
var html = '';

html += "<div id='popupbody'><div id='textwide'>" + description + "</div>";
html += "<a href='area.aspx?lngareaid=" + areaid + "'>More details</a> ";

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) 
                       {  
                       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';
}

function DrawArea(title, mypoints, id, highlight) 
{
var increment = 1;

//get the points into an array
var points = new Array; //for the points

//build the map at the first point
var objItem = mypoints[0];
      
var myPoint = new VELatLong(objItem.lat, objItem.lon);

//build array of points
for(j = 0; j < mypoints.length; j++)
{
    var objItem = mypoints[j]; 
    var myPoint = new VELatLong(objItem.lat, objItem.lon);
    points.push(myPoint);
}

// delete other lines as well
//map.DeleteAllPolylines();
 
//draw the pretty poly
poly = new VEPolygon(id,points);
if (highlight == false)
    {
    //normal version
    poly.SetOutlineWidth(1);
//    poly.SetOutlineColor(new VEColor(220,150,19,0.75));
//    poly.SetFillColor(new VEColor(220,150,19,0.25));
    
    poly.SetOutlineColor(new VEColor(0,150,100,1.0));
    poly.SetFillColor(new VEColor(0,150,100,0.25));

    }
else
    {
    //highlighted version
    poly.SetOutlineWidth(3);
    poly.SetOutlineColor(new VEColor(255,249,22,1.0));
    poly.SetFillColor(new VEColor(255,249,22,0.25));
    }
    
map.AddPolygon(poly);

//debugger;

}

function HighlightArea(row)
{

HidePopup();

//get the values from the js array with everything in it
var objItem = resultscopy[row] ;

//call the draw function
var id = row + 1000;     //new id is the old area plus 1000 to make it easy to get and delete
var title = "Highlighted " & objItem.title;

//alert("objItem.title=" + objItem.title);

DrawArea(objItem.title, objItem.points, id, true);

}
//kills an area
function UnHighlightArea(row)
{
var id = row + 1000;     //new id is the old area plus 1000 to make it easy to get and delete
//debugger;
map.DeletePolygon(id);

}

function debugprint(msg)
{
if (gTestMode == true)
    alert("error: " + msg);
}


