var slideIndex=0;
var presentation = {
  init: function() {
    presentation.updateGeometry();
    $(document).keyup(function(ev) {
      var oldSlideIndex = slideIndex;
      presentation.updateGeometry();
      var marginLeft = parseInt($("#presentation").css("marginLeft"));
      if (ev.keyCode==37) slideIndex = Math.max(0,slideIndex-1);
      if (ev.keyCode==39) slideIndex = Math.min($(".slide").length-1, slideIndex+1);
      // if (slideIndex!=oldSlideIndex)
        $("#presentation").animate({marginLeft: -slideIndex*$("body").width()});
      return false;
    });
  },
  updateGeometry: function() {
    var bodyWidth = $(window).width(), bodyHeight = $(window).height();
    $("body").css("width", bodyWidth);
    $("body").css("height", bodyHeight);
    $(document).css("height", bodyHeight);
    $("#presentation").css("width", $(".slide").length * bodyWidth);
    $("#presentation").css("height", bodyHeight);
    $(".slide").css("width", bodyWidth+1);
    $(".slide").css("height", bodyHeight);
    // $(".slide.flourished img").css("height", bodyHeight); // dnno why height:100% doesn't work on FF

    $(".slide").each(function() {
      var top = 60;
      $(this).find("p").each(function(i) {
        $(this).css("top", top);
        top+=$(this).height()+60;
      });
    });
  }
};

function log() {
  if (typeof(console)!="undefined") console.log.apply(console, arguments);
}

$.fn.flourish = function() {
  // if ($(this).data("slide").explicitHTML) return;
  $(this).addClass("flourished");
  var theme = new Theme();
  var top = 60;
  $(this).find("p").each(function(i) {
    $(this).css({ position: "absolute", left: 20, top: top, background: theme.background, color: theme.color }).find("a").css("color", theme.color);
    top+=$(this).height()+30;
  });
  return $(this);
}

function Theme() {
  var components = [255,255,255];
  for (var i=0; i<2; i++)
    components[Math.floor(3*Math.random())] = Math.floor(128*Math.random());
  this.background = Theme.composeColor(components, 0.95);

  var colorComponents = [];
  for (var i=0; i<3; i++)
    colorComponents[i] = components[i]==255 ? 64 : 0;
  this.color = Theme.composeColor(colorComponents);
}

Theme.composeColor = function(components, alpha) {
  if (!alpha) alpha = 1;
  return "rgba("+components.join(",")+","+alpha+")";
}

