var map;
var markerGroups = { "SHOPPING": [], "TOURISM": []};
var currLayerId = "citytext";

function showCityTab()
{
	showhideMarkers('SHOPPING', 'hide'); 
	showhideMarkers('TOURISM', 'hide'); 
	changeBodyClass('sidebar-right', 'nosidebar');

	map.closeInfoWindow();
}


function showShoppingTab()
{
	showhideMarkers('SHOPPING', 'show'); 
	showhideMarkers('TOURISM', 'hide'); 
	changeBodyClass('nosidebar', 'sidebar-right'); 
	

	var listCollection = document.getElementById('sidebar').getElementsByTagName('li');
	for (j=0;j < listCollection.length;j++) 
	{
		if (listCollection[j].className == "SHOPPING")
		{
			listCollection[j].style.display = "block";
		}
		else 
		{
			listCollection[j].style.display = "none";
		}
	}

	map.closeInfoWindow();
}

function showTourismTab()
{
	showhideMarkers('TOURISM', 'show'); 
	showhideMarkers('SHOPPING', 'hide'); 
	changeBodyClass('nosidebar', 'sidebar-right'); 
	

	var listCollection = document.getElementById('sidebar').getElementsByTagName('li');
	for (j=0;j < listCollection.length;j++) 
	{
		if (listCollection[j].className == "TOURISM")
		{
			listCollection[j].style.display = "block";
		}
		else 
		{
			listCollection[j].style.display = "none";
		}
	}

	map.closeInfoWindow();
}


function togLayer(id)
{
	if(currLayerId) setDisplay(currLayerId, "none");
	if(id)setDisplay(id, "block");
	currLayerId = id;
}

function setDisplay(id,value)
{
	var elm = document.getElementById(id);
	elm.style.display = value;
}

function tabhighlight(obj)
{
	var navBarLinkCollection = document.getElementById('navbar').getElementsByTagName('a');
	var j;

	for (j=0;j < navBarLinkCollection.length;j++) 
	{
		if (obj == navBarLinkCollection[j]) 
		{
			navBarLinkCollection[j].style.backgroundColor = '#ffffff';
			navBarLinkCollection[j].style.textDecoration = 'none';
		}
		else
		{
/*			navBarLinkCollection[j].style.display = 'inline';
			navBarLinkCollection[j].style.borderTop = "1px solid #777788";
			navBarLinkCollection[j].style.borderLeft = "1px solid #777788";
			navBarLinkCollection[j].style.borderRight = "1px solid #777788";
			navBarLinkCollection[j].style.color = "#2b4460";
			navBarLinkCollection[j].style.marginLeft = "3px";
			navBarLinkCollection[j].style.marginRight = "3px";
			navBarLinkCollection[j].style.paddingTop = "3px";
			navBarLinkCollection[j].style.paddingLeft = "5px";
			navBarLinkCollection[j].style.paddingRight = "5px";
*/			navBarLinkCollection[j].style.textDecoration = 'none';
			navBarLinkCollection[j].style.backgroundColor = "#bbccbb";
		}
	}

}

function showhideMarkers(sitetype, showhide) 
{
	for (var i = 0; i < markerGroups[sitetype].length; i++) 
	{
		var marker = markerGroups[sitetype][i];
		if (showhide=="show") 
		{
			marker.show();
		} 
		else 
		{
			marker.hide();
		}
	} 
}


function addMarker(latitude, longitude, name, description, sitetype, iconImage)
{
	var point = new GLatLng(latitude, longitude);

	if (iconImage!='')
	{
		var icon = new GIcon();
		icon.image = iconImage;
		icon.iconSize = new GSize(25,25);
		icon.iconAnchor = new GPoint(0, 0);
		icon.infoWindowAnchor = new GPoint(0,0);
		var marker = new GMarker(point, icon);
	}
	else
	{
		var marker = new GMarker(point);
	}
	
/*	var listItem = document.createElement('li');
	var listItemLink = listItem.appendChild(document.createElement('a'));
	listItemLink.href = "#";
	listItemLink.innerHTML = "<b>" + name + "</b><br /><br />" + description;
	
	var focusPoint = function() {
		deselectCurrent();
		listItem.className = 'current';
		deselectCurrent = function() { listItem.className = ''; }
		marker.openInfoWindowHtml("<b>" + name + "</b>");
		map.panTo(point);
		return false;
	}

	GEvent.addListener(marker, 'click', focusPoint);	
	listItemLink.onclick = focusPoint;

	document.getElementById('sidebar-list').appendChild(listItem);
*/
	GEvent.addListener(marker, 'click', 
		function() {		
			marker.openInfoWindowHtml("<strong>" + name + "</strong>");
		}
	);

	map.addOverlay(marker);
	marker.hide();
	markerGroups[sitetype].push(marker);	
}


function retrieveMarkers() 
{
	var icon;
	
	for (var i = 0; i < markers.length; i++) 
	{
		var longitude = markers[i]["longitude"];
		var latitude = markers[i]["latitude"];

		if(longitude && latitude) 
		{
			if (markers[i]["sitetype"] == "SHOPPING")
			{
				icon = "images/blue" + String.fromCharCode(65+i) + ".gif";
			}
			else
			{
				icon = "images/purple" + String.fromCharCode(65+i) + ".gif";
			}
			addMarker(latitude, longitude, markers[i]["name"], markers[i]["description"], markers[i]["sitetype"], icon);
		}
	}
	
/*	changeBodyClass('loading', 'standby');*/
}

var deselectCurrent = function() {};

function windowHeight() 
{
	if (self.innerHeight) return self.innerHeight;
	if (document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight;

	return 0;
}

function handleResize() 
{
	var height = windowHeight() - document.getElementById('toolbar').offsetHeight - document.getElementById('header').offsetHeight -5;
	document.getElementById('map').style.height = height + 'px';
	document.getElementById('sidebar').style.height = height + 'px';
}

function changeBodyClass(from, to) 
{
     document.body.className = document.body.className.replace(from, to);
     return false;
}

function Gload() 
{
/*
document.getElementById('button-sidebar-hide').onclick = function() { showhideMarkers(); return changeBodyClass('sidebar-right', 'nosidebar'); };
	document.getElementById('button-sidebar-show').onclick = function() { showhideMarkers(); return changeBodyClass('nosidebar', 'sidebar-right'); };
*/
	changeBodyClass('sidebar-right', 'nosidebar');
	
	handleResize();

	if (GBrowserIsCompatible()) 
	{
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(centralLatitude, centralLongitude), startZoom);
		map.addControl(new GSmallMapControl());
		map.addControl(new GHierarchicalMapTypeControl());
	
		retrieveMarkers();
	}
}
    
function getQueryVariable(variable) 
{
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) 
	{
		var pair = vars[i].split("=");
		if (pair[0] == variable) 
		{
			return pair[1];
		}
	}
	
	return "map";
}


function initTabs() 
{
	var selecttab = getQueryVariable("b");

	if(selecttab=='tourism')
	{
		showTourismTab(); 
		tabhighlight(document.getElementById('tourismtab'));
	}
	else if(selecttab=='shopping')
	{
		showShoppingTab(); 
		tabhighlight(document.getElementById('shoppingtab'));
	}
	else 
	{
		showCityTab(); 
		tabhighlight(document.getElementById('citytab'));
	}
}

	

function start() {
  Gload();
  initTabs();
}

window.onload = start;

window.onunload = GUnload;
window.onresize = handleResize;

