// 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() {

	// Following is logic to display the publisher's search results if any instead of
	// all search results. defaults to all if no publisher search results. Retains
	// publisher / all results browsing between requests (fix for #1337)
	
	// unset cookie / reset on search button click.
	$('.form-shopsearch button').click(function() {
		$.cookie("searchmode", null);
	});

	// set search display mode on click and switch if needed.
	$('a[href="#zoekresultaten-alles"]').click(function() {
		$.cookie("searchmode", "all");
		Wpg.Webshop.toggleSearchResults($('a[href="#zoekresultaten-alles"]').get(0));
	});
	
	$('a[href="#zoekresultaten-uitgever"]').click(function() {
		$.cookie("searchmode", "publisher");
		Wpg.Webshop.toggleSearchResults($('a[href="#zoekresultaten-uitgever"]').get(0));
	});

	// if there's search results in the publisher, display those instead of
	// all results. Don't do this if searchmode cookie is set to all.
	if (    ($('.search-results-publisher div.no-search-result').size() == 0)
		&& !($.cookie("searchmode") == "all")) {
		Wpg.Webshop.toggleSearchResults($('a[href="#zoekresultaten-uitgever"]').get(0));
		$.cookie("searchmode", "publisher");
	}
	
	// switch to all results if no publisher results and searchmode is publisher
	if (   ($('.search-results-publisher div.no-search-result').size() == 1)
		&& ($.cookie("searchmode") == "publisher")) {
		Wpg.Webshop.toggleSearchResults($('a[href="#zoekresultaten-alles"]').get(0));
		$.cookie("searchmode", "all");
	}
	
	// switch to publisher results on page change if publisher results was set.
	if ($.cookie("searchmode") == "publisher") {
		Wpg.Webshop.toggleSearchResults($('a[href="#zoekresultaten-uitgever"]').get(0));
	}
	
	$('.books-published-recently-carousel').wpgCarousel();	
	
	// menu hover effect
	$('#navigation').find('li').hover(
	        function () {        
			$(this).children("ul").fadeIn("normal");
	        },function(){			
	        $(this).children("ul").hide();	       
		});
});
