
var selectedMarkerId;
var selectedDay = 1;

jQuery(function(){
	
	slideMap();
	
	jQuery(".mapFilterCheckbox").click(function(ev){
		var type = jQuery(this).attr("filter");
		var markers = typeMarkers.get(type);
		
		if ( markers )
		{
			for ( var i=0; i<markers.size(); i++ )
			{
				var m = markers.get(i);
				if ( this.checked )
					m.show();
				else
					m.hide();
			}
		}
	});
	
	jQuery("#filterHash").click(function(ev){
		var m = markers.get(selectedMarkerId);
		if ( this.checked )
			m.show();
		else
			m.hide();
	});
	
	listenMapFinish(function(){
		map.addControl( new GLargeMapControl() );
		var ov = new GOverviewMapControl();
		map.addControl( ov );
		
		var combo = jQuery("#dirCombo");
		var ms = markers.values();
		ms.sort(function(a,b){
			if (a.title < b.title)
				return -1;
			if (a.title > b.title)
				return 1;
			return 0;
		});
		for ( var i=0; i<ms.length; i++ )
		{
			var m = ms[i];
			combo.append( '<option value="'+m.entityId+'">'+m.title+'</option>' );
		}
		
		combo.change(onSelectDirCombo);
		combo.keyup(onSelectDirCombo);
	});
	
	
	jQuery("#findDir").click(function(){
		var combo = jQuery("#dirCombo");
		var id = combo.find("option:selected").val();
		var m = markers.get(parseInt(id));
		
		var url = "http://maps.google.com/maps?f=d&daddr=$DEST&saddr=$SOURCE&hl=sv&geocode=&mra=cc&ie=UTF8&z=7&pw=2";
		url = url.replace("$DEST", m.getLatLng().lat()+","+m.getLatLng().lng());
		url = url.replace("$SOURCE", jQuery("#fromAddress").val());
		
		window.open(url);
	});
	
});



function slideMatches( entityId, title )
{
	jQuery("#matchesContent").load("/service/2008/Handboll/ArenaMatchesService", {'locationId': entityId, 'day': selectedDay}, 
	function(){
		
		jQuery("#backToMapLink").click(function(){
			slideMap();
			return false;
		});
		
		jQuery(".dayLink").click(function(){
			selectedDay = jQuery(this).attr("day");
			slideMatches( entityId, title );
			return false;
		});
		jQuery(".dayLink[day='"+selectedDay+"']").addClass("selected");
		
		jQuery("#stack2").animate({marginLeft: "0px"}, 1000);
		
		
		var m = markers.get(selectedMarkerId);
		var content = "";
		var subs = subLocations.get(m.entityId);
		if ( subs.size() > 1 )
		{	
			for ( var i=0; i<subs.size(); i++ )
			{
				content += '<a href="" entityId="'+subs.get(i).entityId+'" onclick="return showMatches('+subs.get(i).entityId+', \''+subs.get(i).title+'\')">'+subs.get(i).title+'</a> | ';
			}
			jQuery("#subLocationChooser").html(content);
			
			jQuery("#subLocationChooser a[entityId='"+entityId+"']").addClass("selected");
			
			jQuery("#subLocationChooser").slideDown();
		}
	});
}

function slideMap()
{
	jQuery("#stack2").animate({marginLeft: "-900px"}, 1000);
	jQuery("#textcontainer h1").html("Overview of Lundaspelen");
	jQuery("#subLocationChooser").slideUp();
}


function onSelectDirCombo()
{
	var combo = jQuery("#dirCombo");
	var id = combo.find("option:selected").val();
	var m = markers.get(parseInt(id));
	
	selectMapMarker( m );
	setExtraCheckItem(m);
	jQuery("#directions #address").html( m.address );
	jQuery("#directions #toLocation").html( m.title );
}

function selectDirCombo( m )
{
	jQuery("#dirCombo option").removeAttr("selected");
	jQuery("#dirCombo option[value='"+m.entityId+"']").attr("selected", "selected");
	onSelectDirCombo();
}

function showMatches( entityId, title )
{
	slideMatches(entityId, title);
	
	var suffix = "";
	var m = markers.get(selectedMarkerId);
	var removeLength = m.title.length +1;
	var subs = subLocations.get(m.entityId);	
	for ( var i=0; i<subs.size(); i++ )
	{
		var sub = subs.get(i);
		var s = sub.title.substr( removeLength );
		suffix += s;
		if ( i < subs.size()-1 )
		{
			if( i < subs.size()-2 )
			{
				suffix += ", ";
			} else {
				suffix += " and ";
			}
		}
	}
	
	jQuery("#textcontainer h1").html("Matches in " + m.title + " " + suffix);
	return false;
}

function flashDir()
{
	map.closeInfoWindow();
	jQuery("#directions").fadeOut("def").fadeIn("def", function(){
		jQuery("#fromAddress").focus();
	});
	return false;
}

function markerClicked(m)
{
	selectDirCombo( m );
	selectMapMarker( m );
}

function selectMapMarker(m)
{
	m.show();
	selectedMarkerId = m.entityId;
	
	map.panTo( m.getLatLng() );
	
	if ( m.type == "Arena" ) {
		
		var content = '<h3>'+m.title+'</h3> '
			+'Address: '+m.address+'<br/> '
			+'<h4>Driving directions:</h4><a href="#'+m.entityId+'-'+m.title+'" onclick="flashDir()">Get directions to '+m.title+'</a><h4>Show matches</h4>';
		
		var subs = subLocations.get(m.entityId);	
		for ( var i=0; i<subs.size(); i++ )
		{
			content += '<a href="#'+m.entityId+'-'+m.title+'" onclick="showMatches('+subs.get(i).entityId+', \''+subs.get(i).title+'\')">Show matches in '+subs.get(i).title+'</a><br/>';
		}
		
		map.openInfoWindowHtml(m.getLatLng(), content
			);
	} else {
		map.openInfoWindowHtml(m.getLatLng(), '<h3>'+m.title+'</h3> '
			+'Address: '+m.address+'<br/> '
			+'<a href="#'+m.entityId+'-'+m.title+'" onclick="flashDir()">Get directions to '+m.title+'</a>'
			);
	}
}

function setExtraCheckItem(m)
{
	jQuery(".mapFilterCheckbox").removeAttr("checked");
	
	var li = jQuery("#extraFilterItem");
	var imgHref = getIcon(m.iconId).image;
	li.find("img").attr("src", imgHref);
	li.find("label span").html( m.title );
	li.find("input").get(0).checked = true;
	li.show();
	
}

function hashMarkerExists(m)
{
	setExtraCheckItem(m);
	selectDirCombo( m );
}