(function($){

  $(document).ready(function(){
    /*
     *   = Galleries
     *
     *   v1.01 09/07/11 
     *
     *   If the gallery has the 'numbered' class then it 
     *   looks for element with an class of $GALLERY_NAME-pagination
     *   and will update the active class on seek.
     *
     *   If the gallery has the '.paginated' class then it 
     *   looks for element with an class of $GALLERY_NAME-current-page
     *   and will update that with "#n of #total" as the user
     *   pages through.
     *
     *   If the gallery has the class '.thumbnails' then it
     *   looks for an element of $GALLERY_NAME-thumbnails and switches
     *   the current item for the selected one
     */
    $(".scrollable").each(function(){
      var self       = $(this),
          scrollable = self.scrollable({
            next: '.'+this.id+'-next',   
            prev: '.'+this.id+'-prev', 
            circular: true
          });
      if (self.hasClass('autoplay')) {
        scrollable.autoscroll({ autoplay: true, interval: 6000 });
      }
      
      self.bind("onSeek",function(e){
        var s    = self.data('scrollable'),
            idx  = s.getIndex(),
            item = s.getItems()[idx];

        if (self.hasClass( 'paginated' )) {
          $('.'+self[0].id+'-current-page').each(function(){
            var curPage = $(this),
                txt     = curPage.text();
            txt = txt.replace(/^(\d+)/, idx+1);
            txt = txt.replace(/(\d+)$/, s.getSize());
            curPage.text(txt);
          });
        }

        if (self.hasClass( 'numbered' )) {
          var link = $('.'+self[0].id+'-pagination li a:not(.next,.prev)').eq(idx);
          link.parent().addClass('active').siblings().removeClass('active');
        }

        if (self.hasClass( 'captioned' )) {
          $("."+self[0].id+'-caption').html($(item).find("img").data('caption'));
        }
      });

      if (self.hasClass( 'numbered' )) {
        $('.'+self[0].id+'-pagination li a:not(.next,.prev)').live('click',function(e){
          e.preventDefault();
          var link  = $(this),
              s     = self.data('scrollable');
           s.seekTo(parseInt( link.data('index'), 10 ),100); 
        });
      }

      if (self.hasClass('thumbnails')) {
        $('.'+this.id+'-thumbnails a').live('click',function(e){
          e.preventDefault();
          var thumb = $(this),
              s     = self.data('scrollable'),
              idx   = null;
          // Find the corresponding large item in the stack
          $.each(s.getItems(),function(i,v){
            if ($(v).attr('src') == thumb.attr('href')) { idx = i; }
          });
          if (idx !== null) { s.seekTo(idx, 100); }
        });
      }

    });

    // Big-afy clicked images
    // $(".ui-zoom").fancybox({});

    // Clear inputs with prefilled prompts
    $("input.prompt").live('focus blur',function(e){
      var self = $(this);
      if (!self.data('prompt')) { self.data('prompt', self.attr('value')); }
      if (self.val() !== "" && self.val() !== self.data('prompt')) { return false; }
      e.type == 'focusin' ? self.val('') : self.val(self.data('prompt'));
    });

    // Swap galleries
    $(".scrollable.disabled").closest(".gallery").hide();
    $(".gallery-menu a").live('click',function(e){
      e.preventDefault();  
      var self = $(this).blur(),
          gallery = $(self.attr('href')).closest(".gallery");
      self.parent().addClass('active').siblings().removeClass('active');
      if (gallery.is(":visible")) {
        return false;
      }
      gallery.show().siblings().hide();
      // re-initialize slideshow
    });
  });
})(jQuery);

