﻿var changing = 0;

function slideSwitch() {
    if(changing == 0)
    {
        var $active = $('#slideshow IMG.active');

        if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

        // use this to pull the images in the order they appear in the markup
        var $next =  $active.next().length ? $active.next() : $('#slideshow IMG:first');

        // uncomment the 3 lines below to pull the images in random order

        // var $sibs  = $active.siblings();
        // var rndNum = Math.floor(Math.random() * $sibs.length );
        // var $next  = $( $sibs[ rndNum ] );
        $('#backgroundimagedownload').attr('href', '/download.aspx?file='+$next.attr("src"));
        var text = $next.attr("title");
        if(text != '')
        {
            $('#backgroundcaption').text($next.attr("title"));
            $('#backgroundcaption').show();
        }
        else
        {
            $('#backgroundcaption').hide();
        }
        
        $active.addClass('last-active');
        
        $next.addClass('active');
        $next.fadeIn(1000, function () {
                $active.fadeOut('slow');
                $active.removeClass('active last-active');
                changing = 0;
        });

    //    $next.css({opacity: 0.0})
    //        .addClass('active')
    //        .animate({opacity: 1.0}, 1000, function() {
    //            $active.removeClass('active last-active');
    //        });
    }
}

function slideSwitchBack() {
    if(changing == 0)
    {
        var $active = $('#slideshow IMG.active');

        if ( $active.length == 0 ) $active = $('#slideshow IMG:first');

        // use this to pull the images in the order they appear in the markup
        var $prev =  $active.prev().length ? $active.prev() : $('#slideshow IMG:last');

        // uncomment the 3 lines below to pull the images in random order

        // var $sibs  = $active.siblings();
        // var rndNum = Math.floor(Math.random() * $sibs.length );
        // var $next  = $( $sibs[ rndNum ] );
        $('#backgroundimagedownload').attr('href', '/download.aspx?file='+$prev.attr("src"));
        var text = $prev.attr("title");
        if(text != '')
        {
            $('#backgroundcaption').text($prev.attr("title"));
            $('#backgroundcaption').show();
        }
        else
        {
            $('#backgroundcaption').hide();
        }

        $active.addClass('last-active');

    //    $prev.css({opacity: 0.0})
    //        .addClass('active')
    //        .animate({opacity: 1.0}, 1000, function() {
    //            $active.removeClass('active last-active');
    //        });
        $prev.addClass('active');
        $prev.fadeIn(1000, function () {
                $active.fadeOut('slow');
                $active.removeClass('active last-active');
                changing = 0;
        });
    }
}

$(function() {
    $('#slideshow IMG.active').fadeIn('slow');
    
    $('#backgroundimagedownload').attr('href', '/download.aspx?file='+$('#slideshow IMG.active').attr("src"));
    
    var text = $('#slideshow IMG.active').attr("title");
    if(text != '')
    {
        $('#backgroundcaption').text(text);
        $('#backgroundcaption').show();
    }
    else
    {
        $('#backgroundcaption').hide();
    }
    
    $('#nextslide').click(function() {
            //alert("");
            slideSwitch();
    });
    $('#prevslide').click(function() {
            //alert("");
            slideSwitchBack();
    });
    
//    $('#slideshow').cycle({ 
//        fx:     'scrollLeft', 
//        speed:  300, 
//        next:   '#nextslide', 
//        timeout: 0 
//    });
});
