/*!
* Class ceSlideshow
*
*/
function ceSlideshow( p_settings ) {
	// private members
	var settings = {
			loop			: false,
			animate 		: false,
			delay			: 3000,
			speed			: 800,
			startPosition 	: 0,
			navType			: 1,		// Bitfield
			visibleElements : 1,
			slideType		: "slide",	// "slide", or "fade"
			guiContainerId	: null,
			guiBtnClass		: "",
			guiBtnSelClass	: ""
		},
		numSlides = 0,
		currentPosition,
		currentBuffer = 1,
		hideAfterInit = false,
		that = this;
	
	// public members
	this.slidesContainer = '#'+p_settings.elId.replace(/#/,"");
		
	// Get number of slides
	numSlides = $(this.slidesContainer + " .slide").length;
		
	if ( numSlides == 0 ) { return; }
	
	// If we sit in a hidden tab, no measurement is possible. Temporarily 
	// make element visible until all imit is done.	
	if ( $(this.slidesContainer).closest(".tabContent").length == 1 && !$(this.slidesContainer).closest(".tabContent").is(":visible") ) {		
		$(this.slidesContainer).closest(".tabContent").removeClass("ui-tabs-hide");		
		hideAfterInit = true;
	}
	
	this.slideWidth = $(this.slidesContainer + ' .slidesContainer').width();
		
	// privileged methods
	this.manageControls = function () {
		if ( (settings.navType & 4) == 4 ) {			
			$( settings.guiContainerId + " ." + settings.guiBtnClass ).removeClass(settings.guiBtnSelClass);
			$( settings.guiContainerId + " ." + settings.guiBtnClass + ":eq("+currentPosition+")" ).addClass(settings.guiBtnSelClass);
		}
		
		if ( settings.loop === true ) { return; }
		
		// Hide left arrow if position is first slide		
		currentPosition==0 ? $(that.slidesContainer+' .leftControl').hide() : $(that.slidesContainer+' .leftControl').show();
		// Hide right arrow if position is last slide		
		currentPosition==numSlides-1-(settings.visibleElements-1) ? $(that.slidesContainer+' .rightControl').hide() : $(that.slidesContainer+' .rightControl').show();
	};
	
	this.movePosition = function () {		
		if ( settings.slideType == "slide" ) {
			$( this.slidesContainer + ' .slideInner').animate({
				'marginLeft' : that.slideWidth*(-currentPosition)/settings.visibleElements
			}, settings.speed);			
		}
		else {			
			var backBuffer = currentBuffer == 1 ? 2 : 1;			
			$( this.slidesContainer + " .slideInner" + backBuffer).css({
				"marginLeft" : that.slideWidth*(-currentPosition)/settings.visibleElements
			});
				
			$( this.slidesContainer + " .slideInner" + currentBuffer).fadeOut( settings.speed );
			$( this.slidesContainer + " .slideInner" + backBuffer).fadeIn( settings.speed );
			currentBuffer = backBuffer;
		}
	 };
				
	this.makeAction = function ( button ) {
		currentPosition += ($(button).attr('rel')=='rightControl') ? 1 : -1;		
		if ( currentPosition == numSlides-(settings.visibleElements-1) ){ currentPosition = 0; }		// Loop		
		if ( currentPosition < 0) { currentPosition = numSlides-1; }	// Loop
		this.manageControls();
		this.movePosition();
	}
	
	this.animationStart = function ( p_param ) {
		$.extend( settings, p_param );
		$.doTimeout( 'ceSlideshow_'+this.slidesContainer, settings.delay, function() {				
				if ( currentPosition+1 == numSlides ) {
					if ( settings.loop ) { currentPosition = 0; }
					else { that.animationStop(); return false; }
				}
				else { currentPosition++; }
				that.manageControls();
				that.movePosition();
				return true;
			});
	}
	
	this.animationStop = function () {
		$.doTimeout( 'ceSlideshow_'+this.slidesContainer );
	}
	
	this.setPosition = function( p_position, p_stopAnimation ) {
		if ( currentPosition == p_position ) return;
		currentPosition = p_position;
		this.manageControls();
		this.movePosition();
		if ( p_stopAnimation || p_stopAnimation === undefined) {
			this.animationStop();
		}
	}
	
	// -------------------------------------------------------------------------
	// Init
	// Merge default settings with parameters	
	$.extend( settings, p_settings );
	
	// Set currentPosition to desired startPosition
	currentPosition = settings.startPosition;
	
	// Remove scrollbar in JS
	$(this.slidesContainer + ' .slidesContainer').css('overflow', 'hidden');		
	
	// Wrap all .slides with #slideInner div
	//this.slides()
	$(this.slidesContainer + " .slide")
		.wrapAll('<div class="slideInner"></div>');		
		//.css({ 'float' : 'left', 'width' : that.slideWidth });	// Float left to display horizontally, readjust .slides width
		
	// Set #slideInner width equal to total width of all slides	
	$( this.slidesContainer + ' .slideInner').css('width', that.slideWidth * numSlides);
	
	
	
	// Copy slidesInner if we are in fade mode
	if ( settings.slideType == "fade" ) {
		// $( this.slidesContainer + " .slideInner").attr("rel", "buf1").clone().appendTo( $(this.slidesContainer + " .slidesContainer") ).attr("rel", "buf2");
		$( this.slidesContainer + " .slideInner").removeClass("slideInner").addClass("slideInner1").css({position:"absolute"}).clone().appendTo( $(this.slidesContainer + " .slidesContainer") ).removeClass("slideInner1").addClass("slideInner2");
	}
	
	// Insert controls in the DOM
	if ( (settings.navType & 1) == 1 ) {
		$( this.slidesContainer )
			.prepend('<span class="control leftControl" rel="leftControl">zurück</span>')
			.append('<span class="control rightControl" rel="rightControl">weiter</span>');
	}
	if ( (settings.navType & 2) == 2 ) {
		$( this.slidesContainer )
			.prepend('<span class="control buttonControl" rel="buttonControl">Clicking moves left</span>')
	}
	if ( (settings.navType & 4) == 4 ) {	
		if ( settings.guiContainerId != null ) {
			settings.guiContainerId = "#" + settings.guiContainerId.replace(/\#/,"");
			var cnt=0;
			$( settings.guiContainerId + " ." + settings.guiBtnClass ).each( function() {
				var c = cnt++;				
				if ( c < numSlides ) {
					$(this).click( function() { that.setPosition(c); });
				}
			});
		}
	}
	
	// Hide left arrow control on first load
	this.manageControls();
	this.movePosition();
	
	// Binds
	$( this.slidesContainer + ' .control').bind('click', function(){
		that.makeAction(this);
		that.animationStop();
	});
	
	if ( settings.animate === true ) {
		this.animationStart();
	}
	
	if ( hideAfterInit ) {		
		$(this.slidesContainer).closest(".tabContent").addClass("ui-tabs-hide");		
	}
};
// Shared functions
//ceSlideshow.prototype.slideWidth 		= function() { return $(this.slidesContainer + ' .slidesContainer').width(); };
//ceSlideshow.prototype.slides 			= function() { return $(this.slidesContainer + ' .slide'); };
