jQuery(function(){

	jQuery.fn.lte = function(index){
		return $(Array.prototype.slice.apply(this, [0, index + 1]));
	};

	jQuery.fn.lt = function(index){
		return $(Array.prototype.slice.apply(this, [0, index]));
	};

	jQuery.fn.gt = function(index){
		return $(Array.prototype.slice.apply(this, [index + 1]));
	};

	jQuery.extend( jQuery.easing, {
		easeOutExpо: function (x, t, b, c, d) {
			return (t==d) ? b+c : c * (-Math.pow(2, -8 * t/d) + 1) + b;
		}
	});

	var $ = jQuery, active = -1, speed = 600,

	contents = $('.horizontal_accordion_content')

	.css({
		position: 'absolute',
		width: '460px',
		height: '459px',
		top: 0,
		left: '44px'
	})

	togglers = $('.horizontal_accordion_toggle')

	.each(function(index){

		var	closedx;

		$(this)

		.data('closed', true)
		.data('openx', (index + 1) * 44)
		.data('closedx', closedx = 633 - (4 - index) * 44)

		.css({
			position: 'absolute',
			width: 44,
			top: 0,
			left: closedx + 'px',
			overflow: 'hidden'
		});
	})

	.hover(
		function() {

			var current_index = togglers.index(this),
				current = togglers.eq(current_index),
				closed = current.data('closed');

			if (active == current_index) return;

			active = current_index;

			var slice, cslice, x;

			if (closed) {
				slice = 'lte';
				cslice = 'lt';
				x = 'openx';
			} else {
				cslice = slice = 'gt';
				x = 'closedx';
				togglers.eq(current_index).width(460);
			}

			togglers.stop()

			.find('>img').show().end()
			.find('>.horizontal_accordion_content').animate({left: '44px'}, {duration: speed/3, queue: false}).end()

			[slice](current_index).each(function(index){

				var self = $(this);

				self.animate({
					left: self.data(x) + 'px',
					width: index == current_index ? 460 : 44
				}, {
					duration: speed,
					easing: 'easeOutExpо',
					complete: function() {
						self.data('closed', x == 'closedx');
					}
				});
			});

			togglers.eq(active)

			.find('>img').hide().end()
			.find('>.horizontal_accordion_content').animate({left: 0}, {duration: speed/3, queue: false});
		},

		function(){}
	);


	$('#horizontal_container').show();

	$('.accordion_title').css('opacity', .8);

	$('.horizontal_home_accordion').hover(function(){},function(){

		togglers

		.find('>img')
			.show()
		.end()

		.find('>.horizontal_accordion_content')
			.animate({left: '44px'}, {duration: speed/3, queue: false})
		.end()

		.each(function(){

			var self = $(this);

			self.animate({
				left: self.data('closedx') + 'px',
				width: 44
			}, {
				duration: speed,
				easing: 'easeOutExpо',
				complete: function() {
					self.data('closed', true);
				}
			});

			active = -1;
		});
	});
});
