﻿
var pushpins = new Array();
Array.prototype.GetShowing = MyGet;
var BAWOptions = { fadeSpeed : 300,
				   timeVisible : 800,
				   zindex : 1001,
				   infoWidth : 283,
				   infoHeight : 437,
				   pushpinWidth : 68,
				   pushpinHeight : 68,
				   offsetx : 12,
				   offsety : -100,
				   mapLoaded : false};

function RefreshMap()
{
	map = new VEMap("map");
	map.SetDashboardSize("tiny");
	map.onLoadMap = MapIsLoaded;
	map.LoadMap(new VELatLong(30,0), 2);
	map.AttachEvent('onmouseover', InfoBoxShow);
	map.AttachEvent("onmousewheel", MouseWheelLimiter);
}

function MapIsLoaded()
{
	BAWOptions.mapLoaded = true;
}

function ClearPins()
{
	if(map)
		map.Clear();
}

function BlockRollOver(e)
{
	return true;
}

function InfoBoxShow(e)
{
	// elementID is null when someone clicks on the map and not on a shape
	if(e.elementID)
	{
		var shape = map.GetShapeByID(e.elementID); 
		if(shape)
		{
			// The default info box is drawing at incorrect locations on the map
			// in both FF3 and IE8
			//map.ShowInfoBox(shape);
			var testx = findPosXGlobal($("#" + e.elementID)[0]);
			var testy = findPosYGlobal($("#" + e.elementID)[0]); 
			ShowInfoBox(shape.Notes, testx, testy, e.elementID);//e.mapX, e.mapY

		}
	}
	return true;
}

function MouseWheelLimiter(e)
{
	if(e.zoomLevel < 3 && e.mouseWheelChange < 0)
		return true;
	return false;
}

function AddPushPin(icon, infoContent, lat, lon)
{
	if(!BAWOptions.mapLoaded)
	{
		var timeoutFunc = "AddPushPin(\""+icon+"\", \"" + infoContent.replace(/\"/gi,"\\\"").replace(/\n/gi,"\"\n+\"") +"\", \""+lat+"\", \""+lon+"\")";
		setTimeout(timeoutFunc, 5000)
		return;
	}

	var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(lat, lon));
	shape.SetCustomIcon(icon);
	// Set the info box
	shape.SetDescription(infoContent);
	// Add the shape the the map
	map.AddShape(shape);
	
	
}

function ShowInfoBox(content, coordx, coordy, guid)
{
// Debugging
//	$("#BAWchild").remove();
//	$("<div id=\"BAWchild\" style=\"position: absolute; width:200px; height:200px; top:700px; z-index:1002; background-color:red;\"></div>").appendTo($("body"));

//	$("#BAWchild").click(function(){
//		$(this).css("background-color", "green");
//	});
	
	// Existing popUp class elements can be removed because the new popUp has not been added to the DOM
	// First, push the pushpin back to a level plane
	//$(".popUp").parent().css("z-index", BAWOptions.zindex-1);
	$(".popUp").remove();
	
	// Register the object for our handlers
	pushpins.push({guid : guid,
				   drop : false,
				   mapDrop : true,
				   showing : true
				   });
				   
	var offsetx = GetOffset(BAWOptions.offsetx, coordx, 'x');
	var offsety = GetOffset(BAWOptions.offsety, coordy, 'y');
	
	// Create the popUp in the DOM and assign an unique ID
	//$("#" + guid).append(content);
	//$("#" + guid + " .popUp").addClass(guid);
	$("body").append(content);
	$(".popUp").addClass(guid);
	
	// Bring this pushpin and popUp to the front
	//$("." + guid).parent().css("z-index", BAWOptions.zindex);
	// Show popUp by unique ID
	$("." + guid).css("top", coordy + offsety).css("left", coordx + offsetx).css("z-index", "10001").fadeIn(BAWOptions.fadeSpeed);
	// Register an event for the search box
	$(".searchField").keypress(ListenForKeypress);
	
	// Register the close button top right of pop up
	$(".close").click(function(){
		var el = pushpins.GetShowing();
		el.drop = true;
		RemovePopUp();
	});
	
	// Register entry to stop map clicks killing the box
	$("#" + guid + ", ." + guid).mouseenter(function(){
		pushpins.GetShowing().drop = false;
		$("#map").unbind("click");
	// But exit should kill the box with timeout or click
	}).mouseleave(function(){
		$("#map").click(function() { 
			var el = pushpins.GetShowing();
			el.drop = true;
			RemovePopUp();
		});
		pushpins.GetShowing().drop = true;
		var timeoutCallback = "RemovePopUp()";
		setTimeout(timeoutCallback, BAWOptions.timeVisible);
	});
}

function RemovePopUp()
{
	var el = pushpins.GetShowing();
	if(el.drop)
	{
		el.showing = false;
		$(".popUp").fadeOut(BAWOptions.fadeSpeed, function(){$(this).parent().css("z-index", BAWOptions.zindex-1);$(this).remove();});
		// Unregister the click events
		$("#map, #" + el.guid + " .close").unbind("click");
	}
}

// Gets the offest for x or y coordinate
function GetOffset(offset, pushpinPosition, axis)
{
	var el = $("#map")
	
	var mapWidth = el[0].clientWidth + findPosXGlobal(el[0]);
	var mapHeight = el[0].clientHeight + findPosYGlobal(el[0]);

	switch(axis)
	{
		case "x":
			if(pushpinPosition + BAWOptions.infoWidth + offset + BAWOptions.pushpinWidth > mapWidth)
				// Draw the pushpin content on the other side of the pushpin
				return - offset - BAWOptions.infoWidth;
			else
				return offset + BAWOptions.pushpinWidth;
			break;
		case "y":
			if(pushpinPosition + BAWOptions.infoHeight + offset > mapHeight)
				return mapHeight - BAWOptions.infoHeight - pushpinPosition;
			else if(pushpinPosition + offset < 0)
				return -pushpinPosition;
			else return offset;
			break;
	}
	return 0;
}

// Utility method to get the x coordinate of the current pushpin within the map element
function findPosX(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
	while(1) 
	{	
		if(obj.id && obj.id == "map")//!obj.offsetParent)
			break;
		curleft += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosXGlobal(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
	while(1) 
	{	
		curleft += obj.offsetLeft;
		obj = obj.offsetParent;
		if(!obj.offsetParent)
			break;
	}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

// Utility method to get the y coordinate of the current pushpin within the map element
function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
	while(1)
	{
		if(obj.id && obj.id == "map")//!obj.offsetParent)
			break;
		curtop += obj.offsetTop;
		obj = obj.offsetParent;
	}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}

function findPosYGlobal(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
	while(1)
	{
		curtop += obj.offsetTop;
		obj = obj.offsetParent;
		if(!obj.offsetParent)
			break;
	}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}

// This prototype on Array gets the currently active pushpin
function MyGet()
{
	for(var i =0; i<this.length;i++)
	{
		if(this[i].showing)
			return this[i];
	}
	// nothing was showing - we act only if drop is true, so return a false
	return { drop : false };
}
