(function ($) {
	jQuery.fn.carousel = function (prevPos, nextPos) {
		return $(this).each(function () {

			var $slideshow = $(this);
			var $ul = $slideshow.find('ul');
			var $slides = $slideshow.find('ul li');
			var $prev = $slideshow.find('.buttons span.prev');
			var $next = $slideshow.find('.buttons span.next');
			var $inView = $ul.find('li:first');

			var animating = false;
			var skipPrevOut = false;
			var skipNextOut = false;

			var posLeft = -1;

			var zooming = false;
			var isOver = '';

			var init = function () {
				$ul.css({ top: '0', left: '0' });

				if ($ul.find('li').length > 1) {
					$prev.css('backgroundPosition', prevPos.x + 'px ' + prevPos.y + 'px').css('opacity', 0).show().fadeTo('normal', 0.4).addClass('inactive');
					$next.css('backgroundPosition', nextPos.x + 'px ' + nextPos.y + 'px').css('opacity', 0).show().fadeTo('normal', 0.4);
				}
			};



			var atFirst = function () {
				return $inView.get(0) == $ul.find('li:first').get(0);
			};

			var atLast = function () {
				return $inView.get(0) == $ul.find('li:last').get(0);
			};

			var switchSlide = function () {
				$ul.stop(true);
				$slideshow.find('div.buttons span').fadeTo('fast', 0.4);				
				$ul.animate(
					{ left: posLeft },
					500,
					'swing',
					function () {
						switch (true) {
							case atFirst():
								animating = false;
								$prev.attr('title', '').addClass('inactive');
								$next.removeClass('inactive');
								break;

							case atLast():
								animating = false;
								$next.attr('title', '').addClass('inactive');
								$prev.removeClass('inactive');
								break;

							default:
								switch (isOver) {
									case 'next':
										if (isOver)
											$next.fadeTo('fast', 1, function () {
												animating = false;
											});
										break;

									case 'prev':
										$prev.fadeTo('fast', 1, function () {
											animating = false;
										});
										break;

									default:
										animating = false;
										break;
								}
								$next.attr('title', 'Neste bilde').removeClass('inactive');
								$prev.attr('title', 'Forrige bilde').removeClass('inactive');
						}
					}
				);
			};

			var showNext = function () {
				if (!atLast() && !animating) {
					animating = true;
					$inView = $inView.next();
					posLeft = parseInt($ul.css('left')) - parseInt($inView.width());
					switchSlide();
				}
			};

			var showPrev = function () {
				if (!atFirst() && !animating) {
					animating = true;
					$inView = $inView.prev();
					posLeft = parseInt($ul.css('left')) + parseInt($inView.width());
					switchSlide();
				}
			};

			var newBGPos = function (button, move) {
				pos = button.css('backgroundPosition').split(' ');
				posLeft = parseInt(pos[0]);
				posTop = parseInt(pos[1]);
				posLeft += move;
				newPos = posLeft + 'px ' + posTop + 'px';
				return newPos;
			};



			// Klikk og hoverevents på next
			$next.click(function (e) {
				e.preventDefault();
				showNext();
			});
			$next.hover(
				function () {
					isOver = 'next';
					$next.stop(false, true);
					if (!atLast()) {
						$next.animate({ backgroundPosition: newBGPos($next, 10) }, { queue: false, duration: 150 }).fadeTo('normal', 1);
					} else {
						skipNextOut = true;
					}
				},
				function () {
					isOver = '';
					$next.stop(false, true);
					if (!skipNextOut) {
						$next.animate({ backgroundPosition: newBGPos($next, -10) }, { queue: false, duration: 150 }).fadeTo('normal', 0.4);
					}
					skipNextOut = false;
				}
			);

			// Klikk og hoverevents på prev
			$prev.click(function (e) {
				e.preventDefault();
				showPrev();
			});
			$prev.hover(
				function () {
					isOver = 'prev';
					$prev.stop(false, true);
					if (!atFirst()) {
						$prev.animate({ backgroundPosition: newBGPos($prev, -10) }, { queue: false, duration: 150 }).fadeTo('normal', 1);
					} else {
						skipPrevOut = true;
					}
				},
				function () {
					isOver = '';
					$prev.stop(false, true);
					if (!skipPrevOut) {
						prev.animate({ backgroundPosition: newBGPos($prev, 10) }, { queue: false, duration: 150 }).fadeTo('normal', 0.4);
					}
					skipPrevOut = false;
				}
			);

			init();
		});
	};

	/*
	$(document).ready(function(){
	$('.slideshow').slideshow();
	});
	*/
})(jQuery);

