$(document).ready( function() {
	$('.slideLine').each(function() {
		var slideLine = this;

		// Whether the animation is running (should be 0 as there is'nt any nimation running!)
		var animationRunning = 0;

		// The width of an item
		var itemWidth = $(slideLine).find('.item').width();

		// Number of visable items
		var visableNumber = Math.floor($(slideLine).width() / $(slideLine).find('.item:first').width());
		// Margin for the 'prev' - and 'next' buttons
		var margin = ($(slideLine).width() - (visableNumber * itemWidth));

		// Set margin to enable space for 'prev' - and 'next' buttons
		$(slideLine).find('.item:first').css( { 'marginLeft': Math.ceil(margin / 2) + 'px' } );
		$(slideLine).find('.item:eq(' + (visableNumber - 1) + ')').css( { 'marginRight': Math.floor(margin / 2) + 'px' } );

		// Set style for 'prev' and 'next' -buttons
		$(slideLine).find('.prev').css( { 'width': Math.ceil(margin / 2) + 'px', 'left': '0px' } );
		$(slideLine).find('.next').css( { 'width': Math.floor(margin / 2) + 'px', 'right': '0px' } );

		function animationComplete() {
			animationRunning -= 1;
		}

		function slideLineAnimation(direction) {
			var startNumber = Math.ceil(Number($(slideLine).find('.wrapper').css('left').split('px', 1)[0]) / -itemWidth);
			if (direction > 0) {
				$(slideLine).find('.wrapper').animate( { 'left': '-=' + itemWidth + 'px' }, { queue: false, duration: 400, complete: animationComplete } );
				$(slideLine).find('.item:eq(' + startNumber + ')').animate( { 'marginLeft': '0px' }, { queue: false, duration: 400, complete: animationComplete } );
				$(slideLine).find('.item:eq(' + (startNumber + 1) + ')').animate( { 'marginLeft': Math.ceil(margin / 2) + 'px' }, { queue: false, duration: 400, complete: animationComplete } );
				$(slideLine).find('.item:eq(' + (startNumber + visableNumber - 1) + ')').animate( { 'marginRight': '0px' }, { queue: false, duration: 400, complete: animationComplete } );
				$(slideLine).find('.item:eq(' + (startNumber + visableNumber) + ')').animate( { 'marginRight': Math.floor(margin / 2) + 'px' }, { queue: false, duration: 400, complete: animationComplete } );
			} else if (direction < 0) {
				$(slideLine).find('.wrapper').animate( { 'left': '+=' + itemWidth + 'px' }, { queue: false, duration: 400, complete: animationComplete } );
				$(slideLine).find('.item:eq(' + startNumber + ')').animate( { 'marginLeft': '0px' }, { queue: false, duration: 400, complete: animationComplete } );
				$(slideLine).find('.item:eq(' + (startNumber - 1) + ')').animate( { 'marginLeft': Math.ceil(margin / 2) + 'px' }, { queue: false, duration: 400, complete: animationComplete } );
				$(slideLine).find('.item:eq(' + (startNumber + visableNumber - 1) + ')').animate( { 'marginRight': '0px' }, { queue: false, duration: 400, complete: animationComplete } );
				$(slideLine).find('.item:eq(' + (startNumber + visableNumber - 2) + ')').animate( { 'marginRight': Math.floor(margin / 2) + 'px' }, { queue: false, duration: 400, complete: animationComplete } );
			}
			$(slideLine).find('.prev').css( { 'display': (startNumber + direction <= 0 ? 'none' : 'block') } );
			$(slideLine).find('.next').css( { 'display': (startNumber + direction + visableNumber >= $(slideLine).find('.item').length ? 'none' : 'block') } );
		}

		slideLineAnimation(0);
		if (visableNumber > $(slideLine).find('.item').length) {
				$(slideLine).find('.wrapper').css( { 'width': ($(slideLine).find('.item').length * itemWidth + margin + (visableNumber - $(slideLine).find('.item').length) * itemWidth/2) + 'px' } );
				$(slideLine).find('.item:first').css( { 'marginLeft': ((visableNumber - $(slideLine).find('.item').length) * itemWidth/2 + Math.ceil(margin / 2)) + 'px' } );
		} else {
			// Width of wrapper object equals the sum of the width of all sub elements
			$(slideLine).find('.wrapper').css( { 'width': ($(slideLine).find('.item').length * itemWidth + margin) + 'px' } );

			$(slideLine).find('.next').unbind();
			$(slideLine).find('.next').bind('click', function(event) {
				event.preventDefault();
	    		if (animationRunning <= 0) {
					animationRunning = 5;
					slideLineAnimation(1);
				}
			});
			$(slideLine).find('.prev').unbind();
			$(slideLine).find('.prev').bind('click', function(event) {
				event.preventDefault();
	    		if (animationRunning <= 0) {
					animationRunning = 5;
					slideLineAnimation(-1);
				}
			});
		}

	});
});