// extend Apple Core's slider class
var HotTipsSlider = Class.create();
Object.extend(Object.extend(HotTipsSlider.prototype, AC.Slider.prototype), {
	orientation:'vertical',
	scrollBy:parseInt(2),

	// get's called in initialize upon construction
	populate: function() {
		// actually populate
		var items = document.getElementsByClassName('slideritem');
		for (var i=0, item; item=items[i]; i++) {
			var sliderItem = new AC.SliderItem(item);
			sliderItem.render = function() {
				if(this.html.hasClassName('title')) return this.html;
				var li = Builder.node('li', {'class':'browseritem'}, [this.html]);
				return li;
			}
			this.items.push(sliderItem);
		}
		
		// render the slider
		this.render(6);
		this.resetPages();
	},

	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.scrollBy+1) 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-this.scrollBy+1) {
			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);
		}
	}

});
