/*
	Name: Counties Google Map Generator
	Proj: Counties Website
	Auth: CJH - Newwave Design Ltd
	Date: 14/08/2009
*/

var map

gmarkers = new Array;
glocations = new Array;

xloc = -2.184217;
yloc = 51.259251;

xcen = -2.184217;
ycen = 51.259901;


function addPoint(point, content, markid) {
	var icon 				= new GIcon(G_DEFAULT_ICON);
	icon.image 				= "/images/gmap/icon.png";
	icon.iconSize 			= new GSize(20, 35);
	icon.iconAnchor 		= new GPoint(9, 35);
	icon.infoWindowAnchor 	= new GPoint(9, 5);
	
	var marker = new GMarker(point, icon);
	marker.tabContent = new Object();

	marker.tabContent.address = "<div class='gmap-title'>Counties</div><div class='gmap-address'>30 Haynes Road<br />Westbury<br />BA13 3HD</div>";
	
	marker.internal_id = markid;
	
	GEvent.addListener(marker, "click_action", function() {centreandzoom();show_custom_info_window(marker,true);});
	
	GEvent.addListener(marker, "click", function() {_activate_marker(gmarkers[marker.internal_id]);});
	
	GEvent.addListener(map, "zoomend", function() {if ($('overlay').style.display != "none") {close_window(); show_custom_info_window(marker);}});
	gmarkers[markid] = marker;
	return marker;
}

function load_map() {

	map = new GMap2(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GScaleControl());
	map.enableScrollWheelZoom();
	
	centreandzoom();
	map.addOverlay(addPoint(new GLatLng(yloc,xloc),"",1));
	
	show_custom_info_window(gmarkers[1],true);

}

function centreandzoom() {
	//map.centerAndZoom(new GPoint(xcen, ycen), 0);
	map.setCenter(new GLatLng(ycen,xcen));
	map.setZoom(17);
}

function _activate_marker(marker){
	GEvent.trigger(marker, "click_action");
}

function show_custom_info_window(marker,pos){
	
	map_point = map.fromLatLngToDivPixel(marker.getPoint());
	
	if (!$('overlay')){ // need to create the overlay inside the map pane
		var new_obj = document.createElement("div");
		new_obj.style.display = 'none';
		new_obj.innerHTML =  $('overlay_temp').innerHTML;
		new_obj.getElementsByTagName('div')[0].id = 'overlay_tab';
		new_obj.id = 'overlay';
		document.body.appendChild(new_obj);
	}
	
	var my_window = $('overlay');
	
	// Attach the marker
	my_window.marker = marker;

	// make sure the first tab is in front
	//var alltabs = my_window.getElementsByTagName('ul')[0];
	//Element.removeClassName(alltabs,'tabs2');
	//Element.addClassName(alltabs,'tabs');

	// Unselect tabs
	//var tabs = alltabs.getElementsByTagName("a");
	//for(i=0;i<tabs.length;i++){
	//	tab = tabs[i];
	//	Element.removeClassName(tab, 'selected');
	//}
	// Select tab
	//Element.addClassName(tabs[0], 'selected');
	
	// Populate content
	var my_tab = $('overlay_tab');
	my_tab.innerHTML = marker.tabContent.address;
	
	// Stick it to the map
	map.getPane(G_MAP_MARKER_PANE).appendChild(my_window);
	my_window.style.display = 'block';
	
	position_window(my_window, marker,pos);
	
}

		
function position_window(my_window, marker,pos){
	// Get the positioning right
	var vert_loc = parseInt(map_point.y) - my_window.offsetHeight - 40;
	my_window.style.top = vert_loc  + "px";
	var horiz_loc = parseInt(map_point.x) - 54;
	my_window.style.left = horiz_loc + "px";
	
	if (pos) {
	var pan_to = new GLatLng( marker.getPoint().lat(), marker.getPoint().lng() )
	map.panTo( pan_to );
	}
	
} 

function load_map_initial() {
	first_load = false;
	if (GBrowserIsCompatible()) {
// Initialize the map
		Element.show('gmap', 'map_legend');
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GScaleControl());
		map.setCenter(new GLatLng(37.926868, -97.294922));
		map.setZoom(3);
	}
}
		
//Event.observe (window,'unload',GUnload,false); // Prevent memory leaks

function close_window(){
	$('overlay').style.display = 'none';
	return false;
}

setTimeout('load_map()',100);