(function ($) {
	$.fn.nickcarousel = function (options) {
		return this.each(function () {
			$(".caption").hide();

			options = options || {};
			var opts = $.extend({}, $.fn.nickcarousel.defaults, options || {});

			// start values, do not edit
			opts.t = "";
			opts.playInt = "";
			opts.pauseInt = "";
			opts.pxspeed = 0;
			opts.playing = false;
			opts.isstarting = false;
			opts.ispausing = false;
			opts.startspeed = 0;
			// element declarations
			opts.wrapper = this;
			opts.container = $(">"+opts.contselector, this);
			opts.item = $(">*", opts.container);
			opts.itemcount = $(opts.item).length;
			// dimention calulation
			opts.wrapperwidth = parseInt($(opts.wrapper).width());
			// opts.containerwidth = parseInt($(opts.item).width())*parseInt($(opts.item).length);
			opts.containerwidth = 0;
			// opts.itemwidth = parseInt($(opts.item).width());
			opts.thinnestitem = 0;
			opts.widestitem = 0;
			opts.noloop = false;
			
			for(i=0;i<opts.itemcount;i++) {
				opts.containerwidth += $("img", opts.item.get(i)).outerWidth();
				if(opts.thinnestitem>$("img", opts.item.get(i)).outerWidth() || opts.thinnestitem == 0) {
					opts.thinnestitem = $("img", opts.item.get(i)).outerWidth();
				}
				if(opts.widestitem<$("img", opts.item.get(i)).outerWidth()) {
					opts.widestitem = $("img", opts.item.get(i)).outerWidth();
				}
			}

			
			
			
			//number of partly visible items
			opts.visible = Math.ceil(parseInt($(opts.wrapper).width())/opts.thinnestitem); 
			
			////$.log(opts.visible);
			
			if(opts.containerwidth<opts.wrapperwidth) {
				
				opts.noloop = true;
				
				//$.log("To few images to create a loop", true);
				//return false;
			}
			
			//width of visible items
			// opts.visiblewidth = Math.ceil(opts.widestitem * opts.visible);

			////$.log(opts.visiblewidth);
			if(!opts.noloop) {
			opts.visibleprependwidth = 0;
			opts.visibleappendwidth = 0;
			for(i=0;i<opts.visible;i++) {
				
				opts.visibleprependwidth += $("img", opts.item.get(i)).outerWidth();
				opts.visibleappendwidth += $("img", opts.item.get(opts.itemcount-i)).outerWidth();
	
			}
			
			// set the width
			opts.totalwidth = opts.containerwidth+opts.visibleprependwidth+opts.visibleappendwidth;
			$(opts.container).width(opts.totalwidth);

			// reset the position, with the first real (uncloned) item is first
			opts.startpos = -opts.visibleprependwidth;
			$(opts.container).css("left", opts.startpos);

			// add clones, after and before
			for(i=0;i<opts.visible;i++) {
				if($(opts.item).get(i)) {
					$($(opts.item).get(i)).clone(true).addClass(opts.cloneclass).appendTo(opts.container);					
				}

				if($(opts.item).get(opts.itemcount-1-i))
				$($(opts.item).get(opts.itemcount-1-i)).clone(true).addClass(opts.cloneclass).prependTo(opts.container);
			}
			}
			
			else {
				//$.log("reset and center", true);
				//$.log(opts.containerwidth, true);
				$(opts.container).css("left", opts.wrapperwidth/2-opts.containerwidth/2);
			}

		
			// selector for all items, including clones
			opts.allitems =  $(">*", opts.container);
			

			function aAnimate(elm) {
				clearTimeout(opts.t);
				
				var l = parseInt($(elm).css("left"));
				var newl = Math.round(l+opts.pxspeed);
				if(parseInt($(opts.container).css("left"))>=0) {
					$(elm).css("left", -opts.containerwidth);
				}
				
				else if(parseInt($(opts.container).css("left"))<=-(opts.totalwidth-opts.visibleappendwidth)) {
					$(elm).css("left", opts.startpos);
					////$.log("reset");
				}
				
				else {
					$(elm).css("left", newl);
				}
				

				opts.t = setTimeout(function() {
					aAnimate(elm);
				},
				opts.rate);
			}
			$(window).blur(function(){
				aPause();
			});
			
			
			function aPlay(elm, callback) {
				clearTimeout(opts.t);
				aAnimate(elm);
				opts.isstarting = true;

				if(opts.ispausing) {
					clearInterval(opts.pauseInt);
					opts.ispausing = false;
				}

				opts.playInt = setInterval(function() {
					if(opts.startspeed==0) {
							clearInterval(opts.playInt);
							opts.pxspeed = 0;
							if(callback) callback();
							opts.isstarting = false;
							opts.playing=true;
						}
					else if(opts.startspeed>opts.pxspeed && opts.pxspeed>=0) {
						if(opts.pxspeed==0) {
							//make sure you have something bigger than 0
							opts.pxspeed = 1; 
						}
						opts.pxspeed += opts.pxspeed/opts.ease;
					}

					else if((opts.startspeed)<(opts.pxspeed) && opts.pxspeed<=0) {
						if(opts.pxspeed==0) {
							//make sure you have something bigger than 0
							opts.pxspeed = -1; 
						}
						opts.pxspeed += opts.pxspeed/opts.ease;
					}
				
					else {
						clearInterval(opts.playInt);
						if(callback) callback();
						opts.isstarting = false;
						opts.playing=true;

					}



				},
				opts.rate);
			}
			function aPause(callback) {
				//do some easing and then clearTimout
				opts.ispausing = true;
				opts.pauseInt = setInterval(function() {

					if(opts.isstarting) {
						clearInterval(opts.playInt);
						opts.isstarting = false;
					}


					if(Math.floor(opts.pxspeed)>0) { 
						opts.pxspeed -= opts.pxspeed/opts.ease;
					}

					else if(Math.floor(-opts.pxspeed)>0) {
						opts.pxspeed -= opts.pxspeed/opts.ease;
					}

					else {
						clearInterval(opts.pauseInt);
						clearTimeout(opts.t);
						if(callback) callback();
						opts.ispausing = false;
						opts.pxspeed = 0;
						opts.playing=false;


					}
				},
				opts.rate)
			}
			
			function setStartSpeed(e) {
				var event = e || window.event;
				var target = event.target || event.srcElement;

				mouseposX = Math.ceil((event.pageX-$(opts.wrapper).offset().left)/parseInt($(opts.wrapper).width())*100);
				mouseposY = Math.ceil((event.pageY-$(opts.wrapper).offset().top)/parseInt($(opts.wrapper).height())*100);

				if(mouseposX>55) {	
					accel = (mouseposX-55)*2/100;
					
					opts.startspeed = -opts.speed*accel;
				}
				else if(mouseposX<45) {
					accel = (45-mouseposX)*2/100;
					opts.startspeed = opts.speed*accel;
				}
				else {
					accel = 0;
					opts.startspeed = 0;
				}
			}
			
			function mMove(e) {
				var event = e || window.event;
				var target = event.target || event.srcElement;

				mouseposX = Math.ceil((event.pageX-$(opts.wrapper).offset().left)/parseInt($(opts.wrapper).width())*100);
				mouseposY = Math.ceil((event.pageY-$(opts.wrapper).offset().top)/parseInt($(opts.wrapper).height())*100);

				if(mouseposX>55) {
					accel = (mouseposX-55)*2/100;
					
					opts.pxspeed = -opts.speed*accel;
				}
				else if(mouseposX<45) {
					accel = (45-mouseposX)*2/100;
					
					opts.pxspeed = opts.speed*accel;
				}
				else {
					accel = 0;
					opts.pxspeed = 0;
				}
			}
			
			if(!opts.noloop) {
			$(opts.wrapper).bind("mouseenter", function(e) {				
				setStartSpeed(e);
				aPlay($(opts.container), function() {
					$(document).bind("mousemove", mMove);						

				})
			}).bind("mouseleave", function(e) {
				$(document).unbind("mousemove", mMove);
				aPause();
			});
			}
			
			$(opts.allitems).each(function() {
				$("span", this).hide();
				
				$(this).mousedown(function() {
					var elm = $("a", this);
					
					window.location.hash = $(".share", this).attr("href");
					
					var that = this;
					$(".current").removeClass("current");
					$(".next").removeClass("next");
					$(".prev").removeClass("prev");
					ol($(elm), opts, function() {
						
						animateTo(that, function() {
							arrows();
						});

			
					});
				
					
					
					
					clearTimeout(opts.t);
					return false;
				}).mouseenter(function() {
					$("span.number", this).show();
				}).mouseleave(function() {
					$("span.number", this).hide();
				})
			})
			
			function animateTo(elm, callback) {

				if(!opts.noloop) {
				var itemPos = $(elm).offset().left-$(opts.wrapper).offset().left;
				var currentContainerPos = parseInt($(opts.container).css("left"));
				var itemTargetPos = parseInt($(opts.wrapper).width()/2)-parseInt($(elm).width())/2;
				var distanceToTarget = itemPos-itemTargetPos;
				var newContainerPos = currentContainerPos-distanceToTarget;
				
				var index = $(opts.allitems).index($(elm));
				//$.log(index, true);
				
				if(newContainerPos>0) {
					//$.log(">0", true);
					
					$(opts.container).css("left", -(opts.containerwidth-currentContainerPos));
					newContainerPos = -(opts.containerwidth-newContainerPos);
					
					
					var customcurrent = $(opts.allitems.get(index+opts.itemcount));
					
					//$.log((index+opts.itemcount),true);
					$(customcurrent).addClass("current");
					$(customcurrent).next("li").addClass("next");
					$(customcurrent).prev("li").addClass("prev");
					
				}
				
				else if(newContainerPos<-(opts.totalwidth-opts.visibleprependwidth)) {
					//$.log("<-", true);
					$(opts.container).css("left", opts.startpos+(opts.totalwidth-opts.visibleappendwidth)+currentContainerPos);
					newContainerPos = opts.startpos+(opts.totalwidth-opts.visibleappendwidth)+newContainerPos;
					var customcurrent = $(opts.allitems.get(index-opts.itemcount));
					//$.log(index-opts.itemcount, true);
					$(customcurrent).addClass("current");
					$(customcurrent).next("li").addClass("next");
					$(customcurrent).prev("li").addClass("prev");
				}
				
				else {
					$(elm).addClass("current");
					$(elm).next("li").addClass("next");
					$(elm).prev("li").addClass("prev");
				}
				
				var dur = -(parseInt($(opts.container).css("left"))-newContainerPos);
				if(dur<0) {
					dur = -dur;
				}
				
				dur = dur*opts.speed;
				// dur = 1000;	
					$(opts.container).animate({
							left: newContainerPos
						}, dur, "swing", function() {
							if(callback) callback();
						});
				}
				//if to few images
				else {
					
					var index = $(opts.item).index($(elm));
					
					if(index==0) {
						$("#lookbook li:last-child").addClass("prev");
					}
					else {
						$(elm).prev("li").addClass("prev");
						
					}
					if(index==opts.itemcount-1) {
						$("#lookbook li:first-child").addClass("next");
						
					}
					else {
						$(elm).next("li").addClass("next");
						
					}
					
					$(elm).addClass("current");
					if(callback) callback();
					
				}
			}
		
			

		});
			
	};



	$.fn.nickcarousel.defaults = {
		speed: 5,	// top speed (pixels per second)
		ease: 20,	// easing, higher number = slower easing
		rate: 20,	// animation speed, higher number, slower animation, higher number, smoother animation, but more cpu intense
		contselector: "*", // selector of clone container 
		cloneclass: "clone" // class of the clones

	};


	})(jQuery);
	
		jQuery.log = function(message, force) {
			if(window.console && !force) {
				console.debug(message);
			} 
			else {
				
					if($("#debug").length==0) {
							$("<ul />").attr("id", "debug").css({
								position: "absolute",
								bottom: 0,
								right: 0,
								left: 0,
								height: "100px",
								zIndex: 200,
								background: "#000",
								color: "#fff",
								opacity: 0.5,
								margin: 0,
								padding: 10,
								overflow: "auto",
								listStyle: "none",
								fontFamily: "monaco, monospace"
							}).appendTo("body").hide().slideDown().click(function() {
								$(this).slideUp("fast", function() {
									$(this).remove();
								});
							});
					}
				
					$("#debug").prepend("<li>" + message + "</li>");
					
			}

		};


