fd.video.autoplay = true;

var startViewer = (function(){
   function fix_ie(){
    var w = $(window).width()-320,
        h = $(window).height()-60;
    $("#collection, #collection_navigation, #thumbnails").width(w);  
    $("#collection").height(h);
  }

  if($.browser.msie){
    $(window).resize(fix_ie);
    fix_ie();
  }

  $("#next").click(fd.image.navigation.next);
  $("#prev").click(fd.image.navigation.prev);

  $("#collection").bind("fd:image:show", function(){
    $("#image_count").text(($(".image.selected").index()+1)+" ")
  });

  $("#show_thumbnails").click(function(){
    $("#collection").fadeOut();
    $("#collection_navigation").fadeOut();
    $("#thumbnails").fadeIn();
    fd.video.stopAll();
  });

  $("#show_description").click(function(){
    $("#collection").fadeOut();
    $("#collection_navigation").fadeOut();
    $("#description").fadeIn();
    fd.video.stopAll();
  });

  $("#hide_description").click(function(){
    $("#collection").fadeIn();
    $("#collection_navigation").fadeIn();
    $("#description").fadeOut();
  });


  $("#thumbnails .thumbnail").click(function(){
    $("#collection").fadeIn();
    $("#collection_navigation").fadeIn();
    $("#thumbnails").fadeOut();
    fd.image.navigation.select($("#thumbnails div").index(this));

  })

  fd.image
  .view($("#collection"))
  .images($("#collection .images"))
  .addEffect(fd.image.effects.orient)
  .start();

  function offsetX(e){
    return e.pageX - $(".images").offset().left;   
  }
  function whichSide(e){
    return (offsetX(e) >= $(".images").width()/2) ? "next" : "prev";
  }

  function otherSide(e){
    return (offsetX(e) <= $(".images").width()/2) ? "next" : "prev";
  }

  var timer;

  fd.image.navigation.setClick(whichSide);

  $("#collection .image").mousemove(function(e){
    $("#arrow_"+whichSide(e)).fadeIn(500);
    $("#arrow_"+otherSide(e)).fadeOut(200);
    clearTimeout(timer);
    timer = setTimeout(function(){$("#arrow_next, #arrow_prev").fadeOut(500)}, 1500);
  });

      $.history.init(function(hash){
    var i;
      if(hash == ""){
        i = 0;
      } else {
        hash = hash.replace(/^(\/)?!\//,"");
        i = $("#"+hash).index();
      }

        if(i) fd.image.navigation.select(i);
   });
});

