function resetDossierImgMargins() {
  dLeft = 0;
  $(".dossier .images img").each(function(){
    $(this).css({ marginLeft: 0, left: dLeft });
    dLeft += 270;
  });
}
function makeToolTip(node) {
  $("#footer").after('<div class="tooltip"></div>');
  $(".tooltip:last").stop().animate({ opacity: 1 }, 500).data("node", node).html($(node).data("tooltip"));
  $(node).mousemove(function(e){
    node = this;
    $(".tooltip").each(function(){
      if($(this).data("node") == node) {
        $(this).css({ top: e.pageY, left: e.pageX });
      }
    });
  });
}
function removeToolTip(node) {
  $(".tooltip").each(function(){
    if($(this).data("node") == node) {
      $(this).stop().animate({ opacity:0 }, 500, function(){ $(this).remove(); });
    }
  });
}

$(document).ready(function(){

  //activate default value for search box
  $("#search-top input[name=s]").each(function(){
    $(this)
      .data("dval", $(this).attr("title"))
      .removeAttr("title")
      .val($(this).data("dval"))
      .focus(function(){
        if($(this).val() == $(this).data("dval")) {
          $(this).val("");
        }
      })
      .blur(function(){
        if($.trim($(this).val()) == "") {
          $(this).val($(this).data("dval"));
        }
      });
  });
  
  //activate tooltips
  $("body *[title]").each(function(){
    $(this)
      .data("tooltip", $(this).attr("title"))
      .removeAttr("title")
      .mouseover(function(){ makeToolTip(this); })
      .mouseout(function(){ removeToolTip(this); });
  });
  
  //animations for home page
  $("p.entry img, ul.entries img").load(function(){
    i = $("p.entry").index($(this).parent()) + 1;
    $(this)
      .css({ left: -140 })
      .animate({ opacity: 0 }, i*200)
      .animate({ opacity: 1, left: 0 }, 500, "swing");
  });
  
  //animation for dossiers
  $(".dossier, .dossier *").css({ cursor: 'pointer' });
  $(".dossier .images img:first").clone().appendTo(".dossier .images");
  resetDossierImgMargins();
  $(".dossier").click(function(){
    $(".dossier .images img").stop().animate({ marginLeft: -270 }, 500);
    $(".dossier .images img:first").stop().animate({ marginLeft: -270 }, 500, function(){
      $(".dossier .images img:first").remove();
      $(".dossier .images img:first").clone().appendTo(".dossier .images");
      resetDossierImgMargins();
    });
  });
  
  //wpcf7 stuff
  $(".wpcf7-not-valid-tip-no-ajax").mouseover(function(){
    $(this).animate({ opacity: 0 }, 1000, function(){
      $(this).remove();
    });
  });
  
});