var map;
var default_location = new Array();
default_location['lat'] = "18.397";
default_location['lng'] = "-30.644";
var locations = [];
var bounds = new GLatLngBounds();
var all;
var show_mine = false;
var current_marker_type = "circles";
var view;
var circlesInfo;

function load_circles_info(circlesInfo) {
	this.circlesInfo = circlesInfo;
}

function gmap_build(lat, lng, zoom, div_id, min_zoom, max_zoom) {
	gmap_init(lat, lng, zoom, div_id, min_zoom, max_zoom);
	update_map();
	GEvent.addListener(map, 'zoomend', function() {
		if (current_marker_type == 'circles') {
			gmap_plot(all, map.getZoom());
		}
	});
}

function gmap_init(lat, lng, zoom, div_id, min_zoom, max_zoom) {
	map = new GMap2(document.getElementById(div_id));
	map.addControl(new GSmallMapControl(), new GControlPosition(
			G_ANCHOR_TOP_RIGHT, new GSize(10, 10)));
	map.setCenter(new GLatLng(lat, lng), zoom);
	map.enableContinuousZoom();
	map.enableScrollWheelZoom();
	G_NORMAL_MAP.getMinimumResolution = function() {
		return min_zoom;
	};
	G_NORMAL_MAP.getMaximumResolution = function() {
		return max_zoom;
	};
}

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

function plot_points(points, center, zoom, type) {
	all = points;
	gmap_plot(points, map.getZoom(), type);
	if (zoom) {
		map.setZoom(map.getBoundsZoomLevel(bounds));
	}
	if (center) {
		map.setCenter(bounds.getCenter());
	}
}

function draw_marker(position, image, title, id, currentCity, size, size2) {
	var icon = new GIcon(G_DEFAULT_ICON);
	if (currentCity) {
		image='http://maps.google.com/mapfiles/ms/micons/man.png';
		icon.image = image;
		icon.shadow = 'http://maps.google.com/mapfiles/ms/micons/man.shadow.png';
		icon.iconSize = new GSize(32, 32);
		icon.shadowSize = new GSize(59, 32);
		icon.iconAnchor = new GPoint(16, 32);
	}else{
		if(image == undefined){
			image='http://labs.google.com/ridefinder/images/mm_20_red.png';
			}
		if(size == undefined){
			size='12';
			size2='20';
		}
		icon.image = image;
		icon.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
		icon.iconSize = new GSize(size, size2);
		icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(6, 20);
	}

	var marker = new GMarker(position, {
		icon : icon
	});
	map.addOverlay(marker);

	GEvent.addListener(marker, 'mouseover', function() {
		var tooltip_text = "<div class='tooltip'><b>" + title + "</b></div>";
		this.overlay = new MapTooltip(this, tooltip_text);
		map.addOverlay(this.overlay);
	});

	GEvent.addListener(marker, 'mouseout', function() {
		map.buttonMode = false;
		map.removeOverlay(this.overlay);
	});

	GEvent.addListener(marker, 'click', function() {
		select_city(position, title, id);
	});
	bounds.extend(position);
}

function draw_circle(position, visits, title, id, zoom) {
	var icon = new GIcon();
	var zoom_image = zoom;
	if (zoom < 2) {
		zoom_image = 2;
	} else if (zoom > 9) {
		zoom_image = 9;
	}
	
	var visits_image;
	if (visits <= 21) {
		visits_image = visits;
	} else if (visits >= 101) {
		visits_image = 101;
	} else {
		visits_image = visits - (visits % 10) + 1;
	}

	image = new Image();

	image.src = '/images/markers/'+zoom_image+'_'+visits_image+'.png';
	icon.image = image.src;
	icon.shadow = '';
	var height = circlesInfo[zoom_image][visits_image]; 
	var width = height; 
	icon.iconSize = new GSize(height, width);
	icon.iconAnchor = new GPoint(Math.ceil(height/2), Math.ceil(width/2));
	var marker = new GMarker(position, {
		icon : icon,
		zIndexProcess: function() {return -visits;}
	});
	map.addOverlay(marker);
	GEvent.addListener(marker, 'mouseover', function() {
		if (visits > 1) {
			var visit_word = "visits";
		} else {
			var visit_word = "visit";
		}
		var tooltip_text = "<div class='tooltip'><b>" + title
		+ "</b><br />" + visits + " " + visit_word + "</div>";
		this.overlay = new MapTooltip(this, tooltip_text);
		map.addOverlay(this.overlay);
	});
	
	GEvent.addListener(marker, 'mouseout', function() {
		map.buttonMode = false;
		map.removeOverlay(this.overlay);
	});
	
	GEvent.addListener(marker, 'click', function() {
		select_city(position, title, id);
	});
	bounds.extend(position);

}

function gmap_plot(points, zoom, type) {
	map.clearOverlays();
	locations = points;
	bounds = new GLatLngBounds();
	$.qu.clear();
	for ( var x = 0; x < locations.length; x++) {
		var location = locations[x];
		var position = new GLatLng(location.latitude, location.longitude);
		if (type == "markers") {
			if (location.private) {
				var image = '/images/marker_private.png';
			} else {
				var image = '/images/marker_public.png';
			}
			if (location.user != 'me' || current_marker_type == "markers"
					&& view != 'explore') {
				draw_marker(position, image, location.title, location.id, location.current_city);
			}
		} else {
			if (location.user == 'me') {
				if (show_mine) {
					draw_circle(position, location.visits, location.title, location.id, zoom);
				}
			} else {
				draw_circle(position, location.visits, location.title, location.id, zoom);
			}
		}
	}
}

function draw_arc(lat1, lng1, lat2, lng2) {
	map.clearOverlays();
	var origin = new GLatLng(lat1, lng1, true);
	var destiny = new GLatLng(lat2, lng2, true);
	var originMarker = new GMarker(origin);
	var destinyMarker = new GMarker(destiny);
	var bounds = new GLatLngBounds();
	bounds.extend(originMarker.getPoint());
	bounds.extend(destinyMarker.getPoint());
	map.addOverlay(new GMarker(origin));
	map.addOverlay(new GMarker(destiny));
	map.setZoom(map.getBoundsZoomLevel(bounds));
    map.setCenter(bounds.getCenter());
	var polyline = new GPolyline( [ new GLatLng(lat1, lng1),new GLatLng(lat2, lng2) ], "#ff0000", 2, 1, {geodesic : true});
	map.addOverlay(polyline);
}


