
var addressReceiver,
    country_selector,
    init_map_counter = 0,
    init_selector_counter = 0;

function initMap()
{
    if (!document.getElementById('imagemap') || typeof(addressesFromPHP) == 'undefined')
    {   // wait for it...

        // break if it's taking too long (there's always the php fallback, so no functionality is lost)
        if (init_map_counter >= 100) return;

        // keep trying
        init_map_counter++;
        setTimeout(initMap, 10);
    }
    else
    {   // go!
        var map_areas = document.getElementById('imagemap').getElementsByTagName('area'),
            map_area;

        // loop over all areas in map #imagemap
        for (var m = 0; (map_area = map_areas[m]); m++)
        {
            // remove link function
            map_area.href = 'javascript:void(null);';

            // set javascript function
            map_area.onclick = showAddress;
        }

        // set address receiver
        addressReceiver = document.getElementById('address_container');
    }
} // end initMap()


function initSelector()
{
    if (!document.getElementById('country_list') || typeof(addressesFromPHP) == 'undefined')
    {   // wait for it...

        // break if it's taking too long (there's always the php fallback, so no functionality is lost)
        if (init_map_counter >= 100) return;

        // keep trying
        init_selector_counter++;
        setTimeout(initSelector, 10);
    }
    else
    {   // go!
        var country_list = document.getElementById('country_list'); // the form
        country_selector = document.getElementById('country_selector'); // the select in the form

        // add show function to and stop submit on form
        country_list.onsubmit = function () { showAddress(); return false; };

        // add onchange to select
        country_selector.onchange = showAddress;
    }
} // end initSelector()


function showAddress()
{   // handles both clicks on the map and the country list form!
    var idx = '',
        label;

    // reset addressReceiver
    addressReceiver.innerHTML = '';

    // determine what's been done
    if(this.nodeName == 'AREA')
    {   // map click
        idx = this.id.split('_')[1];

        // direct from address list
        addressReceiver.innerHTML = addressesFromPHP[idx];

        country_selector.selectedIndex = '';
    }
    else
    {   // form
        idx = country_selector.value;

        // get address(es) from link list
        for (label in linksFromPHP[idx])
        {
            //alert(label);

            if (label in labelsFromPHP)
            {
                addressReceiver.innerHTML += '<h2>'+labelsFromPHP[label]+'</h2>';
            }

            addressReceiver.innerHTML += addressesFromPHP[linksFromPHP[idx][label]];
        }
    }
} // end showAddress()


/* ************************************************************************* */
initMap();
initSelector();
/* ************************************************************************* */