/*
Author:
	luistar15, <leo020588 [at] gmail.com>
License:
	MIT-style license.

Class
	noobSlide (rev.17-04-08)

Arguments:
	options - see Options below

Options:
	autoPlay: boolean | default: false
	box: dom element | required
	button_event: string | event type | default: 'click'
	buttons:{
		previous: single dom element OR dom collection| default: null
		next:  single dom element OR dom collection | default: null
		play:  single dom element OR dom collection | default: null
		playback:  single dom element OR dom collection | default: null
		stop:  single dom element OR dom collection | default: null
	}
	duration: int | transition effect | default: 700
	handle_event: string | event type | default: 'click'
	handles: dom collection | default: null
	interval: int | display of each slide | default: 5000
	nav: dom element | navigation container | default: null
	onWalk: event | pass arguments: currentItem, currentHandle | default: null
	slide: string | required
	startItem: int

Properties:
	autoPlay: boolean
	box: dom element
	button_event: string
	buttons: object
	currentIndex: int
	duration: int
	handle_event: string
	handles: dom collection
	interval: int
	items: dom collection
	nextIndex: int
	onWalk: function
	previousIndex: int

Methods:
	createFX(): transition effect
	previous(manual): walk to previous item
		manual: bolean | default:false
	next(manual): walk to next item
		manual: bolean | default:false
	play (delay,direction,wait): auto walk items
		delay: int | required
		direction: string | "previous" or "next" | required
		wait: boolean | required
	stop(): sopt auto walk
	showSlide(c): show specified slide
		c: int | required
	walk(item,manual): walk to item
		item: int | required
		manual: bolean | default:false
	addHandleButtons(handles):
		handles: dom collection | required
	addActionButtons(action,buttons):
		action: string | "previous", "next", "play", "playback", "stop" | required
		buttons: dom collection | required

*/

var noobSlide = new Class({

	initialize: function(params) {
		this._auto         = null;
		this.autoPlay      = params.autoPlay || false;
		this.box           = params.box;
		this.transition    = params.transition || 'fade';
		this.button_event  = params.button_event || 'click';
		this.buttons       = {previous: [], next: [], play: [], playback: [], stop: []};
		this.currentIndex  = params.startItem || 0;
		this.duration      = params.duration || 700;
		this.handle_event  = params.handle_event || 'click';
		this.handles       = null;
		this.interval      = params.interval || 5000;
		this.items         = $(this.box).getElements(params.slide);
		this.nextIndex     = null;
		this.onWalk        = params.onWalk || null;
		this.previousIndex = null;
		this.width         = params.width || 900;
		this.height        = params.height || 100;

		if (params.buttons) {
			for (var action in params.buttons) {
				this.addActionButtons(action, $type(params.buttons[action]) == 'array' ? params.buttons[action] : [params.buttons[action]]);
			}
		}

		if (params.handles) {
			this.handles = $(params.nav).getElements(params.handles);
			this.addHandleButtons(this.handles);
		}

		this.createFx();
		this.showSlide(this.currentIndex);
		if (this.autoPlay) {
			this.play(this.interval,'next',true);
		}
	},

    createFx: function () {
		this.slideFx = new Fx.Elements(this.items, {
			duration:   this.duration,
			transition: Fx.Transitions.Sine.easeInOut
		});
		this.items.each(function (a) {
            a.setStyle('opacity', 0)
        });
	},

	previous: function(manual){
		this.currentIndex += this.currentIndex>0 ? -1 : this.items.length-1;
		this.walk(null,manual);
	},

	next: function(manual){
		this.currentIndex += this.currentIndex<this.items.length-1 ? 1 : 1-this.items.length;
		this.walk(null,manual);
	},

	play: function(delay,direction,wait){
		this.stop();
		if(!wait){
			this[direction](false);
		}
		this._auto = this[direction].periodical(delay,this,false);
	},

	stop: function(){
		$clear(this._auto);
	},

    showSlide: function (c) {
		var d = {};

        var fade = {
        		'opacity' : [1]
        };

        var wipe = {
        		'opacity' : [1],
        		'width': [0, this.width]
        };

		this.items.each(function (a, b) {
			if (b == c) {
				if (this.transition == 'fade') {
					d[b.toString()] = fade;
				} else {
					d[b.toString()] = wipe;
				}
				textIndex = b
			} else {
				d[b.toString()] = {
					'opacity': [0]
				};
			}
		},
		this);

		this.currentIndex = c;
		this.slideFx.start(d);
	},

	walk: function(item,manual) {
		if (isNaN(item)) {
			item   = item[0];
			manual = item[1];
		}

		if ($defined(item)) {
			if (item == this.currentIndex) return;
			this.currentIndex = item;
		}

		this.previousIndex = this.currentIndex + (this.currentIndex>0 ? -1 : this.items.length-1);
		this.nextIndex     = this.currentIndex + (this.currentIndex<this.items.length-1 ? 1 : 1-this.items.length);

		if (manual) {
			this.stop();
		}

		this.showSlide(this.currentIndex);

		if (this.onWalk) { this.onWalk(this.items[this.currentIndex],(this.handles?this.handles[this.currentIndex]:null)); }
		if (manual && this.autoPlay) { this.play(this.interval,'next',true); }
	},

	addHandleButtons: function(handles){
		for (var i = 0; i < handles.length; i++) {
			handles[i].addEvent(this.handle_event, this.walk.bind(this, [i,true]));
		}
	},

	addActionButtons: function(action,buttons){
		for(var i=0; i<buttons.length; i++){
			switch(action){
				case 'previous': buttons[i].addEvent(this.button_event,this.previous.bind(this,true)); break;
				case 'next': buttons[i].addEvent(this.button_event,this.next.bind(this,true)); break;
				case 'play': buttons[i].addEvent(this.button_event,this.play.bind(this,[this.interval,'next',false])); break;
				case 'playback': buttons[i].addEvent(this.button_event,this.play.bind(this,[this.interval,'previous',false])); break;
				case 'stop': buttons[i].addEvent(this.button_event,this.stop.bind(this)); break;
			}
			this.buttons[action].push(buttons[i]);
		}
	}

});

