function GalleryRotator (interval, animate, containerId, className, btnPrevId, btnNextId)
{
	var interval = interval=='undefined'||interval==null ? 5000 : interval;
	var animate = animate=='undefined'||animate==null ? 2000 : animate;
	var containerId = containerId=='undefined'||containerId==null ? 'gallery' : containerId;
	var className = className=='undefined'||className==null ? 'currentImage' : className;

	var btnPrevId = btnPrevId=='undefined'||btnPrevId==null ? 'gallery-prev' : btnPrevId;
	var btnNextId = btnNextId=='undefined'||btnNextId==null ? 'gallery-next' : btnNextId;

	var intervalId = null;
	var timeoutId = null;
	var stopped = false;

	var indexOfLoadedImage = 0;
	var firstLoaded = false;
	var imagesLoaded = false;

	init();
	function init ()
	{
		if (jQuery('#'+containerId+' img').length<=1) {return;}
		loadNextImage();
	}

	function loadNextImage ()
	{
		if (!jQuery('#'+containerId+' img:eq('+indexOfLoadedImage+')').length) {
			next();
			imagesLoaded = true;

			jQuery('<div id="'+btnPrevId+'"><!-- IE --></div>').click(prev).appendTo('#'+containerId);
			jQuery('<div id="'+btnNextId+'"><!-- IE --></div>').click(next).appendTo('#'+containerId);
			return;
		}

		var img = new Image();
		img.onload = function() {
			jQuery('#'+containerId+' img:eq('+indexOfLoadedImage+')').attr('src', this.src);
			setTimeout(function(){
				if (!firstLoaded) {
					jQuery('#'+containerId+' img:first').addClass(className).css({display:'block'});
					jQuery('#'+containerId+' img:gt(0)').css({display:'block',opacity:0.0});
				}
				firstLoaded = true;

				loadNextImage();

				//jQuery('#'+containerId+' img:eq()').each(function(i, n){jQuery(n).attr('src', jQuery(n).attr('rel'))});
				if (jQuery('#'+containerId+' img').length<=2) {return;}

				/*if (!intervalId && firstLoaded) {
					resume(false, function(){stop()});
				}*/

			}, 250);
			indexOfLoadedImage++;
		}
		img.src = jQuery('#'+containerId+' img:eq('+indexOfLoadedImage+')').attr('rel');
	}

	function stop ()
	{
		clearInterval(intervalId);
		stopped = true;
	}
	this.stop = stop;

	function resume (goPrev, callback)
	{
		stopped = false;
		intervalId = setInterval(function(){
				(goPrev ? prev : next)();
				if(callback!='undefined' && callback!=null){
					callback();
				}
			}, interval);
	}
	this.resume = resume;

	function next ()
	{
		clearInterval(intervalId);
		var current = jQuery('#'+containerId+' img.'+className);
		var next = current.next('img').length ? (current.next('img').hasClass(className) ? jQuery('#'+containerId+' img:first') : current.next('img')) : jQuery('#'+containerId+' img:first');

		next.css({opacity:0.0}).addClass(className).animate({opacity:1.0}, {duration:animate});
		current.animate({opacity:0.0}, {duration:animate}).removeClass(className);
		resume();
	}
	this.next = next;

	function prev ()
	{
		clearInterval(intervalId);
		var current = jQuery('#'+containerId+' img.'+className);
		var prev = current.prev('img').length ? (current.prev('img').hasClass(className) ? jQuery('#'+containerId+' img:last') : current.prev('img')) : jQuery('#'+containerId+' img:last');

		prev.css({opacity:0.0}).addClass(className).animate({opacity:1.0}, {duration:animate});
		current.animate({opacity:0.0}, {duration:animate}).removeClass(className);
		resume(1);
	}
	this.prev = prev;
}

jQuery(document).ready(function(){
	// Set the minimum width of the submenu to the width of the button, if the submenu is smaller than the button itself
	jQuery('ul#top-navigation > li ul').each(function(n, obj) {
		obj = jQuery(obj);
		var w = obj.width();
		var pw = obj.parent().width();
		if (obj.width() < obj.parent().width()) {
			jQuery(obj).width(pw - parseInt(obj.css('padding-left')) - parseInt(obj.css('padding-left')) - parseInt(obj.css('border-left-width')) - parseInt(obj.css('border-right-width')));
		}
	});

	// Set the B2B-button
	jQuery('.btn-b2b').click(function(e){
		jQuery(this).toggleClass('over');
		jQuery('#b2b-login').slideToggle('fast');
	});

	setTimeout(function() {
	// Columnize the content
	jQuery('.columnize').each(function(n, el) {
		jQuery(el).html( '<div>'+jQuery(el).html()+'</div>' );
		new MultiColumn(el, new MultiColumnSettings);
	});
	jQuery('.columnize-2').each(function(n, el) {
		jQuery(el).html( '<div>'+jQuery(el).html()+'</div>' );
		new MultiColumn(el, new MultiColumnSettings2);
	});
	}, 50);


	//jQuery('.columnize').columnize({columns:3});
	//jQuery('.columnize-2').columnize({columns:3.2, balanced:false});

	new GalleryRotator();
});
