function distance( lat1 , lon1 , lat2 , lon2 , unit ) 
{
	var radlat1 = Math.PI * lat1/180;
	var radlat2 = Math.PI * lat2/180;
	var radlon1 = Math.PI * lon1/180;
	var radlon2 = Math.PI * lon2/180;
	var theta = lon1-lon2;
	var radtheta = Math.PI * theta/180;
	var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
	dist = Math.acos(dist);
	dist = dist * 180/Math.PI;
	dist = dist * 60 * 1.1515;
	if (unit=="K") { dist = dist * 1.609344 }
	if (unit=="N") { dist = dist * 0.8684 }
	return dist;
}

function textOfDistance( distance )
{

	var distanceText = '1 mile from here';
	
	if ( distance > 1 ) distanceText = '5 miles from here';
	if ( distance > 5 ) distanceText = '10 miles from here';
	if ( distance > 10 ) distanceText = '15 miles from here';
	if ( distance > 15 ) distanceText = '20 miles from here';

	return distanceText;

} // textOfDistance

function textOfMinutes( minutes )
{

	var minutesText = '5 min ago';
		
	if ( minutes > 5 ) minutesText = '10 min ago';
	if ( minutes > 10 ) minutesText = '15 min ago';
	if ( minutes > 15 ) minutesText = '20 min ago';
	if ( minutes > 20 ) minutesText = '30 min ago';
	if ( minutes > 30 ) minutesText = '45 min ago';
	if ( minutes > 45 ) minutesText = '1 hr ago';
	if ( minutes > 60 ) minutesText = '1 hr 15 min ago';
	if ( minutes > 75 ) minutesText = '1 hr 30 min ago';
	if ( minutes > 90 ) minutesText = '1 hr 45 min ago';

	return minutesText;

} // textOfMinutes

function sortByMinutes( a , b )
{

	return ( ( a.minutes < b.minutes ) ? -1 : ( ( a.minutes > b.minutes ) ? 1 : 0 ) );

} // sortByMinutes

function sortByDistanceFromMe( a , b )
{

	return ( ( a.distance < b.distance ) ? -1 : ( ( a.distance > b.distance ) ? 1 : 0 ) );

} // sortByDistanceFromMe

function showActivities()
{

	page = 'activities';
	jQuery( '#activities-page' ).empty();
	resetPage();

	activities = new Array();

	for ( var i = 0; i < nearbyPeople.length; i++ )
	{
	
		var aPerson = nearbyPeople[i];
	
		if ( aPerson.degree <= maxDegree )
		{
	
			for ( var j = 0; j < aPerson.activities.length; j++ )
			{

				var activity = aPerson.activities[j].name;
				// var inArray = -1;
				var personDistance = distance( me.lat , me.lon , aPerson.lat , aPerson.lon , 'M' );
				var personMinutes = aPerson.activities[j].minutes;
	
				activities.push( { 'activity' : activity , 'name' : aPerson.name , 'distance' : personDistance , 'minutes' :  personMinutes , 'degree' : aPerson.degree } );

				/*
	
				for ( var k = 0; k < activities.length; k++ )
					if ( activities[k].activity == activity ) inArray = k;
				
				if ( inArray < 0 )		
				{

					activities.push( { 'activity' : activity , 'names' : [ aPerson.name ] , 'distance' : personDistance , 'minutes' :  personMinutes } );

				} // if
				else
				{

					activities[ inArray ].names.push( aPerson.name );
					
					if ( personDistance < activities[ inArray ].distance )
						activities[ inArray ].distance = personDistance;
						
					if ( personMinutes < activities[ inArray ].minutes )
						activities[ inArray ].minutes = personMinutes;
						
				} // else
				
				*/

			} // for

		} // if
	
	} // for
	
	var activityList = jQuery( '<ul class="activities"></ul>' );

	activities.sort( sortByMinutes );

	for ( i = 0; i < activities.length; i++ )
	{

		var thisActivity = activities[i];
	
		var activityLi = jQuery( '<li id="activity-' + i + '"><h2>' + thisActivity.activity + '</h2><img class="arrow" src="images/icon/arrow.png" /></li>' )
			.click(
			
				function()
				{
				
					idParts = this.id.split( '-' );
					showActivity( activities[ idParts[1] ] );

				} // function
			
			);

		if ( thisActivity.degree == 0 )
		{
		
			$( '<div class="icon"><img src="images/icon/friend.png" /></div>' )
				.appendTo( activityLi );
		
		} // if
		else if ( thisActivity.degree == 1 )
		{
		
			$( '<div class="icon"><img src="images/icon/one.png" /></div>' )
				.appendTo( activityLi );
		
		} // else if
		else if ( thisActivity.degree == 2 )
		{
		
			$( '<div class="icon"><img src="images/icon/everyone.png" /></div>' )
				.appendTo( activityLi );
		
		} // else if
		else if ( thisActivity.degree == 3 )
		{
		
			$( '<div class="icon"><img src="images/icon/ad.png" /></div>' )
				.appendTo( activityLi );
		
		} // else if

		/*

		var nameText = '';
		var moreNames = 0;
		
		for ( j = 0; j < thisActivity.names.length; j++ )
		{

			if ( nameText.length > 30 )
				moreNames++;
			else if ( nameText.length == 0 )
				nameText = thisActivity.names[j];
			else
				nameText = nameText + ', ' + thisActivity.names[j];

		} // for

		if ( moreNames > 0 ) nameText = nameText + ', (' + moreName + ' more)';

		*/

		jQuery( '<div class="people">' + thisActivity.name + '</div>' )
			.appendTo( activityLi );

		var distanceText = textOfDistance( thisActivity.distance );

		jQuery( '<div class="distance">' + distanceText + '</div>' )
			.appendTo( activityLi );
			
		var minutesText = textOfMinutes( thisActivity.minutes );
			
		jQuery( '<div class="time">' + minutesText + '</div>' )
			.appendTo( activityLi );

		activityLi.appendTo( activityList );
	
	} // for
		
	activityList.appendTo( '#activities-page' );

} // showActivities

function showActivity( activity )
{

	page = 'activity';
	jQuery( '#activity-page' ).empty();
	resetPage();

	jQuery( '<div><h1>Everyone ' + activity.activity + ' goes here</h1><div>' )
		.appendTo( '#activity-page' );

	var peopleList = jQuery( '<ul></ul>' );

	for ( var i = 0; i < nearbyPeople.length; i++ )
	{

		var aPerson = nearbyPeople[i];
		var hasActivity = false;
		
		for ( var j = 0; j < aPerson.activities.length; j++ )
		{
		
			if ( aPerson.activities[j] == activity )
				hasActivity = true;
			
		} // for
		
		if ( hasActivity )
			jQuery( '<li class="vcard" id="person-' + i + '">' + aPerson.name + '<li>' )
				.click( 
				
					function()
					{
	
						idParts = this.id.split( '-' );
						showPerson( nearbyPeople[ idParts[1] ] );
					
					} // function
				
				)
				.appendTo( peopleList );

	} // for

	peopleList.appendTo( '#activity-page' );

} // showActivity

function showLocations( personId )
{

	page = 'locations';
	resetPage();

	if ( ! map )
	{

		if ( GBrowserIsCompatible() ) 
		{

			map = new GMap2( document.getElementById( 'locations-page' ) );
			
			if ( typeof personId != 'number' )
				map.setCenter( new GLatLng( me.lat , me.lon ) , 11 );
			else
			{
			
				var aPerson = nearbyPeople[ personId ];
				map.setCenter( new GLatLng( aPerson.lat , aPerson.lon ) , 11 );
			
			} // else
	
		} // if

	} // if
	else 
	{
	
		if ( typeof personId == 'number' )
		{
		
			var aPerson = nearbyPeople[ personId ];
			map.panTo( new GLatLng( aPerson.lat , aPerson.lon ) );
			
		} // if
		else
		{
		
			map.panTo( new GLatLng( me.lat , me.lon ) );
		
		} // else
		
	} // else
	
	map.clearOverlays();
	
	for ( var i = 0; i < nearbyPeople.length; i++ )
	{
	
		if ( nearbyPeople[i].degree <= maxDegree )
		{
	
			if (
				( typeof personId != 'number' ) ||
				( nearbyPeople[i].id == personId )
			)
			{

				var marker = createMarker( nearbyPeople[i] );			
				map.addOverlay( marker );
				
			} // if
	
		} // if
	
	} // for

} // showLocations

function addDistanceHeader( distance )
{
	
	if ( distance == -1 )
		jQuery( '<div class="section"><span>0-2 miles</span></div>' )
			.appendTo( '#people-page' );
	if ( distance == 3 )
		jQuery( '<div class="section"><span>3-8 miles</span></div>' )
			.appendTo( '#people-page' );
	if ( distance == 9 )
		jQuery( '<div class="section"><span>9-49 miles</span></div>' )
			.appendTo( '#people-page' );
	if ( distance == 50 )
		jQuery( '<div class="section"><span>50+ miles</span></div>' )
			.appendTo( '#people-page' );

} // addDistanceHeader

function showPeople()
{

	page = 'people';
	jQuery( '#people-page' ).empty();
	resetPage();
	
	var distanceHeader = -1;
	var distanceHeaders = [ -1 , 3 , 9 , 50 , 1000000 ];
	
	nearbyPeople.sort( sortByDistanceFromMe );
	
	for ( var i = 0; i < nearbyPeople.length; i++ )
	{
	
		var aPerson = nearbyPeople[i];

	
		if ( aPerson.degree <= maxDegree )
		{

			if ( aPerson.distance > distanceHeader )
			{
	
				addDistanceHeader( distanceHeader );
			
				for ( var j = 0; j < distanceHeaders.length; j++ )
				{
							
					if ( aPerson.distance > distanceHeaders[j] )
						distanceHeader = distanceHeaders[ j + 1 ];
				
				} // for
				
			
			} // distanceHeader
		
			/*
		
			var aPersonActivities = jQuery( '<span></span>' );
			
			for ( var j = 0; j < aPerson.activities.length; j++ )
			{

				jQuery( '<b class="activity">' + aPerson.activities[j].name + '</b>' )
					.click(

						function()
						{
						
							showActivity( jQuery( this ).text() );
							return false;

						} // function
					
					)
					.appendTo( aPersonActivities );
			
			} // for
			
			*/
			
			aPerson.activities.sort( sortByMinutes );
	
			var aPersonVcard = jQuery( '<div class="vcard" id="person-' + i + '"><h1 class="fn">' + aPerson.name + '</h1><img class="photo" src="images/photo/' + aPerson.photo + '" /><img class="arrow" src="images/icon/arrow.png" /><div class="activity">' + aPerson.activities[0].name + '</div><div class="time">' + textOfMinutes( aPerson.activities[0].minutes ) + '</div></div>' )
				.click( 
				
					function()
					{
	
						idParts = this.id.split( '-' );
						showPerson( nearbyPeople[ idParts[1] ] );
					
					} // function
				
				)
				
			if ( aPerson.degree == 0 )
			{
			
				$( '<div class="icon"><img src="images/icon/friend.png" /></div>' )
					.appendTo( aPersonVcard );
			
			} // if
			else if ( aPerson.degree == 1 )
			{
			
				$( '<div class="icon"><img src="images/icon/one.png" /></div>' )
					.appendTo( aPersonVcard );
			
			} // else if
			else if ( aPerson.degree == 2 )
			{
			
				$( '<div class="icon"><img src="images/icon/everyone.png" /></div>' )
					.appendTo( aPersonVcard );
			
			} // else if
			else if ( aPerson.degree == 3 )
			{
			
				$( '<div class="icon"><img src="images/icon/ad.png" /></div>' )
					.appendTo( aPersonVcard );
			
			} // else if
				
			/*
				
			var aPersonActivityDiv = jQuery( '<div>Activities: </div>' );
			
			aPersonActivities.appendTo( aPersonActivityDiv );
			aPersonActivityDiv.appendTo( aPersonVcard );
						
			var aPersonDistance = distance( aPerson.lat , aPerson.lon , me.lat , me.lon , 'M' );
			
			var distanceText = textOfDistance( aPersonDistance );
			
			*/
			
			aPersonVcard.appendTo( '#people-page' );

		} // if
	
	} // for

} // showPeople

function showPerson( aPerson )
{

	page = 'person';
	jQuery( '#person-page' ).empty();
	resetPage();

	/*

	var aPersonActivities = jQuery( '<span></span>' );
			
	for ( var j = 0; j < aPerson.activities.length; j++ )
	{

		jQuery( '<b class="activity">' + aPerson.activities[j].name + '</b>' )
			.click(

				function()
				{
				
					showActivity( jQuery( this ).text() );
					return false;

				} // function
			
			)
			.appendTo( aPersonActivities );
	
	} // for
	
	*/

	var aPersonVcard = jQuery( '<div class="vcard"><h1>' + aPerson.name + '&#8217;s profile goes here</h1></div>' );
		
	/*
		
	var aPersonActivityDiv = jQuery( '<div>Activities: </div>' );
	
	aPersonActivities.appendTo( aPersonActivityDiv );
	aPersonActivityDiv.appendTo( aPersonVcard );

	var aPersonOptions = jQuery( '<div><a href="#">Message</a> <a href="#">Call</a> </div>' );
		
	jQuery( '<a href="#">Locate</a>' )
		.click( 
		
			function()
			{
			
				showLocations( aPerson.id );
			
			} // function
		
		)
		.appendTo( aPersonOptions );

	aPersonOptions.appendTo( aPersonVcard );

		
	if ( aPerson.degree == 0 )
		jQuery( '<div>Friend</div>' ).appendTo( aPersonVcard );
	if ( aPerson.degree == 1 )
		jQuery( '<div>Friend of a Friend</div>' ).appendTo( aPersonVcard );
	else
		jQuery( '<div>Someone new to meet</div>' ).appendTo( aPersonVcard );

	*/
	
	aPersonVcard.appendTo( '#person-page' );
	
} // showPerson

function showSettings()
{

	jQuery( '#settings-page' ).empty();
	page = 'settings';
	resetPage();

	jQuery( '<h1>Settings go here</h2>' ).appendTo( '#settings-page' );

} // showSettings

function showUpdates()
{

	jQuery( '#updates-page' ).empty();
	page = 'updates';
	resetPage();
	
	jQuery( '<h1>Updates go here</h2>' ).appendTo( '#updates-page' );

} // showUpdates

function createMarker( nearbyPerson )
{

	var markerIcon = new GIcon();
	markerIcon.shadow = 'http://www.google.com/mapfiles/shadow50.png';
	markerIcon.iconSize = new GSize(32, 32);
	markerIcon.shadowSize = new GSize(37, 34);
	markerIcon.iconAnchor = new GPoint(9, 34);
	markerIcon.infoWindowAnchor = new GPoint(9, 2);
	markerIcon.infoShadowAnchor = new GPoint(18, 25);
	
	if ( nearbyPerson.degree == 0 )
		markerIcon.image = 'http://handshakes.heroku.com/images/green.png';
	else if ( nearbyPerson.degree == 1 )
		markerIcon.image = 'http://handshakes.heroku.com/images/yellow.png';
	else if ( nearbyPerson.degree == 2 )
		markerIcon.image = 'http://handshakes.heroku.com/images/red.png';
		
	var marker = new GMarker( new GLatLng( nearbyPerson.lat , nearbyPerson.lon ) , { icon : markerIcon } );

	GEvent.addListener(
		marker , 'click' , function()
		{
			showPerson( nearbyPerson );
		}
	);

	return marker;

} // createMarker

function redrawPage()
{

	if ( page == 'people' )
		showPeople();
	else if ( page == 'activities' )
		showActivities();
	else if ( page == 'locations' )
		showLocations();

} // redrawPage

function resetPage()
{

	if ( page == 'people' )
	{
	
		jQuery( '.people-link img' ).attr( 'src' , 'images/bottom/active/people.png' );	
		jQuery( '#people-page' ).show();
	
	} // if
	else
	{
	
		if ( page != 'person' )
			jQuery( '.people-link img' ).attr( 'src' , 'images/bottom/people.png' );	
		jQuery( '#people-page' ).hide();		

	} // else

	if ( page == 'person' )
	{
	
		jQuery( '.people-link img' ).attr( 'src' , 'images/bottom/active/people.png' );
		jQuery( '#person-page' ).show();
	
	} // if
	else
	{
	
		if ( page != 'people' )
			jQuery( '.people-link img' ).attr( 'src' , 'images/bottom/people.png' );
		jQuery( '#person-page' ).hide();

	} // else

	if ( page == 'activities' )
	{
	
		jQuery( '.activity-link img' ).attr( 'src' , 'images/bottom/active/activities.png' );
		jQuery( '#activities-page' ).show();
	
	} // if
	else
	{
	
		if ( page != 'activity' )
			jQuery( '.activity-link img' ).attr( 'src' , 'images/bottom/activities.png' );	
		jQuery( '#activities-page' ).hide();
	
	} // else
	
	if ( page == 'activity' )
	{
	
		jQuery( '.activity-link img' ).attr( 'src' , 'images/bottom/active/activities.png' );	
		jQuery( '#activity-page' ).show();
	
	} // if
	else
	{
	
		if ( page != 'activities' )
			jQuery( '.activity-link img' ).attr( 'src' , 'images/bottom/activities.png' );	
		jQuery( '#activity-page' ).hide();
	
	} // else
	
	if ( page == 'locations' )
	{
	
		jQuery( '.locations-link img' ).attr( 'src' , 'images/bottom/active/locations.png' );		
		jQuery( '#locations-page' ).show();
	
	} // if
	else
	{
	
		jQuery( '.locations-link img' ).attr( 'src' , 'images/bottom/locations.png' );	
		jQuery( '#locations-page' ).hide();
	
	} // else
	
	if ( page == 'updates' )
	{
	
		jQuery( '.updates-link img' ).attr( 'src' , 'images/bottom/active/updates.png' );
		jQuery( '#updates-page' ).show();
	
	} // if
	else
	{

		jQuery( '.updates-link img' ).attr( 'src' , 'images/bottom/updates.png' );	
		jQuery( '#updates-page' ).hide();

	} // else

	if ( page == 'settings' )
	{
	
		jQuery( '.settings-link img' ).attr( 'src' , 'images/bottom/active/settings.png' );
		jQuery( '#settings-page' ).show();
	
	} // f
	else
	{
	
		jQuery( '.settings-link img' ).attr( 'src' , 'images/bottom/settings.png' );
		jQuery( '#settings-page' ).hide();

	} // else

} // resetPage

jQuery( document ).ready(

	function()
	{

		jQuery( '.people-link' ).click( showPeople );
		jQuery( '.activity-link' ).click( showActivities );
		jQuery( '.locations-link' ).click( showLocations );
		jQuery( '.updates-link' ).click( showUpdates );
		jQuery( '.settings-link' ).click( showSettings );

		jQuery( '.range-0-link' ).click(
		
			function()
			{

				jQuery( '.range-0-link img' ).attr( 'src' , 'images/range/selected/friend.png' );
				jQuery( '.range-1-link img' ).attr( 'src' , 'images/range/1.png' );
				jQuery( '.range-2-link img' ).attr( 'src' , 'images/range/all.png' );

				maxDegree = 0;
				redrawPage();
			
			} // function
		
		);
		
		jQuery( '.range-1-link' ).click(
		
			function()
			{
			
				jQuery( '.range-0-link img' ).attr( 'src' , 'images/range/friend.png' );
				jQuery( '.range-1-link img' ).attr( 'src' , 'images/range/selected/1.png' );
				jQuery( '.range-2-link img' ).attr( 'src' , 'images/range/all.png' );
			
				maxDegree = 1;
				redrawPage();
			
			} // function
		
		);
		
		jQuery( '.range-2-link' ).click(
		
			function()
			{
			
				jQuery( '.range-0-link img' ).attr( 'src' , 'images/range/friend.png' );
				jQuery( '.range-1-link img' ).attr( 'src' , 'images/range/1.png' );
				jQuery( '.range-2-link img' ).attr( 'src' , 'images/range/selected/all.png' );	

				maxDegree = 2;
				redrawPage();
			
			} // function
		
		);

		for ( var i = 0; i < nearbyPeople.length; i++ )
		{
		
			nearbyPeople[i].distance = distance( me.lat , me.lon , nearbyPeople[i].lat , nearbyPeople[i].lon , 'M' );

		} // for

		showPeople();

	} // function
	
);

var page = 'people';
var activities = new Array();
var maxDegree = 2;
var map = false;

var me = { lat: 39.764631 , lon: -104.972492 };

var nearbyPeople = [
	{
		id : 0 , lat: 39.762631 , lon: -104.972992 , name: 'Henry Kaufman' , 
		activities: [
			{ name : 'Biking' , minutes : 0 } , 
			{ name : 'Glassblowing' , minutes : 5 } , 
			{ name : 'Coding' , minutes : 10 } ,
			{ name : 'Reading' , minutes : 40 } 
		] , degree: 0 , photo : 'henry.png'
	} ,
	{
		id : 1 , lat: 39.758408 , lon: -104.935226 , name: 'Gillian Lewis' , 
		activities: [
			{ name : 'Going to the art museum' , minutes : 9 } , 
			{ name : 'Driving' , minutes : 40 } , 
			{ name : 'Going out' , minutes : 70 } ,
			{ name : 'Watching a movie' , minutes : 100 } 
		] , degree: 1 , photo : 'merilee.png'
	} ,
	{
		id : 2 , lat: 39.770548 , lon: -105.050583 , name: 'Matt Stillman' , 
		activities: [
			{ name : 'Shopping' , minutes : 4 } , 
			{ name : 'Dancing' , minutes : 40 } , 
			{ name : 'Figuring out life goals' , minutes : 70 } ,
			{ name : 'Eating' , minutes : 95 } ,
			{ name : 'Buying a car' , minutes : 110 } 
		] , degree: 2 , photo : 'henry.png'
	} ,
	{
		id : 3 , lat: 39.721448 , lon: -105.050583 , name: 'Lucy Klein' , 
		activities: [
			{ name : 'House hunting' , minutes : 2 } , 
			{ name : 'Running' , minutes : 50 } , 
			{ name : 'Painting' , minutes : 70 } ,
			{ name : 'Cleaning' , minutes : 85 } ,
			{ name : 'Hanging out' , minutes : 89 } 
		] , degree: 0 , photo : 'merilee.png'
	} ,
	{ 
		id : 4 , lat: 39.731448 , lon: -105.040583 , name: 'Willy Masters' , 
		activities: [
			{ name : 'Drinking beer' , minutes : 2 } , 
			{ name : 'Out for cocktails' , minutes : 50 } , 
			{ name : 'Clubbing' , minutes : 70 } ,
			{ name : "Scopin'" , minutes : 85 } ,
			{ name : 'Trolling' , minutes : 89 } 
		] , degree: 1 , photo : 'henry.png'
	}
];