function SecondFloorMap(options) {
    // Keep track of our options.
    if (options) {
	this.options = options;
    } else {
	this.options = {};
    }
    
    // Default HTML id for the map: 'google-map'
    if (! this.options.mapid) {
	this.options.mapid = 'google-map';
    }

    // The actual Google map.
    this.gmap = null;
}

SecondFloorMap.prototype.load = function() {
    // Not a supported browser; abort.
    if (! GBrowserIsCompatible()) { return; }

    var map_element = document.getElementById(this.options.mapid);
    // Non-existent map element; abort.
    if (!map_element) { return; }

    // Compute the size of the map element and resize.
    // this.size(); 

    // Construct the GMap.
    this.gmap = new GMap2(map_element);

    // Unless the user has expressly asked for a vanilla map, 

    // Add pan, zoom, and type controls.
    this.gmap.addControl(new GSmallMapControl());
    //this.gmap.addControl(new GLargeMapControl());
    this.gmap.addControl(new GMapTypeControl());
    this.gmap.addControl(new GScaleControl());
    
    // Helps zooming.
    this.gmap.enableContinuousZoom();
    
    // Enable scrolling using a mouse-wheel or other scroll actions.
    this.addScrollWheel();
};

SecondFloorMap.prototype.unload = function() {
    // There is a bug in the Google Map API (at least v2.76) which
    // triggers an error in IE6 when navigating away from a page with a
    // map. The try/catch hides this error. Remove in the future when
    // v2.77 is the stable 2.x release.
    try { GUnload(); } catch(e) { ; }
};

// Dynamically determine the width of the map when we load the page.
// tbd. resize the map if the browser gets resized.
SecondFloorMap.prototype.size = function() {
    var map_element = document.getElementById(this.options.mapid);
    var viewWidth = parseInt(Element.getStyle(map_element.parentNode, 'width'));
    // var viewWidth = getViewportWidth();
    // var mapWidth = viewWidth - 2 - 200 - 10;
    var mapWidth = viewWidth - 2 /*border*/;
    if (mapWidth < 400) { mapWidth = 400; } // don't size too small.
    map_element.style['width'] = mapWidth + "px";
};

SecondFloorMap.prototype.overlayKML = function(kmlUri) {
    var geoXml = new GGeoXml(kmlUri);
    theMap = this;

    SecondFloor.addLoader(function () {
	theMap.gmap.addOverlay(geoXml);
    });
};

// Add event handlers to allow scrollwheel support.
// For more information about this technique, see:
//     http://adomas.org/javascript-mouse-wheel/
SecondFloorMap.prototype.addScrollWheel = function() {
    var map = this.gmap; // Local variable visible to closures.

    // Zoom our map out for a negative delta (scroll down) and in for
    // positive delta (scroll up)
    function handle(delta) {
	if (delta < 0) {
	    map.zoomOut();
	} else {
	    map.zoomIn();
	}
    }

    // Event handler for mouse wheel event.
    function wheel(event){
	var delta = 0;
	if (!event) { // For IE.
	    event = window.event;
	}
	if (event.wheelDelta) { // IE/Opera.
	    delta = event.wheelDelta/120;
	    // In Opera 9, delta differs in sign as compared to IE.
	    if (window.opera) {
		delta = -delta;
	    }
	} else if (event.detail) { // Mozilla case.
	    // In Mozilla, sign of delta is different than in IE.
	    // Also, delta is multiple of 3.
	    delta = -event.detail/3;
	}
	// If delta is nonzero, handle it.
	// Delta is now positive if wheel was scrolled up,
	// and negative if wheel was scrolled down.
	if (delta) { 
	    handle(delta); 
	}
	// Prevent default actions caused by mouse wheel.
	// That might be ugly, but we handle scrolls somehow
	// anyway, so don't bother here..
	if (event.preventDefault) {
	    event.preventDefault();
	}
	event.returnValue = false;
    }
    
    var div = this.gmap.getContainer();

    // Apply this event only over the Google Map div.
    GEvent.addDomListener(div, "DOMMouseScroll", wheel);
    GEvent.addDomListener(div, "mousewheel", wheel);
};

