(function($) {
	var t = $.tools.scrollable;
	t.plugins = t.plugins || {};
	t.plugins.navigator = {
		version: '1.0.2',
		conf: {
			navi: '.navi',
			naviItem: null,
			activeClass: 'active',
			indexed: false,
			api: false,
			idPrefix: null
		}
	};
	$.fn.navigator = function(conf) {
		var globals = $.extend({}, t.plugins.navigator.conf), ret;
		if (typeof conf == 'string') { conf = {navi: conf}; }
		conf = $.extend(globals, conf);
		this.each(function() {
			var api = $(this).scrollable(),
				 root = api.getRoot(),
				 navi = root.data("finder").call(null, conf.navi),
				 els = null,
				 buttons = api.getNaviButtons();
			if (api) { ret = api; }
			api.getNaviButtons = function() {
				return buttons.add(navi);
			};
			function reload() {
				if (!navi.children().length || navi.data("navi") == api) {
					navi.empty();
					navi.data("navi", api);
					for (var i = 0; i < api.getPageAmount(); i++) {
						navi.append($("<" + (conf.naviItem || 'a') + ">" + (i+1) + "</a>"));
					}
					els = navi.children().each(function(i) {
						var el = $(this);
						el.click(function(e) {
							api.setPage(i);
							return e.preventDefault();
						});
						if (conf.indexed)  { el.text(i); }
						if (conf.idPrefix) { el.attr("id", conf.idPrefix + i); }
					});
				} else {
					els = conf.naviItem ? navi.find(conf.naviItem) : navi.children();
					els.each(function(i) {
						var el = $(this);
						el.click(function(evt) {
							api.setPage(i);
							return evt.preventDefault();
						});
					});
				}
				els.eq(0).addClass(conf.activeClass);
			}
			api.onStart(function(e, index) {
				var cls = conf.activeClass;
				els.removeClass(cls).eq(api.getPageIndex()).addClass(cls);
			});
			api.onReload(function() {
				reload();
			});
			reload();
			var el = els.filter("[href=" + location.hash + "]");
			if (el.length) { api.move(els.index(el)); }
		});
		return conf.api ? ret : this;
	};
})(jQuery);
