var map = null;
var lookup = null;
var marker = null;
var longlat = new Array();
var fips_tmp = null;
var html = '';
var alerts = new Array();
var alert_index = 0;

function place_point(point) {
    if(point) {
        if(!marker) {
            marker = new GMarker(point);
            map.addOverlay(marker);
        } else {
            marker.closeInfoWindow();
            marker.setLatLng(point);
        }
        
        longlat[fips_tmp] = point;
        fips_tmp = null;
        
        marker.openInfoWindowHtml(html);
        
        window.setTimeout('new_alert();', 5000);
    } else {
        new_alert();
    }
}

function load() {
    if(GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById('map'));
        map.addControl(new GSmallMapControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(30, 10)));
        map.setCenter(new GLatLng(40.0000, -90.0000), 5);
        
        new_alert();
    }
}

function new_alert(result) {
    if(result) {
        result = unserialize(result);
        
        if(result) {
            if(result.length) {
                alerts = result;
                alert_index = 0;
                
                result = alerts[alert_index];
                alert_index++;
            }
            
            html = '<div style="width: 300px; padding: 10px;"><span style="font-size: larger; font-weight: bold;">'
                 + result['headline'] + '</span><br/><b><i>'
                 + result['event'] + '</i></b><br /><br />'
                 + String(result['description']).substr(0, 140)
                 + '...<br /><br /><a href="' + result['web']
                 + '" onclick="return !window.open(this.href);">more information...</a></div>';
            
            fips_tmp = result['fips'];
            
            if(result['county'] != '' && result['state'] != '') {
               if(typeof longlat[result['fips']] != 'undefined') {
                   place_point(longlat[result['fips']]);
               } else {
                   if(!lookup) {
                       lookup = new GClientGeocoder();
                   }
                   lookup.getLatLng(result['county'] + ', ' + result['state'], place_point);
               }
            } else {
                new_alert();
            }
        } else {
            new_alert();
        }
    } else if(alerts.length > 0 && alerts.length > alert_index) {
        result = alerts[alert_index];
        alert_index++;
        
        html = '<div style="width: 300px; padding: 10px;"><span style="font-size: larger; font-weight: bold;">'
                 + result['headline'] + '</span><br/><b><i>'
                 + result['event'] + '</i></b><br /><br />'
                 + String(result['description']).substr(0, 140)
                 + '...<br /><br /><a href="' + result['web']
                 + '" onclick="return !window.open(this.href);">more information...</a></div>';
            
            fips_tmp = result['fips'];
            
            if(result['county'] != '' && result['state'] != '') {
               if(typeof longlat[result['fips']] != 'undefined') {
                   place_point(longlat[result['fips']]);
               } else {
                   if(!lookup) {
                       lookup = new GClientGeocoder();
                   }
                   lookup.getLatLng(result['county'] + ', ' + result['state'], place_point);
               }
            } else {
                new_alert();
            }
    } else {
        http_process.send('./', 'action=update', 'new_alert');
    }
}
