(function($) {

  $(function() {
    
    // Fix some column issues
    (function() {
    
      var $headline = $("article.latest h1");
      var webkit = !!$("article").css("-webkit-column-count");
      
      $("article.latest img").each(function() {
      
        var $this = $(this);
        var $parent = $this.parent();
        
        if ($this.css("float") != "none") {
        
          // Move floating to the parent containing element
          $parent.css({"float": $this.css("float"), margin: 0});
          // Align headline to the image
          $headline.css("margin-left", $this.width());
          
          if (webkit) {
          
            $parent.css("margin-top", $headline.height() * 1.5 * -1);
            $headline.css("display", "inline-block");
          } else {
          
            $parent.css("margin-top", $headline.height() / 2 * -1);
          }
        }
      });
    })();
  
    // Columnize via jQuery if necessary
    (function() {
  
      var columnProperties = ["-moz-column-count", "-webkit-column-count", "column-count"];
      var cssColumns = false;
      var $contentBoxContent = $(".contentbox .content").not($(".container .contentbox .content"));
      
      for (var i = 0; i < columnProperties.length; ++i) {
      
        if ($contentBoxContent.css(columnProperties[i])) {
        
          cssColumns = true;
          break;
        }
      }
            
      if (!cssColumns) {
    
        $contentBoxContent.columnize({columns: 2});
        /*@cc_on
        if ($contentBoxContent.length > 0) {
          // Remove spurious tag garbage due to a conflict between iepp and columnizer
          $contentBoxContent.html($contentBoxContent.html().replace(/<:[^>]+>/g, ""));
        }
        @*/
      }
    })();
    
    // Add jQuery date picker if necessary
    (function() {
    
      /* German initialisation for the jQuery UI date picker plugin. */
      /* Written by Milian Wolff (mail@milianw.de). */
      $.datepicker.regional['de'] = {
        closeText: 'Schließen',
        prevText: '&#x3c;zurück',
        nextText: 'Vor&#x3e;',
        currentText: 'Heute',
        monthNames: ['Januar','Februar','März','April','Mai','Juni',
        'Juli','August','September','Oktober','November','Dezember'],
        monthNamesShort: ['Jan','Feb','Mär','Apr','Mai','Jun',
        'Jul','Aug','Sep','Okt','Nov','Dez'],
        dayNames: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
        dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'],
        dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
        weekHeader: 'Wo',
        dateFormat: 'dd.mm.yy',
        firstDay: 1,
        isRTL: false,
        showMonthAfterYear: false,
        yearSuffix: ''};
      $.datepicker.setDefaults($.datepicker.regional['de']);
    
      $("input[type=date]").each(function() {
      
        var $input = $(this);
        
        if ($input.attr("type") != "date") {
        
          $input.datepicker();
        }
      });
    })();
    
    // Prepare location info display
    (function() {
    
      var $locationInfos = $("li .location.info").hide();
      var $dialog = $("<section/>", {className: "location info"}).dialog({
        autoOpen: false,
        resizable: false,
        title: "Details zum ausgewählten Standort"
      });
      
      $(".city > a").click(function() {
      
        $dialog.html($(this).next(".location.info").html());
        $dialog.dialog("open");
        
        return false;
      });
    })();
    
    // Add odd/even information to search results
    (function() {
    
      $(".tx-indexedsearch .results li:odd").addClass("even");
      $(".tx-indexedsearch .results li:even").addClass("odd");
    })();
    
    // Add "Print page" action if possible
    (function() {
    
      if (window.print) {
      
        $("footer .feed").before($("<button/>", {
              className: "print",
              text: "Seite drucken",
              title: "Aktuelle Seite ausdrucken",
              click: function() { window.print() }
        }));
      }
    })();
  });
})(jQuery);
