﻿function HomeImages(containerId, width, height) {
    this.changeInterval = 10000;
    this.changeDuration =  3000;
    this.containerId = containerId;
    this.containerWidth = width;
    this.containerHeight = height;

    this.wrapComplete = function(fn) {
        var that = this;
        return function() { return fn(that); };
    }

    this.configureSlide = function() {
        this.changeFunction = this.changeSlide;
        document.getElementById("home_top_scroll_container").style.width = "1000px";
    }
    
    this.changeSlide = function() {
        if ($j("#" + this.containerId).children().size() > 0) {
            $j("#" + this.containerId).animate({
                left: -this.containerWidth
            }, this.changeDuration, this.wrapComplete(this.changeSlideComplete));
        }
    }

    this.changeSlideComplete = function(that) {
        var slider = $j("#" + that.containerId);
        slider.append($j(slider.children()[0]).remove());
        slider.css("left", 0);
    }

    this.configureDissolve = function() {
        this.changeFunction = this.changeDissolve;
        var container = document.getElementById(this.containerId);
        var children = new Array();
        while (container.childNodes.length > 0) {
            var child = container.removeChild(container.firstChild);
            if (child.tagName != "IMG") continue;
            child.style.position = "absolute";
            child.style.top = "0px";
            child.style.left = "0px";
            children.push(child);
        }
        while (children.length > 0) {
            container.appendChild(children.pop());
        }
    }

    this.changeDissolve = function() {
        var sz = $j("#" + this.containerId).children().size();
        if (sz > 0) {
            $j($j("#" + this.containerId).children()[sz - 1]).animate({
                opacity: 0
            }, this.changeDuration, this.wrapComplete(this.changeDissolveComplete));
        }
    }

    this.changeDissolveComplete = function(that) {
        var slider = $j("#" + that.containerId);
        var children = slider.children();
        var child = $j(children[children.size() - 1]).remove();
        slider.prepend(child);
        child.css("opacity", 1.0);
    }

    this.configureFunction = this.configureDissolve;

    return true;
}

var homeImages = new HomeImages("home_top_scroll_container", 500, 308);

$j(document).ready(function() {
    homeImages.configureFunction();
    setInterval("homeImages.changeFunction();", homeImages.changeInterval);
});
