if(window.console === undefined) {
  var console = { log: function() {}};
}

if(window.informer === undefined) {
  var informer = {};
}

(function($) {
  $.fn.videoStart = function() {
    var createVideo = function(element) {
      var isIDevice = navigator.userAgent.match(/i(Pad|Phone|Pod)/i);
      var poster = element.attr('src');
      var flv = element.attr('data-flv');
      var mp4 = element.attr('data-mp4');
      var ogv = element.attr('data-ogv');
      var a = 'autoplay controls autobuffer';
      a += ' id="' + element.attr('id') + '"';
      a += ' style="' + element.attr('style') + '"';
      a += ' width="640px"';
      a += ' width="360px"';
      a += ' poster="' + element.attr('src') + '"';
      var v = '';
      v += '<video ' + a + '>'; 
      if(mp4) {
        v += '<source src="' + mp4 + '" type="video/mp4">';
      }
      if(ogv) {
        v += '<source src="' + ogv + '" type="video/ogg">';
      }
      if(!isIDevice && flv) {
        v += '<object width="640" height="360" type="application/x-shockwave-flash" data="/assets/player_flv.swf">';
        v += '<param name="movie" value="/assets/player_flv.swf" />';
        v += '<param name="FlashVars" value="flv='+flv+'&width=640&height=360&autoplay=1&autoload=1&startimage='+poster+'" />';
        v += '</object>';
      }
      v += '</video>';
      element.replaceWith(v);
      if(isIDevice) {
        v = $("#"+element.attr('id'));
        fakeClick(function() {
           v.get(0).play();
        });
      }
    };

    var removeVideo = function(element) {
      a = '';
      a += ' id="' + element.attr('id') + '"';
      a += ' style="' + element.attr('style') + '"';
      a += ' src="' + element.attr('poster') + '"';
      element.replaceWith('<img ' + a + '>');
    };

    $(this).fancybox({
      padding: 0,
      scrolling: 'no',
      showNavArrows: false,
      hideOnContentClick: false,
      hideOnOverlayClick: true,
      autoDimensions: false,
      width: 640,
      height: 360,
      autoScale: false,
      titleShow: false,
      onComplete: function(x) {
        createVideo($($(x).attr("href")));
      },
      onClosed: function(x) {
        removeVideo($($(x).attr("href")));
      }
    });
  };
})(jQuery);

var makeEpaper = function() {
  var embeds = $("#epaper");
  $("#show-epaper a").fancybox({title: false});

  function selectEpaper(name) {
     $("#epaper #epaper-inner").html(embeds.attr("data-"+name));
  }

  $("#epaper select").change(function() {
    selectEpaper($(this).val());
  });
  selectEpaper("bredeney");
};

var initLocalizationSelector = function() {
  var l = $("#localization-selector");

  var shown = false;

  var li = l.find("li");

  var first = li.first();

  function showAll() {
    li.fadeIn();
    l.addClass("expanded");
  }

  function hide() {
    li.hide();
    first.show();
    l.removeClass("expanded");
  }

  function process() {
    if(shown) {
      hide();
    } else  {
      showAll();
    }
    shown = !shown;
  }

  first.click(process);

  l.mouseleave(hide);
};

var fakeClick = function(fn) {
  var a = $("<a href='#'>");
  a.click(function(e) {
    e.preventDefault();
    fn();
  });
  $("body").append(a);
  var evt = document.createEvent("MouseEvents");
  evt.initMouseEvent("click",true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  a.get(0).dispatchEvent(evt);
  a.remove();
};

informer.startSocialize = function() {
  var socialize = $(".socialize");
  //socialize.find(".others").hide().before($("<a>[mehr]</a>").click(function() { $(this).hide().parent().find(".others").fadeIn(); }));
};


$(function() {
  informer.startSocialize();

  $(".video-start").videoStart();

  $("#thumbnail-nav a.column-b").fancybox({
    padding: 0,
    showNavArrows: false,
    centerOnScroll: true
  });

  $(".youtube-start").click(function(e) {
    e.preventDefault();
    $.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'		: {
        'wmode'		: 'transparent',
        'allowfullscreen'	: 'true'
      }
    });
    return false;
  });

  $("#content .portraits a").fancybox();

  $("#release-selector a").fancybox({});

  initLocalizationSelector();

  $("a[rel=external], area[rel=external]").attr("target","_blank");
  makeEpaper();
});
