/*!
 * Steigbügel Scroller
 *
 * (c) 2010, buch + musik, ejw Service GmbH
 * mweinschenk / LIGHTon IT-Solutions
 */

var positions    = new Array(8);
var active       = 0; 
var currentImage = -1;

$(window).scroll(function () {
  scroll(); 
});

$(document).ready(function() {
  // System aktivieren, wenn alle Topics gefunden.
  active=1;   
  // positionen auslesen    
  for (var i = 1; i < positions.length; ++i) {  
    if ($('#topic_'+i).offset() != null) {
      positions[i] = $('#topic_'+i).offset().top;
    } else {
      // Deaktivieren
      active=0;
    } 
  }
  scroll();

  
  var top = $('#topic_elements').offset().top - parseFloat($('#topic_elements').css('marginTop').replace(/auto/, 0));
  $(window).scroll(function (event) {
    // what the y position of the scroll is
    var y = $(this).scrollTop();
  
    // whether that's below the form
    if (y >= top) {
      // if so, ad the fixed class
      $('#right-bar').addClass('fixed');
    } else {
      // otherwise remove it
      $('#right-bar').removeClass('fixed');
    }
  });
});    



function scroll() {
  

  if (active) {
  
 
  
    for (var i = 1; i < positions.length; ++i) {
      // Jetzt prüfen
      currentPosition = $(window).scrollTop();
      

      
      
      // Ersten Elemente bis auf letztes
      if (i==1) {
        if (positions[i] > currentPosition && currentImage!=i) {
          resetImage();
          setImage(i);
          currentImage=i;
        }              
      }
                  
      if (i < positions.length-1) {
        if (positions[i] < currentPosition+50 && positions[i+1] >= currentPosition+50) {
          if (currentImage!=i) {
            resetImage();
            setImage(i);
            currentImage=i;
          } 
        }
      } else if (i == positions.length-1) {
        if (positions[i] < currentPosition+50) {
          if (currentImage!=i) {
            resetImage();
            setImage(i);
            currentImage=i;
          } 
        }
      }
    }
  } 
}

// Bilder reset
function resetImage() {
  $('#pic_1').attr("src","fileadmin/templates/images-right/1_no.jpg");
  $('#pic_2').attr("src","fileadmin/templates/images-right/2_no.jpg");
  $('#pic_3').attr("src","fileadmin/templates/images-right/3_no.jpg");
  $('#pic_4').attr("src","fileadmin/templates/images-right/4_no.jpg");
  $('#pic_5').attr("src","fileadmin/templates/images-right/5_no.jpg");
  $('#pic_6').attr("src","fileadmin/templates/images-right/6_no.jpg");
  $('#pic_7').attr("src","fileadmin/templates/images-right/7_no.jpg");
}

// Bilder set
function setImage(imageId) {
  $('#pic_'+imageId).attr("src","fileadmin/templates/images-right/"+imageId+"_act.jpg");
}
