// JavaScript Document
// sliding-tabs.js 
// the Sliding Tabs mootools plugin is a creation of Jenna “Blueberry” Fox!

var intPosSlide = parseInt(0);
var intSlidingTime = parseInt(0);
var intSlidingTimeGo = parseInt(400);
var intSlidingTimeStop = parseInt(0);

var SlidingTabs = new Class({
	options: {
		startingSide: false, // sets the slide to start on, either an element or an id 
		activeButtonClass: 'active', // class to add to selected button
		activationEvent: 'click', // you can set this to ‘mouseover’ or whatever you like
		wrap: true, // calls to previous() and next() should wrap around?
		slideEffect: { // options for effect used to animate the sliding, see Fx.Base in mootools docs
			duration: intSlidingTime // 400 half a second
		}
	},
	current: null, 
	buttons: false,
	outerSlidesBox: null,
	innerSlidesBox: null,
	panes: null,
	fx: null,
	
	
	initialize: function(buttonContainer, slideContainer, options) {
		if (buttonContainer) { this.buttons = $(buttonContainer).getChildren(); }
		this.outerSlidesBox = $(slideContainer);
		this.innerSlidesBox = this.outerSlidesBox.getFirst();
		this.panes = this.innerSlidesBox.getChildren();
		
		this.setOptions(options);
		
		this.fx = new Fx.Scroll(this.outerSlidesBox, this.options.slideEffect);
		
		this.current = this.options.startingSlide ? this.panes.indexOf($(this.options.startingSlide)) : 0;
		if (this.buttons) { this.buttons[this.current].addClass(this.options.activeButtonClass); }
		
		this.outerSlidesBox.setStyle('overflow', 'hidden');
		this.panes.each(function(pane, index) {
			pane.setStyles({
			 'float': 'left',
			 'width': this.outerSlidesBox.getStyle('width'),
			 'overflow': 'hidden'
		  });
		}.bind(this));
		
		this.innerSlidesBox.setStyle('float', 'left');
		
		this.innerSlidesBox.setStyle(
			'width', (this.outerSlidesBox.offsetWidth.toInt() * this.panes.length) + 'px'
		);
		
		if (this.options.startingSlide) this.fx.toElement(this.options.startingSlide);
		
		if (this.buttons) this.buttons.each( function(button) {
		  button.addEvent(this.options.activationEvent, this.buttonEventHandler.bindWithEvent(this, button));
		}.bind(this));
	},
	
	
	changeTo: function(element) {
		var event = { cancel: false, target: $(element) };
		this.fireEvent('change', event);
		if (event.cancel == true) { return; };
		
		if (this.buttons) { this.buttons[this.current].removeClass(this.options.activeButtonClass); };
		this.current = this.panes.indexOf($(event.target));
		if (this.buttons) { this.buttons[this.current].addClass(this.options.activeButtonClass); };
		this.fx.stop();
		this.fx.toElement(event.target);
	},
	
	buttonEventHandler: function(event, button) {
		if (event.target == this.buttons[this.current]) return;
		this.changeTo(this.panes[this.buttons.indexOf($(button))]);
	},
	
	next: function() {
		intSlidingTime = intSlidingTimeGo;
		this.options.slideEffect.duration=intSlidingTimeGo;
		var next = this.current + 1;
		if (next == this.panes.length) {			
			if (this.options.wrap == true) { 
				next = 0 ;
				//alert("this.options.slideEffect="+(this.options.slideEffect.duration));					
			} else { 
				//alert("this.current="+this.current+"\n"+"next="+(next)+"\n"+"this.panes.length="+(this.panes.length - 1));
				return ;
			}
		}
		this.changeTo(this.panes[next]);
		intPosSlide++;
	},
	
	previous: function() {
		intSlidingTime = intSlidingTimeStop;
		this.options.slideEffect.duration=intSlidingTimeStop;
		var prev = this.current - 1
		if (prev < 0) {
			if (this.options.wrap == true) { prev = this.panes.length - 1 } else { return }
		}
		intPosSlide--;
		this.changeTo(this.panes[prev]);
	}

});

SlidingTabs.implement(new Options, new Events);

