var beginLatitude = 52.2278;
var beginLongitude = 5.2954;
var beginZoom = 6;

// Global variable to initialize VEMap in
var map = null;
// Should we render the map now or wait for call back
var waitingForCallback = false;

// To hold values for call back function
var tmpId = new Array();
var tmpTitle = new Array();
var tmpDescription = new Array();
var tmpPhoto = new Array();
var tmpMorelink = new Array();
var currentitem = 0;
var nritems = 0;

function BonoGetMap(mapDivName) {
    map = new VEMap(mapDivName);
    map.SetDashboardSize(VEDashboardSize.Tiny);
    map.LoadMap(new VELatLong(beginLatitude, beginLongitude), beginZoom, VEMapStyle.Road, false);
    map.ShowDashboard();
    map.HideScalebar();
//    map.SetZoomLevel(beginZoom);
    //map.AttachEvent("onchangeview", MapChangeHandler);

}


function BonoFindAndCenter(location)
  {
  	map.Find(null, location,null,null,0,1,false,false,true,true);
  }

// Try to find this location and add a pushpin if successfull
// Location - string value of location to add to map
function BonoFindAndAddPin(title, description, location, photo, morelink,id) {
    if(title == null || description == null || location == null ||
	   title.length == 0 || description.length == 0 || location.length == 0) {
	    alert('Title, Description & Location are all required fields');
    }
    else {
        try {
        	//if(document.getElementById('cbdone').value == 'false')
        	//  {
        	  	//var func = 'BonoFindAndAddPin(' + title + ', '+ description + ', ' + location+ ', ' + photo + ', ' + morelink + ');';
        	//	setInterval('BonoFindAndAddPin("' + title + '", "'+ description + '", "' + location+ '", "' + photo + '", "' + morelink + '");',200);
        	//  }
        	//else
        	//  {
            document.getElementById('cbdone').value='false';
    	    tmpTitle[nritems] = title;
    	    tmpDescription[nritems] = description;
    	    tmpPhoto[nritems] = photo;
    	    tmpMorelink[nritems] = morelink;
    	    tmpId[nritems] = id;
    	    nritems = nritems +1;
    	    tmpPhoto = photo;
    	    tmpMorelink = morelink;
            map.Find(null, location,null,null,0,1,false,false,true,false,BonoMapFindCallback);
            waitingForCallback = true;
        	//  }
        }
        catch (e) {
            alert(e.message);
            waitingForCallback = false;
            document.getElementById('cbdone').value='true';
        }
    }
}

// Call back function for when a map location is found
function BonoMapFindCallback(layer, resultsArray, places, hasMore, veErrorMessage)
{
	if(places != null)
	{
	    var latLon = places[0].LatLong;
	    BonoAddPushpin(tmpTitle[currentitem], tmpDescription[currentitem],tmpPhoto[currentitem],tmpMorelink[currentitem], latLon);

	    ajaxsendlatlon(tmpId[currentitem],latLon.Latitude,latLon.Longitude);

	    //tmpTitle = null;
	    //tmpDescription = null;

    }

    currentitem = currentitem +1;
    document.getElementById('cbdone').value='true';
}

// Add Pushpin at desired latitude & longitude
// Title - Title of Pushpin
// Description - Description to set on Pushpin
// latLon - VELatLong object of location to set PushPin
function BonoAddPushpin(title, description,photo,morelink, latLon) {
    var shape = new VEShape(VEShapeType.Pushpin, latLon);
    shape.SetTitle(title);
    shape.SetDescription(description);

    if(photo != '')
      {
    	shape.SetPhotoURL(photo);
      }

    if(morelink != '')
      {
    	shape.SetMoreInfoURL(morelink);
      }

shape.ShowIcon();
//pin.SetPhotoURL("http://localhost/ve5/Hay.jpg");
//pin.SetMoreInfoURL("http://search.live.com/results.aspx?q=hay&mkt=en-us&FORM=LVCP");

//var icon = "<div style='font-size:12px;font-weight:bold;border:solid 2px
//Black;background-color:Aqua;width:50px;'>Custom</div>";
//shape.SetCustomIcon(icon);
//OF
//var spec = new VECustomIconSpecification();
//spec.CustomHTML = icon;
//spec.Image = 'http://www.geocities.com/cmpoet/Airplane1.png'
//pin.SetCustomIcon(spec);
    map.AddShape(shape);

}