jQuery(document).ready
(
  function($)
  {
    $(".tabs").each
    (
      function()
      {
        animating=false;
        speed=500;
        $tabs=$(this);
        $tabs.append('<div class="menu"></div>');
        $tabs.find(".tab").each
        (
          function()
          {
            $tabs.find(".menu").append('<div class="item">'+$(this).find(".heading").html()+'</div>');
            $(this).find(".heading").remove();
            
            if($(this).index(".tab"))
              $(this).hide();
            else
              $tabs.find(".menu .item:nth-child(1)").addClass("active");
            $tabs.find(".menu .item").each
            (
              function()
              {
                $(this).addClass("index-"+$(this).index());
              }
            );
          }
        );
        
        $tabs.find(".menu .item:nth-child(2)").addClass("neighbour-right");
        
        $tabs.find(".menu .item").bind
        (
          "click",
          function(event)
          {
            $target=$(event.target);
            if(!$target.hasClass("item"))
              $target=$(event.target).parent(".item");
            $tabs.find(".tab:not(:nth-child("+($target.index()+1)+"))").each
            (
              function()
              {
                $tabs.find(".menu .item:not(:nth-child("+($target.index()+1)+"))").removeClass("active");
                $(this).find(".wp-post-image").hide();
                $(this).slideUp(speed/2);
              }
            );
            $tabs.find(".tab:nth-child("+($target.index()+1)+")").each
            (
              function()
              {
                $tabs.find(".menu .item").removeClass("neighbour-left");
                $tabs.find(".menu .item").removeClass("neighbour-right");
                
                $tabs.find(".menu .item:nth-child("+($target.index())+")").addClass("neighbour-left");
                $tabs.find(".menu .item:nth-child("+($target.index()+2)+")").addClass("neighbour-right");
                
                $tabs.find(".menu .item:nth-child("+($target.index()+1)+")").addClass("active");
                $(this).find(".wp-post-image").hide();
                $(this).slideDown
                (
                  speed,
                  function()
                  {
                    $(this).find(".wp-post-image").fadeIn();
                  }
                );
              }
            );
          }
        );
      }
    );
  }
);

