/*
Script:
	Blackbox v0.11 beta
	
Author:
	Yichuan Shen, <http://labs.hostsen.net/>

License:
	MIT-style license
*/

var blackboxClass = new Class({
	options: {
		bgcolor: '#000',
		duration: 500,
		height: 200,
		start: 1,
		wait: 5000
	},
	
	initialize: function(elements, options) {
		this.setOptions(options)
		
		elements.each(function(ulel) { // ulel = ul-Element
			ulel.setStyle('height', this.options.height);
			
			ulel.setStyles({
				position: 'relative'
			});
			
			ulel.getChildren().each(function(liel, i) { // liel = li-Element
				liel.addClass('blackbox' + ++i);
				liel.setStyles({
					backgroundColor: this.options.bgcolor,
					display: 'block',
					opacity: 0,
					overflow: 'hidden',
					position: 'absolute',
					top: 0,
					right: 0,
					bottom: 0,
					left: 0,
					zIndex: 1
				});
				
				// if first
				if(i == this.options.start) {
					liel.setStyles({
						opacity: 1,
						zIndex: 2
					});
				// if second
				} else if(i == this.getNextItem(ulel, this.options.start)) {
					liel.setOpacity(1);
				}
			}.bind(this));
			
			this.start.delay(this.options.wait, this, [ulel, this.options.start]);
		}.bind(this));
	},

	start: function(el, i) { // el = ul-Element
		var cur = el.getElement('.blackbox' + i);
		var next = this.getNextItem(el, i);
		
		cur.effect('opacity', {duration: this.options.duration}).start(0).chain(function() {
			cur.setStyle('z-index', 1);
			
			el.getElement('.blackbox' + next).setStyle('z-index', '2');
			el.getElement('.blackbox' + this.getNextItem(el, next)).setOpacity('1');
			
			this.start.delay(this.options.wait, this, [el, next]);
		}.bind(this));
	},

	getNextItem: function(el, i) {
		var bool;
		
		el.getChildren().each(function(liel) {
			if(liel.hasClass('blackbox' + (i+1))) {
				bool = true;
			}
		});
		
		// if next item exists
		if(bool) {
			return i+1;
		// else return to the first item
		} else {
			return 1;
		}
	}
});
blackboxClass.implement(new Options);
