/*
    slideshow.js

    Copyright 2008 Nir Aides.

    This program 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 any later 
    version.

    This program 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.

    You should have received a copy of the GNU General Public License along 
    with this program; if not, write to the Free Software Foundation, Inc., 
    51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA    
*/




function slideshow(element_id, images,speed)
{
    if( typeof(speed) == 'undefined' ) speed = 13;

    if (!images.last())
    {
        images.pop();
    }

    var pe1;

    element = $(element_id);

    foo1 = function() {
        pe1.stop();

        images.find(function(image, index) {
            if (!element.src.endsWith(image.src))
                return false;

            index = ++index % images.size();
            next = images[index];

            element.alt = next.alt;
            element.src = next.src;

            return true;
        })

        pe1 = new PeriodicalExecuter(foo1, speed);
    }

    element.observe('click', foo1);
    pe1 = new PeriodicalExecuter(foo1, speed);
}

function slideshowetx(element_id, images, first_images,speed)
{
    if( typeof(speed) == 'undefined' ) speed = 13;

    if (!first_images.last())
    {
        first_images.pop();
    }

    var pe2;

    element = $(element_id);

    foo2 = function() {
        pe2.stop();

        first_images.find(
                    function(image, index) 
                    {
                        if (!element.src.endsWith(image.src))
                            return false;
                        index = ++index;
                         if(index>= first_images.size())
                         {
                            slideshow(element_id,images,speed);
                             next = images[0];
                             element.alt = next.alt;
                             element.src = next.src;
                            return false;
                         }
                        next = first_images[index];
                        element.alt = next.alt;
                        element.src = next.src;
                        return true;
                    }
                )

        pe2 = new PeriodicalExecuter(foo2, speed);
    }

    element.observe('click', foo2);
    pe2 = new PeriodicalExecuter(foo2, speed);
}

function slideshow_fadein(div_id, images)
{
    if (!images.last())
    {
        images.pop();
    }

    var pe;

    div = $(div_id);
    div.setStyle({position: 'relative'});

    img1 = div.down();
    img2 = $(document.createElement('img'));
    img2.src = img1.src;
    img2.alt = img1.alt;
    img2.setStyle({position: 'absolute', top: '0', left: '0'});
    
    div.appendChild(img2);
    
    foo = function() {
        pe.stop();

        images.find(function(image, index) {
            if (!img2.src.endsWith(image.src))
                return false;

            index = ++index % images.size();
            next = images[index];

            img1.src = img2.src;
            img1.alt = img2.alt;

            img2.hide({duration: 0.0});
            img2.src = next.src;
            img2.alt = next.alt;
            img2.appear({duration: 1.0});

            return true;
        })

        pe = new PeriodicalExecuter(foo, 6);
    }

    div.observe('click', foo);
    pe = new PeriodicalExecuter(foo, 6);
}



