(function($) {
	$.fn.carousel = function(options) {
		var settings = {
			duration: 300,
			direction: "horizontal",
			minimumDrag: 20,
			beforeStart: function(){},
			afterStart: function(){},
			beforeStop: function(){},
			afterStop: function(){}
		};

		$.extend(settings, options || {});
		
		var c = this;
		var originalList = $(this);
		var pages = originalList.children();
		var width = originalList.parent().width();
		var height = originalList.parent().height();
		var start, stop;
		var currentPage = 1;  //CURRENT PAGE IS 1-BASED
		var lastClass = "";

		//CSS
		var containerCss = {position: "relative", overflow: "hidden", width: width, height: height};
		var listCss = {position: "relative", padding: "0", margin: "0", listStyle: "none", width: pages.length * width};
		var listItemCss = {width: width, height: height};

		var container = $("<div>").css(containerCss);
		var list = $("<ul id='pages'>").css(listCss);
		
		
/* ================================================================================================================== */

		this.moveLeft = function(delta, checkDrag) {
			
			//if no parameters passed, then we're moving 1 page via drag
			if(typeof delta == "undefined") {
				var delta = 1;
				var checkDrag = true;
			}
			
			//if we were dragging, see if we went far enough
			if(checkDrag) {
				if (currentPage === pages.length || c.dragDelta(0) < settings.minimumDrag) {
					list.animate({ left: -1 * width * (currentPage - 1)}, settings.duration, function(e) {
						$('body')[0].className = lastClass;
						if((currentPage - 1) == 5) $('body')[0].className = "page5";
					});
					return false;
				}
			}
			
			//move if we're not at the end
			if(currentPage + delta < numPages + 1) {
				c.move(currentPage + delta);
			}
			
			return true;
		}
		
		/* ------------------------------------------------------------------------------------ */

		this.moveRight = function(delta, checkDrag) {

			//if no parameters passed, then we're moving 1 page via drag
			if(typeof delta == "undefined") {
				var delta = 1;
				var checkDrag = true;
			}
			
			//if we were dragging, see if we went far enough
			if(checkDrag) {
				if (currentPage === 1 || c.dragDelta(0) < settings.minimumDrag) {
					list.animate({ left: -1 * width * (currentPage - 1)}, settings.duration, function(e) {
						$('body')[0].className = lastClass;
						if((currentPage - 1) == 0) $('body')[0].className = "page0";
					});    //"-=" + c.dragDelta(0)
					return false;
				}
			}
			
			//move it if we're not at the beginning
			if(currentPage - delta > 0) {
				c.move(currentPage - delta);
			}
			
			return true;
		}
		
		/* ------------------------------------------------------------------------------------ */
		
		this.move = function(newPage) {
			
			//calculate final left position of newPage
			var newLeft = -1 * width * (newPage - 1);
			
			//move
			list.animate({ left:newLeft}, settings.duration, function() {
				
				//turn selector arrow (in header nav) on when done moving
				$('body')[0].className = 'page' + (newPage - 1);
				
				//set up footer arrow
				switch(currentPage) {
					case 1:
						$('#footer a#prev').hide();
						$('#footer a#next').show();
					break;
					case numPages:
						$('#footer a#prev').show();
						$('#footer a#next').hide();
					break;
					default:
						$('#footer a#prev').show();
						$('#footer a#next').show();
					break;
				}
				
				justSetHash = true;
				setTimeout("justSetHash = false", 500);
				c.setHash();
				
				switch(currentPage) {
					case 2:
						peopleTimeout = setTimeout("changePeople()", peopleChangeSpeed);
						peopleInterval = setInterval("changePeople()", peopleChangeSpeed * 9);
					break;
					case 4:
						clearInterval(peopleInterval);
					break;
					case 5:
						peopleInterval = setInterval("changePeoplePage4()", peopleChangeSpeed * 2);
					break;
					default:
						clearInterval(peopleInterval);
				}
				
				trackPage(pageTracking[currentPage - 1].name);
				
			});
			
			currentPage = newPage;
			
		}
		
		/* ------------------------------------------------------------------------------------ */
		
		this.gotoPage = function(newPage) {
			
			//calculate difference between current and new page
			var delta = Math.abs(currentPage - newPage);
			
			//move in that direction by delta screens
			if(newPage < currentPage) {
				c.moveRight(delta);
			} else {
				c.moveLeft(delta);
			}
		}
		
		/* ------------------------------------------------------------------------------------ */

		this.dragDelta = function(ind) {
			return Math.abs(start.coords[ind] - stop.coords[ind]);
		}
		
		/* ------------------------------------------------------------------------------------ */
		
		this.setHash = function() {
			
			//check existing hash against one we want to set
			var oldHash = window.location.hash.substr(1);
			
			//determine current page and what the hash should be
			var hash;
			switch(currentPage) {
				case 1: hash = "home";    break;
				case 2: hash = "about";   break;
				case 3: hash = "how";     break;
				case 4: hash = "work";    break;
				case 5: hash = "join";    break;
				case 6: hash = "contact"; break;
			}
			
			//add new hash to current page (if we moved to a new page)
			if(oldHash != hash) {
				var baseURL = window.location.pathname;
				document.location.href = baseURL + "#" + hash;
			}
			
		}
		
		/* ------------------------------------------------------------------------------------ */
		
		this.checkHash = function() {

			var hash = window.location.hash.substr(1);
			// alert('hash is ' + hash);
			
			//determine current hash and switch the page
			switch(hash) {
				case "home"   : this.gotoPage(1);  break;
				case "about"  : this.gotoPage(2);  break;
				case "how"    : this.gotoPage(3);  break;
				case "work"   : this.gotoPage(4);  break;
				case "join"   : this.gotoPage(5);  break;
				case "contact": this.gotoPage(6);  break;
			}
			
		}
		
/* ================================================================================================================== */

		return this.each(function() {
			if (this.tagName.toLowerCase() != "ul") return;
			
			list.css({float: "left"});
			$.each(pages, function(i) {
				var li = $("<li>")
						.attr("id", "page"+i)
						.addClass("page")
						.css($.extend(listItemCss, {float: "left"}))
						.html($(this).html());
				list.append(li);
			});

			list.draggable({
				axis: "x",
				start: function(event) {
					settings.beforeStart.apply(list, arguments);

					var data = event.originalEvent.touches ? event.originalEvent.touches[0] : event;
					start = {
						coords: [ data.pageX, data.pageY ]
					};
					
					//remove selector arrow from header nav
					lastClass = $('body')[0].className;
					$('body')[0].className = "";
					
					isDragging = true;

					settings.afterStart.apply(list, arguments);
				},
				stop: function(event) {
					settings.beforeStop.apply(list, arguments);

					var data = event.originalEvent.touches ? event.originalEvent.touches[0] : event;
					stop = {
						coords: [ data.pageX, data.pageY ]
					};

					start.coords[0] > stop.coords[0] ? c.moveLeft() : c.moveRight();
					
					setTimeout("isDragging = false", 500);

					settings.afterStop.apply(list, arguments);
				}
			});

			container.append(list);

			originalList.replaceWith(container);
			
		});
	};
})(jQuery);

/*
function adjustment() {
	return width - dragDelta();
}
*/
