var scrolling = false;

//position wiederherstellen
function resetscroll() {
  if (document.cookie) {
    var galleryposition = Number(readcookie('gallery')); if (isNaN(galleryposition)) galleryposition = 0;
    $('#gallery').attr({'scrollLeft':String(galleryposition)});
  }
}

//aktuelle vorschaubilder anzeigen
function  hilite(bodyclass) {
  if (bodyclass == 'index') {
    var imagecount = 96; /* anpassen, wenn mehr Bilder in die Galerie kommen! */
    var scrollposition = Math.round(Math.random() * (imagecount - 3)) * 75;
  }
  else {
    var dottedclass = String('.' + bodyclass);
    var imageposition = $('#gallery').find(dottedclass+':first').offset().left;
    var galleryposition = $('#gallery').offset().left;
    var scrollposition = imageposition - galleryposition;
  }
  //an die richtige stelle scrollen
  $('#gallery')
    .attr({'scrollLeft':String(scrollposition)})
    //hervorheben?
    //.find('img').not(dottedclass)
    //.fadeTo(0, 0.15)
  ;
  //linkziel gegebenenfalls ändern
  if (bodyclass == 'index') {
    $('#gallery a').attr({'href':'home.htm', 'title':'Link zur Startseite'});
  }
}

//position merken
function savescroll() {
  setcookie('gallery', $('#gallery').attr('scrollLeft'), 365);
}

//nach links scrollen
function scrollleft() {
  var scrollstep = 1;
  var newposition = $('#gallery').attr('scrollLeft');
  newposition -=  scrollstep;
  $('#gallery').attr({'scrollLeft':String(newposition)});
  if (scrolling) window.setTimeout("scrollleft()", 5);
}

//nach rechts scrollen
function scrollright() {
  var scrollstep = 1;
  var newposition = $('#gallery').attr('scrollLeft');
  newposition +=  scrollstep;
  $('#gallery').attr({'scrollLeft':String(newposition)});
  if (scrolling) window.setTimeout("scrollright()", 5);
}

//scrollen starten
function startscrolling() {
  scrolling = true;
}

//scrollen beenden
function stopscrolling() {
  scrolling = false;
}

//galerie einbinden
$(document).ready(function() {
  $.ajax({
    type:'GET',
    url:'/gallery.div', 
    success:function(gallerydiv, text) {
      var bodyclass = String($('body').attr('class'));
      //galerie-scroller
      if (bodyclass != 'index') {
        $('#wrapper').append(
          '<div id="galleryscroll" class="noprint">' + 
          '  <ul class="pager">' +
          '    <li class="previous"><a href="" onclick="return false;" onmousedown="startscrolling();scrollleft();" onmouseup="stopscrolling();" onmouseout="stopscrolling();">←</a></li>' +
          '    <li class="next"><a href="" onclick="return false;" onmousedown="startscrolling();scrollright();" onmouseup="stopscrolling();" onmouseout="stopscrolling();">→</a></li>' +
          '  </ul>' +
          '</div>'
        );
        $('#galleryscroll a')
          .mouseover(function() {overlink(this);})
          .mouseout(function() {outlink(this);})
        ;
      }
      //galerie
      if ($('#cookies').length) $('#cookies').before(gallerydiv);
      else $('#wrapper').append(gallerydiv);
      $('#gallery a img')
        .mouseover(function() {overimg(this);})
        .mouseout(function() {outimg(this);})
      ;
      //zurechtscrollen
      if (bodyclass.length) hilite(bodyclass);
      else resetscroll();
			//höhe des spektrums an navigation und galerie anpassen
			$('#spektrum').height($('#gallery').offset().top + $('#gallery').height() - $('#spektrum').offset().top);
    },
    error:function(request, message, exception) {
      //alert(String(exception));
      $('#wrapper').append(
        '<div id="missinggallery" class="noprint">' +
        '   <h3>Vorschaubildchen fehlen.</h3>' +
        '   <p>Beim Aufrufen der Vorschaubildchen für die Galerie ist leider ein Fehler aufgetreten: ' +
               String(exception) +
        '   </p>' +
        '</div>'
      );
    }
  });
  $(window).unload(function () {
    stopscrolling();
    savescroll();
  });  
});

