/**
 * jQuery Fotoshow
 * requires jQuery v1.3.2
 *  
 * by O. Vogel
 * Version: 1.2
 **/

jQuery(document).ready(function() {
 
  var pageReload = 3;
  var count = 0;
  
  ////
  // Gibt den aktuellen Hashwert der URL zurueck
  ////
  function getHash() {
    strString = window.location.hash;
    var valueHash = parseInt(strString.substr(strString.indexOf("#")+1)); 
    return valueHash;
  }
  
  
  ////
  // Gibt die aktuellen ID zuzrueck fuer Back to Article
  ////
  function getID() {
    temp = window.location.hash;
    valueID = parseInt(temp.substring(temp.indexOf("-") + 1));
    
    if ( isNaN(valueID) ) {
      jQuery('.back2article').attr('href', "/");
      return 0;
    }
    else {
      jQuery('.back2article').html('Zum Artikel');
      jQuery('.back2article').attr('href', "/de/artikel/" + valueID);
      jQuery('.back2article').show();
      return valueID;
    }
  }
  
  
  ////
  //  Blendet des Prevbutton ein, wenn Hash > 1
  ////
  function showButtons() {
    pageHash = getHash();
    
    // Aus- und Einblenden aller nicht benoetigter Buttons
    if ( pageHash == 1 ) {
      jQuery('#image-prev-button').hide();
      jQuery('#image-next-button').show();

      jQuery('#image-next-button1').show();
    }
    else if ( pageHash == itemList.length + 1 ) {
      jQuery('#image-prev-button').show();
      jQuery('#image-next-button').hide();

      jQuery('#image-next-button1').hide();
    }
    else {
      jQuery('#image-prev-button').show();
      jQuery('#image-next-button').show();

      jQuery('#image-next-button1').show();
    }
  }
  
  
  ////
  //  Prueft, ob ein Reload noetig ist
  ////
  function checkReload( reloadHash, reloadBackID ) {
    if ( count == pageReload - 1 ) {
      pageHash = getHash();
      
      if ( reloadBackID ) {
        goTo = window.location.pathname + "#" + reloadHash + "-" + reloadBackID; 
      }
      else {
        goTo = window.location.pathname + "#" + reloadHash;
      }
      window.location.href = goTo;
      window.location.reload();
    }
    count++;
  }
  
  
  ////
  // Funktion zum tauschen der Bilder
  ////


  function changeImage( image, id ) {
    // Ausblenden des Prevbuttons im Randbereich
    if ( image == 0 ) {
      jQuery('.image-prev').hide();
    }
    
    if ( image >= 0 &&  image < itemList.length ) {
      var display = jQuery(itemList[image]).get(0);


      if ( image != 0 ) {
        jQuery('.image-prev').show();
      }
      jQuery('.image-next').show();
      jQuery('.fotoshow-weitere').hide();
      jQuery('#display-image').show();
      jQuery('#display-media').show();
      jQuery('#display-credit').show();
      jQuery('#display-bu').show();
      jQuery('#display-headline h1').html(display.headline);
      jQuery('.fotoshow-image').show();
      //jQuery('#display-image').hide( function () { jQuery('#display-image').attr('src', display.url).fadeIn('fast'); });
      jQuery('#display-image').attr('src', display.url);
      //jQuery('#display-link').attr('href', display.id);
      jQuery('#display-link').attr("href", display.id).show();
Shadowbox.setup("#display-link", {
        gallery:            "Fullview",
		content : display.id
        });
      jQuery('#display-media').html( display.media).show();
    jQuery('.fotoshow-image').show();
      jQuery('#display-bu').html(display.bu).show();
      jQuery('#display-credit').html(display.credit).show();
      jQuery('#display-amount').html(display.bildanzahl).show();
jQuery('#image-prev-button').show();
jQuery('#image-next-button').show();
jQuery('#thumbs').show();
      jQuery('.images').show(); 
jQuery('div.detailbild').show();
      jQuery('#display-dachzeile').show();  
      
      if ( id ) {
        goTo = window.location.pathname  + "#" + (image + 1) + "-" + id;
      }
      else {
        goTo = window.location.pathname  + "#" + (image + 1);
      }
      document.location.href = goTo;
    }
    else if ( image == itemList.length ) {
      jQuery('.fotoshow-image').hide();
      jQuery('#display-dachzeile').hide();
      jQuery('.fotoshow-weitere').show();
      jQuery('.image-next').hide();
      jQuery('.images').hide();
      jQuery('#display-bu').hide();
      jQuery('#display-credit').hide();
      jQuery('#display-amount').hide();
jQuery('#image-prev-button').hide();
jQuery('#image-prev-button1').show();
jQuery('#image-next-button').hide();
jQuery('#thumbs').hide();  




      
      jQuery('#display-headline h1').html('Weitere aktuelle Foto-Shows');
      
      if ( id ) {
        goTo = window.location.pathname + "#" + (image + 1) + "-" + id;
      }
      else {
        goTo = window.location.pathname + "#" + (image + 1);
      }
      window.location.href = goTo;
    }
    
  }
  
  ////
  // Funktion liest einen bestimmten Parameter aus der URL aus
  function get_url_param( name ) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");

    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );

    if ( results == null )
      return "";
    else
      return results[1];
  }
  
  
  function firstLoad() {
    ////
    // Setzt #1, wenn noch kein Wert gesetzt ist
    ////
    var strString = window.location.hash;
    var value_skip = get_url_param('skip');
    
    // Pruefen noch vorher ob skip-Parameter in URL vorhanden ist und mitgeschleift werden muss.
    if ( value_skip ) {
      if ( strString == "" ) {
        setHash = window.location.pathname + "?skip=" + value_skip + "#"+ value_skip + 1;
        window.location.href = setHash;
      } 
    }
    else {
      if ( strString == "" ) {
        setHash = window.location.pathname + "#1";
        window.location.href = setHash;
      }
    }
      
    showButtons();
    getID();
    
    var pageHash = getHash();
    ////
    // Einblenden des Startbildes wenn Hashwert vorhanden. Es wird noch abgeprueft, ob der Hashwert 
    // im erlaubten Bereich ist. Wenn nicht, wird das erste Bild dargestellt.
    // Der Erlaubte Bereich richtet sich nach der Laenge der itemList.
    ////
    if ( pageHash <= itemList.length && pageHash > 1 ) {
      // Darstellung Bild nach Hash
      var display = jQuery(itemList[pageHash - 1]).get(0);
      jQuery('.image-prev').show();
      jQuery('.image-next').show();
      jQuery('.fotoshow-image').show();
      jQuery('.fotoshow-weitere').hide();
      jQuery('#image-prev-button1').hide();
      jQuery('#display-image').attr("src", display.url).show();
      jQuery('#display-media').html( display.media);
      jQuery('#display-link').attr("href", display.id).show();
Shadowbox.setup("#display-link", {
        gallery:            "Fullview",
		content : display.id
        });
      jQuery('#display-headline h1').html(display.headline).show();
      jQuery('#display-bu').html(display.bu).show();
      jQuery('#display-credit').html(display.credit).show();
      jQuery('#display-amount').html(display.bildanzahl).show();
jQuery('div.detailbild').show();
jQuery('#image-prev-button').show();
jQuery('#image-next-button').show();
jQuery('#thumbs').show();    
jQuery('.images').show(); 
jQuery('#display-dachzeile').show();  

    }
    else if ( pageHash == itemList.length + 1 ) {
      // Darstellung weitere Links
      jQuery('.image-prev').show();
      jQuery('.image-next').hide();
      
      jQuery('.fotoshow-weitere').show();

      jQuery('#display-headline h1').html('Weitere aktuelle Foto-Shows');
      jQuery('#display-bu').hide();
      jQuery('#display-credit').hide();
     jQuery('div.detailbild').hide();
      jQuery('#display-amount').hide();
    }
    else {
      // Darstellung erstes Bild
      var display = jQuery(itemList[0]).get(0);
      jQuery('.image-prev').hide();
      jQuery('.image-next').show();
      jQuery('.fotoshow-image').show();
      jQuery('.fotoshow-weitere').hide();
      jQuery('#image-prev-button1').hide();
      jQuery('#display-image').attr("src", display.url).show();
      jQuery('#display-media').html( display.media).show;
      jQuery('#display-link').attr('href', display.id);
Shadowbox.setup("#display-link", {
        gallery:            "Fullview",
		content : display.id
        });
      jQuery('#display-headline h1').html(display.headline).show();
      jQuery('#display-bu').html(display.bu).show();
      jQuery('#display-credit').html(display.credit).show();
      jQuery('#display-amount').html(display.bildanzahl).show();
jQuery('#image-prev-button').hide();
jQuery('#image-next-button').show();
jQuery('#thumbs').show(); 
jQuery('.images').show(); 
jQuery('div.detailbild').show();
jQuery('#display-dachzeile').show();      
      
      pageID = getID();
      if ( pageID ) {
        if ( value_skip ) {
          goTo = window.location.pathname + "?skip=" + value_skip + "#1"  + "-" + pageID;
          window.location.href = goTo;
        } else {
          goTo = window.location.pathname + "#1" + "-" + pageID;
          window.location.href = goTo;
        }
      }
      else {
        if ( value_skip ) {
          goTo = window.location.pathname + "?skip=" + value_skip + "#1";
          window.location.href = goTo;
        } else {
          goTo = window.location.pathname + "#1";
          window.location.href = goTo;
        }
      }
      
    }
  }
  
  ////
  // inhalte werden beim ersten Laden gesetzt
  ////
  firstLoad();



  
  ////
  //  Interaktionen
  ////
  jQuery('#display-image').click(function() {
    pageHash = getHash();
    pageID = getID();
    checkReload( pageHash + 1, pageID );
    changeImage( pageHash, pageID );
    showButtons();


  });

  jQuery('#display-media img').click(function() {
    pageHash = getHash();
    pageID = getID();
    checkReload( pageHash + 1, pageID );
    changeImage( pageHash, pageID );
    showButtons();


  });
  
  jQuery('#image-prev-button').click(function() {
    pageHash = getHash();
    pageID = getID();
    checkReload( pageHash - 1, pageID );
    changeImage( pageHash - 2, pageID );
    showButtons();

  });

  jQuery('#image-prev-button1').click(function() {
    pageHash = getHash();
    pageID = getID();
    checkReload( pageHash - 1, pageID );
    changeImage( pageHash - 2, pageID );
    showButtons();

  });
  
  jQuery('#image-next-button').click(function() {
    pageHash = getHash();
    pageID = getID();
    checkReload( pageHash + 1, pageID );
    changeImage( pageHash, pageID );
    showButtons();

  });

  jQuery('#image-next-button1').click(function() {
    pageHash = getHash();
    pageID = getID();
    checkReload( pageHash + 1, pageID );
    changeImage( pageHash, pageID );
    showButtons();

  });
  
  jQuery('.overview').click(function() {
    jQuery('#fotoshow-slider').fadeOut('fast');
    jQuery('#fotoshow-overview').fadeIn('fast');
  });



$(".thumbimage .itemimage").click(function() {
var counter = $("span.counter",this).text();
    pageHash = getHash();
    pageID = getID();
    checkReload( counter - 1);
    changeImage( counter - 1);
    showButtons(); 


});




  
});
