var map;
var marker;
var markers_locations = new Array();
//var localSearch = new GlocalSearch();
/*
var icon = new GIcon();
icon.image = "http://www.google.com/mapfiles/marker.png";
icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
icon.iconSize = new GSize(20, 34);
icon.shadowSize = new GSize(37, 34);
icon.iconAnchor = new GPoint(10, 34);
*/

var baseIcon = new GIcon(G_DEFAULT_ICON);
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(25, 29);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);

icons_path = "/gfx/map_icons/";
icon_farmacy = new GIcon(baseIcon, icons_path + "ico_pharmacy.gif");
icon_police_station = new GIcon(baseIcon, icons_path + "ico_police.gif");
icon_post_office = new GIcon(baseIcon, icons_path + "ico_postoffice.gif");
icon_bank = new GIcon(baseIcon, icons_path + "ico_bank.gif");
icon_resort_center = new GIcon(baseIcon, icons_path + "ico_center.gif");
icon_shopping_area = new GIcon(baseIcon, icons_path + "ico_shopping.gif");
icon_parking_lot = new GIcon(baseIcon, icons_path + "ico_parking.gif");
icon_gas_station = new GIcon(baseIcon, icons_path + "ico_gas.gif");
icon_hospital = new GIcon(baseIcon, icons_path + "ico_hospital.gif");

function getMarker(location_type, pos, return_type) {
	var location_icon = null;
	switch(location_type) {
		case 'farmacy': location_icon = icon_farmacy;  break;
		case 'police_station': location_icon =  icon_police_station; break;
		case 'post_office': location_icon =  icon_post_office; break;
		case 'bank': location_icon =  icon_bank; break;
		case 'resort_center': location_icon =  icon_resort_center; break;
		case 'shopping_area': location_icon =  icon_shopping_area; break;
		case 'parking_lot': location_icon =  icon_parking_lot; break;
		case 'gas_station': location_icon =  icon_gas_station; break;
		case 'hospital': location_icon = icon_hospital; break;
	}
	if(return_type == "marker")	return new GMarker(pos, location_icon);
	if(return_type == "icon")	return location_icon;
}	

	function removeMarkers(map, markers) {
		for(i = 0; i < markers.length; i++) {
			map.removeOverlay(markers[i]); 
		}
	}
	function addMarkers(map, markers) {
		for(i = 0; i < markers.length; i++) {
			map.addOverlay(markers[i]); 
		}
	}


function setCenterToPoint()
{
	lat_txt = document.getElementById('lat');
	lng_txt = document.getElementById('lng');
	zoom_txt = document.getElementById('zoom');
	map.setCenter(new GLatLng(lat_txt.value, lng_txt.value), zoom_txt.value, G_HYBRID_MAP);
}

function mapLoad() {
	marker = new GMarker(0,0);
	lat_txt = document.getElementById('lat');
	lng_txt = document.getElementById('lng');
	zoom_txt = document.getElementById('zoom');
	try {
	resort_code = document.getElementById('resort_code');
//	alert(resort_code.value);
	} catch(e) { }
	if (GBrowserIsCompatible()) {
		map = new GMap(document.getElementById("map"));
		
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		if(lat_txt.value && lng_txt.value && zoom_txt.value) {
		zoom_level = parseInt(zoom_txt.value);
		lat_value = parseFloat(lat_txt.value);
		lng_value = parseFloat(lng_txt.value);
		map.setCenter(new GLatLng( lat_value, lng_value), zoom_level , G_HYBRID_MAP);	
			marker.setLatLng( new GLatLng(lat_value,lng_value) );
			map.addOverlay(marker);
		} else {
			map.setCenter(new GLatLng(54.622978,-2.592773), 1, G_HYBRID_MAP);
		}
		/*
		
		var url_amenities = "/index.php?op=1000&func=getAmenitiesByDestination&code="+resort_code.value;
		$.getJSON(url_amenities, function(data){
			$.each(data.locations, function(i, item_name){
				markers_locations[i] = new Array();
				markers_locations[i] = getMarker(item_name.location_type, new GLatLng(parseFloat(item_name.lat), parseFloat(item_name.lng)), "marker");
				map.addOverlay(markers_locations[i]); 	
			});
		});
		
		*/
		
	}
}

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);

