// -*- encoding: utf-8 -*-

function openPDFWin(url) {
  // Workaround für Base-HREF-Problem in IE
  var base_href = "";

  if (document.getElementsByTagName("base").length > 0 && !url.match(/^http:\/\//))
    base_href = document.getElementsByTagName("base")[0].getAttribute("href");
  window.open(url, '', 'resizable');
}

function initializeTooltips() {
  $(".image p.caption:not(:empty)").each(function () {
    var img = $(this).parent(".image").get(0);
    if (img) $(img).tooltip({ bodyHandler: function() { return $(this).find("p.caption").get(0).innerHTML }})
  });
}

function initializePDFLinks() {
  // In IE 8 klappt das Öffnen von PDFs in Popups nicht immer, daher target="_blank" als Workaround
  if ($.browser.msie && $.browser.version == "8.0") {
    $(".pdf a").attr("target", "_blank");
    return;
  }

  $(".pdf a").unbind("click.pdflink").bind("click.pdflink", function(e) {
    openPDFWin($(this).context.href);
    return false
  });
}

function initializeSubmenus() {
  $(".has-submenu.inactive .submenu")
    .hide()
    .bind("mouseleave", function() { startHideSubmenuTimer($(this)) })
    .bind("mouseenter", function() { cancelHideSubmenuTimer($(this)) });
  $(".has-submenu.inactive")
    .bind("mouseenter", function() { cancelHideSubmenuTimer($(this).find(".submenu")); $(this).find(".submenu").show(submenuAnimSpeed) })
    .bind("mouseleave", function() { startHideSubmenuTimer($(this).find(".submenu")) });
}

var submenuTimeout   = 250;
var submenuAnimSpeed = "fast";

function startHideSubmenuTimer(submenu) {
  if (submenu.data("hideTimer")) return;
  submenu.data("hideTimer", window.setTimeout(function() { submenu.hide(submenuAnimSpeed); submenu.data("hideTimer", null) }, submenuTimeout));
}

function cancelHideSubmenuTimer(submenu) {
  window.clearTimeout(submenu.data("hideTimer"));
  submenu.data("hideTimer", null);
}


function onShowLightbox(h) {
  var top = (document.documentElement.scrollTop || document.body.scrollTop);
  h.w.css({top: (top + 50) + "px"}).show();
}

function onLoadLightbox(h) {
  initializeTooltips();
  initializePDFLinks();
}

function initializeLightbox() {
  $("#lightbox").jqm({ ajax: "@href",
                       ajaxText: "<div class='loading'><img src='/fileadmin/templates/images/loading.gif' width='47' height='39' alt='Lädt ...' title=''></div>",
                       modal: true,
                       target: "#lightboxcontent",
                       onShow: onShowLightbox,
                       onLoad: onLoadLightbox
                     });
  $('#lightbox').jqmAddTrigger(".event a, .shortevent a"); 
}

// Macht die Smallboxes (rechte Spalte Startseite zB) komplett klickbar (statt nur den "mehr Infos"-Link)
function makeSmallboxesClickable() {
  $(".smallboxes .event").css({ "cursor": "pointer" }).bind("click", function() {
    $(this).find("a[href]").trigger("click");
  }).hover(function() { $(this).find("a[href]").css({ "text-decoration": "underline" }) },
           function() { $(this).find("a[href]").css({ "text-decoration": "none" }) });
  $(".smallboxes .event a[href]").css({ "text-decoration": "none" });

}

$(document).ready(function() {
  initializePDFLinks();
  initializeLightbox();
  initializeTooltips();
  initializeSubmenus();
  makeSmallboxesClickable();
});
