/***************************************************************
*  Copyright notice
*
*  (c) 2011 Plou Clément <cplou@inouit.com>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
 * [CLASS/FUNCTION INDEX of SCRIPT]
 *
 * Hint: use extdeveval to insert/update function index above.
 */
 
 
jQuery(document).ready(function() {
    
    var slideInterval  = 7000;   // Time in ms to next slide
    var interval;

    /**************************************
    *************** Properties ************
    ***************************************/
    var timeout; // Stock timeout function for auto-slide
        
    jQuery('.poleSlideShow').css({'overflow' : 'hidden'});
    
    jQuery('.poleSlideShow ul li img').not('li:first-child img').fadeOut(0);
    jQuery('.poleSlideShow ul li:first-child').addClass('shown');

    poleInterval = setInterval(slide,slideInterval,'.poleSlideShow');
        
    jQuery('.poleSlideShow ul li').css({'position' : 'absolute'});
    
    jQuery('.directorySlide').css({'overflow' : 'hidden'});
    
    jQuery('.directorySlide ul li img').not('li:first-child img').fadeOut(0);
    jQuery('.directorySlide ul li:first-child').addClass('shown');

    directoryInterval = setInterval(slide,slideInterval,'.directorySlide');
        
    jQuery('.directorySlide ul li').css({'position' : 'absolute'});
            
            
     if (jQuery('.areaSlideShow ul li').length != 0){
        jQuery('.areaSlideShow').css({'overflow' : 'hidden'});
        
        jQuery('.areaSlideShow ul li img').not('li:first-child img').fadeOut(0);
        jQuery('.areaSlideShow ul li:first-child').addClass('shown');

        areaInterval = setInterval(slide,slideInterval,'.areaSlideShow');
            
        jQuery('.areaSlideShow ul li').css({'position' : 'absolute'});        
    }
    else {
        jQuery('.areaSlideShow').hide();
    }   

       
        
        
        
    function getMaxImg(album){
        return jQuery(album).find('li').size();
    }
    
    function getCurrentImg(album){
        return jQuery(album).find('li.shown').prevAll().size()+1;
    }
    
    function slide(album){
        jQuery(album).each(function(iPole){
            nthChild = iPole+1;
            
            maxImg = getMaxImg(this);
            
            currentImg = getCurrentImg(this);
            
            if(currentImg == maxImg && maxImg != '1'){
                 currentImg = 1;

                 jQuery(this).find('li:first-child img').fadeIn('2000');
                 jQuery(this).find('li:first-child').addClass('shown');
                 jQuery(this).find('li:nth-child('+maxImg+') img').fadeOut('2000');
                 jQuery(this).find('li:nth-child('+maxImg+')').removeClass('shown');
             }
             else if( maxImg != '1'){
                 currentImg += 1;
                 jQuery(this).find('li:nth-child('+currentImg+') img').fadeIn('2000');
                 jQuery(this).find('li:nth-child('+currentImg+')').addClass('shown');
                 jQuery(this).find('li:nth-child('+(currentImg-1)+') img').fadeOut('2000');
                 jQuery(this).find('li:nth-child('+(currentImg-1)+')').removeClass('shown');('slow');
             }
        });
    }
});

