﻿// 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');    

map.SetDashboardSize(VEDashboardSize.Small);

      
try {
map.LoadMap(new VELatLong(centrelat, centrelon), 4 ,'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;

//draw the area outline
rtn = maps.webservice.mapData.GetAreaPoints(areaid, OnGetAreaRequestComplete, OnTimeout, OnError);

//get the lodges
rtn = maps.webservice.mapData.GetPOIinArea(areaid, OnGetPOIRequestComplete, OnTimeout, OnError);

//get the park boundaries if there are any
rtn = maps.webservice.mapData.GetParksInArea(areaid, OnGetParksRequestComplete, 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 OnGetPOIRequestComplete(result) 
{
//alert("OnGetPOIRequestComplete");

//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")
  }     
   
for(i = 0; i < result.length; i++)
{
    var icon = '';
    var objItem = result[i];
                     
    AddPin(objItem.lat, objItem.lon, objItem.title + ', ' + objItem.location, objItem.description, objItem.photoURL, objItem.urlPath, objItem.iconPath);
    }    
}

function OnGetAreaRequestComplete(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")
  }      


//debugger;

var rtn = DrawArea("area", result, 1000, false)
    
//map.SetMapView(points); 

map.AttachEvent("onstartcontinuouspan", HidePopup);
map.AttachEvent("onstartzoom", HidePopup);

}

//draws a parks in an area
function OnGetParksRequestComplete(result)
{

//debugger;

//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 = DrawPark(objItem.title,objItem.points, objItem.parkid, false)
}
    
}

//AddPin(objItem.lat, objItem.lon, objItem.title + ', ' + objItem.location, objItem.description, objItem.photoURL, objItem.urlPath);

function AddPin(lat, lon, title, description, photoUrl, url, icon)
{   


var html = "<div id='popupbody' >";

    if (icon != undefined && icon != '')
    {
        icon = "icons/" + icon;
    }
    else
     {
        icon = "icons/lodge16x16.png";     //default for lodge
    }


if (url != undefined && url != '')
{
    html +=  "<div id='photo'><a href='" + url + "'><img alt='click for more details' src='" + photoUrl + "' /></a></div>";
    
    html += "<div id='text'>" + description + "</div>";
    html +=  "<div id='link'><a href='" + url + "'>Full description...</a></div>";
    }
else
    {
    html += "<div id='textwide'>" + description + "</div>";
    }

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';
                        
                       //apend stuff for the expander
                       //var expander = "<span id='expando' onclick='toggleExpand();'>Expand</span>";
                       
                       popupInfo.innerHTML = "<div id='top'><h2>" + unescape(title) + "</h2><span id='dismiss' onclick='HidePopup();'>X</a></span></div>"  + unescape(details);
                       
            }
       
map.AddPushpin(pin);
pinID++;
} 

////expands popu
//function toggleExpand()
//{
//var popupInfo = document.getElementById("popupbody")

//if (popupInfo.style.display == "none")
//{
//    popupInfo.style.display = 'block';
//}
//else
//{
//    popupInfo.style.display = 'none';

// }
//        
//}



//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);
}
 
//draw the pretty poly
poly = new VEPolygon(id,points);

//normal version
poly.SetOutlineWidth(1);
poly.SetOutlineColor(new VEColor(0,150,100,0.75));
poly.SetFillColor(new VEColor(0,150,100,0.25));
    
map.AddPolygon(poly);

//now centre the map on the area
map.SetMapView(points); 

//delete it once done
map.DeletePolygon(id);

}



function DrawPark(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);
}
 
//draw the pretty poly
//poly = new VEPolygon(id,points);
poly = new VEPolyline(id,points);
if (highlight == false)
    {
    //normal version
    //poly.SetOutlineWidth(1);
    //poly.SetOutlineColor(new VEColor(0,150,100,0.75));
    //poly.SetFillColor(new VEColor(0,150,100,0.25));
    poly.SetWidth(2);
    //poly.SetColor(new VEColor(255,0,0,0.75));
    poly.SetColor(new VEColor(255,249,22,1.0));
    //poly.SetColor(new VEColor(210,120,110,1.0));

    }
else
    {
    //highlighted version
    poly.SetOutlineWidth(3);
    poly.SetOutlineColor(new VEColor(210,120,110,1.0));
    //poly.SetFillColor(new VEColor(255,0,0,0.25));
    }
    
//map.AddPolygon(poly);
map.AddPolyline(poly);

//debugger;

}

function debugprint(msg)
{
if (gTestMode == true)
    alert("error: " + msg);
}
