/**
 * @fileoverview  JavaScript Slideshow
 *
 * Simple slideshow using prototype and scriptaculous.
 * 
 * Usage:
 * ------
 * <script src="prototype.js"></script>
 * <script src="effects.js"></script>
 * or (e.g.):
 * <script src="protoaculous.1.8.2.min.js"></script>
 * <script src="slideshow.js"></script>
 * <style type="text/css">
 *     #slideshow { position: relative; width: 100px; height: 100px; }
 *     #slideshow div { position: absolute; left: 0; top: 0; }
 * </style>
 * <div class="slideshow" id="slideshow">
 *     <div class="slide"><img src="slide1.jpg"></div>
 *     <div class="slideinfo"><p>slide1</p></div>
 *     <div class="slide"><img src="slide2.jpg"></div>
 *     <div class="slideinfo"><p>slide2</p></div>
 *     <div class="slide"><img src="slide3.jpg"></div>
 *     <div class="slideinfo"><p>slide3</p></div>
 * </div>
 * <script type="text/javascript">new Slideshow('slideshow', 3, 1, 1);</script>
 *
 * See also: http://blog.remvee.net/post/17
 *
 * @package    Slideshow
 * @author     R.W. van 't Veer
 * @author     Murat Purc (murat@purc.de)
 * @author     Chr. Tandler (stonecold@sctools.de)
 * @copyright  (c) 2006 - R.W. van 't Veer
 *
 * Changes:
 * 2006-06-20 by Murat Purc (murat@purc.de), Added handling of Elements having display none styles.
 * 2008-09-28 by Murat Purc (murat@purc.de), Refactoring of code
 * 2009-03-05 by Murat Purc (murat@purc.de), Further refactoring
 * 2010-06-08 by Chr. Tandler (stonecold@sctools.de), Added effects and info parameter, set timeout to seconds,
 * 2010-07-02 by Chr. Tandler (stonecold@sctools.de), modified for rGallery and changed slide-Class for table-Element,
 */


/**
 * Class Slideshow
 */
RGSlideshow = Class.create({

    /**
     * Array of slide elements
     * @type  {array}
     */
    _aSlides: [],

    /**
     * Array of slideinfo elements
     * @type  {array}
     */
    _aInfos: [],

    /**
     * Actual position
     * @type  {int}
     */
    _current: 0,

    /**
     * Time to wait between fades
     * @type  {int}
     */
    _timeout: 4000,

    /**
     * Effect wich to use
     * @type  {int}
	 *
	 * 0: Nothing
	 * 1: Random
	 * 2: DropOut
	 * 3: Fade
	 * 4: Fold
	 * 5: Shrink
	 * 6: Squish
     */
    _effect: 0,

    /**
     * Show info
     * @type  {int}
	 *
	 * 0: No
	 * 1: Yes
     */
    _info: 0,


    /**
     * Constructor
     * 
     * @param  {string}  slideshow  Id of element which contains the images
     * @param  {int}     timeout    Time between two slides in sec's
     * @param  {int}     effect     Effect wich to use (0:Nothing|1:Random|2:DropOut|3:Fade|4:Fold|5:Shrink|6:Squish)
     * @param  {int}     info       Show info slide or not (0:No|1:Yes)
     */
    initialize: function (slideshow, timeout, effect, info) {
        this._aSlides = $(slideshow).select('table.rgslide');
        this._aInfos = $(slideshow).select('div.rgslideinfo');
        var that = this;
        this._aSlides.each(function(item, pos){
            $(item).setStyle({zIndex: (that._aSlides.length - pos + 1)}).hide();
        });
        this._aInfos.each(function(item, pos){
            $(item).setStyle({zIndex: (that._aInfos.length - pos + 1)}).hide();
        });
        this._timeout = timeout * 1000;

        if (effect < 0 || effect > 7) {
			this._effect = 0;
		}
		else {
			this._effect = effect;
		}

        if (info == 1) {
			this._info = info;
		}
		else {
			this._info = 0;
		}

        $(slideshow).show();

        this._displayImage();
    },


    /**
     * Runs effects and prepares next slide
     */
    _next: function() {
        var that = this;
        this._aSlides.each(function(item, pos){
            var slide = that._aSlides[(that._current + pos) % that._aSlides.length];
            $(slide).setStyle({zIndex: (that._aSlides.length - pos)});
        });
        this._aInfos.each(function(item, pos){
            var info = that._aInfos[(that._current + pos) % that._aInfos.length];
            $(info).setStyle({zIndex: (that._aInfos.length - pos)});
        });

		var ieff = this._effect;
		var min = 2;
		var max = 6;
        if (this._effect == 1) {
			ieff = Math.floor(Math.random() * (max - min + 1)) + min;
		}
		switch (ieff) {
			case 2:
					new Effect.DropOut(this._aSlides[this._current]);
					break;
			case 3:
					new Effect.Fade(this._aSlides[this._current]);
					break;
			case 4:
					new Effect.Fold(this._aSlides[this._current]);
					break;
			case 5:
					new Effect.Shrink(this._aSlides[this._current]);
					break;
			case 6:
					new Effect.Squish(this._aSlides[this._current]);
					break;
			default:
					this._aSlides[this._current].setStyle({zIndex: 0}).show();
					break;
		}

		if (this._info == 1) {
			this._aInfos[this._current].setStyle({zIndex: 0}).hide();
		}
		
        this._current = (this._current + 1) % this._aSlides.length;

        this._displayImage();
    },


    /**
     * Displays current slide and info element and sets timer for next slide
     */
    _displayImage: function(){
        $(this._aSlides[this._current]).setStyle({display: 'inline'});
		if (this._info == 1) {
			Effect.SlideDown(this._aInfos[this._current]);
		}
        setTimeout((function(){this._next();}).bind(this), this._timeout);
    }

});