function utf8_decode ( str_data ) {
  // Converts a UTF-8 encoded string to ISO-8859-1
  //
  // version: 905.3122
  // discuss at: http://phpjs.org/functions/utf8_decode
  // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
  // +      input by: Aman Gupta
  // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // +   improved by: Norman "zEh" Fuchs
  // +   bugfixed by: hitwork
  // +   bugfixed by: Onno Marsman
  // +      input by: Brett Zamir (http://brett-zamir.me)
  // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // *     example 1: utf8_decode('Kevin van Zonneveld');
  // *     returns 1: 'Kevin van Zonneveld'
  var tmp_arr = [], i = 0, ac = 0, c1 = 0, c2 = 0, c3 = 0;
    
  str_data += '';
    
  while ( i < str_data.length ) {
    c1 = str_data.charCodeAt(i);
    if (c1 < 128) {
      tmp_arr[ac++] = String.fromCharCode(c1);
      i++;
    } else if ((c1 > 191) && (c1 < 224)) {
      c2 = str_data.charCodeAt(i+1);
      tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
      i += 2;
    } else {
      c2 = str_data.charCodeAt(i+1);
      c3 = str_data.charCodeAt(i+2);
      tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
      i += 3;
    }
  }

  return tmp_arr.join('');
}

function handleSortOpts() {
  if(jQuery("#sortops select.sortby").val() == "score") {
    jQuery("#sortops select.order option:eq(0)").attr("selected", "selected");
    jQuery("#sortops select.order").attr("disabled","disabled");
  } else {
    jQuery("#sortops select.order").attr("disabled","");
  }
}
		
function rawurldecode( str ) {
  // version: 905.3122
  var histogram = {}, ret = str.toString(), unicodeStr='', hexEscStr='';

  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 urlencode.
  histogram["'"]   = '%27';
  histogram['(']   = '%28';
  histogram[')']   = '%29';
  histogram['*']   = '%2A';
  histogram['~']   = '%7E';
  histogram['!']   = '%21';


  for (unicodeStr in histogram) {
    hexEscStr = histogram[unicodeStr]; // Switch order when decoding
    ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing
  }

  // End with decodeURIComponent, which most resembles PHP's encoding functions
  ret = ret.replace(/%([a-fA-F][0-9a-fA-F])/g, function (all, hex) {
    return String.fromCharCode('0x'+hex);
  }); // These Latin-B have the same values in Unicode, so we can convert them like this
  ret = decodeURIComponent(ret);

  return ret;
}

jQuery(document).ready(function() {
  //jQuery(".invisible-on-js").css("visibility","hidden");
	
  var IE7 = jQuery.browser.msie && jQuery.browser.version == "7.0";
  var IE8 = jQuery.browser.msie && jQuery.browser.version >= "8.0";
  var IE6 = jQuery.browser.msie && jQuery.browser.version <= "6.0";
		
  // Prep menu
  jQuery('.sf-menu').superfish({
    delay		: 200,
    animation	: {
      opacity:'show'
    },
    speed		: 'fast',
    autoArrows	: true,
    dropShadows : false,
    disableHI	: false
  });
	
  // Fix IE's crazy behaviour for making a tags block elements
  if(IE6 || IE7) {
    jQuery('a').css('display','inline');
  }
	
  // Fix for text shining through sub menu
  if(IE6 ||IE7 || IE8) {
    jQuery('.sf-menu').mouseover(function(){
      jQuery('.sf-menu').css('z-index','1');
      jQuery(".wave-1").css('z-index','0');
      jQuery(".wave-2").css('z-index','0');
    });
  }
	
  // Pop tooltips
  jQuery( 'a.Tips2' ).tooltip({
    fade: 500,
    track: true,
    showURL: false
  });

  // Events
  jQuery("#switch-1").click(function ()
  {
    jQuery("object").css("visibility","hidden");/* anti-flicker */
    jQuery("embed").css("visibility","hidden");/* anti-flicker */
    jQuery("html").css('overflow-x','hidden');

    jQuery(".wave-1").hide(IE7 || IE8 ? "slide" : "drop",
    {
      direction: "right"
    }, 375);
    jQuery(".wave-2").show(IE7 || IE8 ? "slide" : "drop",
    {
      direction: "left"
    }, 375, function() {
      jQuery("object").css("visibility","visible");/* anti-flicker */
      jQuery("embed").css("visibility","visible");/* anti-flicker */
      jQuery("html").css('overflow-x','auto');
    });

  });
  jQuery("#switch-2").click(function ()
  {
    jQuery("object").css("visibility","hidden"); /* anti-flicker */
    jQuery("embed").css("visibility","hidden");/* anti-flicker */
    jQuery("html").css('overflow-x','hidden');

    jQuery(".wave-2").hide(IE7 || IE8 ? "slide" : "drop",
    {
      direction: "left"
    }, 375);
    jQuery(".wave-1").show(IE7 || IE8 ? "slide" : "drop",
    {
      direction: "right"
    }, 375, function()
    {
      jQuery("object").css("visibility","visible");/* anti-flicker */
      jQuery("embed").css("visibility","visible");/* anti-flicker */
      jQuery("html").css('overflow-x','auto');
    });
  });
  jQuery("#s").blur(function () {
    if(jQuery(this).val().length < 3)
    {
      jQuery(this).val('kompetenz +qualität');
    }
  });
  jQuery("#s").click(function () {
    if(jQuery(this).val() == 'kompetenz +qualität')
    {
      jQuery(this).val('');
    }
  });
  jQuery("a.greyboxtest").fancybox({
    'imageScale': false,
    'hideOnContentClick': true,
    'zoomOpacity'			: false,
    'overlayShow'			: false,
    'frameWidth'			: 450,
    'frameHeight'			: 300
  });
  jQuery("a.greybox").fancybox({
    'imageScale': true,
    'hideOnContentClick': true,
    'zoomOpacity'			: false,
    'overlayShow'			: false,
    'zoomSpeedIn'			: 250,
    'zoomSpeedOut'			: 250
  });
  jQuery("div.boxrow a").fancybox({
    'imageScale': true,
    'hideOnContentClick': true,
    'zoomOpacity'			: false,
    'overlayShow'			: false,
    'zoomSpeedIn'			: 250,
    'zoomSpeedOut'			: 250
  });

  if(jQuery(document).getUrlParam("s")!==null)
  {
    var s = jQuery(document).getUrlParam("s").replace(/\+/g, " ");
    s = utf8_decode(rawurldecode(s));
    jQuery("#s").val(s);
    //jQuery("#ssortopts").val(s);
  }
	
  if(jQuery("#sortops").length !== 0)
  {
    handleSortOpts();
    jQuery("#sortops select.sortby").change(handleSortOpts);
  }
	
  // Inits
  /*jQuery("#mainCol img").lazyload({
    placeholder : "/wp-content/themes/rs09/images/loading.gif"
  });*/
  jQuery('.roundcorner').corner();
  jQuery("#s").blur();
  jQuery(".wave-2").hide();
  jQuery(".wave-1").show();
  jQuery( "#midCol li.cat-item a:first-child" ).each(function(){
    jQuery( this ).after('&nbsp;<a href="'+jQuery( this ).attr('href')+'feed/"><img src="/wp-content/themes/rs09/images/category-rss.jpg" alt="rss" /></a>');
  });
  jQuery(".visible-on-js").css("visibility","visible");
});