jQuery(document).ready
(
  function($)
  {
    if($("body.post-ojete-vozy"))
      cars_reload(1, true);
    
    $("section.filter h2").append(" <span class=\"toggle\">(zobrazit)</span>"); 
    $("section.filter legend").append(" <span class=\"tools\">(<span class=\"all\">vybrat vše</span>, <span class=\"clear\">zrušit výběr</span>)");
    
    $("section.filter h2 span.toggle").live
    (
      "click",
      function()
      {
        if($(this).hasClass("visible"))
        {
          $(this).html("(zobrazit)");
          $(this).removeClass("visible");
          $(this).parents("section.filter").find("form").stop(true, true).slideUp(500);
        }
        else
        {
          $(this).addClass("visible");
          $(this).html("(skrýt)");
          $(this).parents("section.filter").find("form").stop(true, true).slideDown(500);
        }
      }
    ); 
    
    $("section.filter legend span.all").live
    (
      "click",
      function()
      {
        $(this).parents("fieldset").find("input:checkbox").attr("checked", true);
        cars_reload(1, false);
      }
    );
    
    $("section.filter legend span.clear").live
    (
      "click",
      function()
      {
        $(this).parents("fieldset").find("input:checkbox").attr("checked", false);
        cars_reload(1, false);
      }
    );
      
      
    $("section.filter input:checkbox").live
    (
      "change",
      function()
      {
        cars_reload(1, false);
      }
    );
    
    $("p.pagination span").live
    (
      "click",
      function()
      {
        var page=$(this).attr("class").match(/page-(\d)/);
        cars_reload(page[1], false);
      }
    );
    
    function cars_reload(page, init)
    {
      $cars=$("section.cars");
      if($cars.is(".no-ajax"))
        return;
      
      $pagination=$("p.pagination");
      
      $pagination.children("span.page").stop(true, true).hide();
    
      $pagination.append("<span class=\"loading\">Načítání...</span>");
      $pagination.find("span.loading").animate
      (
        {
          opacity: 1
        },
        1000
      );
            
      $cars.stop(true, true).animate
      (
        {
          opacity: 0
        },
        500
      );
      
      var manufacturers=[];
      $("section.filter input[name=manufacturers[]]:checked").each
      (
        function()
        {
          manufacturers.push($(this).val());
        }
      );
      
      var bodies=[];
      $("section.filter input[name=bodies[]]:checked").each
      (
        function()
        {
          bodies.push($(this).val());
        }
      );
      
      var engines=[];
      $("section.filter input[name=engines[]]:checked").each
      (
        function()
        {
          engines.push($(this).val());
        }
      );
      
      jQuery.post
      (
        EWAjax.ajaxurl,
        {
          action: "autojokr_filter",
          manufacturers: manufacturers,
          bodies: bodies,
          engines: engines,
          init: init,
          page: page
        },
        function(response)
        {
          $cars.html(response.cars);
          $cars.stop(true, true).animate
          (
            {
              opacity: 1
            },
            500
          );
          
          $pagination.html(response.pagination);
          
          $pagination.children("span.page").stop(true, true).animate
          (
            {
              opacity: 1
            },
            500
          );
          
          if(init)
          {
            if(response.manufacturers)
            {
              $("section.filter fieldset.manufacturers").children("ol").remove();
              $("section.filter fieldset.manufacturers").append(response.manufacturers);
            }
            
            if(response.bodies)
            {
              $("section.filter fieldset.bodies").children("ol").remove();
              $("section.filter fieldset.bodies").append(response.bodies);
            }
            
            if(response.engines)
            {
              $("section.filter fieldset.engines").children("ol").remove();
              $("section.filter fieldset.engines").append(response.engines);
            }
          }
          
          Cufon.refresh();
          
          $("a.fancybox").fancybox
          (
            {
              "transitionIn": "easeIn",
              "transitionOut": "none",
              "titlePosition": "over",
              "titleFormat": function(title, currentArray, currentIndex, currentOpts)
              {
                return "<span id=\"fancybox-title-over\">Obrázek "+(currentIndex+1)+" z "+currentArray.length+(title.length ? " &nbsp; "+title : "")+"</span>";
              }
            }
          );
        }
      );
    }
    
    $("section.car p.details span").live
    (
      "click",
      function()
      {
        if($(this).hasClass("active"))
        {
          $(this).removeClass("active");
          $(this).parents("section.car").find("div.details").stop(true, true).slideUp(500);
          $(this).parents("section.car").find("section.gallery").stop(true, true).slideUp(500);
        }
        else
        {
          $(this).addClass("active");
          $(this).parents("section.car").find("div.details").stop(true, true).slideDown(500);
          $(this).parents("section.car").find("section.gallery").stop(true, true).slideDown(500);
        }
      }
    );
  }
);
