/** Script (C) 2008 Rob Ewaschuk http://rob.infinitepigeons.org/blog/
    Licenced under MIT Licence, as found at http://www.google.ca/search?q=mit+licence */
$(document).ready(function() {
  function fitNicely(outW, outH, inner) {
    //var outW = outer.width(), outH = outer.height();
    var inW = inner.width(), inH = inner.height();
    //alert([inW, inH, outW, outH].join(", "));
    if (outW > 100 && outH > 100 && inW > 100 && inH > 100) {
      var ratio = outW / inW;
      if (outH / inH < ratio) { ratio = outH / inH; }
      ratio = Math.min(1, ratio); // Never scale up, only down.
      if (outH > 0 && outW > 0) {
        inner.width(inW * ratio);
        inner.height(inH * ratio);
      }
    }
  }

  $("#contact").text("contact").click(
      function() { $("#contact").text("claremcevoy@gmail.com"); }
    );

  var photoW = $("#photo").width();
  var photoH = $("#photo").height();
  $("#initial").appendTo("#photo").show();

  $("#initial").load(
      function() { fitNicely(photoW, photoH, $("#initial")); }
    );
  fitNicely(photoW, photoH, $("#photo img.initial"));
//  $("#photo img.initial").show();


  // Take care of some stuff that's defaulted for a non-javascript browser
  $("#showdetails").show(); $("iframe").hide(); $("#details").hide();
  $("#photos div.description").hide(); $("#photo img").addClass("initial");
  // Try fitting the initial photo nicely; we'll try again after it's loaded in
  // case the dimensions change.

  $(".description").each(function() {
      if ($.trim(this.innerHTML) === "") {
        $(this).remove();
      }
    });
  
  // queue in order and load two photos at a time from it (and as each photo
  // finishes loading, a new load is triggered).  When a user opens a section,
  // we reshuffle that section's photos to the top of the queue in order.  When
  // a user clicks a photo, if it's still in the queue, we bump it to the top.
  //
  // This allows us to (1) always throttle the connection until everything is
  // loaded, (2) anticipate linear flow through sections and (3) respond to
  // non-linear flow reasonably.
  var queue = [];

  // First we tag all the photos with their ID, and populate the queue.
  var i = 0;
  $("#photos a").each(function() {
      $(this).attr("id", "link" + (++i));
      queue.push(i);
    });

  function load(recurse) {
    // Which photo do load?
    if (queue.length === 0) { return; }
    var index = queue.shift();
    var link = $("#link" + index).addClass("loaded");
    var photo = $("<img>").load(function() {
        // kick off another load.
        //setTimeout(load, 1000); //debug
        fitNicely(photoW, photoH, photo);
        if (link.parent().hasClass("current")) {
          photo.show();
        } else {
          photo.hide();
        }
        if (recurse) { load(true); } 
      });

    photo.attr("src", link.attr("href")).attr("id", "photo" + index).hide();
    if (link.hasClass("noborder")) { photo.addClass("noborder"); }
    $("#photo").append(photo);
  }

  setTimeout(function() { load(true); }, 100);

  // Find the given photo in the list and bump it to the front.
  function requeue(link) {
    var index = Number(link.id.substring(4));
    // Find the unloaded photo and move it to the front of the queue.
    for (var i = 0; i < queue.length; i++) {
      if (queue[i] === index) {
        queue.splice(i, 1); queue.unshift(index);
        break;
      }
    }
  }

  $("#photos").accordion({ header: "h2", autoHeight: false, alwaysOpen: false, active:false });

  $("#photos").bind("accordionchange", function(event, ui) {
      ui.newHeader.blur();
      var links = ui.newContent.children().children("a");
      // "click" them in reverse order so the top one ends up at the front.
      for (var i = links.length - 1; i >= 0; i--) {
        requeue(links.get(i));
      } 
    });

  // How many times do we start a new recursive load from an image click?
  var clickStartsLoad = 2;

  $("#photos a").click(function(e) {
      e.preventDefault();
      var photo = $("#photo" + this.id.substring(4));
      $("#photos li div.description").hide("fast");
      $("#photos li.current").removeClass("current");
      $(this).parent().addClass("current");
      $("#photo img").hide(); $("#details").hide();
      photo.show(); $(this).blur();
      $("#photos li.current div.description").show("fast");
      if (!$(this).hasClass("loaded")) {
        requeue(this);
      }
      load(clickStartsLoad > 0); clickStartsLoad--;
    });

  $("#showdetails").click(function(e) { 
      e.preventDefault();
      $("#photo img").hide();
      $("#details").show(); 
      $("#photos").activate(-1);
    });

});
