/**
* Utility module provides a slideshow functionality using YUI carousel widget
* @author : Arnab Chakraborty (arnabc@mangospring.com)
* Copyright (c) 2009, MangoSpring Technologies
*/

/**
    @dependency
        utilities.js
        container-min.js        

*/

( function () {
    
    var carouselLoaded = false;

    // check if Carousel control is avaiilable or not
    // if not there then load it
    if ( !YAHOO.widget.Carousel ) {
        YAHOO.util.Get.script( 
            '/javascripts/thirdparty/yui/2.7.0/build/carousel/carousel-debug.js', 
            { 
                onSuccess : function () {
                    carouselLoaded = true;
                } 
            } 
        );
    }


    // shortcuts for YUI
    var Dom = YAHOO.util.Dom,
        Event = YAHOO.util.Event,
        Lang = YAHOO.lang;
        

    /**
    * @class Slideshow
    * Utility Class to implement slideshow
    * @constructor
    *
    * @param {HTMLElement|String} trigger
    */
    var SlideShow = function ( container, numItems ) {        
        this.carousel = new YAHOO.widget.Carousel( container, {
            numVisible : 1,
            numItems   : numItems,
            navigation : { prev : [ Dom.get( 'slide-prev') ], next : [ Dom.get( 'slide-next' ) ] }
        } );
        
        this.carousel.render();        
    }

    SlideShow.prototype = {
        
        /**
        * Method to show the carousel control
        * @return {void}
        */
        show : function () {
            this.carousel.show();
            
            if ( YAHOO.env.ua.ie > 0 ) {
                var el = this.carousel.getElementForItem( 0 );
                if ( el ) {
                    el.click();
                }
            }
            
        },
        
        /**
        * Method to hide the carousel control
        * @return {void}
        */
        hide : function () {
            this.carousel.hide();
        }
    }


    
    /**
    * @class MangoSlideViewer
    * Utility class to display slideshow in a popup
    * @constructor
    *
    * @param {String|HTMLElement} triger
    * @param {String} loadURI
    * @param {Number} numItems
    */
    MangoSlideViewer = function ( trigger, loadURI, numItems ) {

        this.loadURI = loadURI;
        this.numItems = numItems;

//        // create a dialog
//        this.dialog = new YAHOO.widget.Dialog( 'slideViewer', {
//            width   : '700px',
//            visible : false,
//            constraintviewport : true,
//            modal : true,
//            underlay : 'matte'
//        } );
//        
//
//        // create carousel container
//        this.markup = [ '<table>', '<tr>', '<td class="prev"><button id="slide-prev"> </button></td>', 
//                      '<td id="carousel-container"></td>', '<td class="next"><button id="slide-next"> </button></td>', '</tr>', '</table>'];
//        
//        // set header
//        this.dialog.setHeader( 'MangoSpring SiteGroups Features' );
//        this.dialog.setBody( this.markup.join( '' ) );
//        this.dialog.setFooter( '&nbsp;' );
//
//        this.dialog.render( document.body );
//
//        Event.on( trigger, 'click', this.show, null, this );

        // get carousel content
        
        this.fetchData();
    }

    MangoSlideViewer.prototype.show = function () {
        this.dialog.center();
        this.dialog.show();        
    }

    MangoSlideViewer.prototype.fetchData = function () {

        if ( !this.loadURI ) return;
        
        YAHOO.util.Connect.asyncRequest( 'POST',
            this.loadURI,
            {
                success : function ( xhr ) {
                    if ( xhr.responseText != '' ) {
                        this.writeCarouselContent( xhr.responseText );

                        Lang.later( 500, this, this.setupSlideShow, [ 'carousel-container' ] );
                    }
                },
                
                scope : this
            }
        );
    }

    MangoSlideViewer.prototype.writeCarouselContent = function ( content ) {
        Dom.get( 'carousel-container' ).innerHTML = content;
    }

    
    MangoSlideViewer.prototype.setupSlideShow = function ( container ) {
        Dom.removeClass('slideViewer', 'hide');
        Dom.addClass('loader_container', 'hide');
        this.slideshow = new SlideShow( container, this.numItems );

        if ( this.dialog ) {
            this.dialog.beforeHideEvent.subscribe( this.slideshow.hide, this.slideshow, true );
            this.dialog.showEvent.subscribe( this.slideshow.show, this.slideshow, true );
        }else{
            this.slideshow.show();
        }
    }

} )();