FLT.slider = function () {
	var container;
	var duration;
	var width;
	var visible;
	var top;
	
	var next = 0;
	var current = 0;
	
	var items;
	var originalNumItems
	var numItems;
	var ul;
	
	var currentSet = false;
	
	var trackState;
	var hashPrefix;
	var setStateOn;
	
	var onChange;
	
	var updateView = function(animate) {
				
		if(next == -1) {
			var newLeft = -1 * (numItems-visible) * width;
			dojo.style(ul, "left", newLeft+"px");
			current = numItems - visible;
			next = numItems - visible - 1;
		}
		if(next == numItems - visible + 1) {
			dojo.style(ul, "left", "0px");
			current = 0;
			next = 1;
		}
		
		if(trackState) {
			dojo.forEach(dojo.query(setStateOn), function(node){
				node.href = node.href.replace(/#.*/, '') + '#'+hashPrefix + next
			});
		}
		
		var left = -1 * width * next - 1;
		
		current = next;
		
		onChange(current%originalNumItems);
		
		if(animate) {
			dojo.fx.slideTo({node: ul, duration: duration, left: left, top: top}).play();
		} else {
			dojo.style(ul, 'left', left+'px');
		}
	}
	
	return {
	
		init : function(o) {
			container = o.container;
			duration = o.duration;
			width = o.width;
			visible = o.visible;
			top = o.top;
			trackState = o.trackState || false;
			hashPrefix = o.hashPrefix || '';
			setStateOn = o.setStateOn || '';
			onChange = o.onChange || function(n){};
			
			dojo.connect(dojo.query ("#"+container+" .next")[0], "onclick", this, "next");
			dojo.connect(dojo.query ("#"+container+" .previous")[0], "onclick", this, "previous");
		   
			originalNumItems = dojo.query ("#"+container+" #viewport li").length;
			
		    ul = dojo.query("#"+container+" ul")[0];
			
			// duplicate a visible section for seamless scrolling
			copied = '';
			for(var i =0; i< visible; i++) {
				var liEl = dojo.query("#"+container+" li");
			    copied += '<li class="'+liEl[i].className+'">'+liEl[i].innerHTML+'</li>';
			}
			
			ul.innerHTML += copied;
			
		    items = dojo.query ("#"+container+" #viewport li");
		    numItems = items.length;
		    
		    if(trackState && location.hash.indexOf(hashPrefix) == 1) {
			    next = Number(location.hash.replace('#'+hashPrefix, ''));
			    updateView(false);
			} else if(trackState) {
				dojo.forEach(dojo.query("#"+container+" ul li"), function(node, i) {
					if(!currentSet && dojo.hasClass(node, "current")) {
						next = i;
						updateView(false);
						currentSet = true;
					}
				});				
			}
		},
		
		next : function(event) {
			dojo.stopEvent(event);
			next = current+1;
			updateView(true);
		},
		
		previous : function(event) {
			dojo.stopEvent(event);
			next = current-1;
			updateView(true);
		},
		
		set : function(n) {
			next = n;
			updateView(false);
		}
	}
};
