(function($){ 
    $.fn.extend({  
        dbnSlider: function(options) {
			$(this).each(function() {
				/* default settings */
				settings = jQuery.extend({
					id					: $(this).attr('id'),
					slidesToSlide		: 1,
					slidesShown			: 1,
					previousButtonText	: 'Zurück',
					nextButtonText		: 'Weiter',
					counter				: false,
					externalsSlider		: false,
					sliderClass			: 'slider',
					slideClass			: 'slide',
					speed				: 500
				}, options);
				/* set settings to function global */
				this.id = settings.id;
				this.slidesToSlide = settings.slidesToSlide;
				this.slidesShown = settings.slidesShown;
				this.previousButtonText = settings.previousButtonText;
				this.nextButtonText = settings.nextButtonText;
				this.counter = settings.counter;
				this.externalSlider = settings.externalSlider;
				this.sliderClass = settings.sliderClass;
				this.slideClass = settings.slideClass;
				this.speed = settings.speed;
				/* define slider and slides, calculate and set width of slider */				
				this.slider = $('#'+this.id+' ul.'+this.sliderClass);
				this.slides = $('#'+this.id+' ul.'+this.sliderClass+' li.'+this.slideClass);
				this.slideWidth = this.slides.getSlideWidth();
				this.sliderWidth = this.slideWidth * this.slides.length;
				this.slider.css({ 'width': this.sliderWidth+'px' });
				/* calculate real slides and set position */
				this.slideCount = Math.ceil(this.slides.length / this.slidesToSlide);
				this.slidePosition = 1;
				
				var counterLeftHTML = "";
				var counterRightHTML = "";
				if(this.counter === true) {
					counterLeftHTML = '<span class="currentSlideLeft">1|1</span>';
					counterRightHTML = '<span class="currentSlideRight">4|1</span>';
				}
				
				this.leftSlider = '<a href="javascript:void(0)" class="sliderNav slidePrevious"><span class="sliderNavSpan"><span class="sliderNavTitle">'+this.previousButtonText+counterLeftHTML+'</span></span></a>';
				this.rightSlider = '<a href="javascript:void(0)" class="sliderNav slideNext"><span class="sliderNavSpan"><span class="sliderNavTitle">'+this.nextButtonText+counterRightHTML+'</span></span></a>';
				
				if(this.externalSlider !== false) {
					var max = this.slideCount;
					var mySlider = this.slider.parents(".sliderWrapper");
					$('#'+this.externalSlider).slider({
						animate: true,
						step: 100 / (this.slideCount - 1),
						slide: function(event, ui) {
							var pos = Math.round((max - 1) * ui.value / 100);
							mySlider.jumpTo(pos);
						}
					});
				}
				$(this.id).keyup(function() {
					if(evt.keyCode == 9) {
						$(this).parents(".sliderWrapper").slideForward;
					}
				});
				
				$(this).append(this.leftSlider);
				$(this).append(this.rightSlider);
				
				$('#'+this.id+' a.sliderNav .sliderNavSpan').fadeIn(0).pause(2000).fadeOut(500, function() { $(this).css({'display': 'none'}); });
				
				$(this).setCounter();
				$(this).checkPosition();
				
				//$(this).log();
				
			});
		},/* set counter */
		setCounter: function() {
			$(this).each(function() {
				if(this.counter === true) {
					$('.countSlide').html(this.slideCount);
					$('.currentSlide').html(this.slidePosition);
					$('.currentSlideLeft').html((this.slidePosition /* - 1 */) + ' | ' + this.slideCount);
					$('.currentSlideRight').html((this.slidePosition /* + 1 */) + ' | ' + this.slideCount);
				}
			});
		},/* direct slider */
		jumpTo: function(step) {
			$(this).each(function() {
				this.slidePosition = step+1;
				this.slider.animate({ left: "-"+step*this.slidesToSlide*this.slideWidth+"px" }, this.speed, '', function() { $(this.parentNode).checkPosition(); $(this.parentNode).setCounter(); });
			});
		},/* slide */
		slide: function(step) {
			$(this).each(function() {
				this.slidePosition-=step;
				if(this.externalSlider !== false) {
					var pos = ((this.slidePosition - 1) / (this.slideCount - 1)) * 100;
					$('#'+this.externalSlider).slider('option', 'value', pos);
				}
				this.slider.animate({ left: "+="+step*this.slidesToSlide*this.slideWidth+"px" }, this.speed, '', function() { $(this.parentNode).checkPosition(); $(this.parentNode).setCounter(); });
			});
		},/* slide forward */
		slideForward: function() {
			$(this).each(function() {
				$(this).unbind("click", $(this).parents(".sliderWrapper").slideForward);
				$(this).parents(".sliderWrapper").slide(-1);
			});
		},/* slide backward */
		slideBackward: function() {
			$(this).each(function() {
				$(this).unbind("click", $(this).parents(".sliderWrapper").slideBackward);
				$(this).parents(".sliderWrapper").slide(1);
			});
		},/* get the slides maxwidth */
		getSlideWidth: function() {
			var width = 0;
			$(this).each(function() {
				if(width <= $(this).outerWidth()) {
					width = $(this).outerWidth();
				}
			});
			return width;
		},/* check current position */
		checkPosition: function() {
			$(this).each(function() {
				if(parseInt($(this.slider).css('left')) >= 0) { 
					$('#'+this.id+' a.slidePrevious').addClass('sliderNavHidden');
				} else {
					$('#'+this.id+' a.slidePrevious').removeClass('sliderNavHidden');
					$('#'+this.id+' a.slidePrevious').bind("click", $(this).parents(".sliderWrapper").slideBackward);
				}
				if(parseInt($(this.slider).css('left')) <= -(this.sliderWidth - this.slidesToSlide*this.slidesShown*this.slideWidth)) {
					$('#'+this.id+' a.slideNext').addClass('sliderNavHidden');
				} else {
					$('#'+this.id+' a.slideNext').removeClass('sliderNavHidden');
					$('#'+this.id+' a.slideNext').bind("click", $(this).parents(".sliderWrapper").slideForward);
				}
			});
		},/* just for logging */
		log: function() {
			$(this).each(function() {
				console.log("ID: "+this.id);
				console.log("Slides to slide: "+this.slidesToSlide);
				console.log("Slides shown: "+this.slidesShown);
				console.log("PreviousButtontext: "+this.previousButtonText);
				console.log("NextButtontext: "+this.nextButtonText);
				console.log("Counter: "+this.counter);
				console.log("Slider: "+this.slider);
				console.log("Anzahl Slides: "+this.slides.length);
			});
		}	
	});
})(jQuery);
