(function() {

var Dom = YAHOO.util.Dom,
    Event = YAHOO.util.Event,
    Lang = YAHOO.lang;

    var ImageRotate = function(el, config) {
        var oConfig = {
            element: el,
            attributes: config || {}
        };

        ImageRotate.superclass.constructor.call(this, oConfig.element, oConfig.attributes);    
    };

    YAHOO.extend(ImageRotate, YAHOO.util.Element, {
    
        CSS_MAIN: 'phanfare-imagerotate',
        CSS_IMAGE: 'phanfare-imagerotate-image',
        CSS_IMAGE_HOLDER: 'phanfare-imagerotate-holder',
        CSS_IMAGE_HOLDER_HIDE: 'phanfare-imagerotate-holder-hide',
        
        _imageOuterEl: null,
        _imageHolderEls: null,
        _imageEls: null,
        _currentIndex: null,
        
        getEl: function() {
            return this.get('element');
        },

        init: function(p_oElement, p_oAttributes) {
        
            ImageRotate.superclass.init.call(this, p_oElement, p_oAttributes);
                
            this._currentIndex = 0;
            
            var el = this.getEl();
            this._imageOuterEl = document.createElement("div");
            this._imageOuterEl.style.height = this.get('height') + "px";
            this._imageOuterEl.style.width = this.get('width') + "px";
            Dom.addClass( this._imageOuterEl, this.CSS_MAIN );
            
            this._imageHolderEls = new Array();
            this._imageEls = new Array();
            
            var images = this.get('images');
            for( var n = 0; n < images.length; n++ ) {
                var els = this._initImage( images[ n ] );
                this._imageHolderEls[ n ] = els.imageHolderEl;
                this._imageEls[ n ] = els.imageEl;
                this._imageOuterEl.appendChild( els.imageHolderEl );
                
                if ( n != this._currentIndex ) {
                    Dom.addClass( els.imageHolderEl, this.CSS_IMAGE_HOLDER_HIDE );                
                    this._fadeOutImage( n, false );
                }
            }
            
            el.appendChild( this._imageOuterEl );
        },
        
        _initImage: function( image ) {
        
            var imageHolderEl = document.createElement("div");
            Dom.addClass( imageHolderEl, this.CSS_IMAGE_HOLDER );
            
            var imageEl = document.createElement("img");
            imageEl.style.height = this.get('height') + "px";
            imageEl.style.width = this.get('width') + "px";
            imageEl.src = image;
            Dom.addClass( imageEl, this.CSS_IMAGE );
            
            imageHolderEl.appendChild( imageEl );
            
            return { imageHolderEl:imageHolderEl, imageEl:imageEl };
        },

        initAttributes: function(attr) {
            ImageRotate.superclass.initAttributes.call(this, attr);


            this.setAttributeConfig('width', {
                writeOnce: true,
                validator: YAHOO.lang.isNumber,
                value: attr.width || 0
            });
            
            this.setAttributeConfig('height', {
                writeOnce: true,
                validator: YAHOO.lang.isNumber,
                value: attr.height || 0
            });
            
            this.setAttributeConfig('images', {
                writeOnce: true,
                validator: YAHOO.lang.isArray,
                value: attr.images || []
            });
            
            this.setAttributeConfig('sleepTime', {
                writeOnce: true,
                validator: YAHOO.lang.isNumber,
                value: attr.sleepTime || 3000
            });
            
            this.setAttributeConfig('fadeTime', {
                validator: YAHOO.lang.isNumber,
                value: attr.fadeTime || 1
            });


        },


        start: function() {
        
            this._startFadeTimer();
        },
        
        _startFadeTimer: function() {
        
            if ( this._isRunning == true ) 
                return;
                
            var me = this;
            this._isRunning = true;
            setTimeout( function() { 
                                    return function() { me._doFade(); };
                                   }(), 
            this.get('sleepTime') );
        },
        
        _doFade: function() {
            this._isRunning = false;        
            this._fadeOutImage( this._currentIndex, true );
            this._currentIndex = ( this._currentIndex + 1 ) % this._imageEls.length;
            this._fadeInImage( this._currentIndex );
        },
        
        _fadeOutImage: function( imageIndex, withCallback )
        {
            var anim = new YAHOO.util.Anim(this._imageEls[ imageIndex ], { opacity: { from: 0.99, to: 0.01 } }, this.get('fadeTime'), YAHOO.util.Easing.easeBoth); 
    
	        var o = new Object;
	        o.imageIndex = imageIndex;
	        o.scope = this;
	        if ( withCallback ) {
	            anim.onComplete.subscribe(this._fadeOutImageComplete, o);
	         }
    	    anim.animate();
        },
        
        _fadeOutImageComplete: function( e, args, obj) 
        {
            var imageHolderEl = obj.scope._imageHolderEls[ obj.imageIndex ] ;
            if ( !imageHolderEl )
                return;
                
            Dom.addClass( imageHolderEl, obj.scope.CSS_IMAGE_HOLDER_HIDE );
        },
        
         _fadeInImage: function( imageIndex )
        {
            var anim = new YAHOO.util.Anim(this._imageEls[ imageIndex ], { opacity: { from: 0.01, to: 0.99 } }, this.get('fadeTime'), YAHOO.util.Easing.easeBoth); 
    	    
     	    var o = new Object;
	        o.imageIndex = imageIndex;
	        o.scope = this;
            anim.onComplete.subscribe(this._fadeInImageComplete, o);
    	    anim.animate();
	         
    	    var imageHolderEl = this._imageHolderEls[ imageIndex ] ;
    	    Dom.removeClass( imageHolderEl, this.CSS_IMAGE_HOLDER_HIDE );
        },
        
        _fadeInImageComplete: function( e, args, obj) 
        {
            obj.scope._startFadeTimer();
        },
        
 
        toString: function() {
            if (this.get) {
                return 'ImageRotate (#' + this.get('id') + ')';
            }
            return 'Image Rotate';
        }
    });

    YAHOO.widget.ImageRotate = ImageRotate;


})();

YAHOO.register("imagerotate", YAHOO.widget.ImageRotate, {version: "2.8.0r4", build: "2449"});
