
// Initialize.
function init_rotator()
{
    // rotate speed and fade speed
    var rotate_speed = 6000;
    var fade_speed   = 1000;

    // Pause setting.
    var pause = false;
    var $timer = null;

    // Rotator function.
    function rotate(element){

        // stop if user has interacted
        if (pause) {
            return;
        }

        // next <li> (or first if last)
        var $next_li = $(element).next('li').length ? $(element).next('li') : $('#rotator li:first');

        // next control <a> (or first if last)
        var $next_a = $('#rotator_controls a.current').parent('li').next('li').length ? $('#rotator_controls a.current').parent('li').next('li').find('a') : $('#rotator_controls a:first');

        // add css .current class
        $('#rotator_controls a.current').parent('li').removeClass('current');
        $('#rotator_controls a.current').removeClass('current');
        $next_a.addClass('current');
        $next_a.parent('li').addClass('current');

        { // rearrange rotator controls
            var $lis = $('#rotator_controls li');
            $lis = $lis.clone(true);

            // move the first last
            var $li_first  = $lis[0];
            var $lis_other = $lis.slice(1);
            $lis = $([]);
            $lis = $lis.add( $lis_other );
            $lis = $lis.add( $($li_first) );

            // insert newly ordered <li>s in the DOM
            $('#rotator_controls li').remove();
            $lis.appendTo('#rotator_controls');
        }

        // fade out <li>
        $(element).fadeOut(fade_speed);

        // fade-in next <li> and set timer for the next rotate
        function doIt(){
            rotate($next_li);
        }
        $($next_li).fadeIn(fade_speed, function(){
            $timer = setTimeout(doIt, rotate_speed);
        });
    }

    // Add click listeners for controls.
    $('#rotator_controls a').click(function(){

        { // show the next large picture
            var $large_image_selector = '';

            // The href of the thumb is the id of the <li> around the picture
            $large_image_selector = $(this).attr('href');

            // IE6 seems to put the entire domain before href # -> remove it
            $large_image_selector = $large_image_selector.replace( /^http:.*?(#.*)$/, '$1' );

            // show selected, hide the rest
            $($large_image_selector).show().siblings('li').hide();
        }

        // Add class="current" and remove from all others.
        $(this).addClass('current').parent('li').siblings('li').find('a').removeClass('current');
        $(this).parent('li').addClass('current').siblings('li').removeClass('current');

        // rearrange thumbs
        var $lis           = null;
        var $index_current = null;
        {
            $lis = $('#rotator_controls li');
            var $li_current = $(this).parent('li');
            $index_current = $lis.index( $li_current ); // index of current element
        }

        // from now on work on a copy
        $lis = $lis.clone( true );

        { // swap old and new current
            var $old_current     = $lis[0             ];
            var $new_current     = $lis[$index_current];
            $lis[0             ] = $new_current        ;
            $lis[$index_current] = $old_current        ;
        }

        { // rotate the visible thumbs (keep the current one on the first spot)
            var $current        = $lis[0];
            var $visible_thumbs = $lis.slice(1);
            var $vt_first       = $visible_thumbs[0];
            var $vt_other       = $visible_thumbs.slice(1);

            // rebuild li-s
            $lis = $([]);
            $lis = $lis.add( $($current ) );
            $lis = $lis.add( $($vt_other) );
            $lis = $lis.add( $($vt_first) ); // and the first shall be last << Matthew 20:16 >>
        }

        // insert new order of the controls in the DOM
        $('#rotator_controls li').remove();
        $lis.appendTo('#rotator_controls');

       // Pause animation.
       pause = true;

       // Nofollow.
       this.blur();
       return false;
    });

    // Hide all but first <li>.
    $('#rotator li:first').show();

    // Kick off first time
    function doItFirstTime()
    {
        rotate($('#rotator li:visible:first'));
    }
    $timer = setTimeout(doItFirstTime, rotate_speed);
}

// Kick things off.
$(document).ready(function() {

    // init carrousel rotator script
    init_rotator();

    // submit forms
    $(    '.fp #gateway3 .link')
     .add('.fp #gateway4 .link')
     .click(function(){
        $(this).siblings('form').submit();
        return false;
    });

    // remove ghost text from gateway input boxes
    $(    ".fp #gateway3 input[type='text']")
     .add(".fp #gateway4 input[type='text']")
     .focus(function(){
            $(this).val('');
    });
});


