// setup for smith maps

var Map = new Map();	
var MapControl = new MapControl(Map);
Map.getPlacesUrl = '/get_map_places.php';
Map.getLocationUrl = '/get_map_location.php';
Map.iconPath = '/images/mms-maps/icons';


function createIcon(image)
{
	var icon = new GIcon();
	icon.image = Map.iconPath + '/' + image;
	icon.iconSize = new GSize(18, 23);
	icon.iconAnchor = new GPoint(0, 23);
	icon.infoWindowAnchor = new GPoint(0, 23);
	return icon;
}

// set type icons
Map.setTypeDefault('icon', createIcon('mini.hotel.icon.png'));
Map.setTypeValue('Bar', 'icon', createIcon('mini.bar.icon.png'));
Map.setTypeValue('Club', 'icon', createIcon('mini.club.icon.png'));
Map.setTypeValue('Cafe', 'icon', createIcon('mini.cafe.icon.png'));
Map.setTypeValue('Restaurant', 'icon', createIcon('mini.restaurant.icon.png'));


//destination icon
var icon = new GIcon();
icon.image = Map.iconPath + '/pin.png';
icon.iconSize = new GSize(7, 12);
icon.iconAnchor = new GPoint(3, 12);
icon.infoWindowAnchor = new GPoint(3, 12);

Map.setTypeValue('Destination', 'icon', icon);


// set type zoom settings
Map.setTypeDefault('minZoom', 12);
Map.setTypeDefault('maxZoom', 17);

Map.setTypeValue('Hotel', 'minZoom', 10);

Map.setTypeValue('Destination', 'minZoom', 0);
Map.setTypeValue('Destination', 'maxZoom', 9);

Map.setTypeDefault('click', showOverlay);


var activeMarker = false;

var infoTemplate = new Template('\
<div class="info-bg"></div>\
<div class="info-main">\
	<a id="close" "href="#" onclick="return closeOverlay();">close</a>\
	<div class="info-content">\
		<h2><a href="#{link}">#{name}</a></h2>\
		<p class="info-desc">#{description}</p>\
		<p>#{telephone}</p>\
	</div>\
	<div class="info-footer blk">\
		<a href="#{link}">more info</a>\
	</div>\
</div>\
');

var hotelInfoTemplate = new Template('\
<div class="info-bg"></div>\
<div class="info-main">\
	<a id="close" "href="#" onclick="return closeOverlay();">close</a>\
	<div class="info-image"	style="background-image: url(#{image});">\
	</div>\
	<a class="photos-link" href="#{link}gallery">more photos</a>\
	<div class="info-content">\
		<h2><a href="#{link}">#{name}</a></h2>\
		<h3>#{location}, #{country}</h3>\
		<ul>\
			<li>#{highlight_1}</li>\
			<li>#{highlight_2}</li>\
			<li>#{highlight_3}</li>\
		</ul>\
		<p>#{address}</p>\
	</div>\
	<div class="info-footer">\
		<a class="book" href="#{booking_link}">book now</a>\
		<a href="#{link}">more info</a>\
<span class="fromprice">#{doubles_from}</span>\
	</div>\
</div>\
');

// <p>Doubles from &pound;#{doubles_from} - &pound;#{doubles_to}</p>\

var destInfoTemplate = new Template('\
<div class="info-bg"></div>\
<div class="info-main">\
	<a id="close" "href="#" onclick="return closeOverlay();">close</a>\
	<div class="info-image" style="background-image: url(#{image})"></div>\
	<div class="info-content">\
		<h2><a href="#" onclick="Map.setCenter(#{lat}, #{lng}, #{zoom}); return false;">#{name}</a></h2>\
		<h3>#{country}</h3>\
		<p class="cityscape">\
    	    <strong>#{cityscape_title}</strong> #{cityscape}<br />\
    	    <strong>#{citylife_title}</strong> #{citylife}\
    	</p>\
		<p>#{desc}</p>\
	</div>\
	<div class="info-footer">\
		<a href="#{link}">more info</a>\
		<a class="zoom" href="#" onclick="Map.setCenter(#{lat}, #{lng}, #{zoom}); return false;">zoom in</a>\
	</div>\
</div>\
');


function showOverlay(markerKey)
{
	if (activeMarker)
	{
		closeOverlay();
	}
	
	patt = /[a-z]+_\d+/;

	if (markerKey != null && typeof(markerKey) != 'object')
	{
		activeMarker = Map.getMarker(markerKey);
	}
	else
	{
		activeMarker = this;
	}
	
	if (!activeMarker) return;

	activeMarker.hide();

	if (link = $($(activeMarker.place.key + '-link')))
	{
		link.addClassName('active');
	}
	
	if (activeMarker.place.type == 'Hotel')
	{
		activeMarker.overlay = new InfoBox(activeMarker, 517, 212, 'map-info-box', hotelInfoTemplate, 240, -4);
	}
	else if (activeMarker.place.type == 'Destination')
	{
		activeMarker.overlay = new InfoBox(activeMarker, 517, 212, 'map-info-box destination', destInfoTemplate, 240, -4);
	}
	else
	{
		activeMarker.overlay = new InfoBox(activeMarker, 375, 187, 'map-info-box small', infoTemplate, 173, -6);
	}
	
	Map.setActiveKey(activeMarker.key);
	selectTab(typeToTab(activeMarker.place.type));
	Map.getMap().addOverlay(activeMarker.overlay);
	Map.getMap().panTo(new GLatLng((activeMarker.getPoint().lat() + (Map.getLatSpan() / 5) ), activeMarker.getPoint().lng()));
}

function closeOverlay()
{	
	
	if (activeMarker)
	{
		
	 	Map.getMap().removeOverlay(activeMarker.overlay);
		activeMarker.show();
		
		if (link = $($(activeMarker.place.key + '-link')))
		{
			link.removeClassName('active');
		}
	}
	Map.setActiveKey(false);
	activeMarker = false;
	return false;
}

function toggleIconType(linkId, type)
{
	return false;
	
	Map.toggleType(type);
	
	if (Map.getTypeStatus(type))
	{
		$($(linkId)).removeClassName('disabled');
	}
	else
	{
		$($(linkId)).addClassName('disabled');
	}
	
	return false;
}

var selectedTab = false;

function selectTab(id)
{
	if (!Map.showTabs) return; 
	
	selectedTab = id;
	
	var tab = $($(id + '-map-tab'));
	var content = $($(id + '-map-content'));
	
	tabs = ['destinations', 'hotels', 'cafes', 'restaurants', 'bars-and-clubs'];
	
	tabs.each(function(tabId) {
		
		if (tabId != id)
		{
			$($(tabId + '-map-tab')).removeClassName('active');
			$($(tabId + '-map-content')).hide();
		}
	});
	
	tab.addClassName('active');
	content.show();
	
	return false;
}



function showTabs()
{
	return;
	
	var zoom = Map.getMap().getZoom();
	
	if (zoom >= 12)
	{
		showTabs = ['hotels', 'cafes', 'restaurants', 'bars-and-clubs'];
		hideTabs = ['destinations'];
	} 
	else if (zoom >= 10)
	{
		showTabs = ['hotels'];
		hideTabs = ['destinations', 'cafes', 'restaurants', 'bars-and-clubs'];
	}
	else
	{
		showTabs = ['destinations'];
		hideTabs = ['hotels', 'cafes', 'restaurants', 'bars-and-clubs'];
	}
	
	hideTabs.each(function(tabId) {
		$($(tabId + '-map-tab')).hide();
	});
	
	showTabs.each(function(tabId) {
		$($(tabId + '-map-tab')).show();
	});
	
}

function typeToTab(type)
{
	var tabs = new Array();
	tabs['Destination'] = 'destinations';
	tabs['Hotel'] 		= 'hotels';
	tabs['Cafe'] 		= 'cafes';
	tabs['Restaurant'] 	= 'restaurants';
	tabs['Bar'] 		= 'bars-and-clubs';
	
	return tabs[type];
}

function updateContentTabs(currentMarkers)
{
	var tabs = new Array();
	tabs['Destination'] = 'destinations';
	tabs['Hotel'] 		= 'hotels';
	tabs['Cafe'] 		= 'cafes';
	tabs['Restaurant'] 	= 'restaurants';
	tabs['Bar'] 		= 'bars-and-clubs';
	

	var firstab = true;
	var defaultTab = false;
			
	for (type in tabs)
	{	
		var showtab = false;
		
		if ('function' != typeof tabs[type])
		{
			
			if (currentMarkers[type])
			{
				
				var html = '<ul>';
				var i = 1;
				var j = 1;
				
				for (key in currentMarkers[type])
				{
					if (j < 4)
					{						
						if ('function' != typeof currentMarkers[type][key])
						{
							showtab = true;
							
							name = currentMarkers[type][key];
							
							classHtml = '';
							if (activeMarker && activeMarker.place.key == key)
							{
								selectTab(tabs[type], type);
								classHtml = 'class="active"';
							}
							
							html += '<li><a href="#" id="' + key + '-link" ' + classHtml + ' onclick="showOverlay(\'' + key + '\')">' + name + '</a></li>\n';
							i++;
							if (i > 5)
							{
								i = 1;
								j++;
								html += '\n</ul><ul>'
							}
						}
					}
				}
				
				html += '</ul>';
				
				$(tabs[type] + '-map-content').innerHTML = html;
				
			}
			else
			{
				
				$(tabs[type] + '-map-content').innerHTML = '<p class="no-map-items">No items are in the current map area</p>';
			}
			
			if (showtab)
			{
				if (firstab)
				{
					defaultTab = tabs[type];
					firstab = false;
				}
				
				$($(tabs[type] + '-map-tab')).show();
				$($(tabs[type])).removeClassName('disabled');
			}
			else
			{
				if (tabs[type] == selectedTab)
				{
					selectedTab = false;
				}
				
				$($(tabs[type])).addClassName('disabled');
				$($(tabs[type] + '-map-tab')).hide();
			}
		}
			
	}
	
	if (selectedTab == false && defaultTab != false)
	{
		selectTab(defaultTab);
	}
	
}




window.onload = function() {
	Map.load();
	MapControl.load();
	populate_quick_links_maps();
	
	GEvent.addListener(Map.getMap(), "zoomend", closeOverlay);
	
	GEvent.addListener(Map.getMap(), "zoomend", showTabs);
}

window.onunload = function() {
	GUnload()
}




