var markers = new Map();
var lines = new Array();
var typeMarkers = new Map();
var subLocations = new Map();

var map;
var hasHashMarker = false;

var mapFinishedListeners = new List();

var mouseoverlistener;
var mouseoutlistener;

var glow_suffix = "glow";
var selected_suffix = "glow";
var disabled_suffix = "gray";

function listenMapFinish( f )
{
	if ( map )
	{
		f();
		return;
	}
	mapFinishedListeners.add(f);
}
function notifyMapFinishedListeners()
{
	for ( var i=0; i<mapFinishedListeners.size(); i++)
	{
		mapFinishedListeners.get(i)();
	}
}

jQuery(function() {
	
	mouseclicklistener= function(marker, point) {
		if( this && this.isEnabled ){
			if ( markerClicked )
			{
				markerClicked( this );
			}	
		}
	};
	
	
	mouseoverlistener= function(marker, point) {
		if( this && this.isEnabled ){
			setImage(this.entityId,"http://maps.cupmanager.net/map_icons/"+this.iconId+"_"+glow_suffix+".png");	
		}
	};


	mouseoutlistener= function(marker, point) {
		if( this && this.isEnabled ){
			if( this.isSelected ){
				setImage(this.entityId,"http://maps.cupmanager.net/map_icons/"+this.iconId+"_"+selected_suffix+".png");
			} else {
				setImage(this.entityId,"http://maps.cupmanager.net/map_icons/"+this.iconId+".png");
			}	
		}
	};
	
	jQuery("div.map").each(function(i, el) {
		
		var bounds = new GLatLngBounds();
		
		jQuery("div.marker", jQuery(el)).each(function(i, marker) {
			var lat = jQuery(marker).attr("lat");
			var lng = jQuery(marker).attr("lng");
			
			var point = new GLatLng(lat,lng);
			bounds.extend(point);
			
			
			var type = jQuery(marker).attr("type");
			var icon = "simple";
			if ( type == "Lodging" )
				icon = "lodging";
			if ( type == "Arena" )
				icon = "sport";
			if ( type == "Food" )
				icon = "lodgingfood";
			if ( type == "Other" )
				icon = "pushpin";
			if ( type == "Cup" )
				icon = "";
			if ( type == "Club" )
				icon = "sport";
			
			var title = jQuery(marker).attr("title");
			var address = jQuery(marker).attr("address");
			
			var m = new GMarker( point, {
				title: title, 
				icon: getIcon(icon)
			} );
			m.iconId = icon;
			m.title = title;
			m.type = type;
			m.address = address;
			m.isEnabled = true;
			m.entityId = jQuery(marker).attr("entityId");
			
			GEvent.addListener(m,"mouseover", mouseoverlistener);
			GEvent.addListener(m,"mouseout", mouseoutlistener);
			GEvent.addListener(m,"click", mouseclicklistener);
			
			if ( type ) 
			{
				if ( !typeMarkers.containsKey(type) )
					typeMarkers.put(type, new List());
				typeMarkers.get(type).add(m);
			}
			markers.put(m.entityId, m);
			
			// Ta hand om sublocations
			jQuery(marker).find(".subLocation").each(function(i,el){
				var sl = jQuery(el);
				var subLocation = { entityId: sl.attr("entityId"), title: sl.attr("title") };
				
				if ( !subLocations.containsKey(m.entityId) )
					subLocations.put(m.entityId, new List());
				subLocations.get(m.entityId).add( subLocation );
			});
			
			jQuery(marker).remove();
			
		});
		jQuery("div.line", jQuery(el)).each(function(i, line) {
			var fromlat = jQuery(line).attr("fromlat");
			var fromlng = jQuery(line).attr("fromlng");
			var tolat = jQuery(line).attr("tolat");
			var tolng = jQuery(line).attr("tolng");
			
			var l = new GPolyline( [new GLatLng(fromlat,fromlng), new GLatLng(tolat,tolng)] );
			
			lines.push(l);
			jQuery(line).remove();
		});
		
		map = new GMap2( el );
		map.enableScrollWheelZoom();
		map.enableContinuousZoom();
		
		var zoom = map.getBoundsZoomLevel(bounds);
		map.setCenter(bounds.getCenter(), Math.max(zoom-1, 1));
   		
   		notifyMapFinishedListeners();
   		
   		
		for ( var i=0; i<markers.values().length; i++ )
		{
			var m = markers.values()[i];
			map.addOverlay(m);
			
			if ( window.location.hash.length > 1 )
			{
				var name = window.location.hash.substring(1).toLowerCase();
				var parts = name.split("-");
				var id = "0";
				if ( parts.length > 0 )
					id = parts[0];
				if ( m.entityId == id )
				{
					m.show();
					map.setCenter( m.getLatLng(), 14 );
					
					typeMarkers.put("hash", new List());
					typeMarkers.get("hash").add(m);
					
					if( hashMarkerExists )
					{
						hashMarkerExists(m);
					}
					markerClicked(m);
				} else {
					m.hide();
				}
			}
		}
		for ( var i=0; i<lines.length; i++ )
		{
			var l = lines[i];
			map.addOverlay(l);
		}
		
	});
});

function setImage(id, url) {
	markers.get(id).setImage(url);
}


function getIcon(iconId){
	var icon = new GIcon();
	icon.image = "http://maps.cupmanager.net/map_icons/"+iconId+".png";
	icon.shadow = "http://maps.cupmanager.net/map_icons/"+iconId+"_shadow.png";
	icon.iconSize = new GSize(32.0, 32.0);
	icon.shadowSize = new GSize(49.0, 32.0);
	icon.iconAnchor = new GPoint(15.0, 32.0);
	icon.infoWindowAnchor = new GPoint(16.0, 16.0);
	
	return icon;
}
