// JavaScript Document
// Load Google Maps API Version 2
google.load("maps", "2");

function GoogleMap(containerId){
	this.map = null;
	this.containerId = containerId;
	this.markers = new Array();
	this.mapType = G_NORMAL_MAP;
	this.geocoder = new google.maps.ClientGeocoder();
	this.zoomLevel = 13;
	this.isCentred = false;
	
	// add to Interface Listeners
	Interface.addListener(this);
}

GoogleMap.prototype.init = function(){
	if(GBrowserIsCompatible()){
		// setup the map object
		this.map = new google.maps.Map2(document.getElementById(this.containerId));
		
		this.createMarkers();
		this.map.addControl(new GLargeMapControl());
		this.map.addControl(new GMapTypeControl());
	}
}

// Unload resources particularly for IE
GoogleMap.prototype.onUnload = function(){
	google.maps.Unload();
}

GoogleMap.prototype.createMarkers = function(){
	for(var i=0; i < this.markers.length; i++){
		this.getMarkerPoint(this.markers[i]);
	}
}

// Add a pin to the map
GoogleMap.prototype.addMarker = function(id,address, html){
	var marker=new GoogleMapMarker(id,address, html);
	this.markers[this.markers.length] = marker;
}

GoogleMap.prototype.getMarkerPoint = function(marker){
	var ref = this;
	this.geocoder.getLatLng(marker.address, 
							function(point){
								if(!(point)){
									alert('Unable to locate ' + marker.address);
								} else {
									marker.point = point;
									ref.setMarker(marker);
								}
							}
							);
}
GoogleMap.prototype.setPan = function(marker){
	var ref = this;
	this.geocoder.getLatLng(marker.address, 
							function(point){
									marker.point = point;
									ref.map.panTo(point);
									ref.map.setZoom(ref.zoomLevel);
								}
							);
}

GoogleMap.prototype.infoWindow = function(marker,zoomLevel){
	var googleMarker = new google.maps.Marker(marker.point);
		// add marker
		this.map.addOverlay(googleMarker);
		this.map.setZoom(zoomLevel);
		// add html
		googleMarker.openInfoWindowHtml(marker.html);
}

GoogleMap.prototype.setMarker = function(marker){
	if(!this.centred){
		this.map.setCenter(marker.point, this.zoomLevel,G_HYBRID_MAP);
		this.centred = true;
	}
		// create marker
		var googleMarker = new google.maps.Marker(marker.point);
		/*Register onclick event to the marker, which should open the infoWindow*/
		GEvent.addListener(googleMarker, "click",function(){
	                    googleMarker.openInfoWindowHtml(marker.html);	
					  }
					  ); 
		
		// add marker
		this.map.addOverlay(googleMarker);
		// add html
		googleMarker.openInfoWindowHtml(marker.html);
}

function GoogleMapMarker(id,address, html){
	this.point;
	this.address = address;
	this.html = html;
	this.id=id;
}



/*functionality for map swapping from divSwapper.js*/
function ucfirst(word){
	fstLetter = word.substr(0,1);
	rest = word.substr(1, word.length);
	fstLetter = fstLetter.toUpperCase();
	return fstLetter + rest;
}

document.getElementsByIdMatch = function(regex, tagname){
	var elems = document.getElementsByTagName(tagname)
	var matches = new Array();
	for(i=0; i<elems.length; i++){
		if(elems[i].id.search(regex) != -1) matches.push(elems[i]);
	}
	return matches;
}

function hide_all(){
	var divs = document.getElementsByTagName('div');
	
	var matches = document.getElementsByIdMatch(/^this.namespace/, 'div');
	for(i=0; i<matches.length; i++)
		matches[i].style.display = "none";
}

function show_elem(id){
	document.getElementById(id).style.display = "block";
}

function hide_elem(id){
	document.getElementById(id).style.display = "none";
}



/* MainInfo Class */
var MainInfo = function(namespace,defaultTab){
	
	this.namespace = namespace;
	this.init = function(){
		hide_all();
		this.hideTabs();
		this.raiseTab(defaultTab);
	}

	this.hideTabs = function(){
		// Get all elements whose IDs extend the this.namespace namespace.
		regex = new RegExp("^" + this.namespace + "[^$]+");
		matches = document.getElementsByIdMatch(regex, 'div');
		for(i in matches) matches[i].style.display = "none";		
		regex = new RegExp("^" + this.namespace + "Tab[a-zA-Z0-9]+$");
		matches = document.getElementsByIdMatch(regex, 'li');
		for(i in matches) matches[i].className = "inactive";		
	}

	this.dehoverTabs = function(){
		regex = new RegExp("^" + this.namespace + "Tab[a-zA-Z0-9]+$");
		matches = document.getElementsByIdMatch(regex, 'li');
		for(i in matches)
			if(matches[i].className == "over")
				matches[i].className = "inactive";		
	}
	
	this.showTab = function(tabname) {
		show_elem(this.namespace + ucfirst(tabname));
	}

	this.raiseTab = function(tabname){
		this.hideTabs();
		document.getElementById(this.namespace + "Tab" + tabname).className = "active";
		this.showTab(tabname);
	}

	this.hover = function(tabname){
		tab = document.getElementById(this.namespace + "Tab" + tabname);
		this.dehoverTabs();
		if(tab.className != "active") tab.className = "over";
	}

	this.restoreBg = function(tabname){
		tab = document.getElementById(this.namespace + "Tab" + tabname);
		this.dehoverTabs();
	}
}
