﻿/*

    Quicksilver Theatre Javascript Library
    Created by Climbing Turn Ltd
    11th September 2009
    Requires prototype.js and scriptaculous library

*/

var iFader;

var homeImages = new Array();
homeImages[0] = 'home_banner.jpg';
homeImages[1] = 'LaDiDaDa.jpg';
homeImages[2] = 'rehearsal_room.jpg';
homeImages[3] = 'qs1.jpg';
homeImages[4] = 'productions.jpg';
homeImages[5] = 'qs2.jpg';
homeImages[6] = 'upstairs_in_the_sky2.jpg';
homeImages[7] = 'qs3.jpg';
homeImages[8] = 'upstairs_in_the_sky.jpg';


function imageFader() {

    this.imgs;                  // image array
    this.interval = 5.0;        // fade time in seconds
    this.displayTime = 3000;    // image display tuime in milliseconds
    this.imagePointer = 1;       // tracks the subscript of the image to fade in, fade out is this minus 1
    this.numImages = 0;         // the number of images in the array (for performance)
    this.isEnabled = false;     // set to true if the image array conrtains more than 2 images
    this.imageContainer;        // the id of the containing div
    
    this.intVal = null;         // reference to the timer
    this.objectName;
    this.fadeInContainer = 2;
    this.imagePath = '/';
    

    // initialise the fader and set it going
    this.initialise = function(imgArray, interval, displaytime, imagepath, imagecontainer) {
        this.imgs = imgArray;
        this.interval = interval;
        this.displayTime = displaytime;
        this.numImages = this.imgs.length - 1;
        this.imagePath = imagepath;
        this.imageContainer = imagecontainer;
        if (this.numImages > 1) {
            this.isEnabled = true;
            this.setInterval();
        }
    }

    // set the interval between fades
    this.setInterval = function() {
        if (this.intVal)
            window.clearInterval(this.intVal);
        this.intVal = window.setInterval('iFader.doFades()', ((this.interval * 1000) + this.displayTime));
    }

    this.doFades = function() {

        this.imagePointer == this.numImages ? this.imagePointer = 0 : ++this.imagePointer;

        var i, o;

        if (this.fadeInContainer == 1) {
            i = this.imageContainer + '1';
            o = this.imageContainer + '2';
            this.fadeInContainer = 2;
        }
        else {
            o = this.imageContainer + '1';
            i = this.imageContainer + '2';
            this.fadeInContainer = 1;
        }

        $(i).src = this.imagePath + this.imgs[this.imagePointer];
        $(i).appear({ duration: this.interval });
        $(o).fade({ duration: this.interval, from: 1, to: 0 });

        this.setInterval();

        return false;

    }
    
}



function startFading() {
    $('imageFader1').style.display = ''; // switch on the first image
    iFader = new imageFader();
    iFader.initialise(homeImages, 5, 3000, 'user_resources/Image/home_page_fader/', 'imageFader', 'Quicksilver Children\'s Theatre');
}