var slideshowInterval = null;

function gotoSlide(index){
    var lastIndex = $('#home_slideshow .slides li').size() - 1;
    var currentSlideIndex = $('#home_slideshow .slides li.active').index();
    
    if(typeof index === 'undefined'){
        if(currentSlideIndex == lastIndex){
            index = 0;
        } else {
            index = currentSlideIndex + 1;            
        }

    }
    
    if(index > lastIndex){
        index = lastIndex;
    }
    
    if(index == currentSlideIndex){
        return false;
    }
    var next = null;
    var active = $('#home_slideshow .slides li.active');
    if(index > 0 && index <= lastIndex){
        next = $('#home_slideshow .slides li:eq('+index+')');
    } else {
        next = $('#home_slideshow .slides li:first');
    }
    
    if(next){
        next.css('z-index',2);//move the next image up the pile
        active.fadeOut(500,function(){//fade out the top image
            active.css('z-index',1).show().removeClass('active');//reset the z-index and unhide the image
            next.css('z-index',3).addClass('active');//make the next image the top one
        });
    }
    
}

function cycleImages(){
      var $active = $('#portfolio_cycler .active');
      var $next = ($('#portfolio_cycler .active').next().length > 0) ? $('#portfolio_cycler .active').next() : $('#portfolio_cycler img:first');
      $next.css('z-index',2);//move the next image up the pile
	  $active.fadeOut(1500,function(){//fade out the top image
	  $active.css('z-index',1).show().removeClass('active');//reset the z-index and unhide the image
      $next.css('z-index',3).addClass('active');//make the next image the top one
      });
    }

function startShow(){
    slideshowInterval = setInterval(function(){
            gotoSlide();
        }, 3500);
    return slideshowInterval;
}

function stopShow(){
    clearInterval(slideshowInterval);
}

$(function() { //on DOM ready
    startShow();
    $('#home_slideshow .markers a').click(function(e){
            e.preventDefault();
            stopShow();
            var index = $(this).data('slideindex');
            gotoSlide(index);
        });
    
    
});
//
