window.addEvent('domready', function() {
  // $$('#content .group').each(function(group) {
  //     var angle = 10, alsoAngle = -4;
  //     group.getElements('img').each(function(img, index, arr) {
  //       var rotate = -angle + alsoAngle + ((angle * 2 / arr.length) * (index+1));
  //       if (arr.length == 1) var rotate = ((Math.random() - 0.5) * angle) + alsoAngle;
  //       var indent = (Math.cos((rotate + 180) * Math.PI / 180) + 1) * 800;//1000;
  //       var transform = "rotate("+rotate+"deg) translateY("+Math.round(indent)+"px)";
  //       img.style.webkitTransform = transform;
  //       img.style.MozTransform = transform;
  //       img.style.OTransform = transform;
  //       img.style.transform = transform;
  //     });
  //   });
  
  var emptyFunction = function(){};
  var left = $('button-left') ? function() { window.location.href = $('button-left').href; return true; } : emptyFunction;
  var right = $('button-right') ? function() { window.location.href = $('button-right').href; return true; } : emptyFunction;
  window.addEvent('keydown', function(evt) {
    if (evt.key == 'left') left();
    else if (evt.key == 'right') right();
    else return true;
    return false;
  });
  
  // handle multitouch gesture events (maybe only works on iPhones? Hope becomes standard!)
  var content = $('content');
  if (content.getElement('.display.image')) {
    var startX = 0, startY = 0, lastX = 0, lastY = 0, buffer = 180, going = false, moveBuff = 50, moving = false;
    var trans = function(x) {
      var transform = "translateZ(0) translateX("+x+"px)";
      content.style.webkitTransform = transform;
      content.style.MozTransform = transform;
      content.style.OTransform = transform;
      content.style.transform = transform;
      return true;
    };
    
    window.trans = trans; // for debugging access
    
    content.ontouchstart = function(evt) {
      lastX = startX = evt.touches[0].pageX;
      lastY = startY = evt.touches[0].pageY;
    };
    
    content.ontouchmove = function(evt) {
      if (going) return;
      var pos = evt.touches[0].pageX;
      lastX = evt.touches[0].pageX; lastY = evt.touches[0].pageY;
      
      var move = pos - startX;
      if (moving || move > moveBuff || move < 0-moveBuff) { moving = true; trans(move) };
      
      if (startX < pos - buffer) { left() && trans(+500) && (going = true); }
      else if (startX > pos + buffer) { right() && trans(-500) && (going = true); }
      else return true;
      if (evt.targetTouches.length == 1) evt.preventDefault();
    };
    
    var clicky = function() {
      if (content.hasClass('zoom')) {
        content.removeClass('zoom');
        content.style.height = window.getSize().y + 'px';
      } else {
        content.addClass('zoom');
        content.style.height = '';
      }
    };
    content.onclick = clicky;
    
    window.onresize = function() { content.style.height = window.getSize().y + 'px'; }
    
    var clickBox = 10
    content.ontouchend = function(evt) {
      if (Math.max(startY - lastY, lastY - startY) < clickBox
      && Math.max(startX - lastX, lastX - startX) < clickBox) clicky();
      if (!going) trans(0);
      moving = false;
    };
  }
});

window.onload = function() {
  (function() { if (navigator.userAgent.match(/(iPhone|iPod)/i)) window.scrollTo(0, 1); }).delay(10);
}


