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


function createMarkerClickHandler(clat,clon,marker, text, link, storeNum, services) {
	return function() {
		map.setCenter(new GLatLng(clat, clon), endZoom);
		marker.openInfoWindowHtml(
			'<span class="googs">'+
			'<div>' + text + '</div>' +
			'<p align="center" style="padding-top:10px;"><img src="/images/store'+storeNum+'.gif"></p>' +
			'<p><a href="/locations/directories/ShoppingList'+storeNum+'.pdf" target="_blank" class="googleLinks">View Aisle Directory &amp; Shopping List &raquo;</a></p>' +
			'<p><a href="/wc/" class="googleLinks">Goto Weekly Specials &raquo;</a></p>' +
			services +
			'<p target="_blank"><a href="' + link + '" class="googleLinks">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/mapLogo.png';
	icon.iconSize = new GSize(73, 43);
	icon.iconAnchor = new GPoint(16, 16);
	icon.infoWindowAnchor = new GPoint(35, 21);

	opts = {
		"icon": icon,
		"clickable": true,
		"labelOffset": new GSize(-16, -16)
	};
	var marker = new LabeledMarker(latlng, opts);
	var handler = createMarkerClickHandler(pointData.latitude, pointData.longitude, marker, pointData.name + '<p>hours: '+unescape(pointData.hours), pointData.wp, pointData.abbr, pointData.services);
	
	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);
	
	if(myTown != ''){
		document.getElementById('topItem').innerHTML = '<div style="margin-left:-40px;">Stores Near:<h3 style="padding-top:0px; margin-top:0px;">'+myTown+' '+myZip+'</h3><a href="/locations/">view all</a></div>';
	}
	
	// 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;


