foo = {
	//timeout reference
	running : null,
	
	//integer current slide
	current_slide : 0,
	
	//integer animation progress
	progress : 0,

	//integer frames per second
	fps : 8,

	//object html items
	html : {},

	//integer width of each "slide"
	slide_width : 398,

	//integer slides
	slides : 9,

	//integer boundary
	boundary : -(this.slide_width * this.slides),

	//run this first
	init : function(){
		//next link
		this.html.next = dom.get("#viewmaster_next");

		//back link
		this.html.back = dom.get("#viewmaster_last");

		//div containing the "slides"
		this.html.reel = dom.get("#reel");

		//div containing the "slides"
		this.html.slides = dom.chain("#slides li");

		//go back a slide on click
		this.html.back.onclick = function(){
			foo.back();
			return false;
		}

		//go forward a slide on click
		this.html.next.onclick = function(){
			foo.next();
			return false;
		}

		this.html.slides.click(function(){
			//which slide was clicked?
			var slide = parseFloat(this.innerHTML);
			
			//switch to the appropriate slide
			foo.html.reel.style.marginLeft = -((slide-1) * foo.slide_width) + "px";
			
			//if they choose the last slide
			if(slide == 9) {
				//hide the next arrow
				foo.html.next.style.visibility = "hidden";
				foo.html.back.style.visibility = "visible";
			}
			
			//if they choose the first slide
			else if(slide == 1) {
				//hide the back arrow
				foo.html.back.style.visibility = "hidden";
				foo.html.next.style.visibility = "visible";
			}
			
			//otherwise...
			else {
				//show them both
				foo.html.back.style.visibility = "visible";
				foo.html.next.style.visibility = "visible";
			}
			
			//un-hilite last slide...
			dom.get("#slides li.selected").className = "";
			
			//and hilite this one
			this.className = "selected";
			
			//let the viewmaster know
			foo.current_slide = slide-1;
		});
	},

	//move to the next slide
	next : function(){
		//if the animation is not running
		if(!foo.running) {
			//what's the current state
			var margin = parseFloat(foo.html.reel.style.marginLeft);
			
			//in case NaN
			if(!margin) margin = 0;
			
			//is it more than the max?
			if(margin > -((this.slides-1) * this.slide_width)) {				
				//get a reference for setInterval
				var ref = foo.animate_next();

				//go to the next slide
				foo.running = setInterval(ref, 10);
			}
		}
	},

	//move to the last slide
	back : function(){
		//if the animation is not running
		if(!foo.running) {
			//what's the current state
			var margin = parseFloat(foo.html.reel.style.marginLeft);
			
			//in case NaN
			if(!margin) margin = 0;
			
			if(margin < 0) {
				//get a reference for setInterval
				var ref = foo.animate_back();

				//go to the next slide
				foo.running = setInterval(ref, 10);
			}
		}
	},

	//run the sliding animation
	animate_next : function(){
		//original fps
		var ofps = foo.fps;
	
		//animate
		return function(){
			//move to next slide
			with(foo){
				//get the current left margin
				var margin = parseFloat(html.reel.style.marginLeft);
				
				//might be NaN
				if(!margin) margin = 0;
				
				//fake tweening
				fps += ((Math.abs(margin) - ((current_slide) * slide_width))/100);

				//don't stop the animation
				if(fps < 0) fps = 1;

				//run the animation
				if((progress + fps) < slide_width) {
					//css string
					var move = margin-fps;
						
					//move the slide by the specified fps
					html.reel.style.marginLeft = move + "px";
					
					//the animation has progressed
					progress += fps;
				}
				
				else {
					//we've moved up a slide
					current_slide += 1;
					
					//move it exactly into place
					html.reel.style.marginLeft = -(current_slide * slide_width) + "px";
					
					//hide the next button if necessary
					if(current_slide == 8) {
						html.next.style.visibility = "hidden";
					}
					
					//make sure the back button is visible
					html.back.style.visibility = "visible";
					
					//stop the animation
					clearTimeout(running);
					
					//reset progress and unflag running
					running = progress = 0;
					
					//reset fps
					fps = ofps;
					
					//hilite slide
					select_slide();
				}
			}
		}
	},
	
	select_slide : function(){
		this.html.slides.each(function(){
			if(parseFloat(this.innerHTML) == (foo.current_slide+1)){
				this.className = "selected";
			} else {
				this.className = this.className.replace(/selected/,"");
			};
		});
	},
	
	//run the sliding animation
	animate_back : function(){
		//original fps
		var ofps = foo.fps;

		//animate
		return function(){
			
			//move to next slide
			with(foo){
				//get the current left margin
				var margin = parseFloat(html.reel.style.marginLeft);
				
				//might be NaN
				if(!margin) margin = 0;
				
				//fake tweening
				fps += (Math.abs((Math.abs(margin) - ((current_slide) * slide_width))/100));

				//don't stop the animation
				if(fps < 0) fps = 1;

				//run the animation
				if((progress + fps) < slide_width) {
					//css string
					var move = margin+fps;
						
					//move the slide by the specified fps
					html.reel.style.marginLeft = move + "px";
					
					//the animation has progressed
					progress += fps;
				}

				else {
					//we've moved up a slide
					current_slide -= 1;
					
					//hide the next button if necessary
					if(current_slide == 0) {
						html.back.style.visibility = "hidden";
					} 
					
					//make sure the next button is visible
					html.next.style.visibility = "visible";

					//move it exactly into place
					html.reel.style.marginLeft = -(current_slide * slide_width) + "px";
					
					//stop the animation
					clearTimeout(running);
					
					//reset progress and unflag running
					running = progress = 0;
					
					//reset fps
					fps = ofps;
					
					//hilite slide
					select_slide();
				}
			}
		}
	}
}

foo.init();