/*
Script: steadySwitch.js
	License:
		Not open source.

	Author:
		Shane Thacker / SteadyMade.com
*/

var steadySwitch = new Class({

	Implements: [Options],

	options: {
		buildNav: false,
		navClass: 'col border',
		nav: 'slide_nav',
		btnLeft: 'slideBtnLeft',
		btnRight: 'slideBtnRight',
		transition: 'quint:out',
		changeType: 'slide',
		duration: 400,
		increment: 1,
		xOffset: 0,
		yOffset: 0,
		auto: false,
		keyControl: false,
	    autointerval: 5000
	},

	initialize: function(el, options) {
		var self = this;
		this.setOptions(options);
		this.source = $(el);
		this.childElType = this.source.getFirst().get('tag');
		this.items = this.source.getChildren(this.childElType);
	
		// if(window.console) console.log('this.items: ' + this.items);
		this.sets = Math.ceil(this.items.length / this.options.increment);
		this.setDiff = (this.sets * this.options.increment) - this.items.length;
		this.scroller = new Fx.Scroll(this.source.getParent(), { wait: false, duration: this.options.duration, offset: {'x': this.options.xOffset, 'y': this.options.yOffset}, transition: this.options.transition });

		if (this.options.keyControl) {
			window.addEvent('keyup', function(e){
				e.stop();
				if (e.key == 'right') self.show(1);
				if (e.key == 'left') self.show(-1);
			});
		}

		this.currSet = 0;
		if(this.options.buildNav) this.build();
		// else this.items[0].addClass('active');
		// else $$(this.items[0], null).addClass('active');
		this.items[0].addClass('active');
		if(this.options.auto) this.auto();
	},

	build: function() {
		// if(window.console) console.log('IM IN BUILD!!!');
		var self = this;
		
		this.nav = new Element('div', {
			'id': this.options.nav,
			'class': this.options.navClass,
			'html': '<span> <a href="#" id="'+this.options.btnLeft+'">‹</a> <a href="#" id="'+this.options.btnRight+'">›</a> </span>'
		}).inject(this.source.getParent(), 'before');
		
		this.items.each(function(item, index) {
			if (index >= self.sets) return;
			var link = new Element('a', { 'html': '•', 'href': '#',
				'events': {
					'click': function(e){
						e.stop();
						if(item.hasClass('active')) return; 
						// if(window.console) console.log('Clicked with index of: ' + index );
						self.currSet = index;
						// if(window.console) console.log(self.currSet);
						self.show(null);
					}
				}
			}).inject(self.options.nav, 'bottom');
		});

		this.links = this.nav.getChildren('a');
		$$(this.items[0], this.nav.getFirst('a')).addClass('active');
		$(this.options.btnRight).addEvent('click', this.show.bind(this, 1));
		$(this.options.btnLeft).addEvent('click', this.show.bind(this, -1));
	},

	indexes: function(direction) {
		var way = (direction != null) ? direction : 0;
		// if(window.console) console.log('way: ' + way);
		// if(window.console) console.log('this.currSet + way: ' + (this.currSet + way) + ' this.currSet before: ' + this.currSet + ' sets: ' + sets);
		var sets = this.sets - 1;
		var change = this.currSet + way;
		this.currSet = (change > sets) ? 0 : (change < 0) ? sets : change;
		// if(window.console) console.log('this.currSet after: ' + this.currSet);
		var sum = this.currSet * this.options.increment;
		var newIndex = (this.currSet == sets) ? (this.currSet-1) * this.options.increment + (this.options.increment - this.setDiff) : sum;
		return newIndex;
	},

	show: function(direction) {
		var newIndex = this.indexes(direction);
		
		if(this.options.buildNav){
			this.nav.getElements('.active')[0].removeClass('active');
			this.links[this.currSet].addClass('active');
		}

		this.source.getElement(this.childElType+'.active').removeClass('active'); // old element
		this.items[newIndex].addClass('active');

		if(this.options.changeType == 'slide') this.scroller.toElement(this.items[newIndex]);

		this.auto();
	},

	auto: function(){
		if (!this.options.auto) return false;
		$clear(this.autotimer);
		this.autotimer = this.show.delay(this.options.autointerval, this, [1]);
	}

});
