var l_xInteractiveMap = {
	map: null,
	events: {},
	infoWindow: new google.maps.InfoWindow(),
	markers: {},
	markerImages: {
		future: new google.maps.MarkerImage(
			'/image/entertainment/kisstour/marker_sprite.png',
			new google.maps.Size(32, 37),
			new google.maps.Point(0, 0),
			new google.maps.Point(16, 37)
		),
		past: new google.maps.MarkerImage(
			'/image/entertainment/kisstour/marker_sprite.png',
			new google.maps.Size(32, 37),
			new google.maps.Point(32, 0),
			new google.maps.Point(16, 37)
		)
	}
};

var initMap = function() {
	l_xInteractiveMap.map = new google.maps.Map(document.getElementById($('#map').attr('id')), {
		zoom: 4,
		center: new google.maps.LatLng(37.0625, -95.677068),
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControl: false,
		scrollWheel: false,
		navigationControlOptions: {
			style: google.maps.NavigationControlStyle.ZOOM_PAN,
			position: google.maps.ControlPosition.LEFT
		}
	});
	
	l_xInteractiveMap.closeInfoWindow = function() { l_xInteractiveMap.infoWindow.close(); };
	l_xInteractiveMap.openInfoWindow = function(p_xMarker) {
		var l_xEvent = l_xInteractiveMap.events[p_xMarker.dateId];
		l_xInteractiveMap.closeInfoWindow();
		var l_xAddress = l_xEvent.venue_geocode.results[0].address_components;
		l_sContentHtml = '<div class="info_window_body">';
		l_sContentHtml += '<p class="venue">' + l_xEvent.venue_name + '</p>';
		l_sContentHtml += '<p class="date">' + l_xEvent.event_date + '</p>';
		l_sContentHtml += '<p class="location">' + l_xEvent.venue_geocode.results[0].formatted_address + '</p>';
		
		// Upcoming shows get ticket and Facebook event links
		// Past shows get a link to Dr Pepper's Facebook photo page
		if(l_xEvent.event_timestamp * 1000 > Date.parse(Date())) {
			if(typeof(l_xEvent.tickets_url) == 'string' && l_xEvent.tickets_url.length > 0) {
				l_sContentHtml += '<p><a target="_blank" href="' + l_xEvent.tickets_url + '">Click here to buy tickets</a>';
			}
			
			if(typeof(l_xEvent.facebook_event_id) == 'string' && l_xEvent.facebook_event_id.length > 0) {
				l_sContentHtml += '<p class="facebook_event"><a target="_blank" href="http://www.facebook.com/event.php?eid=' + l_xEvent.facebook_event_id + '">Add Event to Facebook</a></p>';
			}
		}
		else {
			l_sContentHtml += '<p class="facebook_event"><a target="_blank" href="http://www.facebook.com/DrPepper?v=photos">Post photos</a></p>';
		}
		
		l_sContentHtml += '</div>';
		l_xInteractiveMap.infoWindow.setContent(l_sContentHtml);
		l_xInteractiveMap.infoWindow.open(l_xInteractiveMap.map, l_xInteractiveMap.markers[p_xMarker.dateId]);
	};
};

var populateMap = function(p_xData) {
	if(typeof(p_xData.events) != 'undefined') {
		for(var i in p_xData.events) {
			var l_xEvent = p_xData.events[i];
			if(typeof(l_xEvent.venue_geocode) === 'undefined') { continue; }
			if(typeof(l_xEvent.venue_geocode.results) === 'undefined') { continue; }
			if(typeof(l_xEvent.venue_geocode.results[0]) === 'undefined') { continue; }
			if(typeof(l_xEvent.venue_geocode.results[0].geometry) === 'undefined') { continue; }
			if(typeof(l_xEvent.venue_geocode.results[0].geometry.location) === 'undefined') { continue; }
			l_xInteractiveMap.events[i] = l_xEvent;
			var l_xLocation = l_xEvent.venue_geocode.results[0].geometry.location;
			var l_bDateInFuture = (l_xEvent.event_timestamp * 1000 - Date.parse(Date()) >= 0);
			
			l_xInteractiveMap.markers[i] = new google.maps.Marker({
				map: l_xInteractiveMap.map,
				position: new google.maps.LatLng(l_xLocation.lat, l_xLocation.lng),
				icon: (l_bDateInFuture) ? l_xInteractiveMap.markerImages.future : l_xInteractiveMap.markerImages.past,
				dateId: i
			});
			google.maps.event.addListener(l_xInteractiveMap.markers[i], 'click', function() {
				l_xInteractiveMap.openInfoWindow(this);
			});
		}
	}
};

$(document).ready(function() {
	initMap();
	populateMap(l_xData);
	var dateKey = window.location.hash.replace(/[^\d]{1,}/, '');
	try {
		google.maps.event.trigger(l_xInteractiveMap.markers[dateKey], 'click');
		l_xInteractiveMap.map.setCenter(l_xInteractiveMap.markers[dateKey].position);
		l_xInteractiveMap.map.setZoom(10);
	} catch (e) {}
});
