
$(document).ready(function(){
	new KennispleinRotator();
});

var KENNISPLEIN_ROTATOR_AUTO_SWITCH_INTERVAL = 4000;
var KENNISPLEIN_ROTATOR_ITEM_COUNT = 3;
var KENNISPLEIN_ROTATOR_IMAGE_FADE_SPEED = 150;

function KennispleinRotator()
{
	this.__autoRotateIntervalHandle = null;
	
	this.__body = $('#kennisplein .body');
	
	this.__itemQueue = [];
	this.__nextCommand = null;
	
	this.__image = $(document.createElement('div'));
	this.__image.addClass('image-holder');
	this.__body.append(this.__image);
	
	this.__itemList = $('.item-list', this.__body);
	
	var self = this;
	this.__itemList
		.mouseover(function(){
			self.stopAutoRotate();
		})
		.mouseout(function(){
			self.startAutoRotate();
		});
	
	this.initItemEvents();
	
	this.makeFirstItemActive();
	this.addRemainingItemsToQueue();
	
	this.startAutoRotate();
}
$.extend(KennispleinRotator.prototype, {
	initItemEvents: function()
	{
		var self = this;
		$('.item-parts', this.__itemList)
			.mouseover(function(){
				self.setItemActive($(this).parent());
			})
			.click(function(){
				location = $(this).parent().find('.item-header a').attr('href');
			});
	},
	
	makeFirstItemActive: function()
	{
		this.setItemActive($('.item:first', this.__itemList));
	},
	
	addRemainingItemsToQueue: function()
	{
		var remainingItems = $('.item:gt(' + ( KENNISPLEIN_ROTATOR_ITEM_COUNT - 1 ) + ')', this.__itemList);
		this.__itemQueue = remainingItems.get();
		remainingItems.remove();
	},
	
	startAutoRotate: function()
	{
		var self = this;
		this.__autoRotateIntervalHandle = setInterval(
			function()
			{
				self.moveToNextItem();
			},
			KENNISPLEIN_ROTATOR_AUTO_SWITCH_INTERVAL
		);
	},
	
	stopAutoRotate: function()
	{
		if ( this.__autoRotateIntervalHandle )
		{
			clearInterval(this.__autoRotateIntervalHandle);
			this.__autoRotateIntervalHandle = null;
		}
	},
	
	moveToNextItem: function()
	{
		var activeItem = this.getActiveItem();
		if ( this.isLastItem(activeItem) )
		{
			this.rotateItems();
		}
		else
		{
			this.setItemActive(activeItem.next('.item'));
		}
	},
	
	rotateItems: function()
	{
		var self = this;
		
		var visibleItems = $('.item', this.__itemList);
		visibleItems.removeClass('active');
		visibleItems.each(function(){
			self.__itemQueue.push(this);
		});
		visibleItems.remove();
		
		var nextItems = [];
		for ( var i = 0; i < KENNISPLEIN_ROTATOR_ITEM_COUNT && this.__itemQueue.length > 0; i++ )
		{
			nextItems.push(this.__itemQueue.shift());
		}
		this.__itemList.append(nextItems);
		
		this.makeFirstItemActive();
		this.initItemEvents();
	},
	
	isLastItem: function(item)
	{
		var lastItem = $('.item:visible:last', this.__itemList);
		return item.get(0) == lastItem.get(0)
	},
	
	getActiveItem: function()
	{
		return $('.active', this.__itemList);
	},

	setItemActive: function(item)
	{
		var self = this;
		this.setNextCommand(function(){
			self.actualSetItemActive(item);
		});
	},

	actualSetItemActive: function(item)
	{
		var activeItem = this.getActiveItem();
		if ( activeItem.get(0) == item.get(0) )
		{
			this.execNextCommand();
			return;
		}
		
		$('.active', this.__itemList).removeClass('active');
		item.addClass('active');
		
		var self = this;
		this.__image.fadeOut(
			KENNISPLEIN_ROTATOR_IMAGE_FADE_SPEED,
			function()
			{
				self.setImage(item);
				self.__image.fadeIn(
					KENNISPLEIN_ROTATOR_IMAGE_FADE_SPEED,
					function()
					{
						self.execNextCommand();
					}
				);
			}
		);
	},
	
	setImage: function(item)
	{
		this.__image.css('background-image', 'url(' + $('img', item).attr('src') + ')');
	},
	
	setNextCommand: function(command)
	{
		this.__nextCommand = command;
		if ( !this.__isExecuting )
		{
			this.execNextCommand();
		}
	},
	
	execNextCommand: function()
	{
		if ( this.__nextCommand )
		{
			this.__isExecuting = true;
			var command = this.__nextCommand;
			this.__nextCommand = null;
			command();
		}
		else
		{
			this.__isExecuting = false;
		}
	}
});
