// JavaScript Document

(function( $ ) {
	var xoffs = 0;

	var methods = {
		
		init : function( options ) {
			
			return this.each(function() {

				var $this = $(this),
					data = $this.data('RCCScroller');

				var listMask = $('.sliderMask', $this), 
					listPane = $('.sliderPanel', listMask), 
					listItems = $('.sliderItem', listPane);
				var prevButton = $('.prevButton', $this),
					nextButton = $('.nextButton', $this);
	
				if ( !data ) {	// Daten nicht gesetzt also berechnen und setzen
					$(this).data('RCCScroller', {
						target : listPane,
						xoffset : 0,
						xstep : Math.floor(listMask.outerWidth()/listItems.outerWidth()) * listItems.outerWidth(),
						xslides : Math.ceil( listItems.length / Math.floor( listMask.outerWidth() / listItems.outerWidth() )) ,
						xwidth  : listItems.length * listItems.outerWidth(),
						slidewidth : listItems.width(),
						filter : false
					});
					data = $this.data('RCCScroller');
				}
								
				listPane.css('width', data.xwidth + 10);
				listPane.css('position','absolute');
				listPane.css('left', 0);
				
				var currentItem = $('#ACTIVEMACHINE', listPane);
				if (currentItem.position()) {
					data.xoffset = Math.floor(currentItem.position().left / data.xstep) * data.xstep; 
					listPane.animate({ left: -data.xoffset }, 'slow');
				}

				prevButton.bind('click.RCCScroller', methods._prevButtonHandler).fadeOut();
				prevButton.data('RCCScroller', { container : $this });

				nextButton.bind('click.RCCScroller', methods._nextButtonHandler).fadeOut();
				nextButton.data('RCCScroller', { container : $this });
								
				listItems.bind('mouseenter.RCCScroller', methods._showText);
				listItems.bind('mouseleave.RCCScroller', methods._hideText);
				$this.bind('mouseenter.RCCScroller', methods._showControls);
				$this.bind('mouseleave.RCCScroller', methods._hideControls);
				
			});
			
		},
		
		destroy : function( ) {

			return this.each(function() {
				
				var $this = $(this),
					data = $this.data('RCCScroller');
					
				$('.nextButton').unbind('.RCCScroller');
				$('.prevButton').unbind('.RCCScroller');
				data.RCCScroller.remove();
				$this.removeData('RCCScroller');
				
			});

		},
		

		filterToggle : function( filter ) { 
		
			return this.each(function() {
		
				var $this = $(this),
					data = $this.data('RCCScroller');	
				var listMask = $('.sliderMask', $this), 
					listPane = $('.sliderPanel', listMask), 
					listItems = $('.sliderItem', listPane),
					filterButtons = $('.filter', $this).not(filter);
					activeButtons = $('.filter'+filter, $this);
				
				if (!filter || data.filter == filter) {
					data.xwidth = listItems.length * data.slidewidth;
					data.xslides = Math.ceil( listItems.length / Math.floor( listMask.outerWidth() / data.slidewidth )) ,
					listItems.stop().animate( { width:data.slidewidth, opacity:1 }, 'slow');	
					filterButtons.stop().fadeTo('fast', 1);
					filter = false;
				} else {
					var filteredItems = listItems.not(filter),
						visibleItems = $('.sliderItem'+filter, listPane);
					data.xoffset = 0;
					data.xwidth = (listItems.length - filteredItems.length) * data.slidewidth;
					data.xslides = Math.ceil( visibleItems.length / Math.floor( listMask.outerWidth() / data.slidewidth )) ,
					visibleItems.stop().animate( { width:data.slidewidth, opacity:1 }, 'slow'); 
					filteredItems.stop().animate( { width:'0px', opacity:0 }, 'slow');
					filterButtons.stop().fadeTo('fast', 0.3);
					activeButtons.stop().fadeTo('fast', 1);
				}
				listPane.animate({ left: -data.xoffset }, 'slow');
				// listPane.css('width',data.xwidth);
				data.filter = filter;

			});
		},
						

		_nextButtonHandler : function( ) { 

			var $this = $(this),
				data = $this.data('RCCScroller').container.data('RCCScroller'),
				scrollPane = data.target;

			if ( data.xoffset + data.xstep < data.xwidth ) {
				data.xoffset += data.xstep;
			} else {
				data.xoffset = 0;
			}
			scrollPane.stop().animate({ left: -data.xoffset }, 'slow');

			return $this;
			
		},
			
		_prevButtonHandler : function( ) { 
		
			var $this = $(this),
				data = $this.data('RCCScroller').container.data('RCCScroller'),
				scrollPane = data.target;

			if ( data.xoffset - data.xstep >= 0) {
				data.xoffset -= data.xstep;
			} else {
				data.xoffset = data.xstep * (data.xslides - 1);
			}
			scrollPane.stop().animate({ left: -data.xoffset }, 'slow');
			return $this;
			
		},
		
		scrollTo : function( ) { 
		
			return this;
			
		},

		_showControls : function( ) { 
		
			var $this = $(this);
			if ($this.data('RCCScroller').xslides > 1) {
				$('.prevButton',$this).stop().show().animate({ opacity: 1, left: '-39px' }, 'fast');
				$('.nextButton',$this).stop().show().animate({ opacity: 1, right: '-37px' }, 'fast'); 
			}
			return $this;
			
		},
		
		_hideControls : function( ) { 
		
			var $this = $(this);
			$('.prevButton',$this).stop().show().animate({ opacity: 0, left: '0px' }, 'fast');
			$('.nextButton',$this).stop().show().animate({ opacity: 0, right: '0px' }, 'fast');
			return $this;
			
		},
		
		_showText : function( ) { 
		
			var $this = $(this);
			$('.textView',$this).stop().fadeTo('fast', 1);
			$('.thumbView',$this).stop().fadeTo('fast', 0.3);
			return $this;
			
		},

		_hideText : function( ) { 
		
			var $this = $(this);
			$('.thumbView',$this).stop().fadeTo('fast', 1);
			$('.textView',$this).stop().fadeOut('fast');
			return $this;
			
		}
		
	};

	$.fn.RCCScroller = function( method ) {

		if ( methods[method] ) {
			return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.RCCScroller' );
		}
		
  	};

})( jQuery );
