jQuery.fn.slideSwitch = function(time, to, nav) {
    var $active = $('.slide.active', this);
    
    var self = this;
    
    if ( $active.length == 0 ) $active = $('.slide:last', this);

    // use this to pull the divs in the order they appear in the markup
    if( to ) {
    	var $next = to;
    }
    else {
    	var $next =  $active.next('.slide', this).length ? $active.next('.slide', this)
    			: $('.slide:first', this);
    }
    
    if( nav ) {
    	var $inx = $next.attr('class').replace('slide','').replace('active','').replace(' ','').replace('-','').replace('slide','');	    	
    	$('.'+$(self).attr('id')+'_nav ul li').not(':eq('+($inx - 1)+')').removeClass('active');
    	if( $('.'+$(self).attr('id')+'_nav ul li').not(':eq('+($inx - 1)+')').find('.overlay').attr('class') != undefined ) {
    		$('.'+$(self).attr('id')+'_nav ul li').not(':eq('+($inx - 1)+')').find('.overlay').fadeIn();
    	}
    	
		$('.'+$(self).attr('id')+'_nav ul li:eq('+($inx - 1)+')').addClass('active');
		if( $('.'+$(self).attr('id')+'_nav ul li:eq('+($inx - 1)+') .overlay').attr('class') != undefined ) {
			$('.'+$(self).attr('id')+'_nav ul li:eq('+($inx - 1)+') .overlay').fadeOut();
		}
		
    }

    // uncomment below to pull the divs randomly
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, time ? time : 1000, function() {
        	$active.removeClass('active last-active');
        });
    
}

jQuery.fn.setSlide = function(index, interval, nav) {
	if( interval ) {
		eval('clearInterval('+ interval +') ');
	};
	var $active = $('.slide.active', this);	
	var $activeIndex = $active.attr('class').replace('slide','').replace('active','').replace(' ','').replace('-','').replace('slide','');
	
	if( $activeIndex != index ) {
		$(this).slideSwitch( 500, $('.slide-'+index, this), nav );
	}
	return false;
}

jQuery.fn.setNextSlide = function(interval) {
	var $active = $('.slide.active', this);
	var $next =  $active.next('.slide', this).length ? $active.next('.slide', this) : $('.slide:first', this);
	var $index = $next.attr('class').replace('slide','').replace('active','').replace(' ','').replace('-','').replace('slide','');
	this.setSlide( $index, interval, true);
	return false;
}

jQuery.fn.setPrevSlide = function(index, interval) {
	var $active = $('.slide.active', this);
	var $prev =  $active.prev('.slide', this).length ? $active.prev('.slide', this) : $('.slide:last', this);
	var $index = $prev.attr('class').replace('slide','').replace('active','').replace(' ','').replace('-','').replace('slide','');
	this.setSlide( $index, interval, true);
	return false;
}
