(function($) {
	$.fn.moveto = function(settings) {
		var config = {
			use_single_nav: true,
			nav_selector: '.slider-nav',
			nav_link_selector: 'a',
			draggable: false,
			loading_class: 'loading',
			content_selector: null,
			start_animated: false,
			easing: 'easeInOutQuint',
			duration: 1500,
			hash_extension: '-slide',
			current_elem: null
		};

		// extend the config
		if (settings) $.extend(config, settings);
		
		// run through the individual elements
		this.each(function() {
            // the set pos method TODO: where the hell do I put this
			function setPosToElem(elem, set_hash, animate) {
                config.current_elem = elem;
				set_hash = set_hash == false ? false : true;
				
				// set active link
				$('.current-move-item').removeClass('current-move-item');
				$('a[href="#' + elem.attr('id') + '"]').parent().addClass('current-move-item');
				
				// get the positions
				var pos = elem.position();
                var top = pos.top;
				var left = pos.left;
				
				// do the move
				if (animate == true) {
					slider.animate({/*top: -top, */left: -left}, {
						duration: config.duration,
						complete: function() {
							// set the new hash
							if (set_hash)
								window.location.hash = elem[0].id + config.hash_extension;
						}
					});
				} else {
					slider.css({/*top: -top, */left: -left});
					if (set_hash)
						window.location.hash = elem[0].id + config.hash_extension;
				}
				
				// set the nav
				if(config.use_single_nav) {
					// replace with new sections nav
					var new_nav = elem.find(config.nav_selector).clone(true).show();
					single_nav.empty();
					single_nav.append(new_nav);
				}
			}
		
			var wrap = $(this);
			var slider = $('#' + wrap[0].id + '-slider');
			var content_areas = config.content_selector == null ? slider.children() : slider.find(config.content_selector);
			var navs = content_areas.find(config.nav_selector);
			var hash = window.location.hash;
						
			// this resets the hash so the animation start in the right place
			if (config.start_animated && hash != '') 
				window.location.hash = '#' + content_areas.eq(0)[0].id;
			
			// get the current hash and set it as the current elem
			if (hash != '') {
				config.current_elem = slider.find(hash.replace(config.hash_extension, ''));
				if (config.current_elem.length == 0) config.current_elem = null;
			}
			
			// double check the current_elem
			if (!config.current_elem) config.current_elem = content_areas.eq(0);

			// do the click on the nav links
			wrap.find(config.nav_link_selector).bind('click', function(event) {
				var link = $(this);
				var new_hash = this.getAttribute('href');
				
				if (new_hash == '#next') {
					if (config.current_elem.next().length !== 0) {
						var elem = config.current_elem.next();
					} else {
						var elem = content_areas.eq(0);
					}
				} else if (new_hash == '#prev') {
					if (config.current_elem.prev().length !== 0) {
						var elem = config.current_elem.prev();
					} else {
						var elem = content_areas.last();
					}
				}
				else {
					var elem = $('#'+new_hash);
				}
				console.log(config.current_elem)
                setPosToElem(elem, true, true);
				return false;
			});
			
			// set the nav, if using a single one
			if (config.use_single_nav) {
				// add the new nav to the container
				var single_nav = $('<div class="ui-slideabout-single-nav"></div>');
				wrap.append(single_nav);
				
				// hide all other navs
				navs.hide();
			}
			
			// do the initial set
			var set_hash = hash == '' ? false : true;
			var animate = config.start_animated;
			
			config.current_elem.addClass('movearound-current');
            setPosToElem(config.current_elem, set_hash, animate);
		});
 
		return this;
   }; 
}) (jQuery);
