window.onload = function() {

function slider(){

  sliderWidth = 0;
  
  $('#slideshow ul.content li').each(function(){
    sliderWidth += $(this).width() + 120;
  });

  $('#slideshow ul').css('width', sliderWidth);

  // scrollbuttons anzeigen ?
  if (sliderWidth>$('#slideshow').width()) {
    $('.prev', '#scroller').css('visibility', 'visible');
    $('.next', '#scroller').css('visibility', 'visible');
  }
  else{
    $('.prev', '#scroller').css('visibility', 'hidden');
    $('.next', '#scroller').css('visibility', 'hidden');
    $('.next', '#scroller').attr('src', '/img/products/arrownext.gif');
    $('.next', '#scroller').css('cursor', 'pointer');
    $('.content').animate({left:'0px'},'slow', callback);
  }

  // mouse down
  $('.prev', '#scroller').mousedown(clickPrevBtn);
  $('.next', '#scroller').mousedown(clickNextBtn);

  // mouse out
  $('.prev', '#scroller').mouseout(stopAnim);
  $('.next', '#scroller').mouseout(stopAnim);

  // mouse up
  $('#scroller').mouseup(stopAnim);
}

slider();

//######====== slider aufruf bei resize |05.07.2011|
  $(window).resize(function(){

    
    /*
var e = $('div#slideshow');
    var offPos = e.offset();
    
    console.log(offPos.left);
*/    
    if(sliderWidth < $('div#slideshow').width()){
      timer = setTimeout(slider,500);
//      console.log('action');
      slider();
    }

  });

};

function stopAnim() {
  $('.content', '#slideshow').stop();
}

function restTime(sliderWidth, direction) {
  // geschwindikgeit abhängig von position
  var leftPos = (($('.content', '#slideshow').position().left));
  var interval = 1000;
  var maxLeft = (sliderWidth-$('#slideshow').width());
  if (direction == 'prev') {
    var restTime = parseInt(-(interval / maxLeft * leftPos));
  } else {
    var restTime = parseInt(interval - -(interval / maxLeft * leftPos));
  }
  return restTime;
}



function callback() {
   LeftPos = (($('.content', '#slideshow').position().left));
   
 // var direction = (LeftPos == 0 ? 'prev' : 'next');
  if(LeftPos == 0){
    var direction = 'prev';
  }else{
    var direction = 'next';
  }
    
    

  $('.'+direction, '#scroller').attr('src', '/img/products/arrow'+direction+'.disabled.gif');
  $('.'+direction, '#scroller').css('cursor', 'default');
}

function clickPrevBtn() {

  $('.next', '#scroller').attr('src', '/img/products/arrownext.gif');
  $('.next', '#scroller').css('cursor', 'pointer');

  $('.content', '#slideshow').animate({left: 0}, restTime(sliderWidth, 'prev'), 'swing', callback);
  return false;
}

function clickNextBtn() {

  $('.prev', '#scroller').attr('src', '/img/products/arrowprev.gif');
  $('.prev', '#scroller').css('cursor', 'pointer');

  var maxLeft = -(sliderWidth-$('#slideshow').width());
  $('.content', '#slideshow').animate({left: maxLeft}, restTime(sliderWidth, 'next'), 'swing', callback);
  return false;
}

