var SlidingTabs = new Class({
	options: {
		startingSide: false,
		activeButtonClass: 'active',
		activationEvent: 'click',
		wrap: true, 
		slideEffect: { 
			duration: 400 // 
		}
	},
	current: null, 
	buttons: false,
	outerSlidesBox: null,
	innerSlidesBox: null,
	panes: null,
	fx: null,
	
	
	initialize: function(tabsContainer, slideContainer, numbers, nextButton, prevButton, options) {
		if (tabsContainer) {this.buttons = $(tabsContainer).getElements('h2 a');}
		this.outerSlidesBox = $(slideContainer);
		this.innerSlidesBox = this.outerSlidesBox.getFirst();
		this.panes = this.innerSlidesBox.getChildren();
		if(numbers) this.slideNumbers = $(numbers);
		
		this.setOptions(options);
		
		this.fx = new Fx.Scroll(this.outerSlidesBox, this.options.slideEffect);
		
		// set up button highlight
		this.current = this.options.startingSlide ? this.panes.indexOf($(this.options.startingSlide)) : 0;
		if (this.buttons) { this.buttons[this.current].addClass(this.options.activeButtonClass); }
		
		// add needed stylings
		this.outerSlidesBox.setStyle('overflow', 'hidden');
		this.panes.each(function(pane, index) {
			pane.setStyles({
			 'float': 'left',
			 'width': this.outerSlidesBox.getStyle('width'),
			 'overflow': 'hidden',
			 'display':'block'
		  });
		}.bind(this));
		
		// stupidness to make IE work - it boggles the mind why this has any effect
		// maybe it's something to do with giving it layout?
		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);
		
		// add events to the buttons
		if (this.buttons) this.buttons.each( function(button) {
		  button.addEvent(this.options.activationEvent, this.buttonEventHandler.bindWithEvent(this, button));
		}.bind(this));
		if (nextButton) $(nextButton).addEvent('click', this.previous.bindWithEvent(this));
		if (prevButton) $(prevButton).addEvent('click', this.next.bindWithEvent(this));		
		if (this.slideNumbers) this.slideNumbers.setHTML(this.convertToFarsi(this.current.toInt()+1) + '/' + this.convertToFarsi(this.panes.length.toInt()));
	},
	
	
	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);
		if (this.slideNumbers) this.slideNumbers.setHTML(this.convertToFarsi(this.current.toInt()+1) + '/' + this.convertToFarsi(this.panes.length.toInt()));
	},
	
	// Handles a click
	buttonEventHandler: function(event, button) {
		event.stop();
		if (event.target == this.buttons[this.current]) return;
		this.changeTo(this.panes[this.buttons.indexOf($(button))]);
	},
	convertToFarsi: function(str) {
	var output = '';
	var c;
	var numbers = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
	var str = str.toString();
	for(var i=0; i < str.length; i++) {
		c = str.charCodeAt(i);
		if(c >= 48 && c <= 57) {
			output += numbers[c - 48];
		} else {
			output += String.fromCharCode(c);
		}
	}
	return output;
	},
	
	next: function(evt) {
		evt.stop();
		var next = this.current + 1;
		if (next == this.panes.length) {
			if (this.options.wrap == true) { next = 0 } else { return }
		}
		
		this.changeTo(this.panes[next]);
	},
	
	previous: function(evt) {
		evt.stop();
		var prev = this.current - 1
		if (prev < 0) {
			if (this.options.wrap == true) { prev = this.panes.length - 1 } else { return }
		}
		
		this.changeTo(this.panes[prev]);
	}
});

SlidingTabs.implement(new Options, new Events);
