$(document).ready(function() { 

  //Equal heights
  $('#content').equalHeights();
  
  //Set correct link value for Tip-a-Friend!
  var encLoc = rawurlencode(window.location);
  var link = "/index.php?id=146&tipUrl=";
  var newLink = link+encLoc;
  $(".artikelBoxRechts a").attr("href",newLink).attr("target","_blank");
  $("#artikelprint").attr("href","javascript:void(0);");
    
  function rawurlencode( str ) {  
      // URL-encodes string    
      //   
      // version: 901.1411  
      // discuss at: http://phpjs.org/functions/rawurlencode  
      // +   original by: Brett Zamir  
      // *     example 1: rawurlencode('Kevin van Zonneveld!');  
      // *     returns 1: 'Kevin van Zonneveld%21'  
      // *     example 2: rawurlencode('http://kevin.vanzonneveld.net/');  
      // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'  
      // *     example 3: rawurlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');  
      // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'  
     
      var histogram = {}, tmp_arr = [];  
      var ret = str.toString();  
    
      var replacer = function(search, replace, str) {  
          var tmp_arr = [];  
          tmp_arr = str.split(search);  
          return tmp_arr.join(replace);  
      };  
    
      // The histogram is identical to the one in urldecode.  
      histogram["'"]   = '%27';  
      histogram['(']   = '%28';  
      histogram[')']   = '%29';  
      histogram['*']   = '%2A';   
      histogram['~']   = '%7E';  
      histogram['!']   = '%21';  
    
      // Begin with encodeURIComponent, which most resembles PHP's encoding functions  
      ret = encodeURIComponent(ret);  
    
      // Restore spaces, converted by encodeURIComponent which is not rawurlencode compatible  
      ret = replacer('%20', ' ', ret); // Custom replace. No regexing  
    
      for (search in histogram) {  
          replace = histogram[search];  
          ret = replacer(search, replace, ret) // Custom replace. No regexing  
      }  
    
      // Uppercase for full PHP compatibility  
      return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {  
          return "%"+m2.toUpperCase();  
      });  
    
      return ret;  
  }  


  //Set correct link values for the tags                
  $('.tx-vgetagcloud-pi1 ul li a').each(
    function( intIndex ){
      var keyword = $(this).getUrlParam('tx_vgetagcloud_pi2[keyword]');
      var cHash = $(this).getUrlParam('cHash');   
            
      var hostname = 'tag-suche/tag/';
      hostname = hostname + keyword;
      //hostname = hostname + '&cHash=';
      //hostname = hostname + cHash;               
      
      var hostname = $(this).attr('href',hostname);
    }
  );
    
  //Select text in search field and replace it
  //Set search button disabled as log as textbox value is "Suchwort..."
  if ($("#topSearchBox").val() == "Suchwort...")
    $("#topSearchButton").attr("disabled","disabled");
  $("#topSearchBox").focus(function(){
    // Select input field contents
    if (this.value == "Suchwort...") {
      this.value = "";
      $("#topSearchButton").removeAttr('disabled');
    }
  }).blur(function() {
    if (this.value == "") { 
      this.value = "Suchwort...";
      $("#topSearchButton").attr("disabled","disabled");
    } 
});

  //Check if the search term is shorter than 3 characters
  $("#topSearchButton").click(function(){
    if ($("#topSearchBox").val().length < 3) {
      alert("Bitte geben Sie einen Suchbegriff mit mindestens 3 Buchstaben ein");
      return false;
    } else {
      return true;
    }

  });
  
  //$(".tx-indexedsearch-res div:nth-child(2n+1)").css("background" , "#eee");
  
  //Disable link on active page for search results
  $(".browselist-currentPage a").removeAttr("href");
  
  //Wrap path link, leave Category alone and remove link attribute
  var repArr = $(".tx-indexedsearch-path a");
  $.each(repArr, function() {
    $(this).removeAttr("href");
    $(this).text($(this).text().replace("/Themen/","").replace("/Artikel","").replace("/DOCMA-Archiv/",""));    
   });
   
   //$(".tags").text($(".tags").text().replace(",", ", "));   
    
   
  $(".powermail_submit_uid222").click(function() {

    $(".tx_powermail_pi1_fieldwrap_html").each(
      function( intIndex ){
       if($(this).is(':hidden')){
         $(this).find("input").removeAttr("required");
         $(this).find("textarea").removeAttr("required");
       } 
    });
  });

  
  $("span[id^='asmspan_']").each(function(index) {  
    $("#wrapper").css("margin-top","0");
    $("body").css("overflow-x","auto");  
  });
   
});
