window.addEvent('domready', function()
{

  if ($('alumni_rotate'))
  {
    SqueezeBox.assign($$('a[rel=boxed][href^=#]'), {
      size: {x: 650, y: 270}
    });

    var speed = 1000;
    var delay = 3000;

    var container = $('alumni_rotate');
    var elements = container.getElements('.cutepopup');
    var floater = container.getElement('.floater');

    var shadow = container.getElement('.shadow');
    shadow.set('tween', {property: 'opacity', duration: speed, transition: Fx.Transitions.Sine.easeOut});

    floater.addEvent('mouseenter', function()
    {
      floater.play = true;
      floater.effect = new Fx.Morph(floater, {duration: speed, transition: Fx.Transitions.Sine.easeOut, onComplete: hover.pass([floater, shadow])});
      hover(floater, shadow);
    });

    floater.addEvent('mouseleave', function()
    {
      floater.play = false;
    });

    start = $random(0, elements.length-1);

    elements.each(function(el, i)
    {
      el.fade((i == start)? 'show' : 'hide');

    });

    (function() { $('hider').setStyle('display', 'none'); }).delay(100);

    (function() { rotate_images(elements, start, delay); }).delay(delay);
  }


  if ($('alumni_frontpage'))
  {
    SqueezeBox.assign($$('a[rel=boxed][href^=#]'), {
      size: {x: 650, y: 270}
    });

    var speed = 1000;

    $$('.cutepopup').each(function(el)
    {
      var img = el.getElement('img');
      var shadow = $(el.get('id')+'_shadow');

      shadow.set('tween', {property: 'opacity', duration: speed, transition: Fx.Transitions.Sine.easeOut});


      img.addEvent('mouseenter', function()
      {
        img.play = true;
        img.effect = new Fx.Morph(img, {duration: speed, transition: Fx.Transitions.Sine.easeOut, onComplete: hover.pass([img, shadow])});
        hover(img, shadow);
      });

      img.addEvent('mouseleave', function()
      {
        img.play = false;
      });

    });
  }


});

function rotate_images(elements, previous, delay)
{
  var current = previous;
  var to = elements.length-1;

  // make sure the new target is different from the old one.
  while(current == previous)
  {
    current = $random(0, to);
  }

  elements[previous].fade('out');

  // we don't use fade here, because "fade" returns the element, not the FX
  elements[current].get('tween').start('opacity',0,1).chain(function()
  {
    (function() { rotate_images(elements, current, delay); }).delay(delay);
  });
}

function hover(img, shadow)
{
  if (img.effect)
  {
    var from = img.getStyle('top').toInt();
    var to = (from < 0)? 0 : -10;
    img.effect.start({'top':[from,to]});

    shadow.get('tween').start((to == -10)? 0.3 : 1);

    if (img.play == false && to == 0)
    {
      img.effect = null;
    }
  }
}
