/* (C) 2009-present Eshkol Institute. All rights reserved.
 * 
 * This script file is operating the rotating image in the homepage.
 *
 * Author: Ben Barkay
 * Version: 1.0
 */

// Initiate the image rotation package upon page load.
EventManager.addEventHandler(window, 'load', function() {
    ImageRotation.start();
});

// Create the image rotation package object.
var ImageRotation = new Object();

// Define image rotation variables.
ImageRotation.fader = null;
ImageRotation.element = null;
ImageRotation.counter = 0;

/**
 * Initiates the image rotation.
 */
ImageRotation.start = function() {
    // Get the element this rotation is applied to.
    ImageRotation.element = document.getElementById('rotating_image');
    
    // Create a fader object for the rotated element.
    ImageRotation.fader = new Canvas.Fader(ImageRotation.element);
    
    // Begin the rotation in 5 seconds from now.
    setTimeout(function() {
        ImageRotation.fader.hide(21,.06,ImageRotation.onHide);
    }, 1000);
    
    // Display the first image in the sequence.
    ImageRotation.nextImage();
    
    // Configure the image rotation element's style.
    ImageRotation.element.style.backgroundPosition = 'center';
    ImageRotation.element.style.backgroundRepeat = 'no-repeat';
};

/**
 * Retrieves the next image to display by the rotator.
 * Currently (23 Aug 2009), there is no sequence and the images rotate randomly.
 */
ImageRotation.nextImage = function() {
    ImageRotation.element.style.backgroundImage = 'url(rotateimage.php?'
        + (ImageRotation.counter ++) + ')';
};

/**
 * A callback function to be called when the fader finishes hiding the rotating
 * image. This function begins a show, and rotates to the next image in the
 * sequence.
 */
ImageRotation.onHide = function(o) {
    ImageRotation.fader.show(21,.2,ImageRotation.onShow);
    ImageRotation.nextImage();
};

/**
 * A callback function to be called when the fader finishes showing the rotating
 * image. This function schedules the next rotation in 5 seconds from the time
 * it has been called.
 */
ImageRotation.onShow = function(o) {
    setTimeout(function() {
        ImageRotation.fader.hide(21,.1,ImageRotation.onHide);
    }, 1000);
};

