var Accordeon = (function(){
	Entry = {};
	
	Config = {
		elCount: 0,
		
		speed: 450,
		easing: 'easeOutCubic',
				
		current: 0,
		step: 0,
		full: 0
	}
	
	var Accordeon;
	var Legend;
	var Swapper;
	
	Initialisers = {
		setObject: function (selector)
		{
			Accordeon = $(selector);
			Config.elCount = $('li', Accordeon).length;
		},
		
		setLegend: function (selector)
		{
			Legend = $(selector);
		},
		
		setSwapper: function (selector)
		{
			Swapper = $(selector);
		},
		
		setEvents: function()
		{
			$('li', Accordeon).mouseenter( Handlers.element_over );
		},
		
		setLegendEvents: function ()
		{
			$('li', Accordeon).mouseenter( Handlers.element_click_legend );
		},
		
		setSwapperEvents: function ()
		{
			$('li', Swapper).click( Handlers.swapperElement_click );
			$('li', Swapper).click( Handlers.swapperElement_click_legend );
		},
		
		configure: function()
		{
			Config.step = Math.round(Accordeon.width() * 20 / 100 / (Config.elCount - 1));
			Config.full =  Accordeon.width() - ((Config.elCount - 1) * Config.step);
			
			$('li', Accordeon)
			
			$('li', Accordeon).each( function(index){
				$(this).css('left', index * Config.step + 'px');
				$('div#stage'+(index+1), $(this)).css ('padding-left', '5px');
			});
			
			$('li', Accordeon).css('width', Config.full + 'px');
			$('li div.stage', Accordeon).css('width', (Config.full - 5) + 'px');
		},
		
		validate: function()
		{
			if (Accordeon.get(0) && Config.elCount > 1)
				return true;
			else
				return false;
		}
	}
	
	Animators = {
		stop: function(obj)
		{
			obj.stop();
		},
		
		move: function(obj, dest)
		{
			obj.animate( {left: dest + 'px'}, {duration: Config.speed, easing: Config.easing, queue: false} );
		},
		
		hideLegends: function ()
		{
			$('.legend-entry', Legend).hide();
		},
		
		showLegend: function (obj)
		{
			obj.show();
		},
		
		removeCurrentSelector: function ()
		{
			$('li', Swapper).removeClass('current');
		},
		
		setCurrentSelector: function (obj)
		{
			obj.addClass('current');
		}
	}
	
	Handlers = {
		element_over: function(evt)
		{
			Config.current =  $(evt.currentTarget).index();
			
			$('li', Accordeon).each(function(index){
				Animators.stop( $(this) );
				
				var dest = 0;
				if (index <= Config.current)
				{
					if (index == 0)
						dest = index * Config.step;
					else
						dest = index * Config.step;
				}
				else
					dest = (index-1) * Config.step + Config.full;
				
				Animators.move( $(this), dest );
			});
			
			if (Swapper.get(0))
			{
				var id = $('div', evt.currentTarget).attr('id');
				id = /^stage(\d+)$/.exec(id)[1];
				
				Animators.removeCurrentSelector();
				Animators.setCurrentSelector( $('#selector'+id) );
			}
		},
		
		element_click_legend: function(evt)
		{
			var id = $('div', evt.currentTarget).attr('id');
			id = /^stage(\d+)$/.exec(id)[1];
			
			Animators.hideLegends();
			Animators.showLegend( $('#legend'+id) );
		},
		
		swapperElement_click: function(evt)
		{
			var id = $(evt.currentTarget).attr('id');
			id = /^selector(\d+)$/.exec(id)[1];
			
			Config.current = $('#stage'+id).parent().index();
			
			$('li', Accordeon).each(function(index){
				Animators.stop( $(this) );
				
				var dest = 0;
				if (index <= Config.current)
				{
					if (index == 0)
						dest = index * Config.step;
					else
						dest = index * Config.step;
				}
				else
					dest = (index-1) * Config.step + Config.full;
				
				Animators.move( $(this), dest );
			});
			
			Animators.removeCurrentSelector();
			Animators.setCurrentSelector( $(this) );
		},
		
		swapperElement_click_legend: function(evt)
		{
			var id = $(evt.currentTarget).attr('id');
			id = /^selector(\d+)$/.exec(id)[1];
			
			Animators.hideLegends();
			Animators.showLegend( $('#legend'+id) );
		}
	}
	
	Entry.init = function (selector, legend_selector, swapper_selector)
	{
		Initialisers.setObject(selector);
		Initialisers.setLegend(legend_selector);
		Initialisers.setSwapper(swapper_selector);
		
		if (Initialisers.validate())
		{
			Initialisers.configure();
			Initialisers.setEvents();
		}
		
		if (Legend.get(0))
			Initialisers.setLegendEvents();
		
		if (Swapper.get(0))
			Initialisers.setSwapperEvents();
	}
	
	return Entry;
})();

$(document).ready( function(evt) {
	Accordeon.init('ul#accordeon', 'div#accordeon-legend', 'div#accordeon-swapper ul#selector');
});
