$(document).ready(function() {
  var bleft = 0;
  if ($('body#maatwerk').length) {
    $('div.slideshow').slideshow({ interval:3000, first:'17vegplus_638.jpg' });
    bleft = -18 * 84;
  } else if ($('body#partner').length) {
    $('div.slideshow').slideshow({ interval:3000, first:'34cresco.jpg' });
    bleft = -36 * 84;
  } else {
    $('div.slideshow').slideshow({ interval:3000 });
  }
 
  // fotobalk schuift automatisch naar links
  function balkleft() {
    var left = $('#fotobalk').position().left - 84;
    var right = $('#header').width() - $('#fotobalk').width();
    if (left < right ) {
      clearInterval(balkIntervalID);
      $('div.slideshow').slide_stop();
      // console.log($('#fotobalk div.thumbs').html());
      // $('#fotobalk div.thumbs').append($('#fotobalk div.thumbs').html());
      //alert($('#fotobalk div').html());
    }
    $('#fotobalk').animate( {left: left+'px'}, 1000 );
  }

  if ($('#fotobalk').length) {
    $('#fotobalk').css('left', bleft + 'px');
    var balkIntervalID = setInterval( balkleft, 3000 );
  }
  
  // klikken en schuiven met de fotobalk
  var startX = null;
  var startLeft = null;
  var foto = null;
  $('#fotobalk')
    .mousedown( function(e) {
      startLeft = $(this).position().left;
      startX = e.pageX - startLeft;
      e.preventDefault();
      clearInterval(balkIntervalID);
    });
   $(document).mousemove( function(e) {
      if (startX) {
        var left = e.pageX - startX;
        if (left > 0)
          left = 0;
        var right = $('#header').width() - $('#fotobalk').width();
        if (left < right )
          left = right;
        $('#fotobalk').css('left', left+'px');
        window.status = left;
      }
    })
    .mouseup( function(e) {
      //alert($(this).position().left);
      if(startX && e.pageX - startLeft == startX) {
        $('.slideshow').slide_stop();
        var bovenop = $('.slideshow img.bovenop');
        var next = $('.slideshow img[src="' + foto + '"]');
        bovenop.addClass('erachter');
        next.css({opacity: 0.0})
          .addClass('bovenop')
          .animate({opacity: 1.0}, 200, function() {
              bovenop.removeClass('bovenop erachter');
          });
        
        //$('.slideshow img').removeClass('bovenop');
        //$('.slideshow img[src="' + foto + '"]').addClass('bovenop');
      }
      startX = null;
    });
  $('#fotobalk img')
    .mousedown( function(e) {
      foto = $(this).attr('src').replace(/80x80/, '395x395');
//      alert(foto);
    });
  
  set_margins();
  $(window).resize(set_margins);
  
});

function set_margins() {
  var h = $(window).height();
  if (h > 595) {
    h = h - 595;
    $('#wrapper').css('margin-top', parseInt(h * 0.4));
  }
}

function popup(img) {
  // var url = "http://www.qualitypack.nl/" + img;
  // var w = window.open(url, "QualityPack", 'status=0, scrollbars=0, resizable=0, width=580, height=393');
  // w.focus();

  $('img#popup_'+img)
   .fadeIn('medium')
   .click(function() {
       $(this).fadeOut('medium');
     });;
}


