var infobox = -1; // Zustand der Infobox (1: Ausgefahren, 0: Animation läuft, -1: Eingefahren)
var viewerMoreThanOne = false;
var slide = 0; // Momentant angezeigter Slide
	
function onBefore(curr, next, opts) {
 
  if ( $('#infotext' + (opts.nextSlide + 1)).length ) { // Wenn Infotext für aktuellen Slide gesetzt
	$('#infobutton').show();
	if(infobox == 1) { // Wenn Infobox nicht bereits ausgefahren
	  	infoboxVisible();
	  }
	// Infotext laden
	$('#infobox_content').html($('#infotext' + (opts.nextSlide + 1)).html()); // Lade Inhalt in die Infobox
	
	
  } else { // Wenn kein Infotext vorhanden ist
	  if(infobox == 1) { // und eine Infobox ausgefahren ist
	  	infoboxAway(true); // fahre wieder ein
	  }
	  $('#infobutton').hide(); // Blende Button aus
  }
};

function onAfter(curr, next, opts) { 

  viewerMoreThanOne = true; 
	  $('#prev').show();
	  $('#next').show();
	  $('#counter_aktuell').html(opts.currSlide + 1);
	  if(opts.currSlide != 0) { // Generiert Variable für PREV mit rechter Maustaste
		  slide = opts.currSlide;
	  } else {
		  slide = opts.slideCount;
	  }
	  $('#counter_gesamt').html(opts.slideCount); // Zeigt Gesamtzahl im Counter
};

function infoboxVisible() { // Infobox ausfahren
  $('#infobox').animate({
	  width: 288,
	  opacity: 0.9
  }, 'slow', function() {
  	infobox = 1;
  });
  
};

function infoboxAway(visible) { // Infobox einfahren
  $('#infobox').animate({
	  width: 0,
	  opacity: 0
  }, 'slow', function() {
	if(visible) {
		infobox = 1;
	} else {
		infobox = -1;
	}
  });	
  
};
	
	
jQuery(document).ready(function() {
	
	$('#main').css("display", "block"); // Am Anfang die Seite einblenden

	$("#viewer").noContext(); // Viewer Kontextmenü verstecken	
	$("#viewer").rightClick( function() { // Viewer: PREV mit Rechtsklick
        $('#viewer').cycle(slide - 1);
  	});
	  
	$('#infobutton').click(function() { // Infobox ein- und ausfahren
		if(infobox == -1) {
			infoboxVisible();
		} else if (infobox == 1) {
			infoboxAway(false);
		}
		infobox = 0;
	})
	
		$('#viewer').cycle({ 
			fx:     'fade', 
			speed: 700,
			timeout: 0, 
			next:   '.next', 
			prev:   '#prev' ,
			after: onAfter,
			before: onBefore,
			cleartype:  1
		});		
	
	if ( !viewerMoreThanOne ) { // Wenn nur ein Slide da ist
		$('#prev').hide(); // Navigationselemente verstecken
		$('#next').hide();
		$('#navigation').css("background-image", "none"); // Counter-Trennstrich verstecken
		$('#viewer').removeClass('next'); // Viewer NEXT mit Maustaste deaktivieren
  	}
	
	 if ( $('#infotext1').length ) { // Wenn Infotext für den ersten Slide verfügbar
		$('#infobutton').show();
		$('#infobox_content').html($('#infotext1').html());
			
	  } else {
		$('#infobutton').hide();
	 }
	 
	 
	
});
