//Copyright (c) 2009, RBB Architects Inc.
//All rights reserved.

//Page Object
var ThisPage = new Object();

//Page Animation
ThisPage.RunMovingImage = function() {
    var Container = InfoFind.HTML.ByID("Moving_SlideShow");
    var Position = InfoFind.HTML.Element.GetPosition("Moving_SlideShow_Image", true);
    var Direction = (Position.Left == 0 ? "RightToLeft" : "LeftToRight");
    Container.setAttribute("Direction", Direction);
    ThisPage.AnimateMovingImage();
}

ThisPage.AnimateMovingImage = function() {
    //Variables
    var Container = InfoFind.HTML.ByID("Moving_SlideShow");
    var MovingImages = InfoFind.HTML.ByID("Moving_SlideShow_Images");
    var Image = InfoFind.HTML.ByID("Moving_SlideShow_Image");
    var Position = InfoFind.HTML.Element.GetPosition("Moving_SlideShow_Image", true);
    var ContainerWidth = InfoFind.HTML.Element.GetWidth("Moving_SlideShow");
    var Direction = Container.getAttribute("Direction");
    var MoveLeft = 0, n = 0, m = 0, FirstImage = "", FirstAlt = "";
    var DisplayImage = "", DisplayAlt = "", bNextImage = false;
    var LengthToMove = 0, PixelsPerSecond = 0;
    
    //Move from Right to Left
    if (Direction == "RightToLeft") {
        MoveLeft = ContainerWidth - Position.Width;
        Direction = "LeftToRight";
    //Move from Left to Right
    } else {
        MoveLeft = Position.Left + Position.Width - ContainerWidth;
        Direction = "RightToLeft";
    }
    
    //Set the Next Direction
    Container.setAttribute("Direction", Direction);
    
    //Determine Next Image to Show
    //This code loops through all child elements of the "Images" object, and
    //determines the next image. After the last image the Slide Show starts over.
    m = MovingImages.childNodes.length;
    for (n = 0; n < m; n++) {
        if (MovingImages.childNodes[n].nodeName.toLowerCase() == "img") {
            if (FirstImage == "") {
                FirstImage = MovingImages.childNodes[n].src;
                FirstAlt = MovingImages.childNodes[n].alt;
            }
            if (bNextImage == true) {
                DisplayImage = MovingImages.childNodes[n].src;
                DisplayAlt = MovingImages.childNodes[n].alt;
                break;
            } else {
                if (MovingImages.childNodes[n].src == Image.src) {
                    bNextImage = true;
                }
            }
        }
    }
    if (DisplayImage == "") {
        if (FirstImage == "") {
            return;
        }
        DisplayImage = FirstImage;
        DisplayAlt = FirstAlt;
    }
    Container.setAttribute("NextImage", DisplayImage);
    Container.setAttribute("NextAlt", DisplayAlt);
    
    //Scroll the Image based, timing is based on image size rather then a specific time.
    LengthToMove = Math.abs(MoveLeft - Position.Left);
    PixelsPerSecond = 75;
    InfoFind.Animation.Animate("Moving_SlideShow_Image", (LengthToMove / PixelsPerSecond), "MoveToRelative,0," + MoveLeft, null, null, ThisPage.FadeOutImage);
}

//Animate the current image out of view.
ThisPage.FadeOutImage = function() {
    InfoFind.Animation.Animate("Moving_SlideShow_Image", 0.5, "Opacity,100,0", null, null, ThisPage.FadeInImage);
}

//Animate the next image to display into view.
ThisPage.FadeInImage = function() {
    //Set the Image to show the next one.
    var NextImage = new Image();
    var Container = InfoFind.HTML.ByID("Moving_SlideShow");
    var Element = InfoFind.HTML.ByID("Moving_SlideShow_Image");
    NextImage.src = Container.getAttribute("NextImage");
    Element.src = NextImage.src;
    Element.alt = Container.getAttribute("NextAlt");
    if (Container.getAttribute("Direction") == "RightToLeft") {
        Element.style.left = "0px";
    } else {
        Element.style.left = parseInt(Container.style.width) - NextImage.width + "px";
    }
    //Now Fade-In the Image
    InfoFind.Animation.Animate("Moving_SlideShow_Image", 0.5, "Opacity,0,100", null, null,
        //Call main function again in 1 Second
        (function() {window.setTimeout(ThisPage.AnimateMovingImage, 1000);})
    );
}

//Global Code that executes once the Page loads.
InfoFind.HTML.AddEvent(window, "load", ThisPage.RunMovingImage);
