var mapMini = null;
var mapOpen = false;

function LoadMap_(controlID, latitude, longitude)
{
    //var prefix = controlID.substring(0, controlID.lastIndexOf("_maplisting_"));
    var mlsID = controlID.substring(controlID.substring(0, controlID.lastIndexOf("_")).lastIndexOf("_") + 1);
    if(mapOpen) HideMap();
    
    var zoom = 17;
    var mode = 'h';
	var body = null;
	
	if(mapOpen || !latitude || latitude == 0.0 || !longitude || longitude == 0.0)
	    return;
	
	var mapHolder = document.getElementById('mapContainer');
	var mapDiv = document.getElementById('divMap');
	mapHolder.style.width = document.getElementById("listing_"+mlsID).offsetWidth + 'px';
	mapHolder.style.height = document.getElementById("listing_"+mlsID).offsetHeight + 'px';
	var xyPos = GetXYPosition("listing_"+mlsID);
	mapHolder.style.left = xyPos.x + 'px';
	mapHolder.style.top = xyPos.y + 'px';
	
	mapDiv.style.width = document.getElementById("listing_"+mlsID).offsetWidth + 'px';
	mapDiv.style.height = (document.getElementById("listing_"+mlsID).offsetHeight-15) + 'px';

	if(!mapMini)
	{
		mapMini = new VEMap(mapDiv.id);
		mapMini.SetDashboardSize(VEDashboardSize.Small);
		mapMini.LoadMap(new VELatLong(latitude, longitude ), zoom, mode, false);
		mapMini.AttachEvent("onmouseover", mouseOutOverCB);
		mapMini.AttachEvent("onmouseout", mouseOutOverCB);
		mapMini.ClearInfoBoxStyles();
	}
	mapMini.SetCenterAndZoom(new VELatLong(latitude, longitude ), zoom);
	mapMini.AddPushpin(new VELatLong(latitude, longitude ));
	mapHolder.style.visibility = 'visible';
	mapOpen = true;
}
function ListingMapUnload()
{
    if (mapMini != null){
        mapMini.Dispose();
		mapMini = null;
	}
}
function HideMap()
{
	if(document.getElementById('mapContainer'))
		document.getElementById('mapContainer').style.visibility = 'hidden';
	mapOpen = false;
}
function GetXYPosition(control){
	var ctl = document.getElementById(control);
    var xyCoords = { x: ctl.offsetLeft, y: ctl.offsetTop };
    
    //Internet Explorer Requires Offset of all parents
    if(navigator.appName == 'Microsoft Internet Explorer'){
        tempcontrol = ctl.offsetParent;
        while(tempcontrol)
        {
            if(tempcontrol.offsetParent){
            xyCoords.x += tempcontrol.offsetLeft;
            xyCoords.y += tempcontrol.offsetTop;
            }
            tempcontrol = tempcontrol.offsetParent;
         }
    }else{
         xyCoords.x += ctl.offsetParent.offsetLeft;
         xyCoords.y += ctl.offsetParent.offsetTop;
    }
	ctl = null;
    return xyCoords;
}
function mouseOutOverCB(e)
{
    if (e.elementID != null)
    {
		//Cancel default action
		return true;
    }
}
(function()
{
var mouseEvt;
if (typeof document.createEvent !== 'undefined')
{
mouseEvt = document.createEvent('MouseEvents');
}
if (mouseEvt && mouseEvt.__proto__ && mouseEvt.__proto__.__defineGetter__)
{
mouseEvt.__proto__.__defineGetter__('pageX', function()
{
return this.clientX + window.pageXOffset;
});
mouseEvt.__proto__.__defineGetter__('pageY', function()
{
return this.clientY + window.pageYOffset;
});
}
})();
