jQuery(document).ready(function() {

  $('#thumbs-hochzeiten, #controls-hochzeiten, #thumbs-hochzeiten a.hochzeit, .toggleGalleries').hide();
  $('#thumbs-hochzeiten').css('width', '260px');

  if(!jQuery('div.content').hasClass('hochzeiten') || jQuery("#thumbs-hochzeiten ul.thumbs li").length < 1) {
    return;
  }

	// $('.slide_container').css('height', '600px');
	// $('.categories').css('display', 'block');
	var onMouseOutOpacity = 0.67;
  $('#thumbs-hochzeiten ul.thumbs li, #thumbs ul.thumbs a.hochzeit').opacityrollover({
			mouseOutOpacity:   onMouseOutOpacity,
			mouseOverOpacity:  1.0,
			fadeSpeed:         'fast',
			exemptionSelector: '.selected'
	});

  var hochzeitGallery = {};
     // INIT
     $('div#thumbs a.hochzeit').each(function(i, e) {
       var hochzeit = e.id;
       hochzeitGallery[hochzeit] = $("div#thumbs-hochzeiten ul li.thumbs." + hochzeit).galleriffic({
        delay:                     3000, // in milliseconds
        numThumbs:                 18, // The number of thumbnails to show page
        preloadAhead:              40, // Set to -1 to preload all images
        enableTopPager:            false,
        enableBottomPager:         true,
        maxPagesToShow:            7,  // The maximum number of pages to display in either the top or bottom pager
        imageContainerSel:         '#slideshow-hochzeiten', // The CSS selector for the element within which the main slideshow image should be rendered
        controlsContainerSel:      '#controls-hochzeiten', // The CSS selector for the element within which the slideshow controls should be rendered
        captionContainerSel:       '#caption-hochzeiten', // The CSS selector for the element within which the captions should be rendered
        loadingContainerSel:       '#loading', // The CSS selector for the element within which should be shown when an image is loading
        renderSSControls:          true, // Specifies whether the slideshow's Play and Pause links should be rendered
        renderNavControls:         true, // Specifies whether the slideshow's Next and Previous links should be rendered
        playLinkText:              'Slideshow abspielen',
        pauseLinkText:             'Slideshow pausieren',
        prevLinkText:              '',
        nextLinkText:              '',
        nextPageLinkText:          'Weiter &rsaquo;',
        prevPageLinkText:          '&lsaquo; Zurück',
        enableHistory:             true, // Specifies whether the url's hash and the browser's history cache should update when the current slideshow image changes
        enableKeyboardNavigation:  true, // Specifies whether keyboard navigation is enabled
        autoStart:                 false, // Specifies whether the slideshow should be playing or paused when the page first loads
        syncTransitions:           false, // Specifies whether the out and in transitions occur simultaneously or distinctly
        defaultTransitionDuration: 1000, // If using the default transitions, specifies the duration of the transitions
        onSlideChange:             function(prevIndex, nextIndex) {
        	this.find('ul.thumbs').children().eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end().eq(nextIndex).fadeTo('fast', 1.0);
        },
        onPageTransitionOut:       function(callback) {
        		this.fadeTo('fast', 0.0, callback);
        },
        onPageTransitionIn:        function() {
        		this.fadeTo('fast', 1.0);
        }
    });

     });

    var firstClick = true;
    var secondClick = false;
    $("#thumbs a").not(".hochzeit").click(function() { secondClick = false; });

    jQuery('div#thumbs a.hochzeit').bind('click', function(e) {
      var target = e.currentTarget;
      console.log("e: ", e);
      console.log("target: ", target);
      var hochzeit = target.id;
      console.log("XX", hochzeit);
      toggleGalleries(hochzeit);
    });
 
    jQuery('.toggleGalleries').click(function(e) {
      toggleGalleries();
    });

    var toggleGalleries = function(hochzeit) {
      console.log(typeof hochzeit);
      console.log(hochzeit);
      // cleanup
      $("#slideshow-hochzeiten .image-wrapper").slice(1).remove();
      $(".image-wrapper a img").slice(1).remove();

      $('#controls, #controls-hochzeiten').toggle();
      // Verlassen eines Albums
      if(undefined == hochzeit) {
        gallery.enableKeyboard();
        secondClick = true;
        // $('#slideshow').html('').show();
        // console.log("verlasse album");
        // $('.image-wrapper').fadeOut('slow', function() {
        //   $(this).html('');
        //   $(this).show();
        // });
        // gallery.gotoIndex(0, false, false);
        $('#slideshow').hide();
        $('#thumbs-hochzeiten, #slideshow-hochzeiten, #caption-hochzeiten').fadeOut('slow', function() {
            // console.log("anzeigen der bilder erster ebene");
            $('.toggleGalleries, #hochzeit-title').fadeOut('slow', function() {
            $("#slideshow .image-wrapper").html('');
              $('#thumbs, #caption, #slideshow').fadeIn('slow');
            });
            $('#thumbs-hochzeiten a.hochzeit').hide();
        });
        // Öffnen eines Albums
      } else {
        gallery.disableKeyboard();
        // Reset
        $('div#thumbs a.hochzeit').each(function(i, e) {
          var hochzeit_tmp = e.id;
          hochzeitGallery[hochzeit_tmp].disableKeyboard();
        });
        console.log(hochzeitGallery);
        console.log(hochzeit);
        hochzeitGallery[hochzeit].gotoIndex(0);
        hochzeitGallery[hochzeit].enableKeyboard();
        $("#thumbs-hochzeiten ul li.thumbs").not("li." + hochzeit).hide();
        $("#thumbs-hochzeiten ul li.thumbs." + hochzeit).show();

        if(secondClick) {
          $(".slide_container").css('opacity', '0');
        }
        // $('.toggleGalleries').fadeOut('slow');
        // console.log("betrete album");
        // $("#slideshow-hochzeiten").show();
        // $('#slideshow').hide();
        // Anzeigen des ersten Bildes
        // Ausblenden der Übersicht Erster Ebene
        $('#thumbs, #caption, #slideshow').fadeOut('slow', function() {
          if(firstClick) {
            $("#slideshow").html('');
          }

          if(secondClick) {
            $(".slide_container").css('opacity', '100');
          }
          // Name des Brautpaars
          $("#hochzeit-title").html(hochzeit.replace("-","+"));
          $("#hochzeit-title, .toggleGalleries").fadeIn('slow', function() {
            // hochzeitGallery.gotoIndex(0, true, false);
            // Einblenden der Thumbnails und der Brautpaar-Namen
            $('#thumbs-hochzeiten, #caption-hochzeiten, #slideshow-hochzeiten, #thumbs-hochzeiten a.' + hochzeit).fadeIn('slow', function() {
              // fadeIn("slow");
            });
          });
        });
        // var selection = $('#thumbs-hochzeiten a.' + hochzeit);
        // console.log(selection);
      }
    };

    $("#slideshow-hochzeiten, #hochzeit-title, #thumbs-hochzeiten a.hochzeit").hide();

});


