var gmarkers = [];
var beer_icon = new GIcon(G_DEFAULT_ICON);
beer_icon.image = "/images/beer.gif";
beer_icon.shadow = "/images/shadow-beer.png";
beer_icon.iconSize = new GSize(21, 24);
beer_icon.shadowSize = new GSize(34, 24);
beer_icon.iconAnchor = new GPoint(10, 12);
beer_icon.infoWindowAnchor = new GPoint(10, 12);

var beer_event_icon = new GIcon(G_DEFAULT_ICON);
beer_event_icon.image = "/images/beer_event.gif";
beer_event_icon.shadow = "/images/shadow-beer.png";
beer_event_icon.iconSize = new GSize(21, 34);
beer_event_icon.shadowSize = new GSize(34, 24);
beer_event_icon.iconAnchor = new GPoint(10, 12);
beer_event_icon.infoWindowAnchor = new GPoint(10, 12);

var cart_icon = new GIcon(G_DEFAULT_ICON);
cart_icon.image = "/images/cart.gif";
cart_icon.shadow = "/images/shadow-cart.png";
cart_icon.iconSize = new GSize(28, 25);
cart_icon.shadowSize = new GSize(41.0, 25.0);
cart_icon.iconAnchor = new GPoint(14.0, 12.0);
cart_icon.infoWindowAnchor = new GPoint(14.0, 12.0);

function resetMap(map, bounds) {
	var sw = bounds.getSouthWest();
	if (sw.equals(bounds.getNorthEast())) {
		map.setZoom(15);
	} else {
		map.setZoom((map.getBoundsZoomLevel(bounds)));
	}
	map.setCenter(bounds.getCenter());		
}

function createIcon(place_type, event_name) {
	if (place_type=='bar') {
		if (event_name)
			return beer_event_icon;
		else
			return beer_icon;
	} else {
		return cart_icon;
	}
}

function editMap(map) {
	map.disableDoubleClickZoom();
	GEvent.addListener(map, 'dblclick', function(marker, clickedPoint) {
    	map.clearOverlays();
		var m = new GMarker(clickedPoint);
		$("#latitude").attr('value', clickedPoint.lat());
		$("#longitude").attr('value', clickedPoint.lng());
		map.addOverlay(m);
  	});
}

function placeMarker(map, bounds, place) {
	if(place.latitude != "" && place.longitude != "") {
		var point = new GLatLng(place.latitude, place.longitude);
		var marker = new GMarker(point, createIcon(place.place_type, place.event_name));
		var tooltip = new Tooltip(marker, place.name, 4);
		marker.tooltip = tooltip; 
		gmarkers[place.id] = marker;
		map.addOverlay(marker);
		map.addOverlay(tooltip); 
		
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(place.tooltip);
        });
        GEvent.addListener(marker,'mouseover',function(){ this.tooltip.show(); }); 
        GEvent.addListener(marker,'mouseout',function(){ this.tooltip.hide(); });
		
		bounds.extend(point);
	}
}

function loadSingle(place) {
	if (GBrowserIsCompatible()) {
    	var map = new GMap2(document.getElementById('map'));
    	map.addControl(new GSmallZoomControl());
    	map.addControl (new GMapTypeControl());
    	map.enableScrollWheelZoom();
    	map.setCenter(new GLatLng(center_latitude,center_longitude), center_zoom);
    	var bounds = new GLatLngBounds();
		placeMarker(map, bounds, place);
		resetMap(map, bounds);
		return map;
	}
}

function loadList(list) {
	if (GBrowserIsCompatible()) {
		gmarkers = [];
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallZoomControl());
		map.addControl (new GMapTypeControl());
		map.enableScrollWheelZoom();
		map.setCenter(new GLatLng(center_latitude, center_longitude), center_zoom);
		var bounds = new GLatLngBounds();
		for (i=0; i<places.length; i++)
			placeMarker(map, bounds, places[i]);
		resetMap(map, bounds);
	}
}

function loadEmpty() {
	if (GBrowserIsCompatible()) {
		gmarkers = [];
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallZoomControl());
		map.addControl (new GMapTypeControl());
		map.enableScrollWheelZoom();
		map.setCenter(new GLatLng(center_latitude, center_longitude), center_zoom);
		return map;
	}
}

/**
 * @author Marco Alionso Ramirez, marco@onemarco.com
 * @version 1.0
 * The Tooltip class is an addon designed for the Google
 * Maps GMarker class.
 */

/**
 * @constructor
 * @param {GMarker} marker
 * @param {String} text
 * @param {Number} padding
 */
function Tooltip(marker, text, padding){
	this.marker_ = marker;
	this.text_ = text;
	this.padding_ = padding;
}

Tooltip.prototype = new GOverlay();

Tooltip.prototype.initialize = function(map){
	var div = document.createElement("div");
	div.appendChild(document.createTextNode(this.text_));
	div.className = 'tooltip';
	div.style.position = 'absolute';
	div.style.visibility = 'hidden';
	map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
	this.map_ = map;
	this.div_ = div;
}

Tooltip.prototype.remove = function(){
	this.div_.parentNode.removeChild(this.div_);
}

Tooltip.prototype.copy = function(){
	return new Tooltip(this.marker_,this.text_,this.padding_);
}

Tooltip.prototype.redraw = function(force){
	if (!force) return;
	var markerPos = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
	var iconAnchor = this.marker_.getIcon().iconAnchor;
	var xPos = Math.round(markerPos.x - this.div_.clientWidth / 2);
	var yPos = markerPos.y - iconAnchor.y - this.div_.clientHeight - this.padding_;
	this.div_.style.top = yPos + 'px';
	this.div_.style.left = xPos + 'px';
}

Tooltip.prototype.show = function(){
	this.div_.style.visibility = 'visible';
}

Tooltip.prototype.hide = function(){
	this.div_.style.visibility = 'hidden';
}