$(document).ready(function() {
 
    //Speed of the slideshow
    var speed = 8500;
     
    //You have to specify width and height in #slider CSS properties
    //After that, the following script will set the width and height accordingly
    $('#mask-gallery2, #gallery2 li').width($('#slider2').width());   
    $('#gallery2').width($('#slider2').width() * $('#gallery2 li').length);
    $('#mask-gallery2, #gallery2 li, #mask-excerpt2, #excerpt2 li').height($('#slider2').height());
     
    //Assign a timer, so it will run periodically
    var run = setInterval('newsslider2(0)', speed); 
     
    $('#gallery2 li:first, #excerpt2 li:first').addClass('selected');
 
    //Pause the slidershow with clearInterval
    $('#btn-pause2').click(function () {
        clearInterval(run);
        return false;
    });
 
    //Continue the slideshow with setInterval
    $('#btn-play2').click(function () {
        run = setInterval('newsslider2(0)', speed); 
        return false;
    });
     
    //Next Slide by calling the function
    $('#btn-next2').click(function () {
        newsslider2(0); 
        return false;
    });
 
    //Previous slide by passing prev=1
    $('#btn-prev2').click(function () {
        newsslider2(1); 
        return false;
    });
     
    //Mouse over, pause it, on mouse out, resume the slider show
    $('#slider2').hover(
     
        function() {
            clearInterval(run);
        },
        function() {
            run = setInterval('newsslider2(0)', speed); 
        }
    ); 
     
});
 
 
function newsslider2(prev) {
 
    //Get the current selected item (with selected class), if none was found, get the first item
    var current_image = $('#gallery2 li.selected').length ? $('#gallery2 li.selected') : $('#gallery2 li:first');
    var current_excerpt = $('#excerpt2 li.selected').length ? $('#excerpt2 li.selected') : $('#excerpt2 li:first');
 
    //if prev is set to 1 (previous item)
    if (prev) {
         
        //Get previous sibling
        var next_image = (current_image.prev().length) ? current_image.prev() : $('#gallery2 li:last');
        var next_excerpt = (current_excerpt.prev().length) ? current_excerpt.prev() : $('#excerpt2 li:last');
     
    //if prev is set to 0 (next item)
    } else {
         
        //Get next sibling
        var next_image = (current_image.next().length) ? current_image.next() : $('#gallery2 li:first');
        var next_excerpt = (current_excerpt.next().length) ? current_excerpt.next() : $('#excerpt2 li:first');
    }
 
    //clear the selected class
    $('#excerpt2 li, #gallery2 li').removeClass('selected');
     
    //reassign the selected class to current items
    next_image.addClass('selected');
    next_excerpt.addClass('selected');
 
    //Scroll the items
    $('#mask-gallery2').scrollTo(next_image, 800);      
    $('#mask-excerpt2').scrollTo(next_excerpt, 800);                
     
}
