/**
 * rFader Object - requires Prototype.js
 *
 * Pass this object an array of elements. 
 *
 * rFader->start() will start fading them in and out
 * rFader->stop() will stop the rotation (after current has completed)
 * 
 * @copyright Llama Digital http://www.llamadigital.co.uk
 * @author Richard Harrison
 * @date 20061025T15:30+0000

 */
  

var rFader = Class.create();
rFader.prepImages = function(imgsrcArray){
        var imgNodes = new Array();
        
        for(i=0;i<imgsrcArray.length;i++){    
            imgNodes[i] = document.createElement('img');
            imgNodes[i].setAttribute('src', imgsrcArray[i]);      
        }
        return imgNodes;
    }

rFader.prepText = function(textarray, tagname){
    
        if(!document.createElement || !tagname) return;
        
        var textualNodes = new Array();
       
        for(i=0;i<textarray.length;i++){    
            textualNodes[i] = document.createElement(tagname);
            textualNodes[i].appendChild(document.createTextNode(textarray[i])); 
        }
        return textualNodes;
}


rFader.prototype = {

    initialize: function(elementArray, targetParent, options) {
        
        var options = Object.extend({
            holdTime: 0,
            speed: 1,
            type: 'white'
        }, options || {});
        this.options = options;
        
        this.tmp =options.name;  
        
        elementArray.each(function(node){
            Element.setOpacity(node, 0);           
        });
        
        this.elementArray = elementArray;
        this.targetParent = targetParent;
        this.count = 0;
        this.run = true;
        this.duoInit = false;
        
        
      },
      
    fadeOut: function(){
        /* fade out */
        
        if(this.targetParent && this.run){
            new Effect.Opacity(this.targetParent.lastChild, {duration:this.options.speed, from: 1.0, to: 0.0, afterFinish: this.changeElement.bind(this) });
        }
    },
    
    fadeIn: function(){  
        new Effect.Opacity(this.targetParent.firstChild, {duration:this.options.speed, from: 0.0, to: 1.0, afterFinish: function(){
            setTimeout(this.fadeOut.bind(this),this.options.holdTime * 1000);
        }.bind(this)});
        
    },
    
     changeElement: function(){
        if(this.elementArray.length <= this.count){
            this.count = 0; /* reset the counter if it's bigger than the length of the array */
        }
        
        if(this.targetParent.childNodes.length < 1){
            this.targetParent.appendChild(this.elementArray[this.count]) /* append the current node into the parent container */
        }else{
            
            if(this.options.type == 'duo'){
                
                    if(this.targetParent.childNodes.length<2){
                        this.targetParent.replaceChild(this.elementArray[this.count], this.targetParent.lastChild);
                        this.count++;
                    }else{
                        /* replace the last child with the first child */
                        this.targetParent.replaceChild(this.targetParent.firstChild, this.targetParent.lastChild); 
                    }
                        
                    /* insert before it the new element */
                    this.targetParent.insertBefore(this.elementArray[this.count], this.targetParent.lastChild);
                
                
            }else{
  
                this.targetParent.replaceChild(this.elementArray[this.count], this.targetParent.lastChild);
            }
        }
        this.count++;
        this.fadeIn();
     },
      
    start: function(){
        
        if(!this.elementArray || this.elementArray.length==0) { return; }
        
        setTimeout(this.fadeOut.bind(this), this.options.holdTime*1000);
        
    },
    
    stop: function(){
        this.run=false;
    }
}