/**
 * @class	JobCarousel
 * @author	Jeffrey van der Veen
 */
var JobCarousel = new Class(
{
	/**
	 * initialize
	 * @param	string	root_id
	 * @param	integer	time_interval
	 * @param	integer	group_item_amount
	 * @return	void
	 */
	initialize: function(root_id, time_interval, group_item_amount)
	{
		// nodes
		this.root_node = root_id ? $(root_id) : null;
		
		// classes
		this.items_class	= 'items';
		this.item_class		= 'item';
		
		// strings
		this.job_prefix		= 'job_';		
		
		// settings
		this.time_interval		= time_interval ? time_interval : 5000;
		this.group_item_amount	= group_item_amount ? group_item_amount : 1;
		this.item_amount		= 0;
	},
	
	/**
	 * start
	 * @return	void
	 */
	start: function()
	{
		var _this = this;
		
		if (this.root_node)
		{
			var item_nodes = this.root_node.getElements('.'+this.item_class);
			
			if (item_nodes)
			{
				this.item_amount = item_nodes.length;

				item_nodes.each(function(node, index)
				{					
					var anchor_node = node.getElement('.title span a');
					if (anchor_node)
					{
						job         = anchor_node.get('class');
						job_id		= job.replace(_this.job_prefix, '');
						
						if (job_id)
						{
							var hyperlink = '/kandidaten/vacature-overzicht/#'+job_id;
							anchor_node.set('href', hyperlink);	
						}
						else {
							anchor_node.set('onclick', 'return false');
						}
					}
				});			
			}
			
			var timer = new Fx(
			{
				duration	: this.time_interval,
				onComplete	: function()
				{
					_this.showNextItem();
					timer.start();
				}
			});
			
			timer.start();
		}
	},
	
	/**
	 * showNextItem
	 * @return	void
	 */
	showNextItem: function()
	{
		if (this.root_node && this.item_amount > this.group_item_amount)
		{
			var items_node	= this.root_node.getElement('.'+this.items_class);
			var item_nodes	= this.root_node.getElements('.'+this.item_class);
			
			if (items_node && item_nodes)
			{
				var first_item_node		= item_nodes[0];
				var item_height			= first_item_node.getSize().y;
				var item_margin_bottom	= first_item_node.getStyle('margin-bottom').toInt();
				var items_top			= -(item_height+item_margin_bottom);
				
				var effect = new Fx.Morph(items_node,
				{
					duration	: 500,
					transition	: Fx.Transitions.Quad.easeOut,
					onComplete	: function()
					{
						first_item_node.inject(items_node);
						items_node.setStyle('top', 0);
					}
				});
				
				effect.start(
				{
					'top': items_top
				});
			}
		}
	}
});
