var map, manager;
//var centerLatitude = 43.19141, centerLongitude = -112.350483, startZoom = 12;


function createMarkerClickHandler(clat,clon,marker, text, link, pic, phone) {
	return function() {
		//map.setCenter(new GLatLng(clat, clon), endZoom);
		marker.openInfoWindowHtml(
			'<span class="googs">'+
			'<img src="/images/PumpPerksPages/'+pic+'" style="float:right; margin-top:15px;">'+
			'<div>' + text + '</div>' +
			'<div>phone: ' + phone + '</div>' +
			'<p target="_blank"><a href="' + link + '" class="googleLinks" target="_blank">Goto Google Maps &raquo;</a></p></span>'
		);
		return false;
	};
	
}


function createMarker(pointData) {
	var latlng = new GLatLng(pointData.latitude, pointData.longitude);
	var icon = new GIcon();
	icon.image = '/images/PumpPerksPages/'+pointData.icon;
	icon.iconSize = new GSize(16, 26);
	icon.iconAnchor = new GPoint(8, 13);
	icon.infoWindowAnchor = new GPoint(8, 26);

	opts = {
		"icon": icon,
		"clickable": true,
		"labelOffset": new GSize(0, 0)
	};
	var marker = new LabeledMarker(latlng, opts);
	var handler = createMarkerClickHandler(pointData.latitude, pointData.longitude, marker, pointData.name, pointData.wp, pointData.pic, pointData.phone);
	
	GEvent.addListener(marker, "click", handler);
/*
	var listItem = document.createElement('div');
	listItem.innerHTML = '<div class="mapBar"></div><a href="' + pointData.wp + '">' + pointData.name + '<br>'+ pointData.phone + '<span class="map-view">view</span></a>';
	listItem.getElementsByTagName('a')[0].onclick = handler;
	//<br>Open '+ unescape(pointData.hours) + '
	document.getElementById('sidebar-list').appendChild(listItem);
*/
	return marker;
}

function windowHeight() {
	// Standard browsers (Mozilla, Safari, etc.)
	if (self.innerHeight)
		return self.innerHeight;
	// IE 6
	if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	// IE 5
	if (document.body)
		return document.body.clientHeight;
	// Just in case. 
	return 0;
}

function handleResize() {
	//var height = windowHeight() - 30;
	//document.getElementById('toolbar').offsetHeight
	//document.getElementById('map').style.height = height + 'px';
	//document.getElementById('sidebar').style.height = height + 'px';
}

function init() {
	handleResize();
	
	map = new GMap(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), 4);
	map.addControl(new GMapTypeControl());

	manager = new GMarkerManager(map);
	
	// This is a sorting trick, don't worry too much about it.
	markers.sort(function(a, b) { return (a.abbr > b.abbr) ? +1 : -1; }); 
	
	batch = [];
	for(id in markers) {
		if(markers[id]['abbr'] != null){
		batch.push(createMarker(markers[id]));
	}}
	manager.addMarkers(batch, 2);
	manager.refresh();
	
	refreshTime = setTimeout ( "zoomp()", 3000 );
}

function zoomp(){
		
		map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);	
		clearTimeout ( refreshTime );
		
	}

window.onresize = handleResize;
//window.onload = init;
window.onunload = GUnload;

