// --------------------------------------
// ----------- Bernard Teske ------------
// - Webdesign & digitale Spezialitäten -
// -------- info@bernardteske.de --------
// -------- www.bernardteske.de ---------
// --------------------------------------

(function( $ ){
	var methods = {
		init : function() {
			$(this).find("a").hide();
			
			// Macht die Beschriftung
			$(this).append('<div class="btds-moveshow-text">AAA</div>');
			$(this).find('.btds-moveshow-text').css('position', 'absolute').css('z-index', '100').hide();
			
			$(this).data("containerstatus", {
				bilderanzahl: $(this).find('a').length,
				aktuelles_wechselbild: 0
			});				
		},
		bildwechsel : function(options) {		
			var settings = {
				'animationtime'	: 3000,
				'delaytime' : 2000,
				'animationmode' : 'center',	// Kann auch „center“ sein für Bewegung über die ganze Breite oder „none“ wenn sich das ganze nicht bewegen soll...
				'textfade' : true,
			};
			if ( options ) { 
				$.extend( settings, options );
			}
			
			// Beschriftung machen
			var beschriftung=$(this).find('a:eq('+$(this).data("containerstatus").aktuelles_wechselbild+')').html();
			$(this).find('.btds-moveshow-text').html(beschriftung);
			
			if(settings.textfade==false){
				$(this).find('.btds-moveshow-text').show();
			}
			if(settings.textfade=='hide'){
				$(this).find('.btds-moveshow-text').hide();
			}
					
			var bildsrc=$(this).find('a:eq('+$(this).data("containerstatus").aktuelles_wechselbild+')').attr('href');
			$(this).append('<img src="'+bildsrc+'"/>');
			$(this).find('img:last').css("top", "-1000px");
			$(this).find('img:last').hide();
			$(this).find('img:last').load(function(){
								
				// Die Ausrichtung des Bildes zum Start wird per Random ermittelt.
				var randomnumber_x=Math.floor(Math.random()*11);
				x_start=$(this).parent().width()-$(this).width();
				if(randomnumber_x>4){
					x_start=0;
				}
					
				var randomnumber_y=Math.floor(Math.random()*11);
				y_start=$(this).parent().height()-$(this).height();
				if(randomnumber_y>4){
					y_start=0;
				}
				
				// legt fest in welche Richtung Bild verschoben wird.
				if(settings.animationmode=='center'){
					var x_animation=(($(this).width()-$(this).parent().width())/2)*-1;
					var y_animation=(($(this).height()-$(this).parent().height())/2)*-1;
				}
				if(settings.animationmode=='full'){
					var x_animation=(($(this).width()-$(this).parent().width()))*-1;
					var y_animation=(($(this).height()-$(this).parent().height()))*-1;
					if(x_start<0){
						x_animation=0;
					}
					if(y_start<0){
						y_animation=0;
					}
				}
				if(settings.animationmode=='none'){
					x_start=0;
					y_start=0;
					x_animation=0;
					y_animation=0;
				}
				
				// Das Bild wird in seine Startausrichtung gebracht und eingefaded
				$(this).css("left", x_start+"px");
				$(this).css("top", y_start+"px");
								
				$(this).fadeIn('slow', function(){
						if($(this).parent().find('img:first').attr('src')!=$(this).parent().find('img:last').attr('src')){
							$(this).parent().find('img:first').detach();
						}
				});

				// Bild animieren
				$(this).animate({left: x_animation, top: y_animation}, settings.animationtime, function(){					
					
					if(settings.textfade!='hide'){
						$(this).parent().find('.btds-moveshow-text').fadeIn('slow');
					}
					$(this).parent().data("containerstatus").aktuelles_wechselbild+=1;
					if($(this).parent().data("containerstatus").aktuelles_wechselbild==$(this).parent().data("containerstatus").bilderanzahl){
						$(this).parent().data("containerstatus").aktuelles_wechselbild=0;
					}
					$(this).parent().delay(settings.delaytime).queue(function(){
						$(this).dequeue();
						
						if(settings.textfade==false){
							$(this).btdsmoveshow('bildwechsel', settings);	// wenn der Delaymode kleiner ist als 100 wird die Beschriftung permanent eingeblendet.
						}
						else{
							$(this).find('.btds-moveshow-text').fadeOut('slow', function(){
								$(this).parent().btdsmoveshow('bildwechsel', settings);
							});
						}
						
						
					});
    			});
			});
			
		}
	};


	$.fn.btdsmoveshow = function(method){					
		// Method calling logic
		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.btdsmoveshow' );
		}  		
	}
})( jQuery );
