General Event Question

General Event Question

Hi, I'm trying to understand what I'm doing wrong.  What I'm doing is looping through an  array and adding an event.  When that event is fired I need to use the index of the item in the array (to find a related div). 

Here's my code:

  1.         for (var i in $.goMap.markers) {    
  2.             google.maps.event.addListener($.goMap.markers[i],'click',function()
  3.             {
  4.                
  5.                 alert("Opening "+i);
  6.                 $("#res"+i).css("background","yellow");
  7.        
  8.             });           
  9.          }

What's happening is when the event fires i = the length of the array.  I can understand why this is happening but am not sure of the right way to accomplish what I'm trying to do.

Thanks.