$(document).ready(function() {

  // Search in header
  var defaultvalue = $(".search .input-text").attr("value");
  $(".search .input-text").focus(function() {
    if($(this).attr("value")==defaultvalue) {
      $(this).attr("value","")
    }
    $(this).addClass("writing");
  });
  
  $(".search .input-text").blur(function() {
    if($(this).attr("value")=="") {
      $(this).attr("value",defaultvalue);
      $(this).removeClass("writing");
    }
  });

  // Search in detail page
  var defaultsearchvalue = $(".bottomsearch .search-text").attr("value");
  $(".bottomsearch .search-text").focus(function() {
    if($(this).attr("value")==defaultsearchvalue) {
      $(this).attr("value","")
    }
    $(this).addClass("writing");
  });

  $(".bottomsearch .search-text").blur(function() {
    if($(this).attr("value")=="") {
      $(this).attr("value",defaultsearchvalue);
      $(this).removeClass("writing");
    }
  });

  // Input in detail
  var defaultvalue2 = $(".pricepanel .text").attr("value");
  $(".pricepanel .text").focus(function() {
    if($(this).attr("value")==defaultvalue2) {
      $(this).attr("value","")
    }
    $(this).addClass("writing");
  });

  $(".pricepanel .text").blur(function() {
    if($(this).attr("value")=="") {
      $(this).attr("value",defaultvalue2);
      $(this).removeClass("writing");
    }
  });
  
  $(".search").submit(function() {
    if($(".search .input-text").attr("value")==defaultvalue||$(".search .input-text").attr("value")=="") {
      return false;
    }
  });
  
  // Panels in basket
  $(".w-info").prepend('<p class="infobutton"><a href="#" class="ir" title="Více informací">&uarr;<span></span></a></p>');
  $(".overview .panel .in").append('<p class="close"><a href="#">Zavřít</a></p>');
  $(".overview .panel").hide();
  
  $(".infobutton a").click(function() {
    if(!$(this).hasClass("open")) {
      $(this).addClass("open");
      $(this).parents("table").siblings(".panel").slideDown("slow");
      $(this).parents("li").next().addClass("under");
    } else {
      $(this).removeClass("open");
      $(this).parents("li").next().removeClass("under");
      $(this).parents("table").siblings(".panel").slideUp("slow");
    }
    return false;
  });
  
  $(".panel .close").click(function() {
    $(this).parents(".panel").siblings("table").find(".infobutton a").removeClass("open");
      $(this).parents("li").next().removeClass("under");
    $(this).parents(".panel").slideUp("slow");
    return false;
  });
  
  // Basket - step 2
  $("#fakturacniudaje").hide();
  $("#basket").hide();
  $(".moreinfo a.ir").click(function () {
    if($($(this).attr("href")).css("display")=="none") {
      $($(this).attr("href")).slideDown("slow");
    } else {
      $($(this).attr("href")).slideUp("slow");
    }
    return false;
  });

  // Basket - step 3
  $("a[href=#basket-overview]").addClass("open");

  // Tree parts hiding
  $(".tree ul ul").before('<span class="ir switcher" title="Rozbalit subkategorie">+<span></span></span>')
  
  $(".tree .switcher").click(function(){

    if(!$(this).siblings("ul").hasClass("open")) {
      $(this).siblings("ul").slideDown().addClass("open");
      $(this).html("-<span></span>").addClass("open");
    } else {
      $(this).siblings("ul").slideUp().removeClass("open");
      $(this).html("+<span></span>").removeClass("open");
    };
    
    return false;
  });



});


// Strict doesn't know _blank, so we must use rel="external" in anchors ;-)
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;
