// wpgCarousel plugin definition
$.fn.wpgCarousel = function(options) {
	var defaults = {
		wrap: 'last',
		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 = '';
			var imagecounter = '';
			   //create anchor as tabs
			   for(var i = 1; i <= length; i++){
			    imagecounter = i - 1;
			    tabs += '<a href="#" title="'+ i +'" class="nav_tabs">' + $('.wpg-carousel-skin-tabbed').find('li:eq(' + imagecounter + ')').find('img').attr('alt') + " " + '</a>';
			   }

						if (tabs != '') {
				$(wpgCarouselTabbed).find('.jcarousel-container').prepend('<div class="jcarousel-control">'+tabs+'</div>');
				$('.jcarousel-control a').bind('click', function() {
					carousel.scroll($.jcarousel.intval($(this).attr('title')));
					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);
	});
};

$.fn.wpgSlider = function(options) {
	var defaults = {
		wrap: 'circular',
		scroll: 2,
		auto: 0,
		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-slider');
		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-slider-control a').bind('click', function() {
					carousel.scroll($.jcarousel.intval($(this).text()));
					return false;
				});
				//prevent autoscroll on hover
				if (carousel.options.auto) {
					$(wpgCarouselTabbed).find('.jcarousel-slider-container')
						.bind('mouseenter', function() { carousel.stopAuto(); })
						.bind('mouseleave', function() { carousel.startAuto(); });
				}
			}
		}
	}
	
	/*synchronize tabs*/
	function syncTabs(carousel, li, idx, action) {
		$(li).parents('.jcarousel-slider-container').find('.jcarousel-slider-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-slider');
		/*init carousel*/
		$('.wpg-carousel-slider').find('ul').jcarousel(opts);
	});
};

$.fn.wpgSliderCopy = function(options) {
	var defaults = {
		wrap: 'circular',
		scroll: 2,
		auto: 0,
		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-slider-coppy');
		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-slider-control-copy a').bind('click', function() {
					carousel.scroll($.jcarousel.intval($(this).text()));
					return false;
				});
				//prevent autoscroll on hover
				if (carousel.options.auto) {
					$(wpgCarouselTabbed).find('.jcarousel-slider-container-copy')
						.bind('mouseenter', function() { carousel.stopAuto(); })
						.bind('mouseleave', function() { carousel.startAuto(); });
				}
			}
		}
	}
	
	/*synchronize tabs*/
	function syncTabs(carousel, li, idx, action) {
		$(li).parents('.jcarousel-slider-container-copy').find('.jcarousel-slider-control-copy 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-slider-copy');
		/*init carousel*/
		$('.wpg-carousel-slider-copy').find('ul').jcarousel(opts);
	});
};

function wpgCrimezoneFilter(sortValue) {
	$(".sortorder-input").find(" input[type=text]").val(sortValue);
	$(".sortorder-input").parents('form').submit();
	return true;
};
function wpgCrimezoneClearFilter(){
	$(".serie").find(" input[type=text]").val('');
	$(".keyword").find(" input[type=text]").val('');
	$(".sortorder-input").find(" input[type=text]").val('');
	$('select[name=f588583]').val('-1'); 
	$('select[name=f588554]').val('-1'); 
	$('select[name=f799205]').val('-1'); 
	$(".sortorder-input").parents('form').submit();
	return true;
};

/**********************
 * alertmassage
**********************/

(function ($) {
    $.fn.showTopbarMessage = function (options) {

        var defaults = {
            background: "#000",
            borderColor: "#fff",
            foreColor: "#fff",
            height: "30px",
            fontSize: "16px",
            close: "click"
        };
        var options = $.extend(defaults, options);

        var barStyle = " width: 100%;position: fixed;height: " + options.height + ";top: 0px;left: 0px;right: 0px;margin: 0px;display: none; z-index: 2;";
        var overlayStyle = "height: " + options.height + ";filter: alpha(opacity=80);-moz-opacity: 0.8;-khtml-opacity: 0.8;opacity: 0.8;background:" + options.background + ";border-bottom: solid 1px " + options.borderColor + ";";
        var messageStyle = " width: 100%;position: absolute;height: " + options.height + ";top: 0px;left: 0px;right: 0px;margin: 0px;color: " + options.foreColor + ";font-weight: bold;font-size: " + options.fontSize + ";text-align: center;padding: 10px 0px";

        return this.each(function () {
            obj = $(this);

            if ($(".topbarBox").length > 0) {
                // Hide already existing bars
                $(".topbarBox").hide()
                $(".topbarBox").slideUp(200, function () {
                    $(".topbarBox").remove();
                });
            }


            var html = ""
                + "<div class='topbarBox' style='" + barStyle + "'>"
                + "  <div style='" + overlayStyle + "'>&nbsp;</div>"
                + "  <div style='" + messageStyle + "'>" + obj.html() + "</div>"
                + "</div>"

            if (options.close == "click") {
                $(html).click(function () {
                    $(this).slideUp(200, function () {
                        $(this).remove();
                    });
                }).appendTo($('body')).slideDown(200);
            }
            else {
                $(html).appendTo($('body')).slideDown(200).delay(options.close).slideUp(200, function () {
                    $(this).remove();
                });
            }

        });
    };
})(jQuery);

/**********************
 *  documentReady fn
**********************/

$(document).ready(function() {
	
	$("#sort-author").click(function(){ 
		wpgCrimezoneFilter('auteur');
		
	});
	
	$("#sort-title").click(function(){ 
		wpgCrimezoneFilter('title');
	});
	
	$("#sort-date").click(function(){ 
		wpgCrimezoneFilter('date');
	});
	
	$("#sort-rating").click(function(){ 
		wpgCrimezoneFilter('rating');
	});
	$("#clear-filters").click(function(){ 
		wpgCrimezoneClearFilter();
	});
	
	
	$("label[for='f588554']").parent('fieldset').addClass('hiddenfield');
	$("label[for='f588583']").parent('fieldset').addClass('hiddenfield');
	$("label[for='f799207']").parent('fieldset').addClass('hiddenfield');
	$("label[for='f799205']").parent('fieldset').addClass('hiddenfield');
	$("label[for='f799209']").parent('fieldset').addClass('leegfilter');
	$(".filters span.toggle").click(function() {
		$('fieldset.hiddenfield').toggle('slow', function() {});
	});

	$('.books-published-recently-carousel').wpgCarousel();	
	$('.wpg-carousel').wpgSlider();
	$('.wpg-carousel-copy').wpgSliderCopy();
	
	// menu hover effect
	$('#navigation').find('>ul >li').hover(
	        function () {
	        	clearTimeout($.data(document.body, 'menutimeout'));
	        	var el=this;
	    	    $.data(document.body, 'menutimeout', setTimeout(function() {
	    	    	$(el).siblings("li").children("ul").hide();
	    	    	$(el).children("ul").fadeIn("normal"); }, 500));
	        },function(){
	        	/* do nothing */
	});
	
	
	
	$('#navigation').find('>ul >li >ul >li').hover(
	        function () {
	        	clearTimeout($.data(document.body, 'menutimeout'));
	        },function(){
	        	var el=this;
    	    	clearTimeout($.data(document.body, 'menutimeout'));
	    	    $.data(document.body, 'menutimeout', setTimeout(function() {
	    	    	$(el).parent().parent().children("ul").hide();
	    	    	$(el).parent().parent().siblings(".current").children("ul").fadeIn("normal"); }, 500));
	});
	
	$('.wpg-carousel-hide')
			.removeClass('wpg-carousel-hide');
	
	bbImageRotator.init();
	
	 if($('#main-content #two-column-layout').length > 0){
     $('#related-content').addClass('nobackground');
     }
	
	});
	

	

	

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);
			}
			
		}
	}
}();
