/*
	innerfade.js
*/
(function($){
	jQuery.fn.innerfade = function(containerMenue, containerHeight, speed, timeout){
		// build object
		//return this.each(function(i){
		//	return new Innerfade(this, containerMenue[i], containerHeight, speed, timeout);
		//});
		return new Innerfade(this, containerMenue[0], containerHeight, speed, timeout);
	}
})(jQuery);

function Innerfade(container, containerMenue, containerHeight, speed, timeout){
	return this instanceof Innerfade
		? this.init(container, containerMenue, containerHeight, speed, timeout)
		: new Innerfade(container, containerMenue, containerHeight, speed, timeout);
}	

$.extend(Innerfade.prototype, {
	// private
	_speed: 		1000,
	_timeout:		8000,
	_elements:		null,
	_current:		null,
	_timer:			null,
	_timerMenue:	null,

	init: function(container, containerMenue, containerHeight, speed, timeout){
			this._speed 	= speed;
			this._timeout 	= timeout;
	
			var self = this;
	
			// hide all elements
			if(!this.prepare(container, containerMenue, containerHeight)){
				alert('no children available');
				return;
			}

			// set timeout for the first transition
    		setTimeout((function(){
            		self.slide(0, 1);
        		}), this._timeout);
        		
        	return this;
		},

	prepare: function(container, containerMenue, containerHeight){
			var elements 		= $(container).children();
			var elementsMenue 	= $(containerMenue).children();

			// return flase if we have less then 2 elements
			if(elements.length < 2){
				return false;
			}
		
			// return false if we have menue length is not the same like container element length
			if(elements.length != elementsMenue.length){
				alert(elements.length+' '+elementsMenue.length)
				return false;
			}

			// hide all elements
			$(container).css('position', 'relative').css('height', containerHeight);
			for (var i = 0; i < elements.length; i++) {
				$(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
			};

			// show first element	
			$(elements[0]).show();
			$(elementsMenue[0]).addClass('active');
	
			this._elements 		= elements;
			this._elementsMenue	= elementsMenue;
	
			return true;
		},
		
	slide: function(current, next){
			var self = this;
	
			clearTimeout(this._timer);
			clearTimeout(this._timerMenue);

			// if current element is the last element in the list, 
			// then the next one is the first element form the list
			if(next >= this._elements.length ){
				next = 0;
			}
			// lower 0
			if(next < 0){
				next = this._elements.length - 1;
			}

			// ignore transition when we do not have to fade
			if(current != next){
				// fade
				$(this._elements[current]).fadeOut( this._speed);
    			$(this._elements[next]).fadeIn( this._speed, 
    				function(){removeFilter($(this)[0]);} );

				// menue element
				this._timerMenue = setTimeout((function() {
					for (var i = 0; i < self._elementsMenue.length; i++){
						$(self._elementsMenue[i]).removeClass('active');
					};
					$(self._elementsMenue[next]).addClass('active');
				}), this._speed);

				this._current = next;
			}
	
			// next step
			this._timer = setTimeout((function() {
            	self.slide(next, next+1);
        	}), this._timeout);
		},

	stepForward: function(){
			this.slide(this._current, this._current+1);
		},

	stepBackward: function(){
			this.slide(this._current, this._current-1);
		},

	goto: function(index){
			this.slide(this._current, index);
		}
});

function removeFilter(element){
			if(element.style.removeAttribute){
				element.style.removeAttribute('filter');
			}
		}
/*
innerfade = function(container, containerMenue, containerHeight, speed, timeout){
	this._speed 	= speed;
	this._timeout 	= timeout;
	
	// hide all elements
	if(!this.init(container, containerMenue, containerHeight)){
		alert('no children available');
		return;
	}

	// set timeout for the first transition
    setTimeout((function(){
            this.slide(0, 1);
        }), this._timeout);
}

init = function(container, containerMenue, containerHeight, speed, timeout){
//init = function(container, containerMenue, containerHeight){
	var elements 		= $(container).children();
	var elementsMenue 	= $(containerMenue).children();

	// return flase if we have less then 2 elements
	if(elements.length < 2){
		return false;
	}
	// return false if we have menue length is not the same like container element length
	if(elements.length != elementsMenue.length){
		alert(elements.length+' '+elementsMenue.length)
		return false;
	}


	// hide all elements
	$(container).css('position', 'relative').css('height', containerHeight);
	for (var i = 0; i < elements.length; i++) {
		$(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
	};

	// show first element	
	$(elements[0]).show();
	$(elementsMenue[0]).addClass('active');
	
	this._elements 		= elements;
	this._elementsMenue	= elementsMenue;
	
	return true;
}


slide = function(current, next){
	clearTimeout(this._timer);
	clearTimeout(this._timerMenue);

	// if current element is the last element in the list, 
	// then the next one is the first element form the list
	if(next >= this._elements.length ){
		next = 0;
	}
	// lower 0
	if(next < 0){
		next = this._elements.length - 1;
	}

	// ignore transition when we do not have to fade
	if(current != next){
		// fade
		$(this._elements[current]).fadeOut( this._speed);
    	$(this._elements[next]).fadeIn( 	this._speed, 
    			function(){removeFilter($(this)[0]);} );

		// menue element
		this._timerMenue = setTimeout((function() {
			for (var i = 0; i < _elementsMenue.length; i++){
				$(_elementsMenue[i]).removeClass('active');
			};
			$(this._elementsMenue[next]).addClass('active');
		}), this._speed);

		this._current = next;
	}
	
	// next step
	this._timer = setTimeout((function() {
            slide(next, next+1);
        }), this._timeout);
}

stepForward = function(){
	this.slide(this._current, this._current+1);
}

stepBackward = function(){
	this.slide(this._current, this._current-1);
}

goto = function(index){
	this.slide(this._current, index);
}

var _speed 		= 1000;
var _timeout 	= 8000;
var _elements;
var _current;
var _timer;
var _timerMenue;

// **** remove Opacity-Filter in ie ****
function removeFilter(element) {
	if(element.style.removeAttribute){
		element.style.removeAttribute('filter');
	}
}	*/	

