var pushPinZIndex = 4;
var pushPinWidth = 18;
var pushPinHeight = 18;

function AeMap_initialize(mapDivId, center_lat, center_long, zoomlevel, dashPosX, dashPosY, style){
	var params = new Object();

	params.latitude = center_lat;
	params.longitude = center_long;
	params.zoomlevel = zoomlevel;
//	params.mapstyle = Msn.VE.MapStyle.Road;
	params.mapstyle = style;
	params.showScaleBar = true;
	params.showDashboard = true;
	params.dashboardSize = Msn.VE.DashboardSize.Normal;
	params.dashboardX = dashPosX;
	params.dashboardY = dashPosY;

	var map = new VEMap(mapDivId);
	try{
    	map.LoadMap(new VELatLong(center_lat, center_long), zoomlevel, style, false);
	}catch(e){
		//Browser does not support SGV or VML	
	}
	return map;
}

function AeMap_initializeMyHomePage(mapDivId, center_lat, center_long, zoomlevel, dashPosX, dashPosY, style){
	
	var map = new VEMap(mapDivId);
	try{
    	map.LoadMap(new VELatLong(center_lat, center_long), zoomlevel, style, false);
    	map.HideDashboard();
	}catch(e){
		//Browser does not support SGV or VML	
	}
	return map;
}


function AeMap_initializeBox(mapDivId, nw_lat, nw_long, se_lat, se_long, dashPosX, dashPosY, style){
	
	var params = new Object();
	var center_lat = (nw_lat + se_lat)/2.0;
	var center_long = (nw_long + se_long)/2.0;
	var zoomlevel = 1;
	return AeMap_initialize(mapDivId, center_lat, center_long, zoomlevel, dashPosX, dashPosY, style);
}


function AeMap_registerVarFlag(map, flagId, lat, lng, flag_image, width, height, flagclass, callback, altmsg) 
{
    var cstr = "";
    if (callback != null) {
		astr = altmsg + " <a href='" + callback + "'>click.</a>";
    } else {
		astr = altmsg;
	}
/*	alert("adding pushping at " + lat);
    var pin = new VEPushpin(flagId, 
							new VELatLong(lat, lng),
							flag_image, astr);
	try {
	    map.AddPushpin(pin);
	} catch (e) {
	//	alert("exception in adding pushpin! " + flagId + " " + e.name + ": " + e.message);
	}
*/
var pin = new VEShape(VEShapeType.Pushpin, new VELatLong(lat, lng));
pin.SetCustomIcon(flag_image);
pin.SetTitle(astr);
//pin.SetDescription(details);
map.AddShape(pin);

//map.AttachEvent("onmouseover", onMouseOverCallback);


}

/*
	map: a Msn.VE.MapControl object
	flagId: string, the Id of the flag
	lat: float, latitude
	lng: float, longitude
	flag_image: string, which defines the image file name
	callback: string, the callback function and its args in string format when the flag is clicked
*/
function AeMap_registerFlag(map, flagId, lat, lng, flag_image, callback, alt) 
{
	AeMap_registerVarFlag(map, flagId, 
		lat, 
		lng, 
		flag_image,
		pushPinWidth,
		pushPinHeight,
		'pin',
		callback,
		alt);		
}

// flagId MUST exist
function AeMap_deregisterFlag(map, flagId)
{
	try {
		map.DeletePushpin(flagId);
		} catch (e) {
		// my_alert("exception in removing pushpin! " + flagId + " " + e.name + ": " + e.message);
		}
}

function AeMap_deregisterAllFlags(map)
{
	map.DeleteAllPushpins();
}

function AeMap_AddPushpin(map, flagId, lat, lng, icon) {
	var pin = new VEPushpin(flagId, new VELatLong(lat,lng), icon ,"");
	try {
	    map.AddPushpin(pin);
	} catch (e) {
	//	alert("exception in adding pushpin! " + flagId + " " + e.name + ": " + e.message);
	}
}

function AeMap_DeletePushpin(map, flagId) {
	try {
	    map.DeletePushpin(flagId);
	} catch (e) {
	//	alert("exception in deleting pushpin! " + flagId + " " + e.name + ": " + e.message);
	}
}

// direction: A string (left, right, top, down); distance - a number
// distance: integer, the distance to pan the map
function AeMap_pan(map, direction, distance)
{
	var latLong = map.GetCenter();
	
	//my_alert(latLong.ToString());
	
	var pixelLoc = map.LatLongToPixel(latLong);

	switch(direction){
		case "North":
			pixelLoc.y -= distance;
			break;
		case "South":
			pixelLoc.y += distance;
			break;
		case "West":
			pixelLoc.x -= distance;
			break;
		case "East":
			pixelLoc.x += distance;
			break;
		}

	var newLatLong = map.PixelToLatLong(pixelLoc.x, pixelLoc.y);
	map.SetCenter(newLatLong);

}

// valid values 1-19
function AeMap_zoom(map,zoomlevel) 
{
	map.SetZoomLevel(zoomlevel);
}

function AeMap_style(map,style)
{
	map.SetMapStyle(style);
}

function AeMap_center(map, center_lat, center_long)
{
	map.SetCenter(new VELatLong(center_lat, center_long));
}

function AeMap_centerAndZoom(map, center_lat, center_long, zoom)
{
	map.SetCenterAndZoom(new VELatLong(center_lat, center_long), zoom);
}

function AeMap_setView(map, topLeft_lat, topLeft_long, bottomRight_lat, bottomRight_long)
{
	var locs = new Array;
    locs.push(new VELatLong(topLeft_lat, topLeft_long));
    locs.push(new VELatLong(bottomRight_lat, bottomRight_long));
	map.SetMapView(locs);
}

function AeMap_convertLatLongToPixel(map, lat, lng)
{
	return map.LatLongToPixel(new VELatLong(lat, lng));
}

function AeMap_convertPixelToLatLong(map, px, py)
{
	var k = map.PixelToLatLong(px, py);
	var llo = {
	latitude: k.Latitude,
	longitude: k.Longitude
	};
	return llo;
}

function AeMap_panToLatLong(map, lat, lng)
{
	map.PanToLatLong(new VELatLong(lat, lng));
}


function AeMap_panLatLongToPixel(map, lat, lng, pixX, pixY)
{
	var pixel = AeMap_convertLatLongToPixel(map, lat, lng);
	dx = -(pixX - pixel.x) ;
	dy = -(pixY - pixel.y);

	map.Pan(dx,dy);
}

function AeMap_panToLatLongAndPixelDelta(map, lat, lng, dx, dy)
{
	var pixel = AeMap_convertLatLongToPixel(map, lat, lng);
	pixel.x -= dx;
	pixel.y -= dy;
	var ll = AeMap_convertPixelToLatLong(map, pixel.x, pixel.y);
	AeMap_panToLatLong(map, ll.latitude, ll.longitude);
}

function AeMap_getZoom(map) 
{
	return map.GetZoomLevel();
}

function AeMap_getStyle(map)
{
	return map.GetMapStyle();
}

function AeMap_getCenterLatitude(map) 
{
	return map.GetCenter().Latitude;
}

function AeMap_getCenterLongitude(map) 
{
	return map.GetCenter().Longitude;
}

function AeMap_getCenter(map)
{
	var k = map.GetCenter();
	var llo = {
	latitude: k.Latitude,
	longitude: k.Longitude
	};
	return llo;
}

function AeMap_setCenterAndZoom(map, lat, lng, zoomlevel)
{
	map.SetCenterAndZoom(new VELatLong(lat, lng), zoomlevel);
}

// to be unit tested

// WARNING: this will work with one map in the page
AeMap_clickFunction = function(lat, lng) {}

function AeMap_localMSmouseClick(e)
{
   var currentLat = e.view.LatLong.Latitude;
   var currentLong = e.view.LatLong.Longitude;
 //	my_alert("localmouseclick");
 AeMap_clickFunction(currentLat, currentLong);
}

function AeMap_setClickCallback(map, callback)
{
    if (callback) {
		map.AttachEvent("onclick", AeMap_localMSmouseClick);
		AeMap_clickFunction = callback;
	}
}
function AeMap_attachEvent(map, evt, callback)
{
//	my_alert("map event attach " + evt);
	map.AttachEvent(evt, callback);
}

function AeMap_setChangeViewCallback(map, callback)
{
	map.AttachEvent("onchangeview", callback);
}

function AeMap_setPanBeginCallback(map, callback)
{
	map.AttachEvent("onstartcontinuouspan", callback);
}

function AeMap_setPanEndCallback(map, callback)
{
	map.AttachEvent("onendcontinuouspan", callback);
}

function AeMap_setZoomBeginCallback(map, callback)
{
	map.AttachEvent("onstartzoom", callback);
}

function AeMap_setMapStyleCallback(map, callback)
{
	map.AttachEvent("onchangemapstyle", callback);
}


