// 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 serviceIcon;

var zoomHintCtrl;
var updatingCtrl;

var selectedSearchTab;
var selectedMapTab;
var sidebarResources;
var sidebarMarkers;
var boundedMarkers;
var reqId = 0;
var selectMarkerLatLng;
var selectedMarker;

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( "details", "review", "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_marker2.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";
	serviceIcon = new GIcon( icon );
	serviceIcon.image = "/images/service_marker2.png";

	zoomHintCtrl = new ZoomHintControl();
	updatingCtrl = new UpdatingControl();
	
	GDownloadUrl( "/servlet/SignInServlet", function( data, responseCode )
	{
		var xml = GXml.parse( data );
		var html;
		if( xml.documentElement.getAttribute( "error" ) != null )
			html = '<a href="javascript:showSignIn( \'\' )">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;
	} );

	GEvent.addListener( map, "moveend", function()
	{
		updateBoundedResources();
	} );

	GEvent.addListener( map, "maptypechanged", function()
	{
		zoomHintCtrl.hide();
		zoomHintCtrl.height = ( map.getCurrentMapType() == G_NORMAL_MAP ) ? 150 : 165;
		zoomHintCtrl.show();
	} );

	var init = document.getElementById( "init" );
	selectMarkerLatLng = new GLatLng( parseFloat( init.getAttribute( "lat" ) ), parseFloat( init.getAttribute( "lng" ) ) );
	map.setCenter( selectMarkerLatLng, parseInt( init.getAttribute( "zoom" ) ) );
	selectSearchTab( init.getAttribute( "searchtab" ) );
	selectMapTab( init.getAttribute( "maptab" ) );
}

function onResize() // hack for IE 6
{
	if( navigator.appVersion.indexOf( "MSIE 6" ) != -1 )
	{
		markerExt = "_marker2.gif";
		
		var element = document.getElementById( "searchBar" );
		element.style.width = ( document.documentElement.clientWidth - parseInt( element.style.left ) - parseInt( element.style.right ) ) + 'px';
		element.style.overflow = 'hidden';
		
		var element = document.getElementById( "searchBarAd" );
		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( "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 ) );
}

UpdatingControl.prototype.show = function()
{
	if( this.showing )
		return;
		
	map.addControl( this );
	this.showing = true;
}

UpdatingControl.prototype.hide = function()
{
	if( !this.showing )
		return;
		
	map.removeControl( this );
	this.showing = false;
}

function ZoomHintControl()
{
	this.width = 5;
	this.height = 150;
	this.showing = false;
}
ZoomHintControl.prototype = new GControl();

ZoomHintControl.prototype.initialize = function( map )
{
	var img = document.createElement( "img" );
	img.src = "/images/ZoomHint.gif";
	img.width = this.width;
	img.height = this.height;
	map.getContainer().appendChild( img );
	return img;
}

ZoomHintControl.prototype.getDefaultPosition = function() 
{
	return new GControlPosition( G_ANCHOR_TOP_LEFT, new GSize( 48, 92 ) );
}

ZoomHintControl.prototype.show = function()
{
	if( this.showing )
		return;
		
	map.addControl( this );
	this.showing = true;
}

ZoomHintControl.prototype.hide = function()
{
	if( !this.showing )
		return;
		
	map.removeControl( this );
	this.showing = false;
}

function performQuery( type )
{
	updatingCtrl.show();
	
	map.clearOverlays();

	boundedMarkers = null;
	selectedMarker = null;

	if( type.indexOf( "BoundedResources" ) == -1 )
		map.setCenter( new GLatLng( 37.0625, -95.677068 ), 4 );

	GDownloadUrl( "/servlet/QueryServlet?type=" + type, function( data, responseCode )
	{
		var xml = GXml.parse( data );
		var resources = xml.documentElement.getElementsByTagName( "resource" );
		
		sidebarResources = new Array();
		sidebarMarkers = new Array();

		for( var i = 0; i < resources.length; i++ )
		{
			sidebarResources.push( resources[ i ] );

			var point = new GLatLng( parseFloat( resources[ i ].getAttribute( "lat" ) ), parseFloat( resources[ i ].getAttribute( "lng" ) ) );

			sidebarMarkers.push( addMarker( resources[ i ], point ) );
			resources[ i ].setAttribute( "markerIndex", sidebarMarkers.length - 1 );

			if( selectMarkerLatLng != null && sidebarMarkers[ sidebarMarkers.length - 1 ].getPoint().equals( selectMarkerLatLng ) )
			{
				selectMarkerLatLng = null;
				GEvent.trigger( sidebarMarkers[ sidebarMarkers.length - 1 ], "click" );
			}
		}
		
		if( type.indexOf( "ResourcesByName" ) != -1 )
			listNameSearchResults( parseInt( type.substr( type.lastIndexOf( '=' ) + 1 ) ), xml.documentElement.getAttribute( "count" ) );
		else
			listResources();

		updatingCtrl.hide();
		
		updateBoundedResources();
	} );
}

function updateBoundedResources()
{
	if( map.getZoom() < 10 )
	{
		if( boundedMarkers != null )
		{
			for( var i = 0; i < boundedMarkers.length; i++ )
			{
				if( selectedMarker == boundedMarkers[ i ] )
					map.closeInfoWindow();
					
				map.removeOverlay( boundedMarkers[ i ] );
			}

			boundedMarkers = null;
		}

		updateCategories( new Array() );
	}
	else
	{
		updatingCtrl.show();

		var bounds = map.getBounds();
		southWest = bounds.getSouthWest();
		northEast = bounds.getNorthEast();

		reqId++;
		GDownloadUrl( "/servlet/QueryServlet?reqId=" + reqId + "&type=BoundedResources&minLat=" + southWest.lat() + "&minLng=" + southWest.lng() + "&maxLat=" + northEast.lat() + "&maxLng=" + northEast.lng(), function( data, responseCode )
		{
			var xml = GXml.parse( data );

			if( xml.documentElement.getAttribute( "reqId" ) != reqId )
				return;

			var resources = xml.documentElement.getElementsByTagName( "resource" );

			var foundSelectedMarker = false;
			if( boundedMarkers != null )
			{
				for( var i = 0; i < boundedMarkers.length; i++ )
				{
					if( selectedMarker == boundedMarkers[ i ] )
						foundSelectedMarker = true;
					else
						map.removeOverlay( boundedMarkers[ i ] );
				}
			}
			boundedMarkers = new Array();
			if( foundSelectedMarker )
				boundedMarkers.push( selectedMarker );

			var categories = new Array();

			for( var i = 0; i < resources.length; i++ )
			{
				var point = new GLatLng( parseFloat( resources[ i ].getAttribute( "lat" ) ), parseFloat( resources[ i ].getAttribute( "lng" ) ) );

				if( sidebarMarkers != null )
				{
					for( var j = 0; j < sidebarMarkers.length; j++ )
					{
						if( sidebarMarkers[ j ].getPoint().equals( point ) )
						{
							point = null;
							break;
						}
					}
					if( point == null )
						continue;
				}
				
				if( !foundSelectedMarker || !point.equals( selectedMarker.getPoint() ) )
					boundedMarkers.push( addMarker( resources[ i ], point ) );

				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;
			}
			
			updateCategories( categories );

			updatingCtrl.hide();
		} );
	}
}

function addMarker( resource, point )
{
	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( point, icon );
	map.addOverlay( marker );
	
	GEvent.addListener( marker, "click", function()
	{
		var id = resource.getAttribute( "id" );
		
		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>';
		attr = resource.getAttribute( "average_rating" );
		if( attr != null )
			html += '<img src="/images/stars' + attr + '.gif" width="75" height="12" alt="' + ratingDescs[ attr ] + '"> ';
		attr = resource.getAttribute( "reviews" );
		html += '(' + attr + ' review' + ( ( attr == "1" ) ? "" : "s" ) + ')<br>';
		html += '<a href="javascript:showDetails( ' + id + ' )">Show Details</a> | <a href="javascript:writeReview( ' + id + ' )">Write Review</a> | ';
		if( location_verified == "1" )
			html += '<span style="color: #ea7528">Location Verified</span>';
		else
			html += '<a href="javascript:openWin( \'/servlet/CorrectCampgroundServlet?id=' + id + '\', 650, 600 )">Wrong Location?</a>';
		html += '</div>';

		marker.openInfoWindowHtml( html );
		
		selectedMarker = marker;
	} );
		
	GEvent.addListener( marker, "infowindowclose", function()
	{
		selectedMarker = null;
	} );
	
	return marker;
}

function updateCategories( categories )
{
	if( sidebarResources != null )
	{
		var mapBounds = map.getBounds();

		for( var i = 0; i < sidebarResources.length; i++ )
		{
			if( !mapBounds.contains( sidebarMarkers[ sidebarResources[ i ].getAttribute( "markerIndex" ) ].getPoint() ) )
				continue;

			var category = sidebarResources[ 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;
}

function search()
{
	if( selectedMapTab != "Search" )
	{
		selectMapTab( "Search" );
		return;
	}
	
	if( selectedSearchTab == "BoundedResources" )
	{
		if( document.getElementById( "searchText" ).value == '' )
		{
			document.getElementById( "sidebar" ).innerHTML = document.getElementById( "sidebarHelp" ).innerHTML;
			document.getElementById( "searchText" ).focus();
			
			map.clearOverlays();
			sidebarResources = null;
			sidebarMarkers = null;
			boundedMarkers = null;
			selectedMarker = null;
			map.setCenter( new GLatLng( 37.0625, -95.677068 ), 4 );
			
			return;
		}
		
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng( document.getElementById( "searchText" ).value + ", USA", function( point )
		{
			if( point == null )
			{
				document.getElementById( "sidebar" ).innerHTML = '<p>We were unable to locate a match for <span style="font-weight: bold; color: black">' + document.getElementById( "searchText" ).value + '</span>. Please make sure you have done the following:</p><ul><li> Spelled the City correctly</li><li> Entered both a City and State. Google Maps will return an error if there are two cities with the same name and no state to differentiate them.</li></ul>';
				return;
			}

			map.setCenter( point, 11 );

			var bounds = map.getBounds();
			southWest = bounds.getSouthWest();
			northEast = bounds.getNorthEast();

			performQuery( "BoundedResources&minLat=" + southWest.lat() + "&minLng=" + southWest.lng() + "&maxLat=" + northEast.lat() + "&maxLng=" + northEast.lng(), true );
		} );
	}
	else if( selectedSearchTab == "ResourcesByName" )
		fetchNameSearchResults( 0 );
}

function fetchNameSearchResults( offset )
{
	performQuery( "ResourcesByName&name=" + encodeURIComponent( document.getElementById( "searchText" ).value ) + "&offset=" + offset );	
}

function listNameSearchResults( offset, count )
{
	if( sidebarResources.length == 0 )
	{
		document.getElementById( "sidebar" ).innerHTML = '<p>We were unable to locate a match for <span style="font-weight: bold; color: black">' + document.getElementById( "searchText" ).value + '</span>. Please make sure you have done the following:</p><ul><li> Spelled the Resource name correctly.</li><li> If the Resource name is long such as, <b>John James Audubon State Park</b> you may want to use fewer works. For example <b>John James</b> or <b>Audubon</b> in your search.</li></ul>';
		return;
	}
	
	var html = 'Displaying <b>' + ( offset + 1 ) + ' - ' + ( offset + sidebarResources.length ) + '</b> of <b>' + count + '</b> matches for <b>' + document.getElementById( "searchText" ).value + '</b>.<br>';
	html += '<br>';
	html += '<table>';
	for( var i = 0; i < sidebarResources.length; i++ )
		html += listResource( sidebarResources[ i ] );
	html += '</table>';
	if( offset > 0 || offset + 10 < count )
	{
		html += '<table width="90%">';
		html += '<tr>';
		if( offset > 0 )
			html += '<td><a href="javascript:fetchNameSearchResults( ' + ( offset - 10 ) + ' )">&lt; Previous</a></td>';
		if( offset + 10 < count )
			html += '<td align="right"><a href="javascript:fetchNameSearchResults( ' + ( offset + 10 ) + ' )">Next &gt;</a></td>';
		html += '</tr>';
		html += '</table>';
	}

	document.getElementById( "sidebar" ).innerHTML = html;
	document.getElementById( "sidebar" ).scrollTop = 0;
}

function selectMapTab( tab )
{
	selectedMapTab = tab;
	
	document.getElementById( "recentReviewsLink" ).style.color = ( selectedMapTab == "LastReviewedResources" ) ? "#ea7528" : "#939393";
	document.getElementById( "highestRatedLink" ).style.color = ( selectedMapTab == "TopRatedResources" ) ? "#ea7528" : "#939393";
	document.getElementById( "searchLink" ).style.color = ( selectedMapTab == "Search" ) ? "#ea7528" : "#939393";

	if( selectedMapTab == "Search" )
		search();
	else
		performQuery( selectedMapTab );
}

function listResources()
{
	var select = document.getElementById( "sortField" );
	var field;
	if( select != null )

	{
		field = select.options[ select.selectedIndex ].value;
		if( field != "" )
		{
			sidebarResources.sort( function( resource1, resource2 )
			{
				var value1 = resource1.getAttribute( field );
				var value2 = resource2.getAttribute( field );
				if( value1 < value2 )
					return ( field == "average_rating" ) ? 1 : -1;
				if( value1 > value2 )
					return ( field == "average_rating" ) ? -1 : 1;
				return 0;
			} );
		}
	}	

	var html = '<span style="font-weight: bold; color: black">Sort:</span> <select onchange="listResources()" 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><option value="average_rating"';
	if( field == "average_rating" )
		html += ' selected';
	html += '>Rating</option><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 < sidebarResources.length; i++ )
		html += listResource( sidebarResources[ i ] );
	html += '</table>';
	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>';
	var attr = resource.getAttribute( "average_rating" );
	if( attr == null )
		html += '<td>[not&nbsp;yet&nbsp;rated]</td>';
	else
		html += '<td><img src="/images/stars' + attr + '.gif" width="75" height="12" alt="' + ratingDescs[ attr ] + '"></td>';
	html += '</tr>';
	return html;
}

function goToMarker( markerIndex )
{
	GEvent.trigger( sidebarMarkers[ markerIndex ], "click" );
}

function selectSearchTab( tab )
{
	selectedSearchTab = tab;
	document.getElementById( "searchBox" ).src = ( tab == "BoundedResources" ) ? "/images/search_bar_city_and_state.gif" : "/images/search_bar_zip_code.gif";
	document.getElementById( "searchLocationLink" ).style.color = ( tab == "BoundedResources" ) ? 'black' : '';
	document.getElementById( "searchNameLink" ).style.color = ( tab == "ResourcesByName" ) ? 'black' : '';
}

function openWin( url, width, height )
{
	var win = window.open( url, 'win', 'width='+ width + ', height=' + height + ', scrollbars=1, resizable=1' );
	win.focus();
}

function showDetails( id )
{
	if( navigator.appVersion.indexOf( "MSIE 6" ) != -1 ) // hack for IE 6
		hideSelects();

	document.getElementById( "details_script" ).src = "/DetailsOverlay.js";

	GDownloadUrl( "/servlet/DetailsOverlayServlet?id=" + id, function( data, responseCode )
	{
		document.getElementById( "details_content" ).innerHTML = data;

		var element = document.getElementById( "details" );
		element.style.display = "";
		element.scrollTop = 0;

		urchinTracker("/details" );
	} );
}

function writeReview( id )
{
	if( navigator.appVersion.indexOf( "MSIE 6" ) != -1 ) // hack for IE 6
		hideSelects();

	document.getElementById( "review_script" ).src = "/ReviewOverlay.js";

	GDownloadUrl( "/servlet/ReviewOverlayServlet?id=" + id, function( data, responseCode )
	{
		document.getElementById( "review_content" ).innerHTML = data;

		var element = document.getElementById( "review" );
		element.style.display = "";
		element.scrollTop = 0;

		urchinTracker("/review" );
	} );
}

function showSignIn( task )
{
	if( navigator.appVersion.indexOf( "MSIE 6" ) != -1 ) // hack for IE 6
		hideSelects();

	document.getElementById( "sign_in_script" ).src = "/SignInOverlay.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("/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("/" + id + "/close" );
}

