// Copyright © 2007 RVFamilyFun.com. All rights reserved.

var map;

var privateCampgroundIcon;
var publicCampgroundIcon;
var privateCampgroundVerifiedIcon;
var publicCampgroundVerifiedIcon;
var fuelIcon;
var campingWorldIcon;
var foodIcon;
var nascarIcon;
var noEntryIcon;
var shoppingIcon;
var userIcon;
var friendIcon;
var serviceIcon;

var updatingCtrl;

var queryType;
var mapResources;
var markers;
var mapFriends;
var friendDistance;
var favoriteReviews;
var selectFriend;

var ratings = new Array( "cleanliness", "layout", "amenities", "restrooms", "showers", "noise", "staff", "activities_children", "activities_adults", "attractions", "price", "would_return" );
var ratingDescs = new Array( "", "Terrible", "OK", "Average", "Above Average", "Excellent" );

var markerExt = "_marker2.png"; // hack for IE 6
var overlays = new Array( "my_review", "favorite_details", "add_friend", "tell_friend", "my_profile", "sign_in" ); // hack for IE 6

function onLoad()
{
	if( !GBrowserIsCompatible() )
	{
		alert( "This site relies heavily on Google Maps. Unfortunately Google Maps is not compatible with your browser.\nClick OK to see a list of compatible browsers." );
		window.location.assign( "http://local.google.com/support/bin/answer.py?answer=16532&topic=1499" );
	}

	onResize(); // hack for IE 6

	map = new GMap2( document.getElementById( "map" ) );
	map.addControl( new GLargeMapControl() );
	map.addControl( new GHierarchicalMapTypeControl() );
	map.addMapType( G_PHYSICAL_MAP ); 
	map.addControl( new GScaleControl() );
	map.addControl( new GOverviewMapControl() );
	map.enableDoubleClickZoom();
	map.enableContinuousZoom();
	map.enableScrollWheelZoom();

	var icon = new GIcon();
	icon.iconSize = new GSize( 24, 24 );
	icon.iconAnchor = new GPoint( 13, 22 );
	icon.infoWindowAnchor = new GPoint( 12, 1 );
	icon.transparent = "/images/transparent_marker.png";
	privateCampgroundIcon = new GIcon( icon );
	privateCampgroundIcon.image = "/images/private_campground_marker2.png";
	publicCampgroundIcon = new GIcon( icon );
	publicCampgroundIcon.image = "/images/public_campground_marker2.png";
	privateCampgroundVerifiedIcon = new GIcon( icon );
	privateCampgroundVerifiedIcon.image = "/images/private_campground_verified_marker2.png";
	publicCampgroundVerifiedIcon = new GIcon( icon );
	publicCampgroundVerifiedIcon.image = "/images/public_campground_verified_marker2.png";
	fuelIcon = new GIcon( icon );
	fuelIcon.image = "/images/fuel_marker2.png";
	campingWorldIcon = new GIcon( icon );
	campingWorldIcon.image = "/images/camping_world_marker2.png";
	foodIcon = new GIcon( icon );
	foodIcon.image = "/images/food_marker2.png";
	nascarIcon = new GIcon( icon );
	nascarIcon.image = "/images/nascar_marker2.png";
	noEntryIcon = new GIcon( icon );
	noEntryIcon.image = "/images/no_entry_marker2.png";
	shoppingIcon = new GIcon( icon );
	shoppingIcon.image = "/images/shopping_marker2.png";
	userIcon = new GIcon( icon );
	userIcon.image = "/myhomepage/images/user_marker2.png";
	friendIcon = new GIcon( icon );
	friendIcon.image = "/myhomepage/images/friend_marker2.png";
	serviceIcon = new GIcon( icon );
	serviceIcon.image = "/images/service_marker2.png";

	updatingCtrl = new UpdatingControl();

	var init = document.getElementById( "init" );
	map.setCenter( new GLatLng( parseFloat( init.getAttribute( "lat" ) ), parseFloat( init.getAttribute( "lng" ) ) ), parseInt( init.getAttribute( "zoom" ) ) );

	if( init.getAttribute( "approveFriend" ) != null )
		approveFriend();
	else if( init.getAttribute( "viewFriend" ) != null )
	{
		selectFriend = init.getAttribute( "viewFriend" );
		changeQueryType( "MyFriends" );
	}
	else
		changeQueryType( "MyReviewedResources" );

	GDownloadUrl( "/servlet/SignInServlet", function( data, responseCode )
	{
		var xml = GXml.parse( data );
		var html;
		if( xml.documentElement.getAttribute( "error" ) != null )
			html = '<a href="javascript:showSignIn( \'changeQueryType\' )">Sign In | Create Account</a>';
		else
			html = 'Hello ' + xml.documentElement.getAttribute( "first_name" ) + ' | <a href="/servlet/SignOutServlet">Sign Out</a>';
		document.getElementById( "signInOutLink" ).innerHTML = html;
	} );
}

function onResize() // hack for IE 6
{
	if( navigator.appVersion.indexOf( "MSIE 6" ) != -1 )
	{
		markerExt = "_marker2.gif";
		
		var element = document.getElementById( "sidebar" );
		element.style.bottom = '150px';
		element.style.height = ( document.documentElement.clientHeight - parseInt( element.style.top ) - parseInt( element.style.bottom ) ) + 'px';
		
		element = document.getElementById( "map" );
		element.style.bottom = '45px';
		element.style.width = ( document.documentElement.clientWidth - parseInt( element.style.left ) - parseInt( element.style.right ) ) + 'px';
		element.style.height = ( document.documentElement.clientHeight - parseInt( element.style.top ) - parseInt( element.style.bottom ) ) + 'px';

		element = document.getElementById( "legend" );
		element.style.width = ( document.documentElement.clientWidth - parseInt( element.style.left ) - parseInt( element.style.right ) ) + 'px';

		for( var i = 0; i < overlays.length; i++ )
		{
			element = document.getElementById( overlays[ i ] );
			element.style.position = "absolute";
			element.style.width = ( document.documentElement.clientWidth - parseInt( element.style.left ) - parseInt( element.style.right ) ) + 'px';
			element.style.height = ( document.documentElement.clientHeight - parseInt( element.style.top ) - parseInt( element.style.bottom ) ) + 'px';

			element = document.getElementById( overlays[ i ] + "_close" );
			element.style.position = "absolute";
			element.style.top = '8px';
			element.style.right = '24px';
		}
	}
}

function UpdatingControl()
{
	this.width = 126;
	this.height = 25;
	this.showing = false;
}
UpdatingControl.prototype = new GControl();

UpdatingControl.prototype.initialize = function( map )
{
	var img = document.createElement( "img" );
	img.src = "/images/Updating.gif";
	img.width = this.width;
	img.height = this.height;
	map.getContainer().appendChild( img );
	return img;
}

UpdatingControl.prototype.getDefaultPosition = function() 
{
	var size = map.getSize();

	return new GControlPosition( G_ANCHOR_TOP_LEFT, new GSize( ( size.width - this.width ) / 2, ( size.height - this.height ) / 2 ) );
}

function changeQueryType( type )
{
	queryType = type;
	map.setCenter( new GLatLng( 37.0625, -95.677068 ), 4 );

	updateMapTabs( true );
	map.clearOverlays();
	markers = null;
	selectedMarker = null;

	if( !updatingCtrl.showing )
	{
		map.addControl( updatingCtrl );
		updatingCtrl.showing = true;
	}

	GDownloadUrl( "/servlet/MyHomepageQueryServlet?type=" + queryType, function( data, responseCode )
	{
		var xml = GXml.parse( data );
		
		if( xml.documentElement.getAttribute( "error" ) != null )
		{
			showSignIn( "changeQueryType" );
			
			map.removeControl( updatingCtrl );
			updatingCtrl.showing = false;
			
			return;
		}

		if( queryType == "MyReviewedResources" || queryType == "MyFavorites" )
		{
			if( queryType == "MyFavorites" )
				favoriteReviews = xml.documentElement.getAttribute( "favorite_reviews" );
			
			var resources = xml.documentElement.getElementsByTagName( "resource" );

			mapResources = new Array();
			markers = new Array();
			var categories = new Array();

			for( var i = 0; i < resources.length; i++ )
			{
				mapResources.push( resources[ i ] );

				markers.push( addResourceMarker( resources[ i ] ) );
				resources[ i ].setAttribute( "markerIndex", markers.length - 1 );

				var category = resources[ i ].getAttribute( "category" );
				for( var j = 0; j < categories.length; j++ )
				{
					if( categories[ j ] == category )
					{
						category = null;
						break;
					}
				}
				if( category != null )
					categories[ categories.length ] = category;
			}

			var html = "";
			for( var i = 0; i < categories.length; i++ )
			{
				html += '<img src="/images/' + categories[ i ] + markerExt + '"> ';
				
				if( categories[ i ] == "private_campground" )
					html += "Campground";
				else if( categories[ i ] == "public_campground" )
					html += "Public Campground";
				else if( categories[ i ] == "fuel" )
					html += "Fuel";
				else if( categories[ i ] == "camping_world" )
					html += "Service";
				else if( categories[ i ] == "food" )
					html += "Food";
				else if( categories[ i ] == "nascar" )
					html += "Nascar";
				else if( categories[ i ] == "no_entry" )
					html += "Low Bridge";
				else if( categories[ i ] == "shopping" )
					html += "Shopping";
				else if( categories[ i ] == "service" )
					html += "Service";

				html += ' ';
			}
			document.getElementById( "legend" ).innerHTML = html;

			listMapResources();
		}
		else if( queryType == "MyLocation" )
		{
			var user = xml.documentElement.getElementsByTagName( "user" )[ 0 ];
			
			var latLng;
			if( user.getAttribute( "lat" ) == null )
				latLng = map.getCenter();
			else
				latLng = new GLatLng( parseFloat( user.getAttribute( "lat" ) ), parseFloat( user.getAttribute( "lng" ) ) );
			
			markers = new Array( new GMarker( latLng, { draggable: true, icon: userIcon } ) );
			GEvent.addListener( markers[ 0 ], "dragend", function()
			{
				var point = markers[ 0 ].getPoint();
				
				var params = "lat=" + point.lat();
				params += "&lng=" + point.lng();

				var request = GXmlHttp.create();
				request.open( "POST", "/servlet/MyHomepageUpdateLocationServlet", true );
				request.setRequestHeader( "Content-type", "application/x-www-form-urlencoded; charset=utf-8" );
				request.onreadystatechange = function()
				{
					if( request.readyState == 4 )
					{
						var xml = request.responseXML;
						if( xml.documentElement.getAttribute( "error" ) != null )
						{
							showSignIn( "updateLocation" );
							return;
						}
					}
				}
				request.send( params );
			} );
			map.addOverlay( markers[ 0 ] );

			document.getElementById( "sidebar" ).innerHTML = document.getElementById( "my_location_sidebar" ).innerHTML;
			
			var message = user.getAttribute( "message" );
			if( message != null )
				document.getElementById( "my_location_sidebar_message" ).value = message;
			
			document.getElementById( "legend" ).innerHTML = '<img src="/myhomepage/images/user' + markerExt + '"> My Location';
		}
		else if( queryType == "MyFriends" )
		{
			var legend = "";
			
			if( xml.documentElement.getAttribute( "lat" ) != null )
			{
				map.addOverlay( new GMarker( new GLatLng( parseFloat( xml.documentElement.getAttribute( "lat" ) ), parseFloat( xml.documentElement.getAttribute( "lng" ) ) ), userIcon ) );
				legend = '<img src="/myhomepage/images/user' + markerExt + '"> My Location ';
			}
			
			friendDistance = xml.documentElement.getAttribute( "friend_distance" );
			
			friends = xml.documentElement.getElementsByTagName( "friend" );
			
			mapFriends = new Array();
			markers = new Array();
			
			for( var i = 0; i < friends.length; i++ )
			{
				mapFriends.push( friends[ i ] );

				if( friends[ i ].getAttribute( "lat" ) != null )
				{
					markers.push( addFriendMarker( friends[ i ] ) );
					
					friends[ i ].setAttribute( "markerIndex", markers.length - 1 );
					
					if( selectFriend != null && selectFriend == friends[ i ].getAttribute( "friend_id" ) )
					{
						selectFriend = null;
						goToMarker( markers.length - 1 );
					}
				}
			}

			document.getElementById( "legend" ).innerHTML = legend + '<img src="/myhomepage/images/friend' + markerExt + '"> My Friends';

			listMapFriends();
		}

		if( updatingCtrl.showing )
		{
			map.removeControl( updatingCtrl );
			updatingCtrl.showing = false;
		}
	  } );
}

function addResourceMarker( resource )
{
	var category = resource.getAttribute( "category" );
	var location_verified = resource.getAttribute( "location_verified" );

	var icon;
	if( category == "private_campground" )
		icon = ( location_verified == "1" ) ? privateCampgroundVerifiedIcon : privateCampgroundIcon;
	else if( category == "public_campground" )
		icon = ( location_verified == "1" ) ? publicCampgroundVerifiedIcon : publicCampgroundIcon;
	else if( category == "fuel" )
		icon = fuelIcon;
	else if( category == "camping_world" )
		icon = campingWorldIcon;
	else if( category == "food" )
		icon = foodIcon;
	else if( category == "nascar" )
		icon = nascarIcon;
	else if( category == "no_entry" )
		icon = noEntryIcon;
	else if( category == "shopping" )
		icon = shoppingIcon;
	else if( category == "service" )
		icon = serviceIcon;

	var marker = new GMarker( new GLatLng( parseFloat( resource.getAttribute( "lat" ) ), parseFloat( resource.getAttribute( "lng" ) ) ), icon );
	map.addOverlay( marker );

	GEvent.addListener( marker, "click", function()
	{
		var html = '<div style="white-space: nowrap; padding-right: 16px">';
		html += '<span style="font-weight: bold; color: black">' + resource.getAttribute( "name" ) + '</span><br>';
		attr = resource.getAttribute( "city" );
		if( attr != null && attr.length > 0 )
			html +=  attr + ', ';
		attr = resource.getAttribute( "state" );
		if( attr != null && attr.length > 0  )
			html +=  attr;
		html += '<br>';
		if( queryType == "MyReviewedResources" )
		{
			attr = resource.getAttribute( "rating" );
			html += '<img src="/images/stars' + attr + '.gif" width="75" height="12" alt="' + ratingDescs[ attr ] + '"><br>';
			html += '<a href="javascript:showMyReview( ' + resource.getAttribute( "id" ) + ' )">Show My Review</a>';
		}
		else
			html += '<a href="javascript:showFavoriteDetails( ' + resource.getAttribute( "resource_id" ) + ' )">Show Details</a>';
		html += '</div>';

		marker.openInfoWindowHtml( html );
	} );
			
	return marker;
}

function addFriendMarker( friend )
{
	var marker = new GMarker( new GLatLng( parseFloat( friend.getAttribute( "lat" ) ), parseFloat( friend.getAttribute( "lng" ) ) ), friendIcon );
	map.addOverlay( marker );

	GEvent.addListener( marker, "click", function()
	{
		var html = '<div style="padding-right: 16px">';
		html += '<table>';
		html += '<tr>';
		if( friend.getAttribute( "photo" ) != null )
			html += '<td valign="top"><img src="/myhomepage/photos/' + friend.getAttribute( "friend_id" ) + '.jpg" border="1"></td>';
		html += '<td valign="top">';
		html += '<span style="font-weight: bold; color: black">' + friend.getAttribute( "first_name" ) + ' ' + friend.getAttribute( "last_name" ) + '</span><br>';
		var email = friend.getAttribute( "email" );
		html += '<a href="mailto:' +  email + '">' + email + '</a><br>';
		var message = friend.getAttribute( "message" )
		if( message != null )
			html += message;
		html += '</td>';
		html += '</tr>';
		html += '</table>';
		html += '</div>';

		marker.openInfoWindowHtml( html );
	} );
			
	return marker;
}

function updateMapTabs( highlight )
{
	document.getElementById( "myReviewsLink" ).style.color = ( queryType == "MyReviewedResources" && highlight ) ? "#ea7528" : "#939393";
	document.getElementById( "myLocationLink" ).style.color = ( queryType == "MyLocation" && highlight ) ? "#ea7528" : "#939393";
	document.getElementById( "myFriendsLink" ).style.color = ( queryType == "MyFriends" && highlight ) ? "#ea7528" : "#939393";
	document.getElementById( "myFavoritesLink" ).style.color = ( queryType == "MyFavorites" && highlight ) ? "#ea7528" : "#939393";
}

function listMapResources()
{
	var select = document.getElementById( "sortField" );
	var field;
	if( select != null )
	{
		field = select.options[ select.selectedIndex ].value;
		if( field != "" )
		{
			mapResources.sort( function( resource1, resource2 )
			{
				var value1 = resource1.getAttribute( field );
				var value2 = resource2.getAttribute( field );
				if( value1 < value2 )
					return ( field == "rating" ) ? 1 : -1;
				if( value1 > value2 )
					return ( field == "rating" ) ? -1 : 1;
				return 0;
			} );
		}
	}	

	var html = '<span style="font-weight: bold; color: black">Sort:</span> <select onchange="listMapResources()" id="sortField"';
	//if( navigator.appVersion.indexOf( "MSIE 6" ) != -1 && document.getElementById( "details" ).style.display != "none" ) // hack for IE 6
	//	html += ' style="display: none"';
	html += '><option value="">None</option><option value="category"';
	if( field == "category" )
		html += ' selected';
	html += '>Resource Type</option>';
	if( queryType == "MyReviewedResources" )
	{
		html += '<option value="rating"';
		if( field == "rating" )
			html += ' selected';
		html += '>Rating</option>';
	}
	html += '<option value="name"';
	if( field == "name" )
		html += ' selected';
	html += '>Name</option></select>';
	html += '<div style="position: absolute; top: 32px; bottom: 0px; left: 0px; right: 0px; overflow: auto">';
	html += '<table>';
	for( var i = 0; i < mapResources.length; i++ )
		html += listResource( mapResources[ i ] );
	html += '</table>';
	if( queryType == "MyFavorites" )
	{
		html += '<input type="checkbox" id="favoriteReviews" onclick="setFavoriteReviews()"';
		if( favoriteReviews == "1" )
			html += ' checked';
		html += '>';
		html += 'Notify me if a new review is submitted for any of my favorites.';
	}
	html += '</div>';

	document.getElementById( "sidebar" ).innerHTML = html;
	//document.getElementById( "sidebar" ).scrollTop = 0;
}

function listResource( resource )
{
	var html = '<tr>';
	html += '<td valign="top"><img src="/images/' + resource.getAttribute( "category" ) + ( ( resource.getAttribute( "location_verified" ) == "1" ) ? "_verified" : "" ) + markerExt + '"></td>';
	html += '<td><a href="javascript:goToMarker( ' + resource.getAttribute( "markerIndex" ) + ' )">' + resource.getAttribute( "name" ) + '</a></td>';
	if( queryType == "MyReviewedResources" )
	{
		var attr = resource.getAttribute( "rating" );
		html += '<td><img src="/images/stars' + attr + '.gif" width="75" height="12" alt="' + ratingDescs[ attr ] + '"></td>';
	}
	else
		html += '<td><a href="javascript:deleteFavorite( ' + resource.getAttribute( "id" ) + ' )"><img src="/myhomepage/images/delete.gif" width="16" height="16" border="0" alt="Delete This Favorite"></a></td>';
	html += '</tr>';
	return html;
}

function listMapFriends()
{
	var select = document.getElementById( "sortField" );
	var field;
	if( select != null )
	{
		field = select.options[ select.selectedIndex ].value;
		if( field != "" )
		{
			mapFriends.sort( function( friend1, friend2 )
			{
				var value1 = friend1.getAttribute( field );
				var value2 = friend2.getAttribute( field );
				if( value1 < value2 )
					return -1;
				if( value1 > value2 )
					return 1;
				return 0;
			} );
		}
	}	

	var html = '<span style="font-weight: bold; color: black">Sort:</span> <select onchange="listMapFriends()" id="sortField"';
	//if( navigator.appVersion.indexOf( "MSIE 6" ) != -1 && document.getElementById( "details" ).style.display != "none" ) // hack for IE 6
	//	html += ' style="display: none"';
	html += '><option value="">None</option><option value="first_name"';
	if( field == "first_name" )
		html += ' selected';
	html += '>First Name</option><option value="last_name"';
	if( field == "last_name" )
		html += ' selected';
	html += '>Last Name</option></select>';
	html += ' <input type= "button" value="Add Friend" onclick="showAddFriend()"></td>';
	html += '<div style="position: absolute; top: 32px; bottom: 0px; left: 0px; right: 0px; overflow: auto">';
	html += '<table>';
	for( var i = 0; i < mapFriends.length; i++ )
		html += listFriend( mapFriends[ i ] );
	html += '</table>';
	html += '<input type="checkbox" id="friendDistanceCheckbox" onclick="setFriendDistance()"';
	if( friendDistance != null )
		html += ' checked';
	html += '>';
	html += 'Notify me if a friend is within ';
	html += '<select id="friendDistance" onchange="setFriendDistance()">';
	var distances = new Array( "5", "10", "25", "50", "100" );
	for( var i = 0; i < distances.length; i++ )
	{
		html += '<option value="';
		html += distances[ i ];
		html += '"';
		if( friendDistance == distances[ i ] )
			html += ' selected';
		html += '>';
		html += distances[ i ];
		html += '</option>';
	}
	html += '</select>';
	html += ' miles of my location.';
	html += '</div>';

	document.getElementById( "sidebar" ).innerHTML = html;
	//document.getElementById( "sidebar" ).scrollTop = 0;
}

function listFriend( friend )
{
	var html = '<tr>';
	html += '<td valign="top"><img src="/myhomepage/images/friend' + markerExt + '"></td>';
	html += '<td><a href="javascript:';
	var markerIndex = friend.getAttribute( "markerIndex" );
	if( markerIndex == null )
		html += 'alert( \'' + friend.getAttribute( "first_name" ) + ' ' + friend.getAttribute( "last_name" ) + ' has not specified a location yet.\' )';
	else
		html += 'goToMarker( ' + markerIndex + ' )';
	html += '">' + friend.getAttribute( "first_name" ) + ' ' + friend.getAttribute( "last_name" ) + '</a></td>';
	html += '<td><a href="javascript:deleteFriend( ' + friend.getAttribute( "id" ) + ', \'' + friend.getAttribute( "first_name" ) + '\', \'' + friend.getAttribute( "last_name" ) + '\' )"><img src="/myhomepage/images/delete.gif" width="16" height="16" border="0" alt="Delete This Friend"></a></td>';
	html += '</tr>';
	return html;
}

function goToMarker( markerIndex )
{
	GEvent.trigger( markers[ markerIndex ], "click" );
}

function openWin( url, width, height )
{
	var win = window.open( url, 'win', 'width='+ width + ', height=' + height + ', scrollbars=1, resizable=1' );
	win.focus();
}

function showMyReview( id )
{
	if( navigator.appVersion.indexOf( "MSIE 6" ) != -1 ) // hack for IE 6
		hideSelects();

	document.getElementById( "my_review_script" ).src = "/myhomepage/MyReviewOverlay.js";

	GDownloadUrl( "/servlet/MyHomepageMyReviewOverlayServlet?id=" + id, function( data, responseCode )
	{
		document.getElementById( "my_review_content" ).innerHTML = data;

		var element = document.getElementById( "my_review" );
		element.style.display = "";
		element.scrollTop = 0;

		urchinTracker("/myhomepage/my_review" );
	} );
}

function formatPhone( phone )
{
	return '(' + phone.substr( 0, 3 ) + ') ' + phone.substr( 3, 3 ) + '-' + phone.substr( 6, 4 );
}

function updateMyLatLng()
{
	var point = markers[ 0 ].getPoint();
	var _lat = document.getElementById( "my_location_sidebar_lat" ).value;
	var _lng = document.getElementById( "my_location_sidebar_lng" ).value;
	point = new GLatLng( ( _lat == "" ) ? point.lat() : _lat, ( _lng == "" ) ? point.lng() : _lng );

	map.panTo( point );
	markers[ 0 ].setPoint( point );

	GEvent.trigger( markers[ 0 ], "dragend" );
}

function updateMyMessage()
{
	var params = "message=" + encodeURIComponent( document.getElementById( "my_location_sidebar_message" ).value );

	var request = GXmlHttp.create();
	request.open( "POST", "/servlet/MyHomepageUpdateMessageServlet", true );
	request.setRequestHeader( "Content-type", "application/x-www-form-urlencoded; charset=utf-8" );
	request.onreadystatechange = function()
	{
		if( request.readyState == 4 )
		{
			var xml = request.responseXML;
			if( xml.documentElement.getAttribute( "error" ) != null )
			{
				showSignIn( "updateMyMessage" );
				return;
			}

			if( xml.documentElement.firstChild == null )
				element.innerHTML = "";
			else
				document.getElementById( "my_location_sidebar_message" ).value = xml.documentElement.firstChild.data;
		}
	}
	request.send( params );
}

function showAddFriend()
{
	if( navigator.appVersion.indexOf( "MSIE 6" ) != -1 ) // hack for IE 6
		hideSelects();

	document.getElementById( "add_friend_script" ).src = "/myhomepage/AddFriendOverlay.js";

	GDownloadUrl( "/myhomepage/AddFriendOverlay.html", function( data, responseCode )
	{
		document.getElementById( "add_friend_content" ).innerHTML = data;

		var element = document.getElementById( "add_friend" );
		element.style.display = "";
		element.scrollTop = 0;

		urchinTracker("/myhomepage/add_friend" );
	} );
}

function approveFriend()
{
	var params = "id=" + document.getElementById( "init" ).getAttribute( "approveFriend" );
	
	var request = GXmlHttp.create();
	request.open( "POST", "/servlet/MyHomepageApproveFriendServlet", true );
	request.setRequestHeader( "Content-type", "application/x-www-form-urlencoded; charset=utf-8" );
	request.onreadystatechange = function()
	{
		if( request.readyState == 4 )
		{
			var xml = request.responseXML;
			
			var error = xml.documentElement.getAttribute( "error" );
			if( error == "sign_in" )
			{
				showSignIn( "approveFriend" );
				return;
			}
			else if( error == "no_record" )
				alert( "No record of this request could be found." );
			else if( error == "wrong_account" )
				alert( "This request is for a different member. Did you sign in with the wrong email?" );
			else
				alert( "Request approved. You will be added to " + xml.documentElement.getAttribute( "first_name" ) + " " + xml.documentElement.getAttribute( "last_name" ) + "'s friends list." );			
			
			changeQueryType( "MyFriends" );
		}
	}
	request.send( params );
}

function deleteFriend( id, first_name, last_name )
{
	if( !confirm( "Are you sure you want to delete " + first_name + " " + last_name + " as a friend?" ) )
		return;
	
	var params = "id=" + id;
	
	var request = GXmlHttp.create();
	request.open( "POST", "/servlet/MyHomepageDeleteFriendServlet", true );
	request.setRequestHeader( "Content-type", "application/x-www-form-urlencoded; charset=utf-8" );
	request.onreadystatechange = function()
	{
		if( request.readyState == 4 )
		{
			var xml = request.responseXML;
			if( xml.documentElement.getAttribute( "error" ) != null )
			{
				showSignIn( "" );
				return;
			}
			
			changeQueryType( "MyFriends" );
		}
	}
	request.send( params );
}

function setFriendDistance()
{
	var params = "";
	if( document.getElementById( "friendDistanceCheckbox" ).checked )
		params += "friend_distance=" + document.getElementById( "friendDistance" ).value;
	
	var request = GXmlHttp.create();
	request.open( "POST", "/servlet/MyHomepageSetFriendDistanceServlet", true );
	request.setRequestHeader( "Content-type", "application/x-www-form-urlencoded; charset=utf-8" );
	request.onreadystatechange = function()
	{
		if( request.readyState == 4 )
		{
			var xml = request.responseXML;

			var error = xml.documentElement.getAttribute( "error" );
			if( error == "sign_in" )
			{
				showSignIn( "setFriendDistance" );
				return;
			}
			else if( error == "no_location" )
			{
				alert( "You must specify your location first on the My Location tab." );
				document.getElementById( "friendDistanceCheckbox" ).checked = false;
				return;
			}
		}
	}
	request.send( params );
}

function deleteFavorite( id )
{
	var params = "id=" + id;
	
	var request = GXmlHttp.create();
	request.open( "POST", "/servlet/MyHomepageDeleteFavoriteServlet", true );
	request.setRequestHeader( "Content-type", "application/x-www-form-urlencoded; charset=utf-8" );
	request.onreadystatechange = function()
	{
		if( request.readyState == 4 )
		{
			var xml = request.responseXML;
			if( xml.documentElement.getAttribute( "error" ) != null )
			{
				showSignIn( "" );
				return;
			}
			
			changeQueryType( "MyFavorites" );
		}
	}
	request.send( params );
}

function setFavoriteReviews()
{
	var params = "favorite_reviews=" + ( document.getElementById( "favoriteReviews" ).checked ? "1" : "0" );
	
	var request = GXmlHttp.create();
	request.open( "POST", "/servlet/MyHomepageSetFavoriteReviewsServlet", true );
	request.setRequestHeader( "Content-type", "application/x-www-form-urlencoded; charset=utf-8" );
	request.onreadystatechange = function()
	{
		if( request.readyState == 4 )
		{
			var xml = request.responseXML;

			var error = xml.documentElement.getAttribute( "error" );
			if( error == "sign_in" )
			{
				showSignIn( "setFavoriteReviews" );
				return;
			}
		}
	}
	request.send( params );
}

function showFavoriteDetails( id )
{
	if( navigator.appVersion.indexOf( "MSIE 6" ) != -1 ) // hack for IE 6
		hideSelects();

	document.getElementById( "favorite_details_script" ).src = "/myhomepage/FavoriteDetailsOverlay.js";

	GDownloadUrl( "/servlet/MyHomepageFavoriteDetailsOverlayServlet?id=" + id, function( data, responseCode )
	{
		document.getElementById( "favorite_details_content" ).innerHTML = data;

		var element = document.getElementById( "favorite_details" );
		element.style.display = "";
		element.scrollTop = 0;

		urchinTracker("/myhomepage/favorite_details" );
	} );
}

function showMyProfile()
{
	if( navigator.appVersion.indexOf( "MSIE 6" ) != -1 ) // hack for IE 6
		hideSelects();

	document.getElementById( "my_profile_script" ).src = "/myhomepage/MyProfileOverlay.js";
	document.getElementById( "validate_account_script" ).src = "/myhomepage/ValidateAccount.js";

	GDownloadUrl( "/servlet/MyHomepageMyProfileOverlayServlet", function( data, responseCode )
	{
		if( responseCode == 401 )
		{
			showSignIn( "changeQueryType" );
			return;
		}

		document.getElementById( "my_profile_content" ).innerHTML = data;

		var element = document.getElementById( "my_profile" );
		element.style.display = "";
		element.scrollTop = 0;

		urchinTracker("/myhomepage/my_profile" );
	} );
}

function showSignIn( task )
{
	if( navigator.appVersion.indexOf( "MSIE 6" ) != -1 ) // hack for IE 6
		hideSelects();

	document.getElementById( "sign_in_script" ).src = "/myhomepage/SignInOverlay.js";
	document.getElementById( "validate_account_script" ).src = "/myhomepage/ValidateAccount.js";

	GDownloadUrl( "/servlet/SignInOverlayServlet?task=" + task, function( data, responseCode )
	{
		document.getElementById( "sign_in_content" ).innerHTML = data;

		var element = document.getElementById( "sign_in" );
		element.style.display = "";
		element.scrollTop = 0;

		urchinTracker("/myhomepage/sign_in" );
	} );
}

function hideSelects() // hack for IE 6
{
	for( var i = 0; i < overlays.length; i++ )
	{
		if( document.getElementById( overlays[ i ] ).style.display == "" )
			return;
	}

	var element = document.getElementById( "sortField" );
	if( element != null )
		element.style.display = "none";
}

function hideOverlay( id )
{
	document.getElementById( id ).style.display = "none";

	if( navigator.appVersion.indexOf( "MSIE 6" ) != -1 ) // hack for IE 6
	{
		for( var i = 0; i < overlays.length; i++ )
		{
			if( document.getElementById( overlays[ i ] ).style.display == "" )
				return;
		}
		
		var element = document.getElementById( "sortField" );
		if( element != null )
			element.style.display = "";
	}

	urchinTracker("/myhomepage/" + id + "/close" );
}
