

function theRotator() {
	//Set the opacity of all images to 0
	$j('div.rotator ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$j('div.rotator ul li:first').css({opacity: 1.0});
	
	
	$j('#rotator-arrow-l').click(function(){
		clearInterval(rotateTimer);
		rotate('prev');
	});
	$j('#rotator-arrow-r').click(function(){
		clearInterval(rotateTimer);
		rotate();
	});
	
	$j('.rotator-dot').click(function(){
		clearInterval(rotateTimer);
		rotate('specific', $j(this).attr('id'));
	});
	$j('.rotator').mouseenter(function(){
		clearInterval(rotateTimer);
	});
	$j('.rotator').mouseleave(function(){
		clearInterval(rotateTimer);
		rotateTimer = setInterval('rotate()',6000);
	});

	
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	
	var rotateTimer = setInterval('rotate()',6000);
	
}

function rotate(dir, num) {
	var current;
	var next;
	var dot = $j('.current-dot:first');

	switch (dir) {
		case 'specific':
			current = $j('div.rotator ul li.show:first');
			next = $j('div.rotator ul li').eq(num);
			dotnext = $j('.rotator-dot').eq(num);
			
			break;
		case 'prev':
			current = ($j('div.rotator ul li.show')?  $j('div.rotator ul li.show') : $j('div.rotator ul li:last'));
			if ( current.length == 0 ) current = $j('div.rotator ul li:last');
			next = ((current.prev().length) ? ((current.prev().hasClass('show')) ? $j('div.rotator ul li:last') :current.prev()) : $j('div.rotator ul li:last'));
			
			if (dot.prev().length != 0) {
				var dotnext = dot.prev();		
			} else {
				var dotnext = $j('.rotator-dot:last');
			}
			
			break;
		default:
			current = ($j('div.rotator ul li.show')?  $j('div.rotator ul li.show') : $j('div.rotator ul li:first'));
			if ( current.length == 0 ) current = $j('div.rotator ul li:first');
			next = ((current.next().length) ? ((current.next().hasClass('show')) ? $j('div.rotator ul li:first') :current.next()) : $j('div.rotator ul li:first'));
			
			if (dot.next().length == 0) {
				
			}
			
			if (dot.next().length != 0) {
				var dotnext = dot.next();		
			} else {
				var dotnext = $j('.rotator-dot:first');
			}

			break;
	}
	
	//Un-comment the 3 lines below to get the images in random order
	
	//var sibs = current.siblings();
        //var rndNum = Math.floor(Math.random() * sibs.length );
        //var next = $j( sibs[ rndNum ] );
	
	//Hide the current image
	$j('div.rotator ul li').animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.stop(true, false).css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);
	

	
	dot.removeClass('current-dot');
	dotnext.addClass('current-dot');
}


