// wpgCarousel plugin definition
$.fn.wpgCarousel = function(options) {
	var defaults = {
		wrap: 'both',
		scroll: 1,
		auto: 4,
		initCallback: fnCreateTabs,
		itemVisibleInCallback: {
			onBeforeAnimation: syncTabs
		}
	};
	// Extend our default options with those provided.
	var opts = $.extend(defaults, options);
	
	/*create tabs*/
	function fnCreateTabs(carousel) {
		var wpgCarouselTabbed = $('.wpg-carousel-skin-tabbed');
		if (wpgCarouselTabbed && wpgCarouselTabbed.length > 0) {
			var length = $(wpgCarouselTabbed).find('li').length;
			var tabs = '';
			//create anchor as tabs
			for(var i = 1; i <= length; i++){
				tabs += '<a href="#">' + i + '</a>';
			}
			if (tabs != '') {
				
				$('.jcarousel-control a').bind('click', function() {
					carousel.scroll($.jcarousel.intval($(this).text()));
					return false;
				});
				//prevent autoscroll on hover
				if (carousel.options.auto) {
					$(wpgCarouselTabbed).find('.jcarousel-container')
						.bind('mouseenter', function() { carousel.stopAuto(); })
						.bind('mouseleave', function() { carousel.startAuto(); });
				}
			}
		}
	}
	
	/*synchronize tabs*/
	function syncTabs(carousel, li, idx, action) {
		$(li).parents('.jcarousel-container').find('.jcarousel-control a')
			.removeClass('current') //a a a ...
			.parent() //.jcarousel-control
				.children('a:eq('+(idx-1)+')') //a a a ...
					.addClass('current');
		carousel.stopAuto();
		carousel.startAuto();
	}
	
	// Our plugin implementation code goes here.
	return this.each(function() {
		var $this = $(this);
		$this.addClass('wpg-carousel-skin-tabbed');
		/*init carousel*/
		$('.wpg-carousel-skin-tabbed').find('ul').jcarousel(opts);
	});
};

/**********************
 *  documentReady fn
**********************/

$(document).ready(function() {

	$('.books-published-recently-carousel').wpgCarousel();	
	
	// menu hover effect
	$('#navigation').find('li').hover(
	        function () {        
			$(this).children("ul").fadeIn("normal");
	        },function(){			
	        $(this).children("ul").hide();	       
	});
	
	bbImageRotator.init();

});

var bbImageRotator = function() {
	var rotatingImage;

	var teller = 0;

	var urls = new Array();
	var counter = 0;
	var load = true;

	// get all spans with an authorimage class and put them in the array to iterate though.
	function getImageUrls() {
		$('span.authorimage').each(function() {
			if (this.id != 'recurring') {
				urls.push($(this).html());
			}
		});
	}
	
	/**********************
	 * set logo position fn
	**********************/
	function fnLogoPosSetter() {
		$('#logo').css('left', $('#main-content').offset().left);
	}

	/**********************
	 *  background rotator 
	**********************/
	var prev = 0;
	
	function fadeInImage() {
		//console.log('Fading in');
		$(rotatingImage).fadeTo(1000, 1, function() {
			//console.log('Faded in');
		});
	}
	

	function fadeOutImage() {
		//console.log('Fading out');
		if (counter == 5) {
			//console.log('Resetting counter');
			counter = 0;
		}
		
		$(rotatingImage).fadeTo(1000, 0, function() {
			counter += 1;
			//console.log('Image faded out, counter is ' + counter);
			var imgurl;
			if ((counter % 5) == 0) {
				//console.log('Counter is 5, showing recurring image');
				imgurl = $("#recurring").html();
			} else {
				var index = Math.ceil(Math.random() * urls.length);
				while (index == prev) {
					index = Math.ceil(Math.random() * urls.length);
				}
				//console.log('Previous is ' + prev + ', current is ' + index);
				prev = index;
				imgurl = urls[index];
			}
			$(this).attr('src', imgurl);
			//console.log(counter);
			if ((counter % 5) == 0) {
				$(this).attr('class', 'centered');
				//console.log($(this));
			} else {
				$(this).attr('class', '');
				//console.log($(this));
			}
			$(this).attr('alt', counter);
		});
	}

	function showNext(){
		//console.log('showNext');
		fadeOutImage();
	}
	
	return {
		init : function() {
			//console.log('Init');
			fnLogoPosSetter();
			//get images to rotate
			getImageUrls();
			if (urls.length > 0) {
				$('body').append('<img id="rotating-background-image" />');
				rotatingImage = $('#rotating-background-image');
				$(rotatingImage).load(fadeInImage);
				
				showNext();
				setInterval(showNext, 4000);
			}
			
		}
	}
}();
