var gmarkers=[];
var icons=[];
var map;
var bounds = new GBounds(Number.MAX_VALUE, Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE); 
var geocoder;
var errIcon = new GIcon();				 
errIcon.shadow = graphic_path + "templates/js_css/markers/marker_sqr_shadow.png";
errIcon.iconSize = new GSize(12, 20);
errIcon.shadowSize = new GSize(22, 20);
errIcon.iconAnchor = new GPoint(6, 20);
errIcon.infoWindowAnchor = new GPoint(5, 1);
errIcon.infoShadowAnchor = new GPoint(18, 25);

var sqrIcon = new GIcon();
sqrIcon.shadow = graphic_path + "templates/js_css/markers/marker_sqr_shadow.png";
sqrIcon.iconSize = new GSize(28, 28);
sqrIcon.shadowSize = new GSize(40, 26);
sqrIcon.iconAnchor = new GPoint(9, 27);
sqrIcon.infoWindowAnchor = new GPoint(9, 2);
sqrIcon.infoShadowAnchor = new GPoint(18, 25);

var roundIcon = new GIcon();		
roundIcon.shadow = graphic_path + "templates/js_css/markers/marker_round_shadow.png";
roundIcon.iconSize = new GSize(28, 28);
roundIcon.shadowSize = new GSize(43, 28);
roundIcon.iconAnchor = new GPoint(14, 26);
roundIcon.infoWindowAnchor = new GPoint(14, 14);
roundIcon.infoShadowAnchor = new GPoint(14, 14);

var tackIcon = new GIcon();				 
tackIcon.shadow = graphic_path + "templates/js_css/markers/marker_tack_shadow.png";
tackIcon.iconSize = new GSize(12, 20);
tackIcon.shadowSize = new GSize(22, 20);
tackIcon.iconAnchor = new GPoint(6, 20);
tackIcon.infoWindowAnchor = new GPoint(5, 1);
tackIcon.infoShadowAnchor = new GPoint(18, 25);


function get_radio_value() {
    var radios = document.search_form.elements.change_region; 
    for (var i=-1, radio; radio=radios[++i];) 
        if(radio.checked) location.href='locator.php?geo_region=' + radio.value;
}

function set_radio_value() {
    var radios = document.search_form.elements.change_region; 
    for (var i=-1, radio; radio=radios[++i];) {alert(radio.value);
        if(radio.value == document.search_form.geo_region.value) {
					document.search_form.elements.change_region.value.checked = true;
					alert(document.search_form.elements.change_region.value);
				}
		}
}

function set_init_view(world_view) {
	switch (world_view){
		case 'us': 
			lat = 38.75416131535263;
			lng = -96.9848110961914;
			zoom = 4;
		break;
		case 'ca': 
			lat = 61.270232790000605;
			lng = -97.734375;
			zoom = 3;
		break;
		case 'na': 
			lat = 61.270232790000605;
			lng = -97.734375;
			zoom = 2;
		break;
		case 'in': 
			lat = 35.6180165487737;
			lng = 5.625;
			zoom = 1;
		break;
	}
	init_view(lat,lng, zoom);
}

function init_view(lat, lng, zoom) {
		map.setCenter(new GLatLng(lat,lng), zoom);
}	

function color_of_icon(iconColor) {
   var color;
   if ((typeof(iconColor)=="undefined") || (iconColor==null)) { 
      color = "gray" 
   } else { 
      color = iconColor; 
   }
   if (!icons[iconColor]) {
      var icon = new GIcon(baseIcon);
      icon.image = graphic_path + "templates/js_css/markers/mm_20_"+ color +".png";
      icons[iconColor]=icon;

   } 
   return icons[iconColor];
}

function color_of_default_icon(iconColor) {
   var color;
   if ((typeof(iconColor)=="undefined") || (iconColor==null)) { 
      color = "marker_err.png" 
   } else { 
      color = iconColor; 
   }
   if (!icons[iconColor]) {
	 		if (iconColor.indexOf('_sqr') != -1) var icon = new GIcon(sqrIcon);
	 		if (iconColor.indexOf('_round') != -1) var icon = new GIcon(roundIcon);
	 		if (iconColor.indexOf('_tack') != -1) var icon = new GIcon(tackIcon);
	 		if (iconColor.indexOf('_err') != -1) var icon = new GIcon(errIcon);

//      var icon = new GIcon(defaultIcon);
      icon.image = graphic_path + "templates/js_css/markers/" + color;
      icons[iconColor]=icon;

   } 
   return icons[iconColor];
}

GMap2.prototype.centerAndZoomOnBounds = function(bounds) {
   var span = new GSize(bounds.maxX - bounds.minX, bounds.maxY - bounds.minY);
	 var center = new GLatLng(bounds.minY + span.height / 2., bounds.minX + span.width / 2.);
		sw = new GLatLng(bounds.maxY, bounds.minX);
		ne = new GLatLng(bounds.minY, bounds.maxX);
		b = new GLatLngBounds(sw, ne);
		newZoom = map.getBoundsZoomLevel(b);

   if (map.getZoom() != newZoom) {
       map.setCenter(center, newZoom);
   } else {
			map.setCenter(center);
   }
}

function addMarker(lng, lat, title, iconStr, yb) {
   var point = new GPoint(lng, lat);
   bounds.extend(point);
   var icon = color_of_icon(iconStr);
   var marker = new GMarker(point, icon);
   gmarkers.push(marker);
   marker.type = iconStr; 
   GEvent.addListener(marker, "click", function () {
//      var text = "<div align=center>"+title +"<BR> <BR><a href=" + marker_link + yb + ">details...</a></div>";
      var text = "<div align=center>"+title +"</div>";
      marker.openInfoWindowHtml(text);
   });
   map.addOverlay(marker);
}

function addDefaultMarker(lng, lat, title, iconStr, yb) {
   var point = new GPoint(lng, lat);
   bounds.extend(point);
   var icon = color_of_default_icon(iconStr);
   var marker = new GMarker(point, icon);
   gmarkers.push(marker);
   marker.type = iconStr; 
   GEvent.addListener(marker, "click", function () {
//      var text = "<div align=center>"+title +"<BR> <BR><a href=" + marker_link + yb + ">details...</a></div>";
      var text = "<div align=center>"+title +"</div>";
      marker.openInfoWindowHtml(text);
   });
   map.addOverlay(marker);
}

function add_point_of_entry(lng, lat, origin) {
	arrow_point = new GPoint(lng, lat);
  bounds.extend(arrow_point);
	arrow_icon = new GIcon();

	if (origin.search("none") != -1) { 	
      origin = "point_of_origin_transparent.png" 
  }
	arrow_icon.image = graphic_path + "templates/js_css/markers/" + origin;
	arrow_icon.iconSize = new GSize(26, 26);
	arrow_icon.iconAnchor = new GPoint(13, 13);
  arrow_marker = new GMarker(arrow_point, arrow_icon);
  map.addOverlay(arrow_marker);
}

function processMarkers() {
	start = 0;
	init_view(location_list[0][1],location_list[0][0], 16)
	if(document.search_form.geo_region.value != 'in') {
		lng = location_list[0][0];
		lat = location_list[0][1];
		marker_style = location_list[0][3];
		color = location_list[0][4];
		add_point_of_entry(lng,lat,color);
		start++;
	} 
	
	for (var i=start; i < location_list.length; i++) {
		lng = location_list[i][0];
		lat = location_list[i][1];
		title = location_list[i][2];
		color = location_list[i][3];
		year_built = location_list[i][4];
		addDefaultMarker(lng,lat,title,color,year_built);
		}
	map.centerAndZoomOnBounds(bounds);
} 

function plot_locations() {
  map = new GMap2(document.getElementById("map")); 
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());

	if (location_list.length > 0) processMarkers();
	else set_init_view(document.search_form.geo_region.value);
}

function searchLocations() {
	address = document.getElementById('user_origin').value;

//	region_id = document.getElementById('region_id').value;
    geocoder.getLatLng(address, function(latlng) {
		if (!latlng) {
        	alert(address + ' not found');
					return(false);
       	} else {
//        	document.getElementById('lat').value = latlng.lat();
 //       	document.getElementById('lng').value = latlng.lng();
					document.getElementById('user_origin').value = latlng.lat() + ',' + latlng.lng();
					document.search_form.submit();					
       	}
     });
		 return(false);
}
function load() {
    	geocoder = new GClientGeocoder();
	var locations = new plot_locations();
	if(document.search_form.geo_region.value == 'in') set_init_view('in');
//	else set_radio_value();
}