/* goo.js   
 * Google map helper code. 
 *--------------------------------------------------------------------------*/


/* Note radius is in KM, returns a GPolygon that should be added via
   map.addOverlay 
 */
function genCircle(center, radius, nodes, liColor, liWidth, liOpa, 
                   fillColor, fillOpa, metadata)
{
  // Esa 2006
  //calculating km/degree
  var latConv = 
    center.distanceFrom(new GLatLng(center.lat()+0.1, center.lng()))/100;
  var lngConv = 
    center.distanceFrom(new GLatLng(center.lat(), center.lng()+0.1))/100;

  //Loop 
  var points = [];
  var step = parseInt(360/nodes)||10;
  for(var i=0; i<=360; i+=step)
    {
      var pint = new GLatLng(center.lat() + (radius/latConv * Math.cos(i * Math.PI/180)), center.lng() + 
                             (radius/lngConv * Math.sin(i * Math.PI/180)));
      points.push(pint);
      // bounds.extend(pint); //this is for fit function
    }
  points.push(points[0]); // Closes the circle, thanks Martin
  fillColor = fillColor||liColor||"#0055ff";
  liWidth = liWidth||2;
  var poly = new GPolygon(points,liColor,liWidth,liOpa,fillColor,fillOpa);
  return poly;
}


var mapeffects = {
  overlay_ : null,
  imgurl_ : null,
  map_ : null,
  

  // XXX On test the url does not work... hmm.
  init : function(url, map) {
    mapeffects.imgurl_ = url;
    mapeffects.map_ = map;
  },


  set_loading : function() {
    if (!mapeffects.overlay_) {
      mapeffects.overlay_ = new GScreenOverlay(mapeffects.imgurl_,
                                           new GScreenPoint(35, 221),
                                           new GScreenPoint(0, 180),
                                           new GScreenSize(540, 180));      
      mapeffects.map_.addOverlay(mapeffects.overlay_);
    }
  },


  clear_loading : function() {
    if (mapeffects.overlay_) {
      mapeffects.map_.removeOverlay(mapeffects.overlay_);
    }
  }

};



var bubble = {
  /* center - default center point when no lat/lng present in tweet. */
  center:null,
  last_marker : null,

  /* this function places a marker on tweeter's location 
     map - google map object
     t - holds latitude and longitude information
     tweeter - selected tweeter object
     baseurl - base url of the site to access images folder
     prevMarker - the previous selected tweeter's marker
  
     returns - the current marker being drawn
  */
  place_marker : function(map, t, tweeter, baseurl, prevMarker) {

     var pt;
     if (t.lat && t.lng) {
       pt = new GLatLng(t.lat, t.lng);
       map.panTo(pt);
     }
     else {
       pt = bubble.center;
     }

     /* if the user clicks on a different tweet, then remove the previous
        marker  */     
     if(prevMarker != null){
       map.removeOverlay(prevMarker);
     }

     var myIcon = new GIcon(G_DEFAULT_ICON);
     myIcon.image = baseurl + "images/twitter.png";

     myIcon.iconSize = new GSize(35,35);         
     var markerOptions = { icon:myIcon };   
     var marker = new GMarker(pt, markerOptions);

     /* place the marker on the map */
     map.addOverlay(marker);

     bubble.last_marker = marker;
     return marker;

  },


  /* redraw the last_marker after the map has been cleaned */
  redraw_marker : function() {
     map.addOverlay(bubble.last_marker);
  },
   
  show_from_tweet : function(map, t, tweeter) {
    var pt;
    if (t.lat && t.lng) {
      pt = new GLatLng(t.lat, t.lng);
      map.panTo(pt);
    }
    else {
      pt = bubble.center;
    }
    //creates the info window object and populates its with a DOM node
    map.openInfoWindow(pt, bubble.layout_tweet(t,tweeter));
    
  },


  play_tweets : function(map, tlist) {
    /* Tweets are in time order, most recent first.  We want to play
       from old to new... */
    bubble.playnext_(map, tlist, tlist.length - 1);
  },

  
  playnext_ : function(map, tlist, idx) {
    if (idx >= 0) {
      bubble.show_from_tweet(map, tlist[idx]);
      window.setTimeout(function() { bubble.playnext_(map, tlist, --idx); },
                        5000);
    }
  },


  /*
   *  This function generates DOM elements for the info window.
   *  Accepts a tweet and a show_user object.
   */
  layout_tweet : function(t,tweeter) {

    var el = document.createElement('div');
    el.className = 'tbubble';
    var html = '';

    html = '<div class="pic"><img height="48" width="48" src="' +t.pic 
       + '" /></div>';
    var name = (tweeter.name) ? "(" + tweeter.name + ")" : "";
    html += '<div class="info">';
    html += '<span class="sname">' + t.nam + '&nbsp;' + name + '</span><br/>';
    html += '<span class="tloc">' +t.loc+ '</span><br/>';
    if (tweeter.url) {
      html += '<span class="userlink"><a href="' + tweeter.url + '">' 
        + tweeter.url + '</a></span><br/>';
    }
    if (tweeter.friends_count_ != null)
    {
        html += '<span class = "friend_count"> Friends: '+tweeter.friends_count_+'</span>';
    }
    if (tweeter.friends_count_ != null)
    {
        html +=  '<span class = "follower_count"> Followers: '+tweeter.followers_count_+'</span>';
    }
    html += '<br class="clear"/></div>';
    if (tweeter.description_ != null)
    {
      html += '<div class="profile_information"> <span class="description">'+tweeter.description_+'</span>';
    }
    html += '</div>';
    el.innerHTML = html;
    return el;
  }
  
};

function window_resize_handler() {
  contentwidth = $(window).width() - 30;   
  $('#map').width(contentwidth);
  if (map) {
    map.checkResize();
  }
}

  
