$(document).ready(function() {
  /* Gallery slideshow */
  if ($(".gallery-thumb:eq(1)").length) {
    gallery_inits = new Array;
    $("div.node-type-photo-gallery .content .gallery-init").each(function(i) {
      gallery_inits[i] = $(this).html();
    });
//  gallery_inits.sort(function() {return 0.31416 - Math.random()});
    $("div.node-type-photo-gallery .content").html(gallery_inits.join(''));

    gallery_contents = new Array;
    gallery_thumbs = new Array;
    $("div.node-type-photo-gallery .gallery-thumb").each(function(i) {
      desc_text = ($(this).find(".description").text().length) ? '<div class="caption">'+ $(this).find(".description").html() +'</div>' : '';
      photo_link = $(this).find(".photo-link a").attr("href");
      anchor_before = (photo_link) ? '<a href="'+ photo_link +'">' : '';
      anchor_after = (photo_link) ? '</a>' : '';

      alt_text = ' alt="'+ $(this).find("img").attr("alt") +'"';
      title_text = ' title="'+ $(this).find("img").attr("title") +'"';
      gallery_contents[i] = '<div class="gallery-image"><div class="inner">'+ anchor_before +'<img src="'+ $(this).find("a").attr("href") +'"'+ alt_text + title_text +' />'+ anchor_after + desc_text +'</div></div>';
      gallery_thumbs[i] = '<div class="gallery-thumb">'+ $(this).find("a").removeAttr("href").end().html() +'</div>';
    });

    $("div.node-type-photo-gallery .content").html(gallery_contents.join('')).after('<div class="thumbs">'+ gallery_thumbs.join('') +'</div>');

    $("div.node-type-photo-gallery .gallery-image").each(function(i) {
      $(this).attr("id", "gallery-image-"+ (i+1));
      $("div.node-type-photo-gallery .gallery-thumb:eq("+ (i) +")").attr("id", "gallery-thumb-"+ (i+1));
    });
    
    $("#gallery-thumb-1").addClass("active");

    advance = setTimeout("advanceGallery('timer')", Drupal.settings.galleryTimeout);

    var output = "";
    $("div.node-type-photo-gallery .thumbs div").bind("click", function() {
      tmp = parseInt($(this).attr("id").replace("gallery-thumb-",""));
      if (tmp != (nextGalleryImage)) {
        nextGalleryImage = tmp-1;

        clearTimeout(advance);
        advanceGallery();
      }
      else {
        clearTimeout(advance);
        advance = setTimeout("advanceGallery('timer')", Drupal.settings.galleryTimeout);     
      }
    });
    
    $("div.node-type-photo-gallery").addClass("photo-gallery-ready").find(".thumbs").fadeIn("slow");
  }
});

function advanceGallery(type) {
  max = $(".gallery-thumb").length;

  if (nextGalleryImage == max) {
    nextGalleryImage = 1;
  }
  else {
    nextGalleryImage++;
  }
  
  $("div.node-type-photo-gallery .thumbs div").removeClass("active");
  $("div.node-type-photo-gallery .thumbs div#gallery-thumb-"+ (nextGalleryImage)).addClass("active");

//if (type == 'timer') {
//    $(".gallery-image").css("position", "absolute");
    $("#gallery-image-"+ nextGalleryImage).stop(false, true).fadeIn("slow");
    $("#gallery-image-"+ currentGalleryImage).stop(false, true).fadeOut("slow");
/*
  }
  else {
    $(".gallery-image").css("position", "static");
    $("#gallery-image-"+ nextGalleryImage).stop(true, true).slideDown("slow");
    $("#gallery-image-"+ currentGalleryImage).stop(true, true).slideUp("slow");
  }
*/
  currentGalleryImage = nextGalleryImage;

  advance = setTimeout("advanceGallery('timer')", Drupal.settings.galleryTimeout);
}
