// wpgCarousel plugin definition
$.fn.wpgCarousel = function(options) {
	var defaults = {
		wrap: 'both',
		scroll: 1,
        start: 2,
		auto: 4,
		initCallback: fnCreateTabs,
		itemFirstInCallback:  mycarousel_itemFirstInCallback,
		initCallback: mycarousel_initCallback,
		itemVisibleInCallback: {
			onBeforeAnimation: syncTabs
		}
	};

	// Extend our default options with those provided.
	var opts = $.extend(defaults, options);
      var cnt = 0; 

	/*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) {

              $('#showhere').children().each(function(){$(this).fadeOut('slow');});

 	        $(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();
           
	}

      function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
	   var indexi = idx-1;
	   if(indexi < 1) indexi = cnt;

	   var showhere = $('#showhere');
	   $(showhere).html($('li[jcarouselindex=' + indexi.toString() + ']').html());
	   $(showhere).children().each(function(){$(this).css('display','none');});
	   $(showhere).children().each(function(){$(this).fadeIn('slow');});

	   var thehref = $('.read-more', showhere).attr('href');
	   $('.book-cover', showhere).attr("href", thehref);   
	};

	function mycarousel_initCallback(carousel) {

		$('.jcarousel-item').each(function(i) {cnt = cnt+1;}); cnt=cnt - 3;
		$('.jcarousel-item').each(function(i) {
		        var j = i+2;
		        if(j > cnt)j=j-cnt;
		 	  $('.book-cover', this).each(
		               function(){
            		       this.href='#';
		                   $(this).bind('click', function() {
                                                   carousel.scroll(j);
                                                   /* something seems to turn on the scroll somehow */
                                                   setTimeout(function(){carousel.stopAuto();},1000);
                                                   return false;});
               			});
		});

		if (carousel.options.auto) {
			$('.wrappingdiv').each(function(){
                      $(this).bind('mouseenter', function() { carousel.stopAuto(); });
                      $(this).bind('mouseleave', function() { 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() {

   /* add three (= total shown - 1) extra for loop */ 
   $('.books-published-recently-carousel ul li').each(
        function(i){if(i<3){
              $(this.parentNode).append( '<li>' + this.innerHTML + '</li>');
        }});

   $('.books-published-recently-carousel').wpgCarousel();	
});


