//
// Functions related to the loc selection (not the hovers)
//




var loc_ajax = new ajax('text');

function sidebar_loc_load_locations()
{
	loc_load_locations('TRUE');
}

function loc_load_locations( $use_sidebar )
{
	if ( "TRUE" == $use_sidebar )
	{
		var $loc_pulldown = document.getElementById('sidebar_loc_state');
	}
	else
	{
		var $loc_pulldown = document.getElementById('loc_state');	
	}
	if( $loc_pulldown[$loc_pulldown.selectedIndex].value != 0 )
	{
		// Clean all the values from the city pulldown.
		if ( "TRUE" == $use_sidebar )
		{
			var $loc_id_pulldown = document.getElementById('sidebar_loc_id');
		}
		else
		{
			var $loc_id_pulldown = document.getElementById('loc_id');
		}

		// Clear all elements first.
		while( $loc_id_pulldown.length > 0 )
		{
			$loc_id_pulldown.remove($loc_id_pulldown.length - 1);
		}

		// Add a new element to say its loading.
		var $option_new = document.createElement('option');
		$option_new.text = 'Loading...';
		$option_new.value = 0;

		try
		{
			$loc_id_pulldown.add($option_new, null); // standards compliant; doesn't work in IE
		}
		catch(ex)
		{
			$loc_id_pulldown.add($option_new); // IE only
		}

		// Call the ajax request to load the new loc's.
		if ( "TRUE" == $use_sidebar )
		{
			loc_ajax.InitializeRequest('/ajax/location/list/', sidebar_loc_load_success, loc_load_fail);
		}
		else
		{
			loc_ajax.InitializeRequest('/ajax/location/list/', loc_load_success, loc_load_fail);	
		}
		

		var $state = $loc_pulldown[$loc_pulldown.selectedIndex].value;
		loc_ajax.Commit("state=" + $state);
	}
}

function sidebar_loc_load_success ( new_select_html )
{
	loc_load_success(new_select_html, 'TRUE')
}

function loc_load_success(new_select_html, use_sidebar)
{
	var $locs = new_select_html.split("\n");
	if ( "TRUE" == use_sidebar )
	{
		var $loc_pulldown = document.getElementById('sidebar_loc_id');
	}
	else
	{
		var $loc_pulldown = document.getElementById('loc_id');
	}

	// Clear all elements first.
	while( $loc_pulldown.length > 0 )
	{
		$loc_pulldown.remove($loc_pulldown.length - 1);
	}

	// Add the '--' text as the first item in the list.
	$locs.unshift('0|--');

	// Add in the new elements.
	for( $loc in $locs )
	{
		if( $locs[$loc].length > 0 )
		{
			var $option_new = document.createElement('option');

			var $loc_details = $locs[$loc].split('|');
			$option_new.text = $loc_details[1];
			$option_new.value = $loc_details[0];

			try
			{
				$loc_pulldown.add($option_new, null); // standards compliant; doesn't work in IE
			}
			catch(ex)
			{
				$loc_pulldown.add($option_new); // IE only
			}
		}
	}

	$loc_pulldown.disabled = false;
	$loc_pulldown.style.width = '';
	
	if ( document.getElementById('rloc_real_city_id') )
	{
		$loc_pulldown.value = document.getElementById('rloc_real_city_id').value;
	}
}

function loc_load_fail(xmldoc, message)
{
}

function submit_loc_browse_form()
{
	// Check the state first.
	var $loc_state = document.getElementById('loc_state');
	if( $loc_state[$loc_state.selectedIndex].value == 0 )
	{
		loc_display_error('Please select a city and state.');
		return false;
	}

	// Check the loc id.
	var $loc_id = document.getElementById('loc_id');
	if( $loc_id[$loc_id.selectedIndex].value == 0 )
	{
		loc_display_error('Please select a city.');
		return false
	}

	document.getElementById('form_loc_select').submit();
}

function loc_display_error( $message )
{

	// If the loc error div doesn't exist, do not do anything with the error boxes
	var $loc_error = document.getElementById('loc_error');
	if( $loc_error == null )
	{
		return true;
	}

	$loc_error.style.display = 'block';
	$loc_error.innerHTML = $message;
}



function change_location_hover( $min_top_offset, $min_left_offset )
{
	var element_id = document.getElementById('change_location_hover');
	element_id.style.display='block';
	element_id.style.position='absolute';
	
	var dimensions = document_dimensions();
    // Position it in the middle of the screen.
	element_id.style.top = (((dimensions.winheight - element_id.clientHeight) / 2) + dimensions.top) + 'px';
	element_id.style.left = (((dimensions.winwidth - element_id.clientWidth) / 2) + dimensions.left) +'px';

	// To prevent hover from overlapping the navigation tabs set a min height offset (in px)
	if ( $min_top_offset != '' )
	{
		if ( element_id.style.top < $min_top_offset )
		{
			element_id.style.top = $min_top_offset;
		}
	}
	if ( $min_left_offset != '' )
	{
		if ( element_id.style.left < $min_left_offset )
		{
			element_id.style.left = $min_left_offset;
		}
	}
	
	document.body.insertBefore(element_id, document.body.lastChild);
	
	// Load the user's current city/state if they are cookied.
	loc_load_locations();
}

function change_location_hover_close()
{
	document.getElementById('change_location_hover').style.display = 'none';
}