var map;
var zoomSize = 17;
var minLat = -1;
var maxLat = -1;
var minLong = -1;
var maxLong = -1;
var markers = [];
var defaultIcon = '/gfx/marker_default.png';
var selectedIcon = '/gfx/marker_selected.png';
var inactiveIcon = '/gfx/marker_inactive.png';
var showAllPois = false;
var defaultPoiId;
function createMarker(id, name, latitude, longitude, actref) {
    if (id == defaultPoiId) { return; }
	var marker = new google.maps.Marker({
		position: new google.maps.LatLng(latitude, longitude),
		map: map,
		title:name,
		icon: actref == 0 ? defaultIcon : inactiveIcon
	});
	markers[id] = marker;
	
	if (maxLat === -1) {
		maxLat = minLat = latitude;
		minLong = maxLong = longitude;
	} else {
		if (latitude < minLat) {
			minLat = latitude;
		}
		if (latitude > maxLat) {
			maxLat = latitude;
		}
		if (longitude < minLong) {
			minLong = longitude;
		}
		if (longitude > maxLong) {
			maxLong = longitude;
		}
	}

	createMarkerInfo(marker, name, id);
}

function createMarkerInfo(marker, name, id){
	var info = new google.maps.InfoWindow({
		content: '<b>' + name + '</b><br /><a href=/r/' + id + '>Saznaj detalje</a>'
	});
	google.maps.event.addListener(marker, 'click', function() {
		info.open(map, marker);
	});
}


function removeAllMarkers(){
	for(i in markers){
		markers[i].setMap(null);
	}
	markers = [];
}

function centerMap(latitude, longitude) {
	map.setCenter(new google.maps.LatLng(latitude, longitude));
}

function findOnMap(index) {
	centerMap(locations[index].lat, locations[index].lng);
}

function showZoomMsg(){
	$("#zoomMsg").show();
}
function hideZoomMsg(){
	$("#zoomMsg").hide();
}
function addMapListener() {
    google.maps.event.addListener(map, 'idle', function() {
        if (showAllPois) {
            boundarySearch();
        }
    });
}

function boundarySearch() {
    var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    $.ajax({
        type: 'POST',
        url: '/mapSearch.boundary.do',
        data: 'minLat=' + southWest.lat() + '&maxLat=' + northEast.lat() + '&minLng=' + southWest.lng() + '&maxLng=' + northEast.lng(),
        beforeSend: function() {
            pleaseWait();
        },
        success: function(result) {
            pleaseWait(false);
            eval(result);
        }
    });
}

// get current location, using GEO API
var currentLatitude;
var currentLongitude;

function showMyLocation() {
    if (Modernizr.geolocation) {
        navigator.geolocation.getCurrentPosition(geo_location, geo_location_error);
    } 
}

function geo_location(position) {
    currentLatitude = position.coords.latitude;
    currentLongitude = position.coords.longitude;
    centerMap(currentLatitude, currentLongitude);
}

// TODO: Implement some message that have meaning for the user (if it is necessary)
function geo_location_error(error) {
    switch (error.code) {
        case 0:
            // UNKNOWN_ERROR (0)
            break;
        case 1:
            // PERMISSION_DENIED (1): user clicked on "don't share location"
            break;
        case 2:
            // POSITION_UNAVAILABLE (2)
            break;
        case 3:
            // TIMEOUT (3)
            break;
        break;
    }
}
