
function slideShower(numSlideShow) {
    var $active = $('#slideshow_' + numSlideShow + ' .slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow_' + numSlideShow + ' .slideshow IMG:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow_' + numSlideShow + ' .slideshow IMG:first');

    $active.addClass('last-active');
    
     $('#slideshow_' + numSlideShow + ' .subnav li').not('.last').removeClass();
     var index = $('#slideshow_' + numSlideShow + ' .slideshow IMG').not('.last').index($next);
   	 var chrSelector = '#slideshow_' + numSlideShow + ' .subnav li:nth(' + index + ')';
     $(chrSelector).addClass('active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

function slideSwitch(numSlideShow,numNext) {
	var chrSelector = '#slideshow_' + numSlideShow + ' .slideshow IMG:nth(' + numNext + ')';
    var $next =  $(chrSelector);
   if ($next.attr('class') == 'active') {
   	return false;
   }
   var $active = $('#slideshow_' + numSlideShow + ' .slideshow IMG.active');
   var numPrev = $active.prev()
    
   $active.addClass('last-active');

   $next.css({opacity: 0.0})
       .addClass('active')
       .animate({opacity: 1.0}, 1000, function() {
           $active.removeClass('active last-active');
       });
}

$(function() {
	
    $('.subnav li').not('.last').click(function(e) {
		e.preventDefault();
		stopShow();
		//deduct 1 as 0-based array for custom selector being passedin
		numId = parseInt($(this).text()) - 1;
		//change classes
		numSlideShow = $.ListLast($(this).parent().parent().parent().parent().attr('id'),'_');
		 $('#slideshow_' + numSlideShow + ' .subnav li').not('.last').removeClass();
		 $(this).addClass('active');
		slideSwitch(numSlideShow,numId);
	});
	bindShowOn();
    
});

function doShow(numSlideShow) {
	$('.subnav li.last a').text('start slideshow');
	$('#slideshow_' + numSlideShow + ' .subnav li.last a').text('pause slideshow');
	$('#slideshow_' + numSlideShow + ' .subnav li.last').unbind();
	$('#slideshow_' + numSlideShow + ' .subnav li.last').one('click',function(e) {
		e.preventDefault();
		stopShow();
	});
	if (typeof slideShow != 'undefined') {
		clearInterval(slideShow);
	}
	slideShow = setInterval( "slideShower(" + numSlideShow + ")", 2500 );
}

function stopShow() {
	$('.subnav li.last a').text('start slideshow');
	if (typeof slideShow != 'undefined') {
		clearInterval(slideShow);
	}
	bindShowOn();
}

function bindShowOn() {
	 $('.subnav li.last').one('click',function(e) {
		e.preventDefault();
		numSlideShow = $.ListLast($(this).parent().parent().parent().parent().attr('id'),'_');
		doShow(numSlideShow);
	});
}


