  $(document).ready(function() {
    $(function() {
      
      sizeMap();
      $(window).resize(sizeMap);
             
      $('#sendips').click(function() {
        try {
          ips = $('#ipboxin').val();
          ips = ips.split('\n');
          var qs = '';
          for (i=0; i<ips.length; i++) {
            qs += 'ip[]=' + ips[i] + '&';
          }
          var url = 'http://batchiplocator.webatu.com/ips.php?' + qs;
          
          $.get(url,function(response) {
            $('#ipboxout').val(response);
            
            //if (map) {
              newCoords = response.split('\n');
              //mapPoints(coords);
              setTimeout('mapPoints()', 500);
            //}
                      
          },'text');
          
        }
        catch(e) {
          alert('error: ' + e.description);
        }
        
      });
      
      $('#ipboxin').val($('#get-ip').val());
      $('#sendips').click();      
                  
      
      window.onbeforeunload = function() {
        $.get('http://batchiplocator.webatu.com/byebye.php');
        if (map) {GUnload();}
      };
      
    });
  });
var map;
var newCoords;
var gmarkers = [];

    function initialize() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById('map_canvas'));
        map.setMapType(G_PHYSICAL_MAP);
        map.setUIToDefault();
      }
    }

  function mapPoints(coords) {
    if (map) {
      if (!coords) { coords = newCoords;}
      map.clearOverlays();
      for (var i=1; i < coords.length; i++) {
        var temp = coords[i].split('\t');
        var lat = parseFloat(temp[3]);
        var lng = parseFloat(temp[4]);          
        if (!isNaN(lat)) {
          var point = new GLatLng(lat, lng);
          var markhere = makeMarker(point, coords[i]);
          gmarkers[i] = markhere;
          map.addOverlay(markhere);
        }
      }
      
      //map.setCenter(markhere.getLatLng(), 10);
      repositionMap();
    } 
    
  }
  
  function makeMarker(point, coord) {
    var marker = new GMarker(point);
    coord = coord.split('\t');
    GEvent.addListener(marker, 'click', function() {
      marker.openInfoWindowHtml('<div><h5>' + coord[2] + '</h5><h4>' + coord[1] + '</h4><div>IP: ' + coord[0] + '</div>Coordinates: ' + coord[3] + ', ' + coord[4] + '</div>');
    });
    return marker;
  }
  
   function repositionMap() {
      var llbounds = new GLatLngBounds();
      
      for (var i=1; i<gmarkers.length; i++) {
        llbounds.extend( gmarkers[i].getLatLng() );
      }
      
      map.setCenter( llbounds.getCenter(), map.getBoundsZoomLevel( llbounds ));
      
   }
   
   function sizeMap() {
      $('#right-column').width($('#body-wrapper').width()-285).height($('#left-column').height());
    }
 
    function twitterCallback2(twitters) {
      var statusHTML = [];
      for (var i=0; i<twitters.length; i++){
        var username = twitters[i].user.screen_name;
        var dateAt = format_date(twitters[i].created_at);
        var timeAt = format_time(twitters[i].created_at);
        var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
          return '<a href="'+url+'">'+url+'</a>';
        }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
          return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
        });
        statusHTML.push('<li><strong>' + dateAt + ':&nbsp;</strong><span>'+status+'</span> <a style="font-size:85%" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'" class=\'twit\'>&rarr;</a></li>');
      }
      document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
    }

    function format_date(short_time) {
      var timer = short_time.split(' ');
      var t = new Date(timer[1] + ' ' + timer[2] + ', ' + timer[5]);
      var m = ((t.getMonth()+1>9) ? (t.getMonth()+1) : '0' +( t.getMonth()+1));
      var d = ((t.getDate()+1>9) ? t.getDate() : '0' + t.getDate());
      var y = String(t.getFullYear());
      return m + '.' + d + '.' + y.slice(2);
    }

    function format_time(short_time) {
      //var timer = short_time.split(' ');
      var t = new Date(short_time);
      var h = ((t.getHours()>12) ? (t.getHours()-12) : t.getHours());
      var m = ((t.getMinutes()>9) ? t.getMinutes() : '0' + t.getMinutes());
      var ap = ((t.getHours()>12) ? 'pm' : 'am');
      return h + ':' + m + ap;
    }

