(function($) {
	
	// jQuery fixCheckBoxes 1.1
	$.fn.fixCheckBoxes=function(){return this.each(function(){var a=$(this);var b=$('<input type="hidden" />');b.attr({'id':a.attr('id')+'_hidden','name':a.attr('name')});if(a.attr('checked'))b.val('Ja');else b.val('Nee');a.after(b);a.removeAttr('name');a.click(function(){if(b.val()=='Nee')b.val('Ja');else b.val('Nee')})})};
	
	// jQuery classHandler 1.0
	$.fn.classHandler=function(a,b){var c={'mouseover':'mouseout','mouseenter':'mouseleave','focus':'blur'};var d=$(this);d.bind(a,function(){$(this).addClass(b)});d.bind(c[a],function(){$(this).removeClass(b)});return this};
	
})(jQuery);

function _enhanceSearchPage() {
	
	var container = $('#search-page');
	
	if(container.length) {
		
		// Add the complete URL to each element
		container.find('.element').each(function() {
			var element = $(this);
			var link = element.find('h2 a');
			var target = $('base').attr('href') + link.attr('href').substring(1);
			element.append('<p class="address"><a href="' + target + '" title="' + link.attr('title') + '">' + target + '</a></p>');
		});
		
	};
	
};

function _enhanceBoxedCart(selector, total, plural, singular) {
	
	var string = total == 1 ? singular : plural;
	$(selector).append(total + ' ' + string);
	
};

function replaceFormat(source, format) {
	return source.substring(0, source.indexOf('format=') + 7) + format;
};

function galleryImages() {
	
	var gallery = $('#image-gallery'),
		galleryInner = $('#image-gallery-inner'),
		elements = gallery.find('.thumbnail'),
		target = $('#collection'),
		overlay = $('<div class="overlay" />'),
		images = [],
		index = 0,
		counter = 0,
		rotation = null,
		timeout = null,
		show = true,
		currentPageIndex=null,
		//pageCount = null,
		currentFade=null;
	
	$("#image-gallery-prev").click(function(e) {
		index--;
		if(index<0)
			index=elements.length-1;
		elements.eq(index).click();
		e.preventDefault();
	});
	$("#image-gallery-next").click(function(e) {
		index++;
		if(index>elements.length-1)
			index=0;
		elements.eq(index).click();
		e.preventDefault();
	});
	
	function startRotation() {
		rotation = window.setInterval(function() {			
			index++;
			if(index>elements.length-1)
				index=0;
			elements.eq(index).click();
		}, 5000);
	};
	
	function stopRotation() {
		window.clearInterval(rotation);
	};
	
	function showGallery() {
		window.clearTimeout(timeout);
		if(!show) {
			show = true;
			gallery.stop();
			galleryInner.stop();
			gallery.animate({
				height : 106,
				opacity : 1
			}, 500);
			galleryInner.animate({
				marginTop : 0,
				opacity : 1
			}, 500);
		}
	};
	
	function hideGallery() {
		timeout = window.setTimeout(function() {
			show = false;
			gallery.stop();
			galleryInner.stop();
			gallery.animate({
				height : 10,
				opacity : .75
			}, 1000)
			galleryInner.animate({
				marginTop : -96,
				opacity : 0
			}, 1000);
		}, 1000);
	};
	
	gallery.css('overflow', 'hidden');
	target.children().hide();
	
	gallery.hover(function() {
		stopRotation();
		//showGallery();
	}, function() {
		startRotation();
		//hideGallery();
	});
	
	target.hover(function() {
		stopRotation();
		//showGallery();
	}, function() {
		startRotation();
		//hideGallery();
	});
	
	elements.children('img').each(function() {
		
		var image = $(this).clone();
		
		image.attr('src', replaceFormat(image.attr('src'), 'gallery_large'))
			.hide()
			.appendTo(target);
			
		images.push(image);
		
	});
	
	elements.click(function(event) {

		var currentIndex = elements.index(this),
			image = images[currentIndex];
			
		//pageCount.children('span').eq(0).text(currentIndex + 1);
			
		index=currentIndex;
		var newIndex=0;
		if(index>0)
			newIndex=Math.floor((index) / 5)*5;
		if(newIndex>elements.length-6)
			newIndex=elements.length-6;
		if(index>0) {
			if(newIndex==currentPageIndex && Math.floor((index) / 5) == (index)/5) {
				newIndex=newIndex=Math.ceil((currentPageIndex) / 5)*5-5;
				if(newIndex<0)
					newIndex=0;
				if(newIndex>elements.length-6)
					newIndex=elements.length-6;
			}
		}

		scroller.trigger('goto',[newIndex]);
		currentPageIndex=newIndex;
		if(currentFade)
			currentFade.stop();
		elements.children('img').css('opacity', .5);
		currentFade=$(this).children('img').fadeTo('normal', 1);

		if(image.is(':hidden')) {
			$.each(images, function() {
				$(this).hide();
			});
			image.show();
			target.height(image.height());
		};
		
		event.preventDefault();

	});
	
	/*
	$('<div id="image-gallery-count"><span>' + 0 + '</span> van de <span>'+ elements.length +'</span></div>')
		.appendTo(target);
	*/
	
	startRotation();
	elements.eq(0).click();
	index=0;
	counter=1;

};

$(function() {	

	// Navigation
	$('#navigation li.main').menu({
		dropdown : '.sub',
		effect : 'slideDown',
		swapTarget : 'a',
		ignoreDummy : true
	});
	
	// Assign classHandler to form and search fields
	$('.form input[type=text], .form textarea, #search-page input[type=text], #search input[type=text],').classHandler('focus', 'active');
	
	// Object related enhancements
	_enhanceSearchPage();

});