$(document).ready(function() {

  $('div#infoMenuDiv > span > a').bind('click', function (klick) { 

    klick.preventDefault();
    
  });
  
  $('div#infoMenuDiv > span').bind('mouseenter', function () { 
  
    $('div#infoMenu ul').stop(false, true);
  
    var infoMenu = '<li><a href="standort/" target="_self" title="So finden Sie uns in Erfurt">Standort &raquo;</a></li>' +
                   '<li><a href="eintritt/" target="_self" title="Alle Informationen zur Kartenbestellung">Eintrittskarten &raquo;</a></li>' +
                   '<li><a href="gutscheine/" target="_self" title="Alle Informationen zu unseren Gutscheinen">Gutscheine &raquo;</a></li>' +
                   '<li><a href="flyergalerie/" target="_self" title="Alle neuen und alten Flyer im Überblick">Flyer-Galerie &raquo;</a></li>';
                     
    if(!$('div#infoMenu ul').length) {
      $('<ul>', {
        html: infoMenu
      })
      .delay(300)
      .appendTo('div#infoMenu')
      .animate({"height": "toggle", "opacity": "toggle"}, 500);
      
      $('div#infoMenuDiv').mouseleave(function () { 
    
        $('div#infoMenuDiv').unbind('mouseleave');
        $('div#infoMenu ul')
        .stop(true, false)
        .fadeOut(500, function(){$('div#infoMenu').html('');});   
  
      });

    }   

  })
  
  $('span#flyerDropdownLink').bind('click', function (klick) {
  
    klick.stopPropagation(); 

    if($('div#flyerDropdownListe').css("display")=="none") { 

      $('div#flyerDropdownListe').fadeIn(500); 
      $('body').click(function() {
        $('div#flyerDropdownListe').fadeOut(500, function(){ $('body').unbind('click') });
      })
    
    } else {
      $('div#flyerDropdownListe').fadeOut(500, function(){ $('body').unbind('click') });
    }
    
  })

  // Flyer-Array erstellen, wenn Flyer-Galerie aufgerufen wird
  flyerIndex = new Array();
  if($('table#flyerGalerie').length) {
    
    var txt = jQuery.trim($('div#flyerListeHidden').html());
    var flyerListe = txt.split('\n');
    var flyer = new Array();
    
		for(var x=0; x < flyerListe.length; x++) {
			
			flyer = flyerListe[x].split(':');
			flyerIndex[x] = new Object();
			flyerIndex[x]['nr'] = flyer[0]; 
      flyerIndex[x]['monat'] = flyer[1];

		}	
		
	}  
  
  // Link zu Flyer löst Slide aus
  $('a.flyerSlide').live('click', function (klick) {
    
    klick.preventDefault();
    
    if (!$(':animated').length) {
     
      var linkNeu = $(this).attr('href');
      linkNeu = linkNeu.split('/');
      var nrNeu = linkNeu[1];
      var hintenDatei = ''; 
      var hintenURL = ''; 
      if(linkNeu[2].length) {
        hintenDatei = '-hinten'; 
        hintenURL = 'hinten/';
      }
      for(var x = 0; x < flyerIndex.length; x++) {
  			if(flyerIndex[x]['nr'] == nrNeu) {
          var arrKeyNeu = x;
          break;
        }
  		}	
  
      var arrKey = parseInt($('table#flyerGalerie td.flyerWahl').attr('id'));
      var nr = flyerIndex[arrKey]['nr'];
      
      // Links an neuen Flyer anpassen
      if(arrKeyNeu<2) { 
        $('#flyerAktuellsterInaktiv').css('display', 'inline-block'); 
        $('#flyerAktuellsterAktiv').css('display', 'none');
      } else {
        $('#flyerAktuellsterInaktiv').css('display', 'none'); 
        $('#flyerAktuellsterAktiv').css('display', 'inline-block');    
      }
  
      if(arrKeyNeu>0) {
        $('#flyerDanachInaktiv').css('display', 'none'); 
        $('#flyerDanachAktiv').attr({href: 'flyergalerie/' + flyerIndex[arrKeyNeu-1]['nr'] + '/' + hintenURL, title: 'Nächster Flyer: ' + flyerIndex[arrKeyNeu-1]['monat']}).css('display', 'inline-block');  
      } else {
        $('#flyerDanachInaktiv').css('display', 'inline-block'); 
        $('#flyerDanachAktiv').css('display', 'none');  
      }
  
      if(arrKeyNeu<flyerIndex.length-1) {
        $('#flyerDavorInaktiv').css('display', 'none'); 
        $('#flyerDavorAktiv').attr({href: 'flyergalerie/' + flyerIndex[arrKeyNeu+1]['nr'] + '/' + hintenURL, title: 'Voriger Flyer: ' + flyerIndex[arrKeyNeu+1]['monat']}).css('display', 'inline-block');  
      } else {
        $('#flyerDavorInaktiv').css('display', 'inline-block'); 
        $('#flyerDavorAktiv').css('display', 'none');  
      }
  
      if(arrKeyNeu>flyerIndex.length-3) { 
        $('#flyerErsterInaktiv').css('display', 'inline-block'); 
        $('#flyerErsterAktiv').css('display', 'none');
      } else {
        $('#flyerErsterInaktiv').css('display', 'none'); 
        $('#flyerErsterAktiv').css('display', 'inline-block');    
      }
      
      $('#flyerVorne').attr({href: 'flyergalerie/' + flyerIndex[arrKeyNeu]['nr'] + '/'});  
      $('#flyerHinten').attr({href: 'flyergalerie/' + flyerIndex[arrKeyNeu]['nr'] + '/hinten/'});
      $('#flyerVorneHD').attr({href: 'flyer/flyer' + flyerIndex[arrKeyNeu]['nr'] + '.jpg'});  
      $('#flyerHintenHD').attr({href: 'flyer/flyer' + flyerIndex[arrKeyNeu]['nr'] + '-hinten.jpg'});       
  
      // vorige Flyer
      if(arrKeyNeu > arrKey) {
      
        var diff = arrKeyNeu - arrKey;
        
        for(x = arrKey+3; x < arrKeyNeu+3; x++) {
  
      		if(!$('td#' + x).length && x<=flyerIndex.length-1) {
            $('<td id="' + x + '"><div><a title="' + flyerIndex[x]['monat'] + '" href="flyergalerie/' + flyerIndex[x]['nr'] + '/' + hintenURL + '" target="_self" class="none flyerSlide"><img src="flyer/flyer' + flyerIndex[x]['nr'] + hintenDatei + '-thumb.jpg"></a></div></td>')
            .appendTo('table#flyerGalerie tr');
            $('table#flyerGalerie')
            .css( { width: $('table#flyerGalerie').width()+70 } );
          }
          var y = x-5;
          $('td#' + x + ' a').fadeIn(1400);
          $('td#' + y + ' a').fadeOut(800);
            
      	}
        
        var marginLeft = parseFloat($('table#flyerGalerie').css('marginLeft')) - (diff*70);	     
      
      }
      
      // nächste Flyer
      if(arrKeyNeu < arrKey) {
      
        var diff = arrKey - arrKeyNeu;
        
        for(x = arrKey-3; x > arrKeyNeu-3; x--) {
  
      		if(!$('td#' + x).length && x>=0) {
            $('<td id="' + x + '"><div><a title="' + flyerIndex[x]['monat'] + '" href="flyergalerie/' + flyerIndex[x]['nr'] + '/' + hintenURL + '" target="_self" class="none flyerSlide"><img src="flyer/flyer' + flyerIndex[x]['nr'] + hintenDatei + '-thumb.jpg"></a></div></td>')
            .prependTo('table#flyerGalerie tr');
            $('table#flyerGalerie')
            .css( { width: $('table#flyerGalerie').width()+70 } )
            $('table#flyerGalerie')
            .css({marginLeft: parseFloat($('table#flyerGalerie').css('marginLeft'))-70});
          }
          var y = x+5;
          $('td#' + x + ' a').fadeIn(1400);
          $('td#' + y + ' a').fadeOut(800);
            
      	}
        
        var marginLeft = parseFloat($('table#flyerGalerie').css('marginLeft')) + (diff*70);	     
      
      }    
       
      // derzeit auswählten Flyer ausblenden
      $('#flyerDesc').slideUp(200, function() {
  
        $('#flyerDesc').css('display', 'none', 'opacity', '0');
        $('td#' + arrKey + ' > div > div')
        .css({'border-bottom': '1px solid #550003', 'bottom': '0px'})
        .fadeOut(500, function() {
          $('td#' + arrKey)
          .removeClass('flyerWahl');   
          $('td#' + arrKeyNeu)
          .addClass('flyerWahl');
          $('td#' + arrKey + ' > div > div')
          .css({'border-bottom': 'none', 'bottom': '1px'})
        });
        $('td#' + arrKey + ' > div > a')
        .fadeIn(800);
        
        
        // eigentliche Slide-Animation
        $('table#flyerGalerie')
        .animate({marginLeft: marginLeft}, 500);
        
        $('td#' + arrKey)
        .animate({width: 70}, 500);
        
        $('td#' + arrKeyNeu)
        .animate({width: 200}, 500, function() {
        
          // Texte austauschen
          $('#flyerMonat').html(flyerIndex[arrKeyNeu]['monat']);
          $('#flyerNr').html('Flyer #' + flyerIndex[arrKeyNeu]['nr']);
        
          $('td#' + arrKeyNeu + ' > div > a')
          .fadeOut(300);
          
          if(!$('td#' + arrKeyNeu + ' > div > div').length) {
          
            $('<div><a href="flyer/flyer' + nrNeu + hintenDatei + '-normal.jpg" rel="lightbox" target="_blank" title="Flyer f&uuml;r ' + flyerIndex[arrKeyNeu]['monat'] + '" id="flyerLink' + nrNeu + '"><img src="flyer/flyer' + nrNeu + hintenDatei + '-thumb.jpg" title="Vergr&ouml;&szlig;ern" id="flyer' + nrNeu + '"></a></div>')
            .appendTo('td#' + arrKeyNeu + ' > div');
            
            $('#flyerLink' + nrNeu).slimbox();
    
            $('img#flyer' + nrNeu).load(function() {
    
              var flyerAuswahlWeite = $(this).attr('naturalWidth');
              
              $('td#' + arrKeyNeu + ' > div > div')
              .css({marginRight: Math.ceil((flyerAuswahlWeite+32)/2) * -1, 'border-bottom': '1px solid #550003', 'bottom': '0px'})
              .fadeIn(1000);
              
              $('td#' + arrKeyNeu + ' > div > div')
              .css({'border-bottom': 'none', 'bottom': '1px'})
  
              $('div#flyerDesc')
              .css({width: flyerAuswahlWeite+20, height: ''})
              .animate({'opacity': 'toggle'}, 1000);                       
  
            });         
          
          } else {
          
            var flyerAuswahlWeite = $('img#flyer' + nrNeu).attr('naturalWidth');
            
            $('td#' + arrKeyNeu + ' > div > div')
            .css({marginRight: Math.ceil((flyerAuswahlWeite+32)/2) * -1, 'border-bottom': '1px solid #550003', 'bottom': '0px'})
            .fadeIn(1000)
            
            $('td#' + arrKeyNeu + ' > div > div')
            .css({'border-bottom': 'none', 'bottom': '1px'})
  
            $('div#flyerDesc')
            .css({width: flyerAuswahlWeite+20, height: ''})
            .animate({'opacity': 'toggle'}, 1000);           
              
    
          
          }        
        
        });    
      
      });
      
    } 
    
  })      
  
});
