$.fn.pilotView = function(direction){
  
  var index = $(this).parent().parent().index();
  var altura = parseInt(projetoSelecionado.jobs[index].imagem.altura);
  var desvio = parseInt($(this).css("top"));
  var toptime = (desvio+altura)*7;

  if(typeof(direction) == "undefined") var direction = "down";
  var goingDown = (direction == "down");

    $(this).animate(
      {
        top: goingDown ? -(altura-350) : 0
      },
      {
        easing: "easeInOutQuad",
        duration: toptime,
        complete: function(){
          if(isSelected(index) && !stopAllPilots) goingDown ? $(this).pilotView("up") : $(this).pilotView("down");
        }
      }
    )
    return $(this);
  }
function isSelected(index){
  return (jobSelecionado == index);  
}

$.fn.jobView = function(){

  stopAllPilots = false;
  var index = $(this).parent().parent().index();

  var altura = projetoSelecionado.jobs[index].imagem.altura;

    if(altura > $(this).parent().height()){

      if(isSelected(index)) $(this).css("cursor", "crosshair").pilotView();

    $(this).parent().mousemove(function(m){
      stopAllPilots = true;
      if(isSelected(index)) $(this).find("img").stop(false, true); 
      var mouseY = (m.pageY - $(this).offset()['top'])/350;
      if(isSelected(index)){
        maxTop = 350-altura;
        $(this).find("img").css({top: mouseY*maxTop});
      }
      });
    }

    $(this).parent().mouseenter(function(){
      stopAllPilots = true;
      if(isSelected(index)) $(this).find("img").stop(false, true); 
    });

    $(this).parent().mouseleave(function(){
      stopAllPilots = false;
      if(isSelected(index)) $(this).find("img").stop(false, false).css("cursor", "crosshair").pilotView();
    });
  return $(this);
  }


$.fn.alignFlow = function(jobSelecionado, hard){
  if(typeof(hard)=="undefined") var hard = false;
  meio = ($(window).width()/2)-($(".job").eq(0).width()/2);
  var espacoElemento = $(".job").outerWidth(true);

  $(this).stop(true, false).animate(
    {
      left: meio-espacoElemento*jobSelecionado
    },
    {
      duration: hard ? 0 : 900,
      easing: "easeInOutQuad"
    }
  );
  return $(this);
}

$.fn.enlarge = function(){
  var childrenWidth = 0;
  $(this).children().each(function(i){
    childrenWidth += $(this).outerWidth(true)+2;
  });
  $(this).width(childrenWidth);
  return this;
}
