(function($) {

	$.fn.bdpSlideshow = function(options){
		return this.each(function(){
			$.bdpSlideshow(this, options);
		});
	};
	
	$.bdpSlideshow = function(container, options){
		var settings = $.extend({
			speed:500,
			timeout:3500,
			height:null,
			width:null,
			children:null
		}, options);
		var elements = $(container).children();
		if (elements.length > 1) {
			$(container).css("position", "relative").css("height", settings.height).css("width", settings.width);
			for ( var i=0; i<elements.length; i++ ){
				$(elements[i]).css("z-index", String(elements.length-1)).css("position", "absolute").hide();
			}
			$(elements[0]).show();
			setTimeout(function(){
				$.bdpSlideshow.next(elements, settings, 1, 0);
			}, settings.timeout);
		}
	}
	
	$.bdpSlideshow.next = function(elements, settings, current, last){
		$(elements[last]).fadeOut(settings.speed);
		$(elements[current]).fadeIn(settings.speed, function(){
			$.bdpSlideshow.removeFilter($(this)[0]);
		});
		if((current+1)<elements.length){
			current = current + 1;
			last = current-1;
		} else {
			current = 0;
			last = elements.length - 1;
		}
		setTimeout((function(){
			$.bdpSlideshow.next(elements, settings, current, last);
		}), settings.timeout);
	}
	
	$.bdpSlideshow.removeFilter = function(element){
		if(element.style.removeAttribute){
			element.style.removeAttribute('filter');
		}
	}

})(jQuery);

