(function($){

  var ag;
  var IMAGE_CONTAINER = ".image_container";
  var CAPTION_CLASS = ".pp_caption";
  ag = $.fn.pp_audio_gallery = function (options) {
    /* INIT */
  	
    var me = this;
    var default_options = {
      images: [],
      mp3:null,
      onDisplay: null
    }
    $.extend(default_options, options);
      
    for (var i in options) {
  		$.pp_audio_gallery[i] = options[i];
  	}
  	
    // build template 
    var inner = '<div class="image_container">';
    $.each(options.images, function(){
      inner += '<img src="';
      inner += this[0];
      inner += '" alt="';
      inner += this[1];
      inner += '" />';
    });
    inner += "</div>";
    if(options.images.length > 1){
      inner += '<span class="prev"><img src="http://www.propublica.org/images/design/gallery_nav_left_arrow.gif" style="display: inline" /> </span> <span class="current_index"></span> <span class="separator">/</span> <span class="total_index"></span> <span class="next"><img src="http://www.propublica.org/images/design/gallery_nav_right_arrow.gif" style="display: inline" /> </span> ';
    }
    inner += '<div id="audioplayer"></div><div class="pp_caption"></div>';
    $(this).html(inner);
    
    // audio setup
    if(options.hasOwnProperty('mp3')){
      AudioPlayer.setup("http://www.propublica.org/audio/audio_player.swf", { width: 395 });  
  	  AudioPlayer.embed("audioplayer", {soundFile: options.mp3[0], autostart: "no", titles: options.mp3[1]})
	  }
    
  	
  	// Slideshow setup
    $(this).children(IMAGE_CONTAINER).children("img").hide();
    $(this).children(IMAGE_CONTAINER).children("img:first").show().addClass('active');
    if(options.images.length > 1){
      $(this).children("span.next").click(function(){$.pp_audio_gallery.next()});
      $(this).children("span.prev").click(function(){$.pp_audio_gallery.prev()});
    }
    ag.parentSelector = $(this);
    ag.activeSelector = $(this).children(IMAGE_CONTAINER).children("img.active");
    ag.elementSelector = $(this).children(IMAGE_CONTAINER).children("img");
    ag.captionSelector = $(this).children(CAPTION_CLASS);
    $(ag.captionSelector).text(ag.activeSelector.attr('alt'));
    ag.elementAtIndex = function (index) { 
      if($(me).children(IMAGE_CONTAINER).children("img:eq(" + index + ")") !== null)
          return $(me).children(IMAGE_CONTAINER).children("img:eq(" + index + ")");
       return $(me).children(IMAGE_CONTAINER).children("img:first");
      };
    
    ag.currentIndex = 0;
    ag.updateDisplay();
    return this;
  }
  
  ag.nextItem = function(){    
    ag.activate(ag.currentIndex + 1 > ag.elementSelector.size()-1 ? 0 : ag.currentIndex + 1);
  };
  
  ag.prevItem = function(){
    ag.activate(ag.currentIndex - 1 < 0 ? ag.elementSelector.size()-1 : ag.currentIndex - 1);
  };
  
  ag.activate = function(index){
    ag.elementAtIndex(ag.currentIndex).removeClass("active").hide();
    ag.currentIndex = index;
    var selector = ag.elementAtIndex(index).addClass("active").show();
    $(ag.captionSelector).text(selector.attr('alt'));
    ag.updateDisplay();
  };
  
  ag.updateDisplay = function() {
    ag.parentSelector.children("span.current_index").text(ag.currentIndex+1);
    ag.parentSelector.children("span.total_index").text(ag.elementSelector.length);
  };
  
  $.extend({
    pp_audio_gallery:{
      next: function(){
        ag.nextItem();
      },
      prev: function(){
        ag.prevItem();
      }
    }
  });
  
  
})(jQuery);