// TODO: Ideally we would stop scrolling if am_isRendered ('RightColumnContent') returns false

var currentScroller=null;
function imageScroller(images, container, options){
    if(!options){
        
        this.options = {};
    }else{
        this.options = options;
    }
    this.images = images;
    this.container = container;
    this.innerContainer=null;
    this.element=null;
    this.shownImages=new Array();
    this.showIndex=0;
    this.smoothScroller=null;
    
    /**
     * Sets default values for options if they are not set
     */
    this.setDefaults = function(){
        var opts= this.options;
        
        if(!opts.scrollSpeed){
            opts.scrollSpeed = 2;
        }
        
        if(!opts.scrollDelay){
            opts.scrollDelay = 3000;
        }
        
        /**
         * how many images to show at once
         */
        if(!opts.showImageNumber){
            opts.showImageNumber=2;
        }
        
        if(!opts.scrollAmount){
            opts.scrollAmount = 1;
        }
        
        if(opts.imageMargin){
            for(var x=0;x<this.images.length;x++){
                this.images.x.style.marginLeft=opts.margin;
            }
        }
        
    }
    
    this.createScroller = function(){
        this.loadImages();
        this.innerContainer = document.createElement('div');
        this.element = document.createElement('div');
        this.element.style.width="1000px";
        this.element.style.verticalAlign="text-bottom";
        if(this.options.height){
            this.innerContainer.style.height = this.options.height;
            this.element.style.height = this.options.height;
            
            this.innerContainer.style.height = this.options.height;
        }else{
            this.innerContainer.style.height="100%";
            this.element.style.height = "100%";
        }
        if(this.options.width){
            this.innerContainer.style.width = this.options.width;
        }else{
            this.innerContainer.style.width = "100%";
        }
        
        this.innerContainer.style.overflow = "hidden";
        
        this.container.appendChild(this.innerContainer);
        this.innerContainer.appendChild(this.element);
        
        //Add our first two images    
        for(var i=0;i<this.options.showImageNumber;i++){
            if(this.images[i]){
                this.log(this.images[i]);
                this.element.appendChild(this.images[i]);
                this.showIndex=i;
            }
        }
    }
    
    this.loadImages=function(){
        for (var x=0;x<this.images.length;x++){
            if(typeof(this.images[x])!='object'){
                var image=document.createElement('img');
		image.src= this.images[x];
                this.images[x]=image;
            }
        }
    }
    
    this.scroll = function(object){
        if(!object){
            object=currentScroller;
        }
        for(var x=0; x<object.options.scrollAmount;x++){
            var imageIndex= ++object.showIndex + x ;
            if(imageIndex >= object.images.length){
                imageIndex=0;
                object.showIndex=0;
            }
            object.element.appendChild(object.images[imageIndex]);
            object.smoothScroller = new smoothScroller(object.innerContainer, 119,5,50, {onFinished: function(object){
                if (object.firstChild.firstChild)
                    object.firstChild.removeChild(object.firstChild.firstChild);
                object.scrollLeft=0;
            }});
        }
        
        //object.element.
    }


    this.smoothScroll = function(object){       
        if(!object){
            object=currentScroller;
        }
    }
        
    this.log=function(ob){
        if(window.console){
            window.console.log(ob);
        }
    }
    this.setDefaults();
    this.createScroller();
    
    currentScroller=this;
    window.setInterval(this.scroll,this.options.scrollDelay, this);
    
    
}

var currentSmoothScroller=null;
function smoothScroller(target,amount, speed, interval, options){
    this.target = target;
    this.speed=speed;
    this.interval=interval;
    this.options = options;
    this.amount=amount;
    this.scrolled = 0;
    this.scroll = function(object){
        if(!object){
            object = currentSmoothScroller;
        }
        if(object.options.onEndReached){
            if(object.target.scrollLeft + object.speed >= object.target.scrollWidth){
                object.options.onEndReached();
            }
        }
        object.target.scrollLeft = object.target.scrollLeft + object.speed;
        object.scrolled = object.scrolled + object.speed;
        if(object.scrolled >= amount){
            window.clearInterval(object.intervalID);
            if(object.options.onFinished){
                object.options.onFinished(object.target);
            }
        }
    }
    currentSmoothScroller=this;
    this.intervalID = window.setInterval(this.scroll, this.interval,this);
}
