/**
* Home Tab Functionality
* Copyright (c) 2009, All rights reserved
* @author: Arnab Chakraborty (arnabc@mangospring.com)
*/

( function ( App ) {
    
    App.$ = YAHOO.util.Dom.get;
    App.E = YAHOO.util.Event;
    App.D = YAHOO.util.Dom;
    App.S = YAHOO.util.Selector;
    App.A = YAHOO.util.Anim;

    /**
    * @class BannerRotater
    * Rotates the banner with different images using a animation
    * @constructor
    * @namespace App.ui
    *
    * @param {String|HTMLElement} container
    */
    App.ui.BannerRotater = function ( container, image_arr ) {
        this.container = App.$( container );
        this.imagePaths = image_arr;

        this.images = [];
        
        if ( this.container ) {
            this._setup();
        }

        this.onEachInterVal = new YAHOO.util.CustomEvent( 'onEachInterval', this, true );
    }


    App.ui.BannerRotater.prototype = {
        
        _setup : function () {
            
            //assume the first image to show first time
            var im = new Image();
            im.src = this.imagePaths[0];
            App.D.addClass( im, "banner-rotate-image" );
            
            this.container.appendChild( im );
            this.images.push( im );

            this.pointer = 0;

            this.showAnim = new YAHOO.util.Anim( im, {
                opacity : { to : 1.5 }
            }, 2, YAHOO.util.Easing.easeIn );

            this.showAnim.onComplete.subscribe( this.showEnd, this, true );

            this.hideAnim = new YAHOO.util.Anim( im, {
                opacity : { to : 0 }
            }, 2, YAHOO.util.Easing.easeOut );

            this.hideAnim.onComplete.subscribe( this.hideEnd, this, true );
            
            this.hide();            
        },

        show : function () {
            this.onEachInterVal.fire( this.pointer );
            this.showAnim.animate();
        },
        
        hide : function () {
            YAHOO.lang.later( 2500, this.hideAnim, this.hideAnim.animate );
        },
        
        showEnd : function () {
            this.hide();            
        },
        
        hideEnd : function () {
            this.setPointer();

            var el = this.hideAnim.getEl();
            el.src = this.imagePaths[ this.pointer ];

            YAHOO.lang.later( 500, this, this.show );
        },
        
        setPointer : function () {
            if ( this.pointer >= ( this.imagePaths.length - 1 ) ) {
                this.pointer = 0;
            } else {
                this.pointer++;
            }
        }
    };


    YAHOO.util.Event.onDOMReady( function () {
        App.ui.rotater = new App.ui.BannerRotater( 'banner-rotate-cont', [ 
                                                                            '/ab/images/banner_image1.jpg', 
                                                                            '/ab/images/banner_image2.jpg', 
                                                                            '/ab/images/banner_image3.jpg' ] );
        
        var selEl = App.S.query( '#main-banner ul.banner-list li.one', null, true );
        var cont = App.S.query( '#main-banner ul.banner-list', null, true );

        App.ui.rotater.onEachInterVal.subscribe( function ( type, args ) {
            var index = args[0], li;
            
            switch ( index ) {
                case 0:
                    li = App.S.query( 'li.one', cont, true );
                    break;
                case 1:
                    li = App.S.query( 'li.two', cont, true );
                    break;
                case 2:
                    li = App.S.query( 'li.three', cont, true );
                    break;            
            }

            if ( li ) {
                App.D.addClass( li, 'selected' );

                if ( !selEl ) {
                    selEl = li;
                } else {
                    App.D.removeClass( selEl, 'selected' );
                    selEl = li;
                }
            }
        });


    } );

})( MangoSpring );
