(function($){
	jQuery.fn.slideshow = function(prevPos, nextPos){
		return $(this).each(function(){
			var slideshow = $(this);
			var ul = $(this).find('ul');
			var slides = $(this).find('ul li');
			var prev = $(this).find('.buttons span.prev');
			var next = $(this).find('.buttons span.next');
			var animating = false;
			var skipPrevOut = false;
			var skipNextOut = false;
			var posLeft = -1;
			var inView = ul.find('li:first');
			var zooming = false;
			var isOver = '';

			var init = function(){
				ul.css({top:'0',left:'0'});
				if(ul.find('a.zoom').length>0){
					slideshow.find('div.canvas').append('<span class="zoom"></span>');
					slideshow.find('div.canvas>span.zoom').mouseover(function(e){
						$(this).stop(true,true);
						$(this).show();
					});
					slideshow.find('div.canvas>span.zoom').click(function(e){
						zoom();
					});
					ul.find('a.zoom').click(function(e){
						e.preventDefault();
						zoom();
					});
					
				}	
				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 zoom = function(){
				if(!zooming){
					zooming=true;
					$('body').append('<div id="zoom-bg" class="hidden"></div>');
					$('body').append('<div id="zoom-content" class="hidden"><img src="'+inView.find('a.zoom').attr('href')+'" alt="'+$('.hproduct .fn').text()+'" /><p>'+$('.hproduct .fn').text()+'</p><span class="close"></span></div>');
					$('div#zoom-content span.close').click(function(e){
						hideZoom();
					});
					$('div#zoom-content').click(function(e){
						hideZoom();
					});
					$('div#zoom-bg').css('height', $('body').height()).css('opacity',0).removeClass('hidden').fadeTo('normal',0.8,function(e){
						$('div#zoom-content').css('opacity',0).removeClass('hidden').fadeTo('fast',1);
					});
				}
			};
			
			var hideZoom = function(){
				if(zooming){
					zooming=false;
					$('div#zoom-bg').stop(false,true);
					$('div#zoom-content').stop(false,true);
					$('div#zoom-bg').fadeOut('slow',function(e){
						$('div#zoom-bg').remove();
						zooming=false;
					});
					$('div#zoom-content').fadeOut('fast',function(e){
						$('div#zoom-content').remove();
					});
				}
			};
			
			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;
			};
			
			// Hoverevents på zoom knappen
			ul.find('a.zoom').hover(
				function(e){
					if($.support.opacity){
						slideshow.find('div.canvas>span.zoom').stop(false,true).fadeIn('fast');
					}else{
						slideshow.find('div.canvas>span.zoom').show();
					}
				},
				function(e){
					if($.support.opacity){
						slideshow.find('div.canvas>span.zoom').stop(false,true).fadeOut('fast');
					}else{
						slideshow.find('div.canvas>span.zoom').hide();
					}
				}
			);
				
			// 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);
