// Image rotator v1.03
// Rotates through a list of images using various transitions
//
// To make an imade gradually fade up on init set control visibility to hidden (css or style)
// Controls (img&div) can be set up with style properties using CSS 
// Changes:
//      1.01 - Slide transitions (can be in any direction or random),
//      1.02 - In random mode, first image is random as well as concurrent ones
//           - Start-up timings revised 
//           - All images load dynamically
//		1.03 - Addition of jquery, increasing efficency and reducing code size
// Inputs:
//      image_control_ID = ID of the html image control
//      div_control_ID = ID of the html text control set to "" if not required
//      image_filenames_s = Semi-colon seperated image filenames
//      image_htmltext_s = Semi-colon seperated html text
//      random - if equal to 1 images are randomised if 0 they are in order given
//      trans_type - 0=fade transition, 1=slide left, 2=slide right, 3=slide up, 4=slide down, 5=slide random

// User can change the following settings:
//    transitionSteps - Levels of transition
//    transitionTime - Seconds
//    pauseTime - Seconds
//    pictureIndex - Start image
//    random - if equal to 1 images are randomised if 0 they are in order given
//    trans_type - 0=fade transition, 1=slide left, 2=slide right, 3=slide up, 4=slide down, 5=slide random


function ImageRotator(parentId, imageFilenames, imageHtmlText, imageClass, textClass ) {
    
    // Checks if and extra semi-colon has been added to the end, if so it removes it (split command)
    while (true) { if (imageFilenames.substr(imageFilenames.length - 2, 1) != ";") break; imageFilenames = imageFilenames.substr(0, imageFilenames.length - 2); }
    
    // ********************************* User variables *************************************
    // Image settings var
    this.transitionTime = 1000;    // ms
    this.pauseTime = 1500;         // ms

    // *********************************** Private variables ********************************
    var imageFilenamesArr = imageFilenames.split(';');         // Array of filenames
    var imageHtmlTextArr = imageHtmlText.split(';');           // Array of htmltext
    this.pictureIndex = 0;      // Start image

    var image1;         // Control objects          
    var text1;          // '  

    var thisObj = this;

    // *********************************** User functions ************************************


    // Creates additional controls for smooth transition and start the interval timer
    $('document').ready(function() {

        for (var i = 0; i < imageFilenamesArr.length; i++) {
            // Create image controls 
            image1 = document.createElement('img');
            image1.src = imageFilenamesArr[i];
            image1.className = imageClass;
            image1.id = "imgrot" + i;
            image1.style.display = "none";
            document.getElementById(parentId).appendChild(image1);
        }

        // Get main text control
        if (imageHtmlText.length > 0) {
            for (var i = 0; i < imageFilenamesArr.length; i++) {
                // Create text control with style properties
                text1 = document.createElement('div');
                text1.innerHTML = imageHtmlTextArr[i];
                text1.className = textClass;
                text1.id = "txtrot"+i;
                text1.style.display = "none";
                document.getElementById(parentId).appendChild(text1);
            }
            // Start transitions with text
            window.setTimeout(transitionWithText, thisObj.pauseTime);
        }
        else window.setTimeout(transition, thisObj.pauseTime);
    });

    function transition() {
        $('#imgrot' + ((thisObj.pictureIndex + 1) % imageFilenamesArr.length)).fadeIn(thisObj.transitionTime);
        $('#imgrot' + thisObj.pictureIndex).fadeOut(thisObj.transitionTime,
            function() { window.setTimeout(transition, thisObj.pauseTime); });
        
        thisObj.pictureIndex = (thisObj.pictureIndex + 1) % imageFilenamesArr.length;
    }
    function transitionWithText() {
        $('#imgrot' + ((thisObj.pictureIndex + 1) % imageFilenamesArr.length)).fadeIn(thisObj.transitionTime);
        $('#imgrot' + thisObj.pictureIndex).fadeOut(thisObj.transitionTime,
            function() { window.setTimeout(transitionWithText, thisObj.pauseTime); });
        $('#txtrot' + ((thisObj.pictureIndex + 1) % imageFilenamesArr.length)).fadeIn(thisObj.transitionTime);
        $('#txtrot' + thisObj.pictureIndex).fadeOut(thisObj.transitionTime);

        thisObj.pictureIndex = (thisObj.pictureIndex + 1) % imageFilenamesArr.length;
    }

}