  /**
   * Header rotation
   *
   * @author Kai Vogel <k.vogel@21torr.com>
   * @package TYPO3
   * @copyright (c) 2009 by 21TORR Interactive GmbH
   * @version 1.0
   * @since 27.03.2009
   */
  var iRotationPointer = 0;
  var aBoxes = new Array();
  var oObserver = {
    stopLink: function(oEvent) {
      Event.stop(oEvent);
    }
  }

  oObserver.oHandle = oObserver.stopLink.bindAsEventListener(oObserver);
  
  
  /**
   * Get number from text
   */
  function iGetNum(sText) {
    var iNum = parseInt(sText);

    if (isNaN(iNum)) {
      return 0;
    }

    return iNum;
  }

  /**
   * Get all IDs, Positions and make Boxes opaque
   */
  function vSetBoxes () {
    aElements = $$('div#header_rotation .header_box');

    // Switch objects with their id
    for (i = 0; i < aElements.length; i++) {
      sID = aElements[i].identify();
      sID = sID.substr(sID.indexOf('box_')+4, sID.length);
      aBoxes[i] = new Object();
      aBoxes[i]['id'] = sID;
      aBoxes[i]['left'] = aElements[i].style.left;
    }

    // Make boxes opaque
    $$('div#header_rotation .header_box').each(function (poTextBox) {
      poTextBox.setOpacity(0.8);
    });
  }

  /**
   * Show first
   */
  function vShowFirst () {
    var sFirstID = $$('img.header_image')[0].identify();
    
    // Show first image
    $(sFirstID).show();
     
    // Show first box if not empty
    var sID = sFirstID.substr(13, sFirstID.length); // length "header_image_"
    var sFirstBox = 'header_box_' + sID;

    if ($(sFirstBox).innerHTML.length) {
      $(sFirstBox).show();
    }
  }

  /**
   * Get next pointer
   */
  function iGetNextPointer () {
    if(typeof aBoxes[iRotationPointer + 1] != "undefined") {
      return iRotationPointer + 1;
    } else {
      return 0;
    }
  }

  /**
   * Start / stop event observer
   */
  function vStartStopObserver (bEnable) {
    var oCurrentLink = $('header_link_'+aBoxes[iRotationPointer]['id']);

    if (bEnable) {
      oCurrentLink.stopObserving('click', oObserver.oHandle);
      oCurrentLink.style.cursor = 'pointer';
    } else {
      oCurrentLink.observe('click', oObserver.oHandle);
      oCurrentLink.style.cursor = 'default';
    }
  }

  /**
   * Rotate images and related text box
   */
  function vRotate () {
    var iContainerWidth = iGetNum($('header_rotation').getWidth());
    var oCurrentImage   = $('header_image_'+aBoxes[iRotationPointer]['id']);
    var oCurrentBox     = $('header_box_'+aBoxes[iRotationPointer]['id']);
    var iNext           = iGetNextPointer();
    var oNextImage      = $('header_image_'+aBoxes[iNext]['id']);
    var oNextBox        = $('header_box_'+aBoxes[iNext]['id']);
    var iCurrentLeft    = iGetNum(oCurrentBox.getStyle('left'));
    var iCurrentTop     = iGetNum(oCurrentBox.getStyle('top'));
    var iNextWidth      = iGetNum(oNextBox.getWidth());
    var iNextTop        = iGetNum(oNextBox.getStyle('top'));
    var sNextLeft       = aBoxes[iNext]['left'];
    var iNextLeft       = iGetNum(sNextLeft.substr(0,sNextLeft.length-2)); // remove "px"

    iRotationPointer    = iNext;

    // Move current box out
    new Effect.Move(oCurrentBox, {
      x: iContainerWidth,
      y: iCurrentTop,
      mode: 'absolute',
      duration: 0.6,
      transition: Effect.Transitions.sinoidal,
      queue: {
        position: 'front',
        scope: 'rotation'
      },
      beforeStart : function () {
        // Make link unclickable
        vStartStopObserver(false);
      },
      afterFinish : function () {
        oCurrentBox.hide();
      }
    });

    // Fade current image out and next image in
    new Effect.Parallel([
      new Effect.Opacity(oCurrentImage, {
        from: 1.0,
        to: 0,
        sync: true
      }),
      new Effect.Opacity(oNextImage, {
        from: 0,
        to: 1.0,
        sync: true
      })
    ], {
      duration: 1,
      transition: Effect.Transitions.sinoidal,
      queue: {
        position: 'end',
        scope: 'rotation'
      },
      beforeStart : function () {
        oNextImage.setOpacity(0);
        oNextImage.show();
      },
      afterFinish : function () {
        oCurrentImage.hide();
      }
    });

    var oNextText = document.getElementById('header_box_'+aBoxes[iNext]['id']);
    if (parseInt(oNextText.innerHTML.length) > 0) {
      // Move next box in
      new Effect.Move(oNextBox, {
        x: iNextLeft,
        y: iNextTop,
        mode: 'absolute',
        duration: 0.7,
        transition: Effect.Transitions.sinoidal,
        queue: {
          position: 'end',
          scope: 'rotation'
        },
        beforeStart : function () {
          oNextBox.style.left = iContainerWidth + 'px';
          oNextBox.show();
        },
        afterFinish : function () {
          // Make link clickable again
          vStartStopObserver(true);
        }
      });
    } else {
      // Make link clickable again
      vStartStopObserver(true);
    }
  }

  vSetBoxes();

  vShowFirst();

  if (aBoxes.length > 1) {
    // Rotate
    new PeriodicalExecuter(function(poExecuter) {
      vRotate();
    }, 12);
  }

