var Map = null; var Shape = null; var ZoomLevel = null; var ErrorMessage = null; var CenterPoint = null; var SingleShape = null; var NavigationSize = null; var EnableResizing = true; var ParentPage = null; var IsIE = false; var IsIE6OrEarlier = false; var EnableLoadMap = true; var EnableRepos = false; var OldMapCenter = null; var IsLockedMap = false; var Token = null;
var MultipleCenterPoints = null; var Restaurant = null; var Nightlife = null; var Sightseeing = null; var Shopping = null; var Sports = null; var General = null; var Destinations = null;
var FoundResultsLayer = null; var RestaurantLayer = null; var NightlifeLayer = null; var SightseeingLayer = null; var ShoppingLayer = null; var SportsLayer = null; var GeneralLayer = null; var DestinationsLayer = null;
var ShapeIDs = null; var ShapeLinks = null;

function InitializeMap()
{
    if (window.attachEvent)
    {
        window.attachEvent("onload", PreLoadMap);
        window.attachEvent("onunload", DisposeMap); 
    }
    else
    {
        window.addEventListener("DOMContentLoaded", PreLoadMap, false);
        window.addEventListener("unload", DisposeMap, false);
    }
}

function PreLoadMap()
{
    window.setTimeout("LoadMap()", 200);
}

function DisposeMap()
{
    if (Map != null)
    {
        Map.Dispose();
        Map = null;
    }
}

function LoadMap()
{
	try
	{
		if((MultipleCenterPoints != null) || (SingleShape != null) || (Restaurant != null) || (Nightlife != null) || (Sightseeing != null) || (Shopping != null) || (Sports != null) || (General != null) || (Destinations != null))
		{
			// Check if the Browser is IE and Version smaller than 7
			if(navigator.appName.indexOf("Microsoft") != -1)
			{
				IsIE = true;
				if(Number(navigator.appVersion.substr(navigator.appVersion.indexOf("MSIE") + 5, 1)) < 7)
					IsIE6OrEarlier = true;
			}
			
			if(ZoomLevel == null)
				ZoomLevel = 10;
			
			// Set an inital centerpoint if no other is set yet
			if(CenterPoint == null)
			{			
				if((SingleShape != null) && (SingleShape.length > 0))
					CenterPoint = SingleShape[0][0];
				else if((MultipleCenterPoints != null) && (MultipleCenterPoints.length > 0))
					CenterPoint = MultipleCenterPoints[0];
				else if((Restaurant != null) && (Restaurant.length > 0))
					CenterPoint = Restaurant[0][0];
				else if((Nightlife != null) && (Nightlife.length > 0))
					CenterPoint = Nightlife[0][0];
				else if((Sightseeing != null) && (Sightseeing.length > 0))
					CenterPoint = Sightseeing[0][0];
				else if((Shopping != null) && (Shopping.length > 0))
					CenterPoint = Shopping[0][0];
				else if((Sports != null) && (Sports.length > 0))
					CenterPoint = Sports[0][0];
				else if((General != null) && (General.length > 0))
					CenterPoint = General[0][0];
			}
			
			ShapeIDs = new Array();
			ShapeLinks = new Array();
			
			var NumberOfTurns = 1;
			if((MultipleCenterPoints != null) && (MultipleCenterPoints.length > 0))
			    NumberOfTurns = MultipleCenterPoints.length;
			
			// In case that there are more than one Map per Page
			for (i = 0; i < NumberOfTurns; i++)
		    {
		        var CurrentMapIndex = -1;		        
		        if((MultipleCenterPoints != null) && (MultipleCenterPoints.length > i))
		        {
			        CurrentMapIndex = i;
			        CenterPoint = MultipleCenterPoints[i];
			    }
			    
			    // Create and load the Map
			     Map = new VEMap('TravelMap' + CurrentMapIndex);
			    
			    // Mouse-Event
			    Map.AttachEvent('onclick', OnMouseClick);
			    Map.AttachEvent('onmouseover', OnMouseOver);
			    
			    // Token
			    Map.SetClientToken(Token);
			    Map.AttachEvent('ontokenexpire', MyHandleTokenExpire);
			    Map.AttachEvent('ontokenerror', MyHandleTokenError);
			    if(EnableRepos)
			        EnablePixelToLatLong();
    			
			    if(NavigationSize != null)
				    Map.SetDashboardSize(NavigationSize);
				
			    if(EnableLoadMap)
			    {
				    Map.LoadMap(CenterPoint, ZoomLevel, "r", IsLockedMap, VEMapMode.Mode2D, true);
				    Map.ClearInfoBoxStyles();
				    Map.SetScaleBarDistanceUnit(VEDistanceUnit.Kilometers);
			    }
			    else
				    ShowErrorMessage(ErrorMessage);
    			
			    // Create Shapes and InfoBoxes
			    if((IsIE6OrEarlier) && (Shape != null) && (Shape != ""))
			    {
				    // Workarround:
				    // IE 6 and earlier freezes when no Infobox is being displayed at startup!!
				    // After showing the last created Shape, hide it again after 500 ms.
				    DisplayInfoBox(Shape);
				    setTimeout("HideInfoBox()", 500);
			    }
			    
			    // Create Single Shape to set the Position of one Point in Travelfeedback
			    if((MultipleCenterPoints != null) && (MultipleCenterPoints.length > 0))
			    {
				    SingleCenterPoint = new VEShape(VEShapeType.Pushpin, MultipleCenterPoints[i]);
				    SingleCenterPoint.SetCustomIcon("<img src='/Images/symb_map_camera.png' border='0' style='cursor:default;' />");
				    SingleCenterPoint.SetTitle("");
				    Map.AddShape(SingleCenterPoint);
				    if(!IsLockedMap)
				        setTimeout("DisplayInfoBox(SingleShape)", 500);
			    }
			    if((Restaurant != null) && (Restaurant.length > 0))
			    {
				    RestaurantLayer = new VEShapeLayer();
				    Map.AddShapeLayer(RestaurantLayer);
    				
				    for (var i = 0; i < Restaurant.length; i++)
					    AddShape(RestaurantLayer, Restaurant[i][0], Restaurant[i][1], Restaurant[i][2], Restaurant[i][3]);
			    }
			    if((Nightlife != null) && (Nightlife.length > 0))
			    {
				    NightlifeLayer = new VEShapeLayer();
				    Map.AddShapeLayer(NightlifeLayer);
    				
				    for (var i = 0; i < Nightlife.length; i++)
					    AddShape(NightlifeLayer, Nightlife[i][0], Nightlife[i][1], Nightlife[i][2], Nightlife[i][3]);
			    }
			    if((Sightseeing != null) && (Sightseeing.length > 0))
			    {
				    SightseeingLayer = new VEShapeLayer();
				    Map.AddShapeLayer(SightseeingLayer);
    				
				    for (var i = 0; i < Sightseeing.length; i++)
					    AddShape(SightseeingLayer, Sightseeing[i][0], Sightseeing[i][1], Sightseeing[i][2], Sightseeing[i][3]);
			    }
			    if((Shopping != null) && (Shopping.length > 0))
			    {
				    ShoppingLayer = new VEShapeLayer();
				    Map.AddShapeLayer(ShoppingLayer);
    				
				    for (var i = 0; i < Shopping.length; i++)
					    AddShape(ShoppingLayer, Shopping[i][0], Shopping[i][1], Shopping[i][2], Shopping[i][3]);
			    }
			    if((Sports != null) && (Sports.length > 0))
			    {
				    SportsLayer = new VEShapeLayer();
				    Map.AddShapeLayer(SportsLayer);
    				
				    for (var i = 0; i < Sports.length; i++)
					    AddShape(SportsLayer, Sports[i][0], Sports[i][1], Sports[i][2], Sports[i][3]);
			    }
			    if((General != null) && (General.length > 0))
			    {
				    GeneralLayer = new VEShapeLayer();
				    Map.AddShapeLayer(GeneralLayer);
    				
				    for (var i = 0; i < General.length; i++)
					    AddShape(GeneralLayer, General[i][0], General[i][1], General[i][2], General[i][3]);
			    }
			    if((Destinations != null) && (Destinations.length > 0))
			    {
				    DestinationsLayer = new VEShapeLayer();
				    Map.AddShapeLayer(DestinationsLayer);
    				
				    for (var i = 0; i < Destinations.length; i++)
					    AddShape(DestinationsLayer, Destinations[i][0], Destinations[i][1], Destinations[i][2], Destinations[i][3]);
			    }
			    // Create Single Shape to set the Position of one Point in Travelfeedback
			    if((SingleShape != null) && (SingleShape != ""))
			    {
				    var Temp = SingleShape;
				    SingleShape = new VEShape(VEShapeType.Pushpin, Temp[0][0]);
				    SingleShape.SetDescription(Temp[0][1]);
				    SingleShape.SetCustomIcon("<img src='" + Temp[0][2] + "' border='0' style='cursor:default;' />");
				    SingleShape.SetTitle("");
				    Map.AddShape(SingleShape);
				    
				    ShapeIDs[ShapeIDs.length] = SingleShape.GetID();
	                ShapeLinks[ShapeLinks.length] = Temp[0][3];
				    
				    if(!IsLockedMap)
				        setTimeout("DisplayInfoBox(SingleShape)", 500);
			    }
			}
		}
		else
			ShowErrorMessage(ErrorMessage);
	}
	catch(VEException)
	{
		//alert("Message: " + VEException.message + ". Name: " + VEException.name + ". Source: " + VEException.source);
		//ShowErrorMessage(ErrorMessage);
	}
}

function AddShape(LayerName, LatLong, ToolTip, Icon, Link)
{
	if(IsIE6OrEarlier)
		Icon = Icon.replace(/png/, "gif");
		
	Shape = new VEShape(VEShapeType.Pushpin, LatLong);
	Shape.SetDescription(ToolTip);
	Shape.SetCustomIcon("<img src='" + Icon + "' border='0' style='cursor:default;' />");
	Shape.SetTitle("");
	LayerName.AddShape(Shape);
	
	ShapeIDs[ShapeIDs.length] = Shape.GetID();
	ShapeLinks[ShapeLinks.length] = Link;
}

function RePos(Lat, Long)
{
    if(SingleShape != null)
    {
        var NewPoints = new VELatLong(Lat, Long);
        SingleShape.SetPoints(NewPoints);
        Map.SetCenter(NewPoints);
    }
        
    document.getElementById('ctl00_CenterContent_GeoLatitude').value = Lat;
    document.getElementById('ctl00_CenterContent_GeoLongitude').value = Long;
}

function OnMouseClick(e)
{
    if((e!=null) && (e.elementID!=null))
    {
        if (e.leftMouseButton) 
        {
            for(i = 0; i < ShapeIDs.length; i++)
            {
                if(e.elementID.indexOf(ShapeIDs[i]) != -1)
                    window.location.href = ShapeLinks[i];
            }
        }
    }
    
    return false;
}

function OnMouseOver(e)
{
    if((e!=null) && (e.elementID!=null))
    {
        var TempShape = Map.GetShapeByID(e.elementID);
        DisplayInfoBox(TempShape);
    }
    
    return false;
}

function DisplayInfoBox(Shape)
{
	Map.ShowInfoBox(Shape);
}

function HideInfoBox()
{
	Map.HideInfoBox();
}

function FindResults()
{
	if(document.getElementById('What').value != "")
	{
		if(FoundResultsLayer != null)
			Map.DeleteShapeLayer(FoundResultsLayer);
		FoundResultsLayer = new VEShapeLayer();
		Map.AddShapeLayer(FoundResultsLayer);
		FoundResultsLayer.Hide();
		Map.Find(document.getElementById('What').value, null, VEFindType.Businesses, FoundResultsLayer, 1, 20, true, true, true, false);
		document.getElementById('DeleteLayer').style.display = 'block';
		// Wait until all Shapes are avalable on the Layer...
		window.setTimeout("ChangeFindResultsIcons()", 1000);
	}
}

function ChangeFindResultsIcons()
{
	for(i = 0; i < FoundResultsLayer.GetShapeCount(); i++)
	{
		var TempShape = FoundResultsLayer.GetShapeByIndex(i);	
		TempShape.SetCustomIcon('/images/symb_map_tf_nightlife.GIF');
	}
	FoundResultsLayer.Show();
}

function MyHandleTokenExpire()
{
   location.reload();
}

function MyHandleTokenError()
{
   location.reload();
}

function DeleteResults()
{
	if(FoundResultsLayer != null)
		Map.DeleteShapeLayer(FoundResultsLayer);
	document.getElementById('DeleteLayer').style.display = 'none';
}

function EnablePixelToLatLong()
{
    Map.AttachEvent('ondoubleclick', PixelToLatLong);
}

function DisablePixelToLatLong()
{
	Map.DetachEvent("ondoubleclick", PixelToLatLong);
}

function PixelToLatLong(e)
{
    var x = e.mapX;
    var y = e.mapY;
    var LatLong = Map.PixelToLatLong(new VEPixel(x,y));

    if(SingleShape != null)
    {
        var NewPoints=[new VELatLong(LatLong.Latitude, LatLong.Longitude)];
        SingleShape.SetPoints(NewPoints);
    }
        
    document.getElementById('ctl00_CenterContent_GeoLatitude').value = LatLong.Latitude;
    document.getElementById('ctl00_CenterContent_GeoLongitude').value = LatLong.Longitude;

    return false;
}

function MapResize()
{
	if(!IsIE6OrEarlier)
	{
		if(Map != null)				
			OldMapCenter = Map.GetCenter();			
		SetNewLayout();
	}
}

function SetNewLayout()
{
	var VerticalSpace = 150;
	var HorizontalSpace = 207;
	var NewHeight = null;
	var NewWidth = null;
		
	if(EnableResizing)
	{
		if(ParentPage == 'Overview')
			HorizontalSpace = 195;
		
		NewHeight = document.body.offsetHeight;
		NewWidth = document.body.offsetWidth;		
		VerticalSpace += document.getElementById('mobs_hoteldetail_content_maps').offsetTop;
			
		if(document.getElementById('Navigation').style.display == 'none')
		{
			if(ParentPage == 'Overview')
				HorizontalSpace = 30;
			else
				HorizontalSpace = 42;
		}
		
		NewWidth = NewWidth - HorizontalSpace;
		document.getElementById('MapContent').style.width = NewWidth + 'px';
		
		if((NewHeight - VerticalSpace) > 410)
		{
			NewHeight = NewHeight - VerticalSpace;
			document.getElementById('MapContent').style.height = NewHeight + 'px';
		}
		else
		{
			NewHeight = 440;
			document.getElementById('MapContent').style.height = '440px';
		}
		
		if(Map != null)
		{
			Map.Resize(NewWidth + 15, NewHeight);
			if(OldMapCenter != null)
				Map.PanToLatLong(OldMapCenter);
		}
	}
}

function ShowErrorMessage(Message)
{
	document.getElementById('TravelMap').className = "travel_map_without_waitwheel";
	document.getElementById('TravelMap').innerHTML = "<br /><br /><table width='100%'><tr><td align='center' valign='middle'><strong>" + Message + "</strong></td></tr></table>";
}

function ShowHideMapLayer(Checkbox, LayerId)
{
	if(LayerId != null)
	{
		if(Checkbox.checked == true)
			LayerId.Show();
		else
			LayerId.Hide();
    }
}
