	var currentMarker = null;
	function ShowMarker( i ) {
    	GEvent.trigger( markers[i], "click" );
	}	
	
	function EmailFriend( form ) {
		
		var valid = new Validation(form, { 
		   immediate : true, 
		   onSubmit:false
	   });
		
		$(form).up('.friend-container', 0).select('.friend-thank-you')[0].show();
		$(form).up('.friend-container', 0).select('.friend-form-container')[0].hide();
			
		if( valid.validate() ) {
			//email($event_id = null, $your_name = null, $your_email = null, $friend_name = null, $friend_email = null) 
			var params = $(form).serialize(true);
			//	alert( "Email to a Friend: " + $(form).serialize() );
			var url = "/events/email/" + params.event_id + '/' + params.your_name+ '/' + params.your_email + '/' + params.friends_name +  '/' + params.friends_email;
			//alert( url );
			new Ajax.Request( url, {
				onSuccess: function(transport) { 
					//alert('We have sent this event to your friend on your behalf.');	
				},
				onFailure: function() {
					alert('An error occured');
					
				}
			});
		
		}
		
		
		return false;
	}
	  
	  
    function GetDirections( from, to ) {
    	GEvent.addListener( dir, "load", onGDirectionsLoad ); 
		dir.load( from + " to " + to, { getPolyline:true } );
		$('panel').style.display = 'none';		
	}

	function onGDirectionsLoad() {
		//var pline = dir.getPolyline();
		//pline.show();
		//map.addOverlay(pline);	
		//dir.clear();
	}
	
	
	// Functions related to popup marker controls.
	function OpenDirectionsForm( openLink, directions_type) {
		
		var divContainer = $(openLink).up('div.body', 0);
		openLink.up('div.directions-container', 0).addClassName('open');
		divContainer.select('.friend-container')[0].removeClassName('open');
		
		// Close if open
		if( divContainer.select('.from_address')[0].visible() ) 
			new Effect.SlideUp(divContainer.select('.from_address')[0], { duration: .2, queue: 'end' });
		if( divContainer.select('.to_address')[0].visible() ) 
			new Effect.SlideUp(divContainer.select('.to_address')[0], { duration: .2, queue: 'end' });
		
		if( divContainer.select('.friend-form-container')[0].visible() ) 	
			new Effect.SlideUp(divContainer.select('.friend-form-container')[0], { duration: .2, queue: 'end' });
		
		// Open if Closed
		if( !divContainer.select('.' + directions_type + '_address')[0].visible() ) 
			new Effect.SlideDown(divContainer.select('.' + directions_type + '_address')[0], { duration: .2, queue: 'end' });
	}
	
	
	function OpenSendToFriendsForm( openLink ) {
		
		var divContainer = $(openLink).up('div.body', 0);
		openLink.up('div.friend-container', 0).addClassName('open');
		divContainer.select('.directions-container')[0].removeClassName('open');
		
		// Close if open
		if( divContainer.select('.from_address')[0].visible() ) 
			new Effect.SlideUp(divContainer.select('.from_address')[0], { duration: .2, queue: 'end' });
		if( divContainer.select('.to_address')[0].visible() ) 
			new Effect.SlideUp(divContainer.select('.to_address')[0], { duration: .2, queue: 'end' });
		
		// Open if Closed
		if( !openLink.next('div.friend-form-container').visible() ) 	
			new Effect.SlideDown(openLink.next('div', 0), { duration: .2, queue: 'end' });
	}

	function CreateMarker(point, flapjack) { //address,html,event_name) {
		if( !flapjack.id ) alert( 'That isn\'t a real Event ID!');
	

		var icon = new GIcon();
		icon.image = "/img/pan/pan_" + flapjack.marker_char + ".png";
		icon.shadow = "/img/shadow-pan.png";
		icon.iconSize = new GSize( 22.0, 21.0 );
	    icon.shadowSize = new GSize( 33.0, 21.0 );
	    icon.iconAnchor = new GPoint( 11.0, 10.0 );
    	icon.infoWindowAnchor = new GPoint( 11.0, 10.0 );
    	
    	var marker = new GMarker( point, icon );
        marker.id = flapjack.id; // Attach flapjack.id to marker object to recall it on click - jp
		
        GEvent.addListener( marker, "click", function() {
    		//alert(this.id);
			if( currentMarker ) $( currentMarker ).removeClassName('selected');
			if( $( 'event_' + this.id ) ) {
				$( 'event_' + this.id ).addClassName('selected');
				currentMarker =  $( 'event_' + this.id );
			}
			// Select/Deselect result
			
		
    		//new EBubble(map, "img/map_bubble.png",imageSize, contentSize, contentOffset, anchor, noCloseOnClick);
        	bubble = new EBubble( map, "img/map_bubble.png", new GSize(-100,200), new GSize(214,0), new GPoint(10, 30), point, true );
			        				
			var divs = $$('.custom_made_popup'); 
            for(i = 0; i < divs.length; i++ ) { 
            	if( divs[i].id != "map" ) $(divs[i]).hide();	
          	}
            
			map.panTo( point );
					
              				
          	//bubble.openOnMarker(marker, form);
          	// form one: 
          	bubble.openOnMap(
				point, 
				'<h3>'
				//+  events['event'+flapjack.id].marker_char + ': '
				+ flapjack.event_name + '</h3>' 
				+ '<p>' + flapjack.location + '<br />' + flapjack.event_date + '</p>'
				+ '<p>' + flapjack.event_description + '</p>'
				+ '<div class="directions-container">'
					+ '<div class="btn-direction"><strong>Get directions:</strong> ' 
					+ '<a href="#" onclick="OpenDirectionsForm( this, \'from\' );return false;">To here</a> - ' 
					+ '<a href="#" onclick="OpenDirectionsForm( this, \'to\' );return false;">From here</a></div>' 
					
					+ '<div id="div_from_address_unique_id" class="from_address" style="display:none;">'
						+ '<div class="input directions">'
							+ '<label for="from_address_unique_id">Start address</label>'
							+ '<input type="text" class="directions-text" id="from_address_unique_id" /> '
							+ '<input type="image" class="directions-go" src="/img/events/go.png" alt="Go" onclick="GetDirections($(\'from_address_unique_id\').value,\'unique_point\');return false;" />'
						+ '</div>' 
					+ '</div>' 
					
					+ '<div id="div_to_address_unique_id" class="to_address" style="display:none;">'
						+ '<div class="input directions">'
							+ '<label for="from_address_unique_id">End address</label>'
							+ '<input type="text" class="directions-text" id="to_address_unique_id" /> '
							+ '<input type="image" class="directions-go" src="/img/events/go.png" alt="Go" onclick="GetDirections($(\'to_address_unique_id\').value,\'unique_point\');return false;" />'
						+ '</div>' 
					+ '</div>' 
				+ '</div>' 
				
				+ '<div class="friend-container">'
					+ '<a href="#" onclick="OpenSendToFriendsForm(this); return false;" class="btn-send-to-friend">Send To Friend</a>'
					+ '<div style="display: none;" class="friend-form-container"><form id="email_to_friend_unique_id" class="friend-fields" action="" method="post" onsubmit="return EmailFriend(this)">'
						+ '<input type="hidden" name="event_id" value="unique_id" />'
						+ '<div class="input text"><label>Your Name</label><input type="text" class="required" name="your_name" id="your_name_unique_id" /></div>'
						+ '<div class="input text"><label>Your Email:</label><input type="text" class="required validate-email" name="your_email" id="your_email_unique_id" /></div>'
						+ '<div class="input text"><label>Friend\'s Name:</label><input type="text" class="required" name="friends_name" id="friends_name_unique_id" /></div>'
						+ '<div class="input text"><label>Friend\'s Email:</label><input type="text" class="required validate-email" name="friends_email" id="friends_email_unique_id" /></div>'
						+ '<div class="submit"><input type="image" src="/img/events/submit.png" alt="Send To Friend" /></div>'
					+ '</form>'
					
   					  
					
					+ '</div>'
					+ '<div class="friend-thank-you" style="display:none;"><p>We have sent this event to your friend on your behalf. <br />Thanks for sharing the pancake love.</p></div>'
				+ '</div>' ,
			
				new GPoint(100,170),
				flapjack.location, 
				map, 
				flapjack.id
			);	
			//	bubble.openOnMap(point, name, new GPoint( 100, 170 ) );				    		
		});
        
		markers[flapjack.id] = marker;
		
    			
        return marker;
    }

	function AddPoint( flapjack ) { 
		var point = new GLatLng( flapjack.latitude, flapjack.longitude );
		map.setCenter( point, 10 );
    	var marker = CreateMarker( point, flapjack ); 
		
        map.addOverlay( marker );
	}

	
