var more_de = '(Mehr Informationen)';
var less_de = '(Weniger Informationen)';
var more_en = '(More Information)';
var less_en = '(Less Information)';

$(function() {
  $("a[href$=.pdf]").click(function(e) { openPDFWin($(this).attr("href")); e.preventDefault(); });
  $("a[href^=http://]:not(a[href^=http://" + location. host + "])").attr("target", "_blank");
  $("a[href*=wp-content]").addClass("fancybox");
  $("#registration_link").click(function() { $("#notice").toggle(); });

  $(".events .description p:not(:first-child), .events .description ul, body#index #content #main div p:not(:first-child)").hide();
  if ($(".events .description p:not(:first-child)").size() > 0) {
    $(".events .description p:last-child").after(function() { return '<div class="more">' + eval("more_" + lang()) + '</div>'; });
  }

  $(".more").live("click", function() {
    $($(this).parent()).find("p:not(:first-child), .description ul").slideToggle("slow");
    $(this).html(($(this).html() == eval("more_" + lang())) ? eval("less_" + lang()) : eval("more_" + lang()));
  });

  $("a.fancybox").fancybox({
    'transitionIn' : 'none',
    'transitionOut': 'none',
    'titlePosition': 'over',
    'titleFormat'  : function(title, currentArray, currentIndex, currentOpts) {
        return '<span id="fancybox-title-over"><strong>Foto ' + (currentIndex + 1) + '/' + currentArray.length + '</strong>' + ((title == undefined) ? "" : ": " + title) + '</span>';
      }
    }
  );
  
  $("a.video.Vimeo").click(function() {
     $.fancybox({
      'padding'      : 0,
      'autoScale'    : false,
      'transitionIn' : 'none',
      'transitionOut': 'none',
      'title'        : this.title,
      'width'        : 680,
      'height'       : 495,
      'href'         : this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1') + '&autoplay=1&fullscreen=1',
      'type'         : 'swf',
      'swf'          : {'allowfullscreen':'true'}
      });
     return false;
  });
  
  $("a.video.YouTube").click(function() {
    $.fancybox({
      'padding'             : 0,
      'autoScale'           : false,
      'transitionIn'        : 'none',
      'transitionOut'       : 'none',
      'title'               : this.title,
      'width'               : 680,
      'height'              : 495,
      'href'                : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
      'type'                : 'swf',
      'swf'                 : { 'allowfullscreen':'true' }
    });
    return false;
  });
  
  $("a.video.Lokal").click(function() {
      $.fancybox({
         'padding'      : 0,
      'autoScale'    : false,
      'transitionIn' : 'none',
      'transitionOut': 'none',
      'title'        : this.title,
      'width'        : 680,
      'height'       : 495,
         'type'    : "swf",
         'href'    : "/_swf/player.swf?file=" + $(this).attr("href") + "&skin=/_swf/slim.zip&autostart=1&controlbar.position=over",
         'swf'   : { "wmode": "transparent", "allowfullscreen": "true" }
       });
      return false;
    });
  
  if (!Modernizr.input.placeholder) {
    $("input[placeholder]").each(function() {
      if ($(this).val() == '') { $(this).val($(this).attr("placeholder")); }
    });
    $("input[placeholder]").click(function() {
      if ($(this).val() == $(this).attr("placeholder")) { $(this).select(); }
    });
  }
  
  if (!Modernizr.boxshadow) boxshadowWorkaround();
});

function openPDFWin(url) {
  window.open(url, '', 'resizable');
}

function lang() {
  return ($("body").hasClass("de")) ? "de" : "en";
}

function boxshadowWorkaround() {
  var frame = $("#frame"), right = $("<div class='boxshadow-workaround right'></div>"), bottom = $("<div class='boxshadow-workaround bottom'></div>");
  right.appendTo("body").css({position: "absolute"});
  bottom.appendTo("body").css({position: "absolute"});
  
  var clonePosition = function() {
    var offset = frame.offset();
    right.css({left: offset.left + frame.outerWidth(), top: offset.top + 5}).height(frame.height());
    bottom.css({left: offset.left + 5, top: offset.top + frame.outerHeight()}).width(frame.width());
  };
  
  clonePosition();
  $(window).bind("load resize", clonePosition);
  setInterval(clonePosition, 250); // Make sure we react to any changes of the size of the frame (with some delay, though)
}
