  // -----------------------------------------
  // SLIDESHOW
  // Bildergalerie
  // mit Slider für Mediabox
  // -----------------------------------------
	
	//jQuery(document).ready(function(){
	jQuery(document).ready(function($) { 
		jQuery(".mediaslideshow").SlideShowManager();
	});
	 
	jQuery.fn.SlideShowManager = function() {
		return this.each(function(){
		new SlideShow(this);
	  });
	};
	 
	 
	function SlideShow(base){
	  var self = this;
	 
	  self.currentPosition = 0;
	  self.slideWidth = 585;
	  self.slides = jQuery('.mediaslide', base);
	  self.numberOfSlides = this.slides.length;
	  self.base = base;
	  self.direction = 1;
	  self.Timer = null;
	 
	  self.slides
		.wrapAll('<div class="slideInner"></div>')
		// Float left to display horizontally, readjust .slides width
		.css({
			'float' : 'left',
			'width' : self.slideWidth
		});
	 
	  jQuery('.mediaslidesContainer',base).css('overflow', 'hidden');
	  jQuery('.slideInner',base).css('width', self.slideWidth * self.numberOfSlides);
	 
	 
	  jQuery('.mediaslidesContainer',base).prepend ('<div class="autoplayControl">Play</div>');
	 
	  jQuery(base)
	  .prepend('<span class="mediacontrol" id="medialeftControl">Clicking moves left</span>')
	  .append('<span class="mediacontrol" id="mediarightControl">Clicking moves right</span>');
	 
	  jQuery('#medialeftControl', base).bind('click', jQuery.proxy(self.onLeftControlClick, self));
	  jQuery('#mediarightControl', base).bind('click', jQuery.proxy(self.onRightControlClick, self));
	  jQuery('.autoplayControl',base).bind('click', jQuery.proxy(self.onAutoPlayControlClick, self));
	 
	  self.manageControls();
	  self.onAutoPlayControlClick();
	}
	 
	SlideShow.prototype.manageControls = function(){
	var self = this;
		if (self.currentPosition == 0){
			jQuery('#medialeftControl', self.base).hide();
	self.direction = +1;
		}
		else
		{
			jQuery('#medialeftControl', self.base).show();
		}

		if (self.currentPosition == (self.numberOfSlides - 1)){
			jQuery('#mediarightControl', self.base).hide();
	self.direction = -1;
		}
		else
		{
			jQuery('#mediarightControl', self.base).show();
		}
	}

	SlideShow.prototype.animate = function(){
		var self = this;
	jQuery('.slideInner', self.base).animate({ 'marginLeft' : self.slideWidth * (-self.currentPosition)});
	}      

	SlideShow.prototype.onLeftControlClick = function(){
	//for( k in this)   console.log(k);
	var self = this;
		self.currentPosition--;
		self.manageControls();
		self.animate();
	}

	SlideShow.prototype.onRightControlClick = function(){
	//for( k in this)   console.log(k);
	var self = this;
		self.currentPosition++;
		self.manageControls();
		self.animate();
	}

	SlideShow.prototype.autoAnimate = function(){
	var self = this;
	if (self.direction == 1){
	  self.currentPosition++;
	}
	else
	{
	  self.currentPosition--;
	}
	//for (k in o) console.log(k);
	self.manageControls();
	self.animate();
	}
	

SlideShow.prototype.onAutoPlayControlClick = function(){
	var self = this;

	if (self.Timer == null){
	self.Timer = window.setInterval(function(){
						self.autoAnimate();}
					,8000);
	jQuery('.autoplayControl',self.base).html("Stop");
	}else
	{
	window.clearInterval(self.Timer);
	self.Timer = null;
	jQuery('.autoplayControl',self.base).html("Play")
	}
}
