// for compatibility, since this file might or might not be cached and iframe.html uses this file
function bumaps_load() {}
	
var bumaps_init = function() {
	new BUDepartmentMap();
}

function BUDepartmentMap() {
	if (!GBrowserIsCompatible()) return -1;
	BUDepartmentMap.instance = this;
	this.markers = {};
	this.addReferencedData(eval( '(' + item_cache_str + ')' ));
	
	this.map_div = document.getElementById('map');
	this.map = new GMap2(this.map_div);
	this.map.addControl(new GLargeMapControl());
	
	var dept_map = this;

	GEvent.addListener(this.map, 'infowindowclose', function() {
		if (dept_map.restoreInfoWindow) {
			dept_map.restoreInfoWindow = false;
		}
	});

	GEvent.addListener(this.map, "click", function(overlay, point) {
		if (overlay && overlay.bumaps_info) {
			dept_map.openMarker(overlay.id);
		}
	});

	jQuery('body').unload(GUnload);

	
	this.popupXslTemplate = string2xml(popupXslString);
	jQuery.ajax({
		dataType: 'jsonp',
		url: BUDepartmentMap.baseURL + "ajax/id-search-map.php",
		data: { id: parent.myId},
		success: function(data) {
			BUDepartmentMap.instance.processData(data);
		}
	});
}

BUDepartmentMap.bootstrap = function() {
	var addLinkTag = function(url) {
		var scr = document.createElement('link');
		scr.type = 'text/css';
		scr.rel = 'stylesheet';
		scr.href = url;
		document.getElementsByTagName('head')[0].appendChild(scr);
	}

	var addScriptTag = function(url) {
		var scr = document.createElement('script');
		scr.type = 'text/javascript';
		scr.src = url;
		document.getElementsByTagName('head')[0].appendChild(scr);
	}
	
	BUDepartmentMap.baseURL = 'http://www.bu.edu/maps/';
	addLinkTag(BUDepartmentMap.baseURL + 'common/popup.css');
	var d = new Date();
	var no_cache = d.getFullYear() + d.getMonth() + d.getDate() + d.getHours();
	addScriptTag(BUDepartmentMap.baseURL + 'ajax/dept-maps-init.php?nc=' + no_cache );
}


BUDepartmentMap.prototype.openClosest = function(item_id, original_id) {
	this.openMarker(item_id);
	this.restoreInfoWindow = true;
}

BUDepartmentMap.prototype.openMarker = function(id) {
	if (this.markers.hasOwnProperty(id)) var marker = this.markers[id];
	else return false;

	if (!marker.bu_html) {
		try {
			var xml_string = object2xml(marker.bumaps_info, 'popup');
			var xml = string2xml(xml_string);
			marker.bu_html = xml2html(xml, this.popupXslTemplate);
		}
		catch(e) {
			alert('Error opening marker: ' + e);
			return false;
		}
	}
	
	var div = $('<div />').html(marker.bu_html);
    div.find('#icons').hide().remove();
    div.find('#view_full_map_link').show();

	this.addReferencedInfoToPopup(marker, div);
	div.find('.item-reference').click(function() {
		var params = parseUrl( $(this).attr('href') );
		BUDepartmentMap.instance.openClosest( params['id'], this.current_open_marker_id);
		return false;
	});

	div.find('.directions-link').click(function() {
		var from = ( $(this).attr('rel') == 'from-here' );
		BUDepartmentMap.instance.showDirectionsForm(from, id);
		return false;
	});

	div.find( '#popup_back_button a').click(function() {
		BUDepartmentMap.instance.resetPopup();
		return false;
	});


	marker.openInfoWindow(div[0]);
	this.current_open_marker_id;
}

BUDepartmentMap.prototype.resetPopup = function() {
	jQuery('#popup_closest').show();
	jQuery('#popup_get_directions').show();

	jQuery('#from_dir').hide();
	jQuery('#to_dir').hide();
	jQuery('#popup_back_button').hide();

	jQuery('#get_dirs').css('font-weight', '');

	this.resizeInfoWindow();
}

BUDepartmentMap.prototype.showDirectionsForm = function(from, id) {
	if (from) {
		jQuery('#from_dir').show();
		jQuery('#to_dir').hide();
	}
	else {
		jQuery('#from_dir').hide();
		jQuery('#to_dir').show();
	}

	jQuery('#get_dirs').css('font-weight', 'bolder');

	jQuery('#popup_closest').hide();

	jQuery('#popup_back_button').show();
	this.resizeInfoWindow();
	return false;
}

BUDepartmentMap.prototype.resizeInfoWindow = function() {
	try{
		this.map.updateInfoWindow(this.map.getInfoWindow().getTabs());
	}
	catch(e) {}
}

BUDepartmentMap.prototype.addReferencedInfoToPopup = function(marker, div) {
	var item_cache = this.referencedData;

	div.find('.item-reference[rel="building"]').each(function() {
        var ref_id = jQuery.trim($(this).text());
		var ref_item = item_cache[ref_id];
		if ( typeof ref_item != 'undefined' )
			$(this).text(ref_item.title);
	});
	
	div.find('.item-reference[rel="train-stop"]').each(function() {
        var ref_id = jQuery.trim($(this).text());
		var ref_item = item_cache[ref_id];
		if ( typeof ref_item != 'undefined' )
			$(this).text(ref_item.title);
	});
	
	div.find('.item-reference[rel="parking-lot"]').each(function() {
        var ref_id = jQuery.trim($(this).text());
		var ref_item = item_cache[ref_id];
		if ( typeof ref_item != 'undefined' )
			$(this).text(ref_item.title.substring(ref_item.title.indexOf(':') + 2));
	});

	div.find('.item-reference[rel="bus"]').each(function() {
        var ref_id = jQuery.trim($(this).text());
		var ref_item = item_cache[ref_id];
		if ( typeof ref_item != 'undefined' )
			$(this).text(ref_item.title.substring(0, ref_item.title.indexOf(':')));
	});
}

BUDepartmentMap.prototype.getMarkerIcon = function(type, icon_name) {
	var icon;
	var server_path = BUDepartmentMap.baseURL;
	var m_path = server_path + 'icons/map-icons/markers/'; //marker icon path
	var p_path = server_path + 'icons/map-icons/markers/'; //print icon path
	var p_icon = p_path+icon_name+'.gif';
	var m_icon = m_path+icon_name+'.png';

	if (type == 'normal' || type == 'normal2') {
		icon = new GIcon(G_DEFAULT_ICON, m_icon);
		icon.shadow = m_path + "shadow.png";
		icon.iconSize = new GSize(27, 26);
		icon.shadowSize = new GSize(41, 26);
		icon.iconAnchor = new GPoint(15, 27);
		icon.ifnoWindowAnchor = new GPoint(12, 2);
		icon.printShadow = p_path + "shadow.gif";
	}

	if (type == 'bus') {
		icon = new GIcon(G_DEFAULT_ICON, m_icon);
		icon.shadow = null;
		icon.iconSize = new GSize(18,18);
		icon.iconAnchor = new GPoint(10, 10);
		icon.printShadow = null;
	}

	if (type == 'parking') {
		icon = new GIcon(G_DEFAULT_ICON, m_icon);
		icon.shadow = null;
		icon.iconSize = new GSize(14,14);
		icon.iconAnchor = new GPoint(8, 8);
		icon.printShadow = null;
	}

	if (type == 'wireless') {
		icon= new GIcon(G_DEFAULT_ICON, m_icon);
		icon.shadow = null;
		icon.iconSize = new GSize(18,18);
		icon.iconAnchor = new GPoint(9, 9);
		icon.printShadow = null;
	}

	if (type == 'train') {
		icon= new GIcon(G_DEFAULT_ICON, m_icon);
		icon.shadow = null;
		icon.iconSize = new GSize(17,17);
		icon.iconAnchor = new GPoint(9, 9);
		icon.printShadow = null;
	}

	if (type == 'blue') {
		icon = new GIcon(G_DEFAULT_ICON, m_icon);
		icon.shadow = null;
		icon.iconSize = new GSize(14,14);
		icon.iconAnchor = new GPoint(7, 7);
		icon.printShadow = null;
	}

	if (type == 'tty') {
		icon = new GIcon(G_DEFAULT_ICON, m_icon);
		icon.shadow = null;
		icon.iconSize = new GSize(19,19);
		icon.iconAnchor = new GPoint(10, 10);
		icon.printShadow = null;
	}

	if (type == 'door') {
		icon = new GIcon(G_DEFAULT_ICON, m_icon);
		icon.shadow = null;
		icon.iconSize = new GSize(19,19);
		icon.iconAnchor = new GPoint(10, 10);
		icon.printShadow = null;
	}

	if (type == 'audible') {
		icon = new GIcon(G_DEFAULT_ICON, m_icon);
		icon.shadow = null;
		icon.iconSize = new GSize(19,19);
		icon.iconAnchor = new GPoint(10, 10);
		icon.printShadow = null;
	}

	if (type == 'curb') {
		icon = new GIcon(G_DEFAULT_ICON, m_icon);
		icon.shadow = null;
		icon.iconSize = new GSize(10,10);
		icon.iconAnchor = new GPoint(5, 5);
		icon.printShadow = null;
	}
	
	if (type == 'bicycle') {
		icon = new GIcon(G_DEFAULT_ICON, m_icon);
		icon.shadow = null;
		icon.iconSize = new GSize(16,16);
		icon.iconAnchor = new GPoint(8, 8);
		icon.printShadow = null;
	}

	if (type == 'mail') {
		icon = new GIcon(G_DEFAULT_ICON, m_path + icon_name + '.gif');
		icon.shadow = null;
		icon.iconSize = new GSize(19,19);
		icon.iconAnchor = new GPoint(10, 10);
		icon.printShadow = null;
	}

	if (type == 'bus_commencement') {
		icon = new GIcon(G_DEFAULT_ICON, m_icon);
		icon.shadow = null;
		icon.iconSize = new GSize(18,18);
		icon.iconAnchor = new GPoint(10, 10);
		icon.printShadow = null;
	}
	
	icon.printImage = p_icon;
	icon.mozPrintImage = p_icon;

	return icon;
}

BUDepartmentMap.prototype.processData = function(data) {
	var data = data.markers[0];
	this.mainMarker = this.createMarker(data);

	var id = this.mainMarker.bumaps_info.parking_lot_id;
	var ref_data = this.referencedData[id];
	this.createMarker(ref_data);

	var id = this.mainMarker.bumaps_info.bus_inbound_id;
	var ref_data = this.referencedData[id];
	this.createMarker(ref_data);

	var id = this.mainMarker.bumaps_info.bus_outbound_id;
	var ref_data = this.referencedData[id];
	this.createMarker(ref_data);

	var id = this.mainMarker.bumaps_info.train_stop_id;
	var ref_data = this.referencedData[id];
	this.createMarker(ref_data);

	this.adjustZoom();

	var dept_map = this;
	setTimeout(function() {
		dept_map.openMarker(dept_map.mainMarker.id);
	}, 300);
}

BUDepartmentMap.prototype.adjustZoom = function() {
	var bounds = new GLatLngBounds();
	for (var id in this.markers) {
		if (!this.markers.hasOwnProperty(id)) continue;
		bounds.extend(this.markers[id].getPoint());
	}

	this.map.setCenter(bounds.getCenter(), this.map.getBoundsZoomLevel(bounds, this.map.getSize()));
}

BUDepartmentMap.prototype.createMarker = function(data) {
	if (typeof data == 'undefined') return false;
	var marker = new GMarker(new GLatLng(data.lat, data.lng), 
			this.getMarkerIcon(data.type, data.icon));
	this.map.addOverlay(marker);
	marker.bumaps_info = data;
	marker.id = data.id;
	this.markers[data.id] = marker;
	return marker;
}

BUDepartmentMap.prototype.addReferencedData = function(data) {
	this.referencedData = data;
}

//Load the data:
if ( window.addEventListener ) {
	window.addEventListener('load', BUDepartmentMap.bootstrap, false);
}
if ( window.attachEvent ) {
	window.attachEvent( 'onload', BUDepartmentMap.bootstrap );
}
