$(document).ready(function(){
	
	numberOfPhotos = $('#fpCount').val();
	if (numberOfPhotos == '') numberOfPhotos = 0;
	if (numberOfPhotos > 0) rotateAds(1);
	
	$.each( $('.banner'),function(i,e) {
		numberOfBanners++;
	});
	if (numberOfBanners > 0) rotateBanners(1);
	
});

var timeout;
var currentAd = 1;
var numberOfPhotos = 0;

var currentBanner = 1;
var numberOfBanners = 0;

function rotateBanners(nextBanner) {
	if (nextBanner < 1) nextBanner = 1;
	
	$('#banner' + currentBanner).fadeOut(700, function () {
		if (nextBanner > numberOfBanners) nextBanner = 1;
		$('#banner' + nextBanner).fadeIn(700);
		currentBanner = nextBanner;
				
		timeout = setTimeout(function () { rotateBanners(nextBanner+1); }, 4000);
	});
	
}

function rotateAds(nextPhoto) {
	
	if (nextPhoto < 1) nextPhoto = 1;
	
	$('#fp' + currentAd).fadeOut(400, function () {
		if (nextPhoto > numberOfPhotos) nextPhoto = 1;
		$('#fp' + nextPhoto).fadeIn(400);
		currentAd = nextPhoto;
		
		var clr;
		var tmp = '<a href="javascript: clickPic(currentAd-1);">&#171;</a>';
		for (i=1;i<=numberOfPhotos;i+=1) {
			if ( i == currentAd ) { clr = ' style="color: red;"' } else { clr = ''; }
			tmp = tmp + '&nbsp;&nbsp;<a href="javascript: clickPic(' + i + ');"' + clr + '>' + i + '</a>';
		}
		tmp = tmp + '&nbsp;&nbsp;<a href="javascript: clickPic(currentAd+1);">&#187;</a>'
		
		$('#admenu').html(tmp);
		
		timeout = setTimeout(function () { rotateAds(nextPhoto+1); }, 5000);
	});
}
		
function clickPic(pnum) {
	clearTimeout(timeout);
	rotateAds(pnum);
}

