/**
 *
 *
 */

(function($) {
	new function() {
		$.fn.tableScroll = function(options) {
			var defaults = {
				start_at_index: false,
				scroll_height: 0,
				visible_items: 3
			};

			var settings = $.extend(false, defaults, options);

			var scrolltables = [];
			$(this).each(function() {
				var scrolltable = new TableScroll(this, settings);
				scrolltables[scrolltables.length] = scrolltable;
			});
			return scrolltables;
		};

		function TableScroll(wrapper, settings) {
			this.init(wrapper, settings);
		}
		
		TableScroll.prototype = {
			
			current_index: 0,
			settings: false,

			init: function(wrapper, settings) {
				var context = this;


				this.scroll_container =  $(wrapper);
				this.scroll_box = $(this.scroll_container).find('.scroll-content');
				this.scroll_items = $(this.scroll_box).find('tr.level');
				this.scroll_items_count = $(this.scroll_items).length;

				if(!settings.scroll_height) {
					this.scroll_height = $(this.scroll_items).outerHeight();
				} else {
					this.scroll_height = $(this.scroll_items).outerHeight();
				}

				this.visible_items = settings.visible_items;
				
				this.scroll_offset = $(this.scroll_container).attr('rel');

				this.scroll_navi = $(this.scroll_container).find('#level-navi table');

				this.scroll_btn_top = $(this.scroll_container).find('.scroll-top');
				this.scroll_btn_bottom = $(this.scroll_container).find('.scroll-bottom');

				this.level_buttons = $(this.scroll_navi).find('tr');

				/* BUTTONS */
				$(this.scroll_btn_top).click(function() {
					if(!$(this).hasClass('scroll-top-disabled')) {
						context.nextItem();
					} else {
						return false;
					}
				});

				$(this.scroll_btn_bottom).click(function() {
					if(!$(this).hasClass('scroll-bottom-disabled')) {
						context.prevItem();

					} else {
						return false;
					}
				});

				// Zu Stufe x Springen per Zeile Stufe X
				$(this.level_buttons).click(function() {

					var level_nr = $(this).attr('id');
					level_nr = level_nr.split('level');
					if(level_nr.length == 2 && !isNaN(level_nr[1])) {
						level_nr = parseInt(level_nr[1]) +1;
						if(level_nr != context.current_index) { // Nicht auf selbe Höhe springen
							context.showItem(level_nr);
						}
					}
					return false;
				});
				/* Ende: BUTTONS */

				if(!settings.start_at_index) {
					this.start_at_index = 0;// - this.visible_items+(this.visible_items-1);
				} else {
					this.start_at_index = settings.start_at_index;
				}
				this.current_index = 21;//this.start_at_index; 
				this.showItem(this.start_at_index);

			},
			prevIndex: function() {
				if(((this.current_index-1)-(this.visible_items-1)) == 0) {
					return false;
				} else {
					var prev = this.current_index - 1;
					return prev;
				}
			},
			prevItem: function() {
				var prev = this.prevIndex();
				if(prev === false) {
					return false;
				} else {
					this.showItem(prev);
					return true;
				}
			},
			nextIndex: function() {
				if (this.current_index < this.scroll_items_count) {
					return (this.current_index+1);
				} else {
					return false;
				}
			},
			nextItem: function() {
				var next = this.nextIndex();
				if(next === false) {
					return false;
				} else {
					this.showItem(next);
					return true;
				}
			},
			
			showItem: function(i) {
				var context = this;

				if(i < context.visible_items) { // Nicht kleiner als Mindestanzahl
					i = context.visible_items;
				}

				// unterschiedliche Scrollgeschwindigkeit bei kleinen / großen Sprüngen
				var scroll_speed = 0;
				var scroll_difference = Math.abs(i - context.current_index);
				if (scroll_difference == 1) {
					scroll_speed = 250;
				} else if(scroll_difference <= 5) {
					scroll_speed = 500;
				} else if(scroll_difference <= 10) {
					scroll_speed = 750;
				} else if(scroll_difference <= 20) {
					scroll_speed = 1000;
				} else {
					scroll_speed = 1250;
				}

				context.scroll_box.animate({top:-((context.scroll_items_count - i) * context.scroll_height)+'px'},scroll_speed,function() {
					context.current_index = i;//this.scroll_items_count - Math.floor($(this.scroll_box).scrollTop() / this.scroll_height);

					// Scroll-Button updaten
					if(context.prevIndex() === false) {
						$(context.scroll_btn_bottom).addClass('scroll-bottom-disabled');
					} else {
						$(context.scroll_btn_bottom).removeClass('scroll-bottom-disabled');
					}
					if(context.nextIndex() === false) {
						$(context.scroll_btn_top).addClass('scroll-top-disabled');
					} else {
						$(context.scroll_btn_top).removeClass('scroll-top-disabled');
					}
					
					$(context.scroll_navi).find('tr.active').removeClass('active');

					for(var x = 0;x < context.visible_items;x++) {
						$(context.scroll_navi).find('tr:eq('+((context.scroll_items_count-context.current_index)+x)+')').addClass('active');
					}
				});
			}
		};
	};

})(jQuery);


