var duplicate = 0;
var markers_buildings = new Array();
for(id in markers) {
	if(duplicate != parseInt(markers[id].key))
	{
		markers_buildings.push(markers[id]);
	}
	duplicate = parseInt(markers[id].key);
}
var markers_departments = new Array();
for(id in markers) {
	if(markers[id].department != '')
	{
		markers_departments.push(markers[id]);
	}
}
var map;
var centerLatitude = 41.60281;
var centerLongitude = -93.6537;
var startZoom = 16;
var deselectCurrent = function() {};
var currentMarker;

function ToolTip(marker,html,width) {
	this.html_ = html;
	this.width_ = (width ? width + 'px' : 'auto');
	this.marker_ = marker;
}

ToolTip.prototype = new GOverlay();

ToolTip.prototype.initialize = function(map) {
	var div = document.createElement("div");
	div.style.display = 'none';
	map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
	this.map_ = map;
	this.container_ = div;
}

ToolTip.prototype.remove = function() {
	this.container_.parentNode.removeChild(this.container_);
}

ToolTip.prototype.copy = function() {
	return new ToolTip(this.html_);
}

ToolTip.prototype.redraw = function(force) {
	if (!force) return;
	var pixelLocation = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
	this.container_.innerHTML = this.html_;
	this.container_.style.position = 'absolute';
	this.container_.style.left = pixelLocation.x + "px";
	this.container_.style.top = pixelLocation.y + "px";
	this.container_.style.width = this.width_;
	this.container_.style.font = 'bold 10px/10px verdana, arial, sans';
	this.container_.style.border = '1px solid black';
	this.container_.style.background = 'yellow';
	this.container_.style.padding = '4px';
	
	//one line to desired width
	this.container_.style.whiteSpace = 'nowrap';
	if(this.width_ != 'auto') {
		this.container_.style.overflow = 'hidden';
	}
	
	this.container_.style.display = 'block';
}

GMarker.prototype.ToolTipInstance = null;

GMarker.prototype.openToolTip = function(content) {
	//don't show the tool tip if there is a custom info window
	if(this.ToolTipInstance == null) {
		this.ToolTipInstance = new ToolTip(this,content);
		map.addOverlay(this.ToolTipInstance);
	}
}

GMarker.prototype.closeToolTip = function() {
	if(this.ToolTipInstance != null) {
		map.removeOverlay(this.ToolTipInstance);
		this.ToolTipInstance = null;
	}
}

/* [listing 6-20] */
function initializeBuildingPoint(pointData) {
	var point = new GLatLng(pointData.latitude, pointData.longitude);
	var icon = new GIcon();
	icon.image = "img.php?type=" + pointData.type + "&key=" + pointData.key;
	icon.iconSize = new GSize(18,18);
	icon.iconAnchor = new GPoint(18,18);
	var marker = new GMarker(point,icon);
	var listItem = document.createElement('li');
	listItem.className = pointData.type + '_class';
	var listItemLink = listItem.appendChild(document.createElement('a'));
	var visible = false;

	listItemLink.href = "#";
	listItemLink.innerHTML = '<b>' + pointData.key + '</b>&nbsp;' + pointData.description;
	
	var focusPoint = function() {
		deselectCurrent();
		listItem.className = 'current';
		deselectCurrent = function() { listItem.className = ''; }
		if(currentMarker != null) {
			currentMarker.closeToolTip();
			currentMarker = null;
		}
		currentMarker = marker;
		marker.openToolTip(pointData.description);
		map.panTo(point);
		return false;
	}

	var openTip = function() {
		if(currentMarker != null) {
			currentMarker.closeToolTip();
			currentMarker = null;
		}
		currentMarker = marker;
		marker.openToolTip(pointData.description);
		return false;
	}
	
	var closeTip = function() {
		marker.closeToolTip();
		return false;
	}

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

	GEvent.addListener(marker, 'mouseover', openTip);
	
	GEvent.addListener(marker, 'mouseout', closeTip);

	pointData.showBuilding = function() {
		if (!visible) {
			document.getElementById('bottombar-list-building').appendChild(listItem);
			map.addOverlay(marker);
			visible = true;
		}
	}
	pointData.hideBuilding = function() {
		if (visible) {
			document.getElementById('bottombar-list-building').removeChild(listItem);
			map.removeOverlay(marker);
			visible = false;
		}
	}

	pointData.showBuilding();
}

function initializeDepartmentPoint(pointData) {
	var point = new GLatLng(pointData.latitude, pointData.longitude);
	var icon = new GIcon();
	icon.image = "img.php?type=" + pointData.type + "&key=" + pointData.key;
	icon.iconSize = new GSize(18,18);
	icon.iconAnchor = new GPoint(18,18);
	var marker = new GMarker(point,icon);
	var listItem = document.createElement('li');
	listItem.className = pointData.type + '_class';
	var listItemLink = listItem.appendChild(document.createElement('a'));
	var visible = false;

	listItemLink.href = "#";
	listItemLink.innerHTML = '<b>' + pointData.key + '</b>&nbsp;' + pointData.department;
	
	var focusPoint = function() {
		deselectCurrent();
		listItem.className = 'current';
		deselectCurrent = function() { listItem.className = ''; }
		if(currentMarker != null) {
			currentMarker.closeToolTip();
			currentMarker = null;
		}
		currentMarker = marker;
		marker.openToolTip(pointData.description);
		map.panTo(point);
		return false;
	}

	var openTip = function() {
		if(currentMarker != null) {
			currentMarker.closeToolTip();
			currentMarker = null;
		}
		currentMarker = marker;
		marker.openToolTip(pointData.description);
		return false;
	}
	
	var closeTip = function() {
		marker.closeToolTip();
		return false;
	}

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

	GEvent.addListener(marker, 'mouseover', openTip);
	
	GEvent.addListener(marker, 'mouseout', closeTip);

	pointData.showDepartment = function() {
		if (!visible) {
			document.getElementById('bottombar-list-department').appendChild(listItem);
			map.addOverlay(marker);
			visible = true;
		}
	}
	pointData.hideDepartment = function() {
		if (visible) {
			document.getElementById('bottombar-list-department').removeChild(listItem);
			map.removeOverlay(marker);
			visible = false;
		}
	}

	pointData.showDepartment();
}

function initializeBuildingSortTab(type) {
	var listItem = document.createElement('li');
	listItem.className = type + '_building_menu';
	var listItemLink = listItem.appendChild(document.createElement('a'));
	listItemLink.href = "#";
	listItemLink.innerHTML = type;
	listItemLink.onclick = function() {
		for(id in markers_buildings) {
			markers_buildings[id].hideBuilding();	
		}
		for(id in markers_buildings) {
			if (markers_buildings[id].type == type || 'All' == type)
			{
				markers_buildings[id].showBuilding();
			}
			else
			{
				markers_buildings[id].hideBuilding();	
			}
		}
		for(id in markers_departments) {
			markers_departments[id].hideDepartment();	
		}
		for(id in markers_departments) {
			if (markers_departments[id].type == type || 'All' == type)
			{
				markers_departments[id].showDepartment();
			}
			else
			{
				markers_departments[id].hideDepartment();	
			}
		}
		return false;
	}
	document.getElementById('building-filters').appendChild(listItem);
}

function initializeDepartmentSortTab(type) {
	var listItem = document.createElement('li');
	listItem.className = type + '_department_menu';
	var listItemLink = listItem.appendChild(document.createElement('a'));
	listItemLink.href = "#";
	listItemLink.innerHTML = type;
	listItemLink.onclick = function() {
		for(id in markers_departments) {
			markers_departments[id].hideDepartment();	
		}
		for(id in markers_departments) {
			if (markers_departments[id].type == type || 'All' == type)
			{
				markers_departments[id].showDepartment();
			}
			else
			{
				markers_departments[id].hideDepartment();	
			}
		}
		for(id in markers_buildings) {
			markers_buildings[id].hideBuilding();	
		}
		for(id in markers_buildings) {
			if (markers_buildings[id].type == type || 'All' == type)
			{
				markers_buildings[id].showBuilding();
			}
			else
			{
				markers_buildings[id].hideBuilding();	
			}
		}
		return false;
	}
	document.getElementById('department-filters').appendChild(listItem);
}
/* [listing 6-22 end] */

function changeView(selection)
{
	if(selection == 'department')
	{
		document.getElementById("building-filters").style.display = "none";
		document.getElementById("department-filters").style.display = "block";
		document.getElementById("bottombar-list-building").style.display = "none";
		document.getElementById("bottombar-list-department").style.display = "block";
		document.getElementById("button-bottombar-building").style.backgroundColor = "#ffffff";
		document.getElementById("button-bottombar-building").style.color = "#0000ff";
		document.getElementById("button-bottombar-department").style.backgroundColor = "#000000";
		document.getElementById("button-bottombar-department").style.color = "#ffffff";
	}
	else
	{
		document.getElementById("building-filters").style.display = "block";
		document.getElementById("department-filters").style.display = "none";
		document.getElementById("bottombar-list-building").style.display = "block";
		document.getElementById("bottombar-list-department").style.display = "none";
		document.getElementById("button-bottombar-building").style.backgroundColor = "#000000";
		document.getElementById("button-bottombar-building").style.color = "#ffffff";
		document.getElementById("button-bottombar-department").style.backgroundColor = "#ffffff";
		document.getElementById("button-bottombar-department").style.color = "#0000ff";
	}
	return false;
}

function init() {
	document.getElementById("department-filters").style.display = "none";
	document.getElementById("button-bottombar-building").style.backgroundColor = "#000000";
	document.getElementById("button-bottombar-building").style.color = "#ffffff";
	document.getElementById("bottombar-list-department").style.display = "none";
	var type;
	var allBuildingTypes = new Array();
	var allDepartmentTypes = new Array();
	
	document.getElementById("button-bottombar-building").onclick = function() { return changeView('building'); };
	document.getElementById("button-bottombar-department").onclick = function() { return changeView('department'); };
	
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom, G_HYBRID_MAP);

	for(id in markers_buildings) {
		initializeBuildingPoint(markers_buildings[id]);
		allBuildingTypes[markers_buildings[id].type] = true;
	}
	
	var sortBuildingTypes = new Array();
	var sortedBuildingTypes = new Array();
	var indexB = 0;
	for(type in allBuildingTypes)
	{
		sortBuildingTypes[indexB] = type;
		indexB++;
	}
	sortedBuildingTypes = sortBuildingTypes.sort();
	
	initializeBuildingSortTab('All');
	for(indexB = 0; indexB < sortedBuildingTypes.length; indexB++) {
		initializeBuildingSortTab(sortedBuildingTypes[indexB]);
	}
	
	for(id in markers_departments) {
		initializeDepartmentPoint(markers_departments[id]);
		allDepartmentTypes[markers_departments[id].type] = true;
	}
	
	var sortDepartmentTypes = new Array();
	var sortedDepartmentTypes = new Array();
	var indexD = 0;
	for(type in allDepartmentTypes)
	{
		sortDepartmentTypes[indexD] = type;
		indexD++;
	}
	sortedDepartmentTypes = sortDepartmentTypes.sort();
	
	initializeDepartmentSortTab('All');
	for(indexD = 0; indexD < sortedDepartmentTypes.length; indexD++) {
		initializeDepartmentSortTab(sortedDepartmentTypes[indexD]);
	}
	
	var listItemB = document.createElement('li');
	listItemB.className = 'full_screen';
	var listItemBLink = listItemB.appendChild(document.createElement('a'));
	listItemBLink.href = 'campus.php';
	listItemBLink.target = '_blank';
	listItemBLink.innerHTML = 'Full Screen Mode';
	document.getElementById('building-filters').appendChild(listItemB);
	
	var listItemD = document.createElement('li');
	listItemD.className = 'full_screen';
	var listItemDLink = listItemD.appendChild(document.createElement('a'));
	listItemDLink.href = 'campus.php';
	listItemDLink.target = '_blank';
	listItemDLink.innerHTML = 'Full Screen Mode';
	document.getElementById('department-filters').appendChild(listItemD);
	
	changeView('building');
}

window.onload = init;
