jQuery(function ($) {
  
  // archiv tabs
  $('#archiv').tabs();
  
  // select all
  $('#perma-link').focus(function () {
    $(this).select();
  });
  
  // search focus
  $('#s').focus(function () {
    var text = $(this).val();
    $(this).val('');
    $(this).blur(function() {
      $(this).val(text);
    });
  });
  
  // externi odkazy
  $("a[href^=http]").each(function() {
    if(this.href.indexOf(location.hostname) == -1) {
      $(this).click(function(){window.open(this.href);return false;});
    }
  })
  
});

// archiv tabs
$.fn.tabs = function () {
  // wrapper
  $(this).prepend('<div id="tabs"></div>');
  
  // odkazy pro taby
  var legend = [];
  var tabs = $('#tabs', $(this));
  
  $(this).find('legend').each(function (i) {
    var name = $(this).text();
    var link = $(this).parent('fieldset').attr('id');
    tabs.append('<a href="#' + link + '">' + name + '</a>')
  });
  
  // upravy
  var fieldset = $(this).children('fieldset');
  var tabsAhref = $('#tabs a', $(this));
  
  tabs.children('a:first').addClass('selected');
  fieldset.hide().filter(':first').show();
  fieldset.css({'border-top': '0'})
  $('legend', $(this)).hide();
  
  // preklikavani tabu
  tabsAhref.click(function () {
    fieldset.hide().filter(this.hash).show();
    tabsAhref.removeClass('selected');
    $(this).addClass('selected');
    return false;
  });
  
  return $(this);
}