/**
* Slideshow implementation in home page
* @author : Arnab Chakraborty ( arnabc@mangospring.com )
* Copyright (c) 2009, MangoSpring Technologies
*/

( function ( Y ) {
    
    var $ = Y.util.Dom.get,
        Get = Y.util.Get,
        Event = Y.util.Event,
        Dom = Y.util.Dom,
        Lang = Y.lang,
        Selector = Y.util.Selector;


    //create a namespace
    Y.namespace( 'custom' );

    Y.custom.SlideShow = function ( container, navig_cont, _config ) {
        // store the container id
        this.container = container;

        // navigation container
        this.navigation = $( navig_cont );

        // parse the config options passed
        this.config = this._parseConfig( _config );

        // setup the carousel
        this._setup();
    }

    Y.custom.SlideShow.prototype = {
        
        SELECTED_HANDLE_CLASS : 'current-item',

        /**
        * Method to initialize/setup the slide show component
        * @private
        * @return {void}
        */
        _setup : function () {
            try {
                //create carousel
                this.carousel = new Y.widget.Carousel( this.container , this.config );
                // debug carousel
                //this.debug( 'carousel component created %s', this.carousel );
                this.carousel.render();
                this.carousel.show();
                
                // stores currently selected handle/switch
                this.selected_handle

                // setup nabigation elements
                var switches = Selector.query( '.switch', this.navigation );
                //set event handlers
                var i = 0;

                Dom.batch( switches, function ( el ) {
                    // attach handler
                    Event.on( el, 'click', this._handleSwitchClick, i, this );

                    // if the current item is the selected item then set this as the current item
                    if ( Dom.hasClass( el, 'current-item' ) ) {
                        this.selected_handle = el;
                        this.carousel.set( 'firstVisible', i );
                    }

                    // increase counter to point to the next item
                    i++;

                }, this, true );

            }catch ( e ) {
                //this.debug( 'Error occured => %s at %d ', e.message, e.lineNumber );
            }
        },
        
        /**
        * Method to parse the configuration object specified during instantiation
        * @private
        * @param {Object} config
        * @return {Object}
        */
        _parseConfig : function ( config ) {
            
            // default configuration options
            var conf = {
                isCircular : true,
                firstVisible : 0,
                numVisible : 1,
                revealAmount : 0,
                animation : { speed : 0.5, effect : Y.util.Easing.easeOut },
                scrollIncrement : 1,
                numItems : 4,
                navigation : { prev : null, next : null }
            };

            // if config is an object then augment else not
            if ( !Lang.isObject( config ) ) {
                return conf;
            }

            return Lang.merge( conf, config );
        },
        
        /**
        * Event listener of the element click
        * @param {Event} e
        * @param {Number} args
        * @return {void}
        */
        _handleSwitchClick : function ( e, args ) {
            // set the selected class to the handle
            this._setSelectedClass( Event.getTarget( e ) );

            // show the item
            this.show( args );

            // stop the event
            Event.stopEvent( e );
        },
        
        /**
        * Method to show the particular slide, takes a 'position/index' of the slide to show
        * @param {Number} pos
        * @return {void}
        */
        show : function ( pos ) {
            if ( Lang.isNumber( pos ) ) {
                this.carousel.scrollTo( pos, false /* dont select */ );
            }
        },
        
        /**
        * Method to toggle the selected class of the switces
        * @private
        * @param {HTMLElement} el
        * @return {void}
        */
        _setSelectedClass : function ( el ) {

            if ( this.selected_handle ) {
                Dom.removeClass( this.selected_handle, this.SELECTED_HANDLE_CLASS );   
            }

            this.selected_handle = el;

            Dom.addClass( this.selected_handle, this.SELECTED_HANDLE_CLASS );
        },
        
        /**
        * Method to print debugging messages in the console if provided
        * @return {void}
        */
        debug : function () {
            if ( undefined !== window.console ) {
                //console.log.apply( window, Array.prototype.slice.call(arguments) );
            }
        }
    }

} )( YAHOO );