var map;
var gmarkers=[];
var i=0;
var gdir;

function placeMarkerAtPoint(point,title,bubble)
{
	var marker=createMarker(point,title,bubble);
	map.addOverlay(marker);
}

function createMarker(point, name, bubble){
	var marker = new GMarker(point);
	
	var destination = point + '';
	
	destination = destination.replace('(', '');
	destination = destination.replace(')', '');
	
	GEvent.addListener(marker, "click", function(){
		marker.openInfoWindowHtml(bubble);
		
		defaultLL_arr = defaultLL.split(',');
		default_latlong = new GLatLng(defaultLL_arr[0], defaultLL_arr[1]);
		
		if ( default_latlong.distanceFrom(point) > 0 ) {
			$("#gmap_directions_container").show();
			gdir.load("from: " + defaultLL + " to: " + destination, {
				"locale": "en"
			});
		}
	});
	gmarkers[i] = marker;
	return marker;
	
}

function pan(i){
	GEvent.trigger(gmarkers[i], "click");
}

function zoomin(i){
	GEvent.trigger(gmarkers[i], "zoomin");
}

function zoomout(i){
	GEvent.trigger(gmarkers[i], "zoomout");
}

function setCenterToPoint(point){
	map.setCenter(point, 17);
}

function showPointLatLng(point){
	alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
}

var defaultLL = document.getElementById('default_location').value;
var default_latlong;

function mapLoad(){

	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		//gdir = new GDirections(map, document.getElementById("directions"));
		gdir = new GDirections(map,document.getElementById("gmap_directions"));
		
		defaultLL_arr = defaultLL.split(',');
		default_latlong = new GLatLng(defaultLL_arr[0], defaultLL_arr[1]);
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(default_latlong, 12);
		
		var locations = $('.map_locations');
		
		$('.map_location').each( function() {
			var loc_id = $(this).val();
			 
			var locLL= $( '#map_ll_' + loc_id ).val().split(',');
			locLL = new GLatLng(locLL[0],locLL[1]);
			
			var cur_dist = locLL.distanceFrom(default_latlong);
			var miles = ( Math.round(((cur_dist / 1000) *  0.62137119224 ) * 100 ) ) / 100 ;
			var miles_html = miles > 0 ? '<br/><br/><strong>' + miles + ' miles' : "";
			$( '#map_distance_' + loc_id ).html( miles + " miles" );
			
			var locNAME=$( '#map_name_' + loc_id ).val();
			var locADDRESS=$( '#map_address_' + loc_id ).val();
			var locBUBBLE='<span style="font-size:12px; font-weight:bold; color:#021f3d;">'+locNAME+'</span><br /><span style="font-size:11px; font-weight:normal; color:#021f3d;">'+locADDRESS+ miles_html + '</strong></span>';
						
			placeMarkerAtPoint(locLL,locNAME,locBUBBLE);
			
			//var zoom = 14 - Math.round( Math.sqrt( miles / 2 ) + 1 );
			var zoom = 3;
			
			if( miles < 256 )
				zoom = 4;
				
			if( miles < 128 )
				zoom = 5;
			
			if( miles < 64 )
				zoom = 6;
			
			if( miles < 32 )
				zoom = 8;
			
			if( miles < 16 )
				zoom = 9;
			
			if( miles < 8 )
				zoom = 10;
			
			if( miles < 4 )
				zoom = 11;
			
			if( miles < 2 )
				zoom = 12;
			
			//zoom = zoom < 2 ? 2 : zoom;
			map.setCenter(default_latlong, zoom);
			
		});
		
	}
}	

function addLoadEvent(func){
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
}

function addUnLoadEvent(func){
	var oldonunload = window.onunload;
	
	if (typeof window.onunload != 'function') {
		window.onunload = func;
	}
	else {
		window.onunload = function(){
			oldonunload();
			func();
		}
	}
}

addLoadEvent(mapLoad);
addUnLoadEvent(GUnload);