// Tips Slider
var TipsSlider = Class.create();
Object.extend(Object.extend(TipsSlider.prototype, AC.Slider.prototype), {
	orientation:'vertical',
	scrollBy:parseInt(1),

	renderItems: function() {
		var list = [];
		var lastPageStart = this.items.length-this.items.length%this.itemsPerPage;
		if (lastPageStart==this.items.length) lastPageStart = this.items.length-this.itemsPerPage;
		var overflowAmount = this.itemsPerPage-this.items.length%this.itemsPerPage;
		if (overflowAmount==this.itemsPerPage) overflowAmount = 0;
		
		// create all the pages
		for (var i=0; i<this.items.length; i++) {
			var listItem = this.items[i].render().cloneNode(true);
			list.push(listItem);
		}

		return list;
	},

	createArrows: function() {
		// previous arrow
		this.prevArrow = Builder.node('a', { 'class':'ACSliderPreviousArrow' }, '&lt;');
		Event.observe(this.prevArrow, 'click', function(evt) {
			Event.stop(evt);
			if (this.currentPage!=1) this.getPrevious();
		}.bind(this));
		this.container.appendChild(this.prevArrow);
		
		// next arrow
		this.nextArrow = Builder.node('a', { 'class':'ACSliderNextArrow' }, '&gt;');
		Event.observe(this.nextArrow, 'click', function(evt) {
			Event.stop(evt);
			if (this.currentPage!=this.totalPages) this.getNext();
		}.bind(this));
		this.container.appendChild(this.nextArrow);
	},

	positionWithinMask: function(mask) {
		this.maskInnerDimension = Element.getInnerDimensions(mask).height/this.scrollBy;
		this.list.style.top = 0;
	},

	resetPages: function() {
		if (this.currentPage == 1) {
			Element.addClassName(this.prevArrow, 'inactive');
			Element.removeClassName(this.nextArrow, 'inactive');
		} else if (this.currentPage == this.totalPages) {
			Element.addClassName(this.nextArrow, 'inactive');
			Element.removeClassName(this.prevArrow, 'inactive');
		} else {
			Element.removeClassName(this.prevArrow, 'inactive');
			Element.removeClassName(this.nextArrow, 'inactive');
		}
	},

	scrollToItem: function(index) {
		var page = null;

		var itemsPerPage = this.itemsPerPage*this.scrollBy;
		for (var i=1; i<=this.totalPages-this.scrollBy+1; i++) {
			// figure out the upper and lower bounds on this page
			var lowerBound = (i*this.itemsPerPage)-this.itemsPerPage;
			var upperBound = lowerBound+itemsPerPage-1;

			// check if it's visible on the current page so we don't scroll if we don't have to
			if (i==this.currentPage && index>=lowerBound && index<=upperBound) page = i;

			// if we don't have a page number, and we're between the bounds, set the page number
			if (!page && index>=lowerBound && index<=upperBound) page = i;
		}

		if (page!=this.currentPage) {
			this.scrolltoPageNumber(page);
		}
	},
	
	scrolltoPageNumber: function(toPageNumber, direction) {
		// if we're not going to the current page & we're not already doing something ...
		if (this.currentPage != toPageNumber && !this.isAnimating) {

			// if we don't know the direction, figure it out (-1 is next, 1 is previous)
			if (!direction) var direction = this.currentPage-toPageNumber;

			this.isAnimating = true;
			
			// set the current page to the one we're going to
			this.currentPage = toPageNumber;
			// and fix the number if we're going to before the first page or page after the last page
			this.resetPages();

			// remove the 'active' class on the page nav items
			var pageNavAnchors = this.pageNav.getElementsByTagName('a');
			for (var i=0, anchor; anchor=pageNavAnchors[i]; i++) {
				if (Element.hasClassName(anchor, 'active')) Element.removeClassName(anchor, 'active');
				if (anchor.innerHTML==this.currentPage) Element.addClassName(anchor, 'active');
			}
			// try {console.debug(this.list,"Scrolling to page", toPageNumber);} catch(e) {}
			// do the scroll
			var options = { 
				duration: 0.5,
				queue: { position:'end', scope:'sliderQueue', limit:1 },
				afterFinish: this.afterScroll.bind(this) 
			};
			
			// TODO slider needs to be reworked, pretty inflexible
			//may not always have the mask dimensions prior to this point.
			//probably need to maintain a reference to the mask itself to 
			//recalculate the dimendions if necessary. Only recalculating 
			//if it's 0 right now but could do it more often to aid in
			//flexibile resizing
			if (!this.maskInnerDimension) {
				this.positionWithinMask(this.container);
			}
			
			if (this.orientation == 'horizontal') {
				// try {console.debug("X: ", this.maskInnerDimension*direction);} catch(e) {}
				new Effect.MoveBy(this.list, 0, this.maskInnerDimension*direction, options);
			} else {
				new Effect.MoveBy(this.list, this.maskInnerDimension*direction, 0, options);
			}
		}
	}

});
