
var imgs = [
  "http://farm5.static.flickr.com/4094/4871802920_90b52a219d_b.jpg",
  "http://farm3.static.flickr.com/2737/4468031894_88531a362b_b.jpg",
  "http://farm4.static.flickr.com/3149/2755906203_9b8c9b893b_o.jpg",
  "http://farm4.static.flickr.com/3216/2892446645_ed5876acf8_b.jpg",
  "http://farm4.static.flickr.com/3151/2669355688_bb3184ffc9_b.jpg",
  "http://farm4.static.flickr.com/3098/2808675355_b11ed4ec33_b.jpg",
  "http://farm4.static.flickr.com/3584/3807173580_5aeb867cde_b.jpg"
];

var time = 25000;
var i = Math.round(Math.random() * 100) % imgs.length;
var oldel = false;
var loaded = false;

$(document).ready(function() {
  if ($("#slideshow").get().length) {
    $("<p style='position:absolute;bottom:10px;right:10px;z-index:100;padding:0;margin:0;font-weight:bold;text-shadow:0 0 10px #000;'><a href='http://www.flickr.com/photos/xthom/' target='_blank' style='text-decoration:none'><span style='font-weight:normal;color:#6699AA;font-size:90%'>xthom@</span><span style='color:#0063DC'>flick</span><span style='color:#FF0084'>r</span></a></p>").appendTo($("#slideshow"));
    animate(load(imgs[i]), true);
  }
  if ($("#article-header").get().length) {
    var r = Math.round(Math.random() * 100) % imgs.length;
    var imgurl = (typeof forceCarousel != "undefined" ? (typeof forceCarousel != "string" ? forceCarousel[(r^2)%forceCarousel.length] : forceCarousel) : imgs[r]);
    $("<img>").attr("src", imgurl).addClass("zoomable").css({"display":"none","top":0,"left":0}).load(function() {
      $(this).appendTo($("#article-header"));
      var ty = ($(this).height() - 200)/2;
      var tx = ($(this).width() - 1000)/2;
      $(this).css({"left":-tx,"top":-ty}).fadeIn();
   }).click(function() {
    $.fancybox({"href": $(this).attr("src")});
   });
  }
});

function load(url) {
  loaded = false;
  return $("<img>").attr("src", url).addClass("zoomable").css({"display":"none","top":0,"left":0}).load(function() {
    $(this).appendTo($("#slideshow"));
    loaded = true;
  }).click(function() {
    $.fancybox({"href": $(this).attr("src")});
  });
}

function animate(el, first) {
  if (oldel !== false && first)
    $(oldel).fadeOut(function() { $(this).remove() });
  oldel = el;
  if (loaded) {
    var img = load(imgs[(++i) % imgs.length]);
    var ty = $(el).height() - 210;
    var tx = $(el).width() - 1020;
    $(el).css("left", -tx/2);
    $(el).fadeIn().animate({
      "top": -ty
//      "left": -tx
    }, time, function() {
      $(oldel).fadeOut(function() { $(this).remove() });
      oldel = el;
      animate(img, true);
    });
  }
  else {
    setTimeout("animate(oldel, false)", 1000);
  } 
}

