$(document).ready(function() {  
      
    /* TemplaVoilà Flexible CE "01 FCE Intro" */
    /*$("#content").find("object").each(function(){
      $(this).find("embed").each(function(){
        $(this).attr('wmode','opaque');
      });
      $(this).clone().after("object");
      $(this).remove();
    }
    ); */
    
 
    /* TemplaVoilà Flexible CE "02 FCE Störer" */
    // Bei Klick auf den Störer wird dieser geschlossen      
    $("#stoerer").click(function () { 
      $("#stoerer").css("display","none"); 
      return false;
    });

    /* TemplaVoilà Flexible CE "03 FCE Text + Fotoalbum" */    
    initPhotoAlbum();
});

function initPhotoAlbum()
{
    // Foto-Elemente (Bild + optionale Bildunterschrift) in Array zwischenspeichern
    photos = $('.pic_ele');
    // Anzahl der Foto-Elemente
    cntPhotos = photos.length;
    // Aktuelles Foto-Element ist beim Laden der Site das erste Element
    curPhoto = 0;
    // ... dass dann auch gleich eingeblendet wird.
    changePhoto();

    photos.each ( 
      function (intIndex) { 
        // Foto-Elemente (Bild + optionale Bildunterschrift) durchnummerieren (eindeutige IDs vergeben)
        $(this).attr('id','pic_ele'+intIndex);
      }
    );
   
    // Foto-Browser aufbauen und in das DOM einhängen 
    buildPhotoBrowser();

    // onClick-Handler für den Wechsel zum vorherigen Foto-Element
    $("#prevPhoto").click(function (pE) { 
      // das aktuelle Foto-Element verstecken
      hideCurPhoto();
      
      // Index des vorherigen Elements festlegen
      if (curPhoto == 0) 
      {
        curPhoto = cntPhotos - 1;
      } else {
        curPhoto--;      
      }
      
      // zum vorherigen Foto-Element wechseln
      changePhoto();
      
      StopEvent(pE);  
    });
    
    // onClick-Handler für den Wechsel zum nächsten Foto-Element
    $("#nextPhoto").click(function (pE) {
      // das aktuelle Foto-Element verstecken
      hideCurPhoto();
      
      // Index des nächsten Elements festlegen
      if (curPhoto == (cntPhotos-1)) 
      {
        curPhoto = 0;
      } else {
        curPhoto++;      
      }

      // zum nächsten Foto-Element wechseln
      changePhoto();

      StopEvent(pE);       
    });
}

// Baut den Foto-Browser auf und hängt ihn in das DOM, nach dem letzten Foto-Element, ein 
function buildPhotoBrowser()
{
    lastPicEleIndex = cntPhotos - 1;
    $('#pic_ele'+lastPicEleIndex).after('<div class="select"><div class="back"><a href="#" id="prevPhoto">back</a></div><div class="next"><a href="#" id="nextPhoto">next</a></div></div><div class="akt"><span id="curPhoto">'+curPhotoIndicator+'</span>/'+cntPhotos+'</div>');
}

// Das aktuelle Foto-Element verstecken um das vorherige/nächste einzublenden
function hideCurPhoto()
{
  $(photos[curPhoto]).css('display','none');
}

// Das vorherige/nächste Foto-Element einblenden
function changePhoto()
{
  $(photos[curPhoto]).css('display','block');
  
  updateCurPhotoIndicator();
}

// Anzeige des aktuellen Foto-Elements im Foto-Browser aktualiseren
function updateCurPhotoIndicator()
{
  curPhotoIndicator = curPhoto+1;
  $('#curPhoto').text(curPhotoIndicator);
}

function StopEvent(pE)
{  
   if (!pE)
     if (window.event)
	     pE = window.event;
     else
	     return;
       if (pE.cancelBubble != null)
          pE.cancelBubble = true;
       if (pE.stopPropagation)
          pE.stopPropagation();
       if (pE.preventDefault)
          pE.preventDefault();
       if (window.event)
          pE.returnValue = false;
       if (pE.cancel != null)
          pE.cancel = true;
}  // StopEvent