var map;
var loadCounter = 0;
var lat_sw = "";
var lng_sw = "";
var lat_ne = "";
var lng_ne = "";
var t_loader = null;
var loaderClear = 10; //Default time to clear loaders after no results or error
var maxZoom = 18;
var minZoom = 8;
var minUpdateZoom = minZoom; //can only do updates when zoom is grated than this
var rounder = 1; //sets the courseness of lat longs should be 1-100
var halfrounder = (rounder/2)/rounder;
var manager;
var allowedBounds = new GLatLngBounds(new GLatLng(-47,166), new GLatLng(-32,179));
var nzcentre = new GLatLng(-41, 173);

var mapTypes = G_DEFAULT_MAP_TYPES;
for(var i = 0; i < mapTypes.length; i++){
	mapTypes[i].getMaximumResolution = function(latlng){ return maxZoom;};
	mapTypes[i].getMinimumResolution = function(latlng){ return minZoom;};
}

function updateMapBoundaries(){
	lat_sw = Math.round(map.getBounds().getSouthWest().lat()*rounder)/rounder;
	lng_sw = Math.round(map.getBounds().getSouthWest().lng()*rounder)/rounder;
	lat_ne = Math.round(map.getBounds().getNorthEast().lat()*rounder)/rounder;
	lng_ne = Math.round(map.getBounds().getNorthEast().lng()*rounder)/rounder;	

	if(lat_sw == lat_ne){
		lat_sw = lat_sw-halfrounder;
		lat_ne = lat_ne+halfrounder;
	}
	if(lng_sw == lng_ne){
		lng_sw = lng_sw-halfrounder;
		lng_ne = lng_ne+halfrounder;
	}
}

function getRoundedMapBounds(){
	var map_lat_sw = Math.round(map.getBounds().getSouthWest().lat()*rounder)/rounder;
	var map_lng_sw = Math.round(map.getBounds().getSouthWest().lng()*rounder)/rounder;
	var map_lat_ne = Math.round(map.getBounds().getNorthEast().lat()*rounder)/rounder;
	var map_lng_ne = Math.round(map.getBounds().getNorthEast().lng()*rounder)/rounder;	
	
	if(map_lat_sw == map_lat_ne){
		map_lat_sw = map_lat_sw-halfrounder;
		map_lat_ne = map_lat_ne+halfrounder;
	}
	if(map_lng_sw == map_lng_ne){
		map_lng_sw = map_lng_sw-halfrounder;
		map_lng_ne = map_lng_ne+halfrounder;
	}
	
	return new GLatLngBounds(new GLatLng(map_lat_sw,map_lng_sw),new GLatLng(map_lat_ne,map_lng_ne))
}

// If the map position is out of range, move it back
function checkBounds() {
	// Perform the check and return if OK
	if (allowedBounds.contains(map.getCenter())) {
	  return;
	}
	// It`s not OK, so find the nearest allowed point and move there
	var C = map.getCenter();
	var X = C.lng();
	var Y = C.lat();
	
	var AmaxX = allowedBounds.getNorthEast().lng();
	var AmaxY = allowedBounds.getNorthEast().lat();
	var AminX = allowedBounds.getSouthWest().lng();
	var AminY = allowedBounds.getSouthWest().lat();
	
	if (X < AminX) {X = AminX;}
	if (X > AmaxX) {X = AmaxX;}
	if (Y < AminY) {Y = AminY;}
	if (Y > AmaxY) {Y = AmaxY;}

	map.setCenter(new GLatLng(Y,X));
}

function LSdateformat(sqlDateTime,lang){
	var thisdate = parseDate(sqlDateTime.slice(0,sqlDateTime.lastIndexOf(" ")),true);
	switch (lang){
		case "ja":
			return formatDate(thisdate,"yy")+"年"+formatDate(thisdate,"MM")+"月"+formatDate(thisdate,"dd")+"日"
		break;
		
		default:
			return formatDate(thisdate,"dd NNN yy")
	}
	
}

function formatwebsite(url){
	if(url.indexOf("http://")<0){
		return "http://" + url
	}else{
		return url
	}
}