/* code to handle the county map on the search page */

counties = [];
map_window = null;

function toggle_county( name ) {
    if( $( 'counties' ).disabled )
	toggleInput(); 
    
    if( ! in_array( selected_counties, name ) )
	selected_counties.unshift( name );
    else
	remove( selected_counties, name );
    
    fill_county_list()
}

function fill_county_list() {
    list = $( 'counties' );
    
    for( i = 0; i < list.options.length; i++ ) {
	elem = list.options[i];
	    
	if( in_array( selected_counties, elem.value ) )
	    elem.selected = true;
	else
	    elem.selected = false;
    }
}

function fill_county_map() {
    image = $( 'county_map' );
    
    image.src = "map_test.php?line=" + selected_counties.join( '%20' );
}

function update_counties() {
    load_county_map();
    load_county_list();
}

function county_selected() {
    counties = $( 'counties' );
    county = counties.options[ counties.selectedIndex ];

    if( ! county.selected )
	selected_counties = remove( selected_counties,
				    county.text );
    else
	selected_counties.unshift( county.text );

    fill_county_list();
    fill_county_map();
}

function open_county_map() {
    if( map_window && !map_window.closed )
	map_window.close();
    el( 'check_Counties' ).checked  = true;
    el( 'counties' ).disabled = false;

    map_window = window.open( "map_interface.php", "map",
			      "menubar=no,resizable=no,width=400,height=500" );

    setTimeout( "fill_map_from_list()", 1 );
}

function fill_map_from_list() {
    var counties = el( 'counties' );
    for( var i = 0; i < counties.options.length; i++ )
	if( counties.options.selected )
	    map_window.counties.push( counties.options[i].value );
}

function select_all( ctl ) {
    ctl = $( ctl );
    for( i = 0; i < ctl.options.length; i++ )
	ctl.options[i].selected = true;
}

function select_none( ctl ) {
    ctl = $( ctl );
    for( i = 0; i < ctl.options.length; i++ )
	ctl.options[i].selected = false;
}

    
function map_toggle_county( county_name ) {
    if( counties.indexOf( county_name ) != -1 )
	counties = counties.without( county_name )
    else
	counties.push( county_name );

    draw_map();
    window.opener.list_toggle_county( county_name );
}

function coords_test( e ) {
    alert( e.offsetX );
}

function map_select_none() {
    counties = [];
    draw_map();

    window.opener.list_select_none();
}

function map_select_all() {
    counties = 
	[
	 'albany', 'allegany', 'bronx', 'broome', 'cattaraugus', 'cayuga',
	 'chautauqua', 'chemung', 'chenango', 'clinton', 'columbia',
	 'cortland', 'delaware', 'dutchess', 'erie', 'essex', 'franklin',
	 'fulton', 'genesee', 'greene', 'hamilton', 'herkimer', 'jefferson',
	 'kings', 'lewis', 'livingston', 'madison', 'monroe', 'montgomery',
	 'nassau', 'newyork', 'niagara', 'oneida', 'onondaga', 'ontario',
	 'orange', 'orleans', 'oswego', 'otsego', 'putnam', 'queens',
	 'rensselaer', 'richmond', 'rockland', 'saratoga', 'schenectady',
	 'schoharie', 'schuyler', 'seneca', 'steuben', 'stlawrence', 'suffolk',
	 'sullivan', 'tioga', 'tompkins', 'ulster', 'warren', 'washington',
	 'wayne', 'westchester', 'westchester2', 'wyoming', 'yates'
	 ];
    draw_map();
    
    window.opener.list_select_all();
}

function draw_map( ) {
    el( 'map_img' ).src = "map_image.php?counties=" + counties.join('+');
}

window.list_select_all = function () {
    el( 'check_Counties' ).checked = false;
    el( 'counties' ).disabled      = false;
}

window.list_select_none = function () {
    toggleInput( el( 'check_Counties' ), 'counties' );
    el( 'check_Counties' ).checked = true;
    el( 'counties' ).disabled      = false;
}

window.list_toggle_county = function ( county ) {
    toggleCounty( county.toLowerCase() );
}

