(function(){
  $(init);

  function init(){
    $(".video").each(function(){ createVideo($(this))});

   $("#homepage_links .nav span").click(nav_click);

   $("#homepage_links .collection a").hover(cycle, cycle_end);
   $(".item").hover(nav_item_hover, nav_item_out);
   $(".nolink").mouseover(remove_images);

   if($("#collection").length == 0){ return true;}

   $(".detail").click(select_image);

    fd.image
    .view($("#collection"))
    .images($("#collection .images"))
    .addEffect(fd.image.effects.orient, {offset: 0})
    .start();

    $("#image_next, #image_prev").click(function(){details('off')});
    $("#image_next").click(fd.image.navigation.next);
    $("#image_prev").click(fd.image.navigation.prev);

    $("#collection_info").find(".on, .off").click(toggle_info); 

    $.history.init(function(hash){
      if(hash == ""){
        i = 0;
      } else {
        hash = hash.replace(/^!\//,"");
        if(hash == 'details'){ return details('on')}
        var i = $("#"+hash).index();
      }

      if(i) { details('off'); fd.image.navigation.select(i)};
             }, {unescape : ",/"});
  }

  var vplayer, videoElement;
  function createVideo(el){
    var videoSrc = $(el).attr("data-url");

    videoElement = $("<video height='405' width='705' autoplay loop>")[0];
    if(videoElement.canPlayType && videoElement.canPlayType('video/mp4').match(/maybe|probably/)){
      if(window.navigator.userAgent.match(/ipad/i)){
         $(videoElement).attr("controls","controls");
      }
      el.html(videoElement);
      videoElement.src = videoSrc;
    } else {
      dumb_player.player_url = "/vendor/super_dumb_player-0.2.swf";
      dumb_player.ui.template("");
      vplayer = el.superDumbPlayer(videoSrc);
      el.find("img").remove();

      $("body").bind("sdpState", function(event, state){
        if(state == false){
          vplayer.seek(0);
        };
      });
    }
  }


  function select_image(){
     var image_id = $(this).attr("class").match(/image_\d+/).pop();
     details("off");
     fd.image.navigation.select($("#"+image_id).index());
  }

  var current_image

  function details(mode){
    var t = $("#collection_info"), p = $("#details");

    if(mode == "off"){
      t.find(".on").hide();
      t.find(".off").show();
      $("#collection").show();
      setTimeout(function(){$(window).resize()},10);
      p.hide();
    } else {
      t.find(".on").show();
      t.find(".off").hide();
      $("#collection").fadeOut();
      p.find(".detail").removeClass("selected");
      var s = p.find("."+current_image).addClass("selected");
      p.show();
      if(s) $(window).scrollTop((s.offset().top + 120) - $(window).height());
    }
  }

  function toggle_info(){
    var t = $(this), p = $("#details");

    if(t.hasClass("on")){
      details("off");
    } else {
      current_image = document.location.hash.replace(/#!\//ig,"");
      $.history.load("!/details");
    }
  }

  function cycle(){
    $(this).stop(false, true).animate({backgroundColor: "#EEEEEE"}, 500); 
  }

  function cycle_end(){
    $(this).stop(false, true).animate({backgroundColor: "transparent"}, 250); 
  }

  function nav_item_hover(){
    var id = $(this).attr("image_id");
    $("#homepage_images .image").fadeOut();
    remove_images();
    insert_image($(this).data("image"));

    //$("#"+id).stop(false,true).fadeIn();
  }
   
  function insert_image(src){
    var img = $("<div class='image inserted'>&nbsp;</div>");
    $("#homepage_images").append(img);
    img.hide().css("background-image", "url("+src+")").fadeIn();
  }
      
  function remove_images(){
    $("#homepage_images .inserted").fadeOut(100, function(){ $(this).remove()}).removeClass("inserted");
  }

  function nav_item_out(){
    var id = $(this).attr("image_id");
    //$("#"+id).stop(false,true).fadeOut();
  }

  function nav_click(){
    $("#homepage_links .nav span").removeClass("active");
    $("#events, #current, #archive, #contact, #projects").hide();
    var klass = $(this).attr("class") || "_";
    $("#"+klass).fadeIn();
    $(this).addClass("active");
    $("#homepage_images .image").hide();
    $("."+klass+"_images").stop().fadeIn();
    nav_item_hover.apply($("#"+klass+" a:first"))
    $("#image_"+klass).stop(false,true).fadeIn();
    // fix bug with black remaining
    $("body").hide(); 
    setTimeout(function(){$("body").show();},1);

  }
})();

