Shadowbox.init({
  handleOversize: "resize",
  troubleElements: ["select", "object", "embed"]
});

$(function() {
  iPhone = checkiPhone();
  iPad = checkiPad();

  // iDevices shadowbox fix
  if (iPhone || iPad) {
    $('.content a').click(function(e) {
      var relTag = $(this).attr('rel');

      if ((/shadowbox/gi.test(relTag)) || (/lightbox/gi.test(relTag))) {
        var wScroll = $(window).scrollTop();
        var wScrollCSS = wScroll+'px';
        $('#sb-container').css('top', wScrollCSS);
      }
    });
  }

  // menu line animation
  if ($('.menu #selected').length != 0) {
    var $el, leftPos, newWidth, $mainNav = $(".menu");

    $mainNav.append("<div id='magic-line'></div>");
    var $magicLine = $("#magic-line");

    $magicLine
        .width($("#selected").width())
        .css("left", $("#selected a").position().left)
        .data("origLeft", $magicLine.position().left)
        .data("origWidth", $magicLine.width());

    $(".menu li a").hover(function() {
        $el = $(this);
        leftPos = $el.position().left;
        newWidth = $el.parent().width();
        $magicLine.stop().animate({
            left: leftPos,
            width: newWidth
        });
    }, function() {
        $magicLine.stop().animate({
            left: $magicLine.data("origLeft"),
            width: $magicLine.data("origWidth")
        });
    });
  }

  // icon after <h1>
  $('h1, h2').append("<img id='title-icon' src='/img/title-icon.png' />");

  // thumbs hover effekt
  /*
  $(".thumbBox").hover(function() {
    fullname = $(this).children("a").attr('ref');
    $(this).append("<div id='fullname'>"+fullname+"</div>");
  },
  function() {$("#fullname").remove();})

  $(".thumbBox").click(function() {
    $("#fullname").remove();
  })
  */

  // filter box
  /*
  $("#filterBox").click(function() {
    $(this).hide();
    $("#filterBox_full").css({'display': 'block'});
  })

  // hide filter box if clicked outside
  $("body").click(function(e) {
    if ($(e.target).attr('id') != "filterBox_full" && $(e.target).attr('id') != "filterBox") {
      $("#filterBox_full").hide();
      $("#filterBox").show();
    }
  });
  */

  $(".videoPlayContainer").hover(function(e) {
    position = $(this).position();
    position.left += ($(this).width() / 2) - ($("#playButton").width() / 2);
    position.top += ($(this).height() / 2) - ($("#playButton").height() / 2);

    $("#playButton").css({
      left: position.left+"px",
      top: position.top+"px"
    })
    .attr('rel', $(e.target).attr('rel'))
    .show();
  },
  function(e) {
    if ($(e.relatedTarget).attr('id') != "playButton") {
      $("#playButton").hide();
    }
  })

  $(".videoPlayContainer, #playButton").click(function() {
    $("#showreel").css({'display' : 'block'});
    $f("showreel").play(this.getAttribute('rel'));


    var targetOffset = $("#showreel").offset().top;
    $('html,body').animate({scrollTop: targetOffset}, 1000);
  })

  try {
    flowplayer("showreel", "/flowplayer/flowplayer.commercial-3.2.5.swf", {
      key: '#$718bb9b7e2b23c631b7',
      canvas: {backgroundColor: "#cccccc"},
      play: {replayLabel: "Wiederholen"},
      plugins: {
        controls: {
          url: '/flowplayer/flowplayer.controls-3.2.3.swf',

          buttonColor: 'rgba(0, 0, 0, 0.9)',
          buttonOverColor: '#000000',
          backgroundColor: '#D7D7D7',
          backgroundGradient: 'medium',
          sliderColor: '#FFFFFF',

          sliderBorder: '1px solid #808080',
          volumeSliderColor: '#FFFFFF',
          volumeBorder: '1px solid #808080',

          timeFontSize: 9,
          timeColor: '#000000',
          durationColor: '#535353',
          height: 20
        }
      },
      clip: {
        autoPlay: false
      }
    }).ipad();
  }
  catch(e) {}
  
  $(".imageBox img[title]").tooltip({
    position: "top center",
    offset: [85, 0],
    opacity: 0.8
  });
})

function checkiPhone() {
  var iPhone=((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)));
  return iPhone;
}

function checkiPad() {
  var iPad=navigator.userAgent.match(/iPad/i);
  return iPad;
}
