// tsMapSchool is an overlay of a transparent image of the school
function tsMapSchool(bounds, imgSrc) {
	this.bounds_ = bounds;
	this.imgSrc_ = imgSrc;
}
tsMapSchool.prototype = new GOverlay();

// Create the image
tsMapSchool.prototype.initialize = function(map) {
	var img = document.createElement("img");
	img.src = this.imgSrc_;
	img.style.position = "absolute";
	
	// Our rectangle is flat against the map, so we add our selves to the
	// MAP_PANE pane, which is at the same z-index as the map itself (i.e.,
	// below the marker shadows)
	map.getPane(G_MAP_MAP_PANE).appendChild(img);

	this.map_ = map;
	this.img_ = img;
}

// Remove the main DIV from the map pane
tsMapSchool.prototype.remove = function() {
	this.img_.parentNode.removeChild(this.img_);
}

// Copy our data to a new Rectangle
tsMapSchool.prototype.copy = function() {
	return new tsMapSchool(this.bounds_, this.imgSrc_);
}

// Redraw the rectangle based on the current projection and zoom level
tsMapSchool.prototype.redraw = function(force) {
	// We only need to redraw if the coordinate system has changed
	if (!force) return;

	// Calculate the DIV coordinates of two opposite corners of our bounds to
	// get the size and position of our rectangle
	var c1 = this.map_.fromLatLngToDivPixel(this.bounds_.getSouthWest());
	var c2 = this.map_.fromLatLngToDivPixel(this.bounds_.getNorthEast());

	// Now position our DIV based on the DIV coordinates of our bounds
	this.img_.style.width = Math.abs(c2.x - c1.x) + "px";
	this.img_.style.height = Math.abs(c2.y - c1.y) + "px";
	this.img_.style.left = (Math.min(c2.x, c1.x) ) + "px";
	this.img_.style.top = (Math.min(c2.y, c1.y) ) + "px";
};


tsMap = {
	// Map constant
	map:	0,
	mapObj:	0,
	imgPath:	"/images/contactus/",
	tsLatLng:	new GLatLng(51.200202, 0.275384),

	
	addOverlay: function (sw, ne, imgFile) {
		var schoolBounds = new GLatLngBounds(sw, ne);
		tsMap.overlay = new tsMapSchool(schoolBounds, tsMap.imgPath+imgFile);
		tsMap.map.addOverlay(tsMap.overlay);
	},
	
	// Show the decorations for this zoom level
	showLevel: function (zoomLevel) {
		if (zoomLevel > 15) {
			tsMap.addOverlay(
				new GLatLng(51.1987164350906, 0.262756972507916),
				new GLatLng(51.2064480893957, 0.278299705689725),
				"map17.png"
			);
			
		} else if (zoomLevel == 15) {
			tsMap.addOverlay(
				new GLatLng(51.1988406679382, 0.262886424397313),
				new GLatLng(51.2063420922893, 0.277374511541322),
				"map15.png"
			);
		} else {
			tsMap.overlay = new GMarker(tsMap.tsLatLng);
			GEvent.addListener(tsMap.overlay, "click", function() {
				tsMap.overlay.openInfoWindowHtml('<b>Tonbridge School</b>, TN9 1JP<br>By car: TN10 3AB (avenue gates)<p><a href="javascript:tsMap.zoomTo(17)">Zoom to map of school</a><br><a href="http://maps.google.com/maps?daddr=TN10+3AB,+UK&z=14">Get directions at maps.google.com</a></p>');
			});
			tsMap.map.addOverlay(tsMap.overlay);
		}
	},
	
	// Hide
	hideLevel: function (zoomLevel) {
		tsMap.map.closeInfoWindow();
		tsMap.map.removeOverlay(tsMap.overlay);
	},
	
	// Called when zoom ends
	zoomend: function (oldLevel, newLevel) {
		tsMap.hideLevel(oldLevel);
		tsMap.showLevel(newLevel);
	},
	
	zoomTo: function (zoomLevel) {
		tsMap.map.setCenter(tsMap.tsLatLng, zoomLevel);
	},
    
    zoomSchool: function () {
        this.zoomTo(17);
    },
    zoomTown: function () {
        this.zoomTo(15);
    },
    zoomCounty: function () {
        this.zoomTo(10);
    },
	
	load: function () {
		if (! GBrowserIsCompatible()) return;
		
		var startLevel = 15;
		
		// Find and resize the map
		tsMap.mapObj = document.getElementById("map");
		tsMap.resize();
		
		var map = new GMap2(tsMap.mapObj);
		map.addControl(new GLargeMapControl());
		map.addControl(new GOverviewMapControl());
		map.setCenter(tsMap.tsLatLng, startLevel);
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
        
        // Set maximum resolution to 17
        var mt = map.getMapTypes();
        for (var i=0; i<mt.length; i++) {
            mt[i].getMaximumResolution = function () {
                return 17;
            }
        }
        
        // Store
		tsMap.map = map;
		
		// Show the starting zoom level, and listen for level changes
		tsMap.showLevel(startLevel);
		GEvent.addListener(map, "zoomend", tsMap.zoomend);
		
		window.onresize = tsMap.resize;
	},
	
	findPosY: function (o) {
		var y = 0;
		if (o.offsetParent) {
			do {
				y += o.offsetTop;
			} while (o = o.offsetParent);
		} else if (o.y) {
			y = o.y;
		}
		return y;
	},
	
	resize: function () {
        if (window.tsMapNoResize) {
            return;
        }
        
		// Find inner height
		var innerHeight = 0;
		if (self.innerHeight) { // all except Explorer
			innerHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			innerHeight = document.documentElement.clientHeight;
		} else if (document.body) {
			innerHeight = document.body.clientHeight;
		}
		
		// New height = screen height - (map top + footer margin)
		var newHeight = innerHeight - (tsMap.findPosY(tsMap.mapObj) + 80);
		
		// Resize to height
		tsMap.mapObj.style.height = Math.max(400, newHeight) + 'px';
	}

};

// Add fancy "Get directions to" thing
$(document).ready(function () {
    $('#map').before( $('#src_control_bar').html() );
    
    var directions_background = '#eee';
    var $directions = $('#directions_go')
        .click(function () {
            var s_addr = $('#directions_input').val();
            document.location = "http://maps.google.com/maps?saddr=" + escape(s_addr) + "&daddr=TN10+3AB,+UK&z=14";
        })
    ;
    
	//window.onload = function () {
        tsMap.load();
        $.historyInit(function (hash) {
            if (hash == 'school') {
                tsMap.zoomSchool();
            } else if (hash == 'county') {
                tsMap.zoomCounty();
            } else {
                tsMap.zoomTown();
            }
        });
    //};
    document.onunload = GUnload;
});

