/* Copyright (c) 2009 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 *
 * Version: 3.0.2
 * 
 * Requires: 1.2.2+
 */
(function(c){var a=["DOMMouseScroll","mousewheel"];c.event.special.mousewheel={setup:function(){if(this.addEventListener){for(var d=a.length;d;){this.addEventListener(a[--d],b,false)}}else{this.onmousewheel=b}},teardown:function(){if(this.removeEventListener){for(var d=a.length;d;){this.removeEventListener(a[--d],b,false)}}else{this.onmousewheel=null}}};c.fn.extend({mousewheel:function(d){return d?this.bind("mousewheel",d):this.trigger("mousewheel")},unmousewheel:function(d){return this.unbind("mousewheel",d)}});function b(f){var d=[].slice.call(arguments,1),g=0,e=true;f=c.event.fix(f||window.event);f.type="mousewheel";if(f.wheelDelta){g=f.wheelDelta/120}if(f.detail){g=-f.detail/3}d.unshift(f,g);return c.event.handle.apply(this,d)}})(jQuery);

  var images = new Array();
  var timer;
  var loading = true;
  var delay = 400;
  var loaded = 0;
  var activeImageNo = 0;
  var num_of_img = (max - min) / step +1;

  function setStatus(status) {
//    $('#status').html('<p>' + status);
  }

  function initModelImg() {
    $('#modelimg')
     .mousewheel(function(event, delta) {
       if ((activeImageNo - delta >= 0) && (activeImageNo - delta < num_of_img))
         showImage(activeImageNo - delta, true);
       event.stopPropagation();
       event.preventDefault();
     });
  }

  function startAnimate() {
    if (activeImageNo +1 >= images.length) 
      showImage(0);
    else
      showImage(activeImageNo +1);

    if (activeImageNo +1 < images.length)
      timer = setTimeout(startAnimate, delay)
    else
      timer = setTimeout(startAnimate, delay *5);
  }

  function stopAnimate() {
    if (timer) clearTimeout(timer);
  }

  function preLoad() {
    $('#loading').css({'background':'url(/common_images/loading.gif) no-repeat', 'backgroundPosition':'center center'});

    loaded = 0;
    for (i = 0; i < num_of_img; i++) {
      images[i] = new Image();
      images[i].onload = Loaded;
      images[i].onerror = Loaded;
      number = min + i * step;
      if (number < 10) number = '0' + number;
      images[i].src = urlhead + number + urltail;
    }
  }

  function Loaded() {
    $('#imageno_' + loaded).css('backgroundColor', '#999999');

    loaded++;
    setStatus('Beeld ' + loaded + ' van ' + num_of_img + ' ingeladen...');
    if (loaded == num_of_img) {
      loading = false;
      $('#loading').css({'background':'url(/common_images/empty.gif) no-repeat', 'backgroundPosition':'center center'});
      startAnimate();
      setStatus('');
    }
  }

  function showImage(no, stop) {  // Laat 1 beeld zien, stop evt. de animatie
    if (loading) return;
    if (stop == true) stopAnimate();

    $('#imageno_' + activeImageNo).css('backgroundColor', $('#imageno_' + activeImageNo).attr('mycolor'));
    $('#imageno_' + no).css('backgroundColor', '#FF0000');
    $('#modelimg').attr('src', images[no].src);

    activeImageNo = no;
  }

  function resizeImgTable() {
    $("#imgtable").attr({width: imgwidth, height: imgheight});
  }

  function fillTimeLine() {
    e = document.getElementById('timeline');

    // Verwijder alle timeline items
    while (e.childNodes.length > 0) e.removeChild(e.firstChild);

    for (i = 0; i < num_of_img; i++) {
      tr = document.createElement('tr');
      td = document.createElement('td');
      td.setAttribute('id', 'imageno_' + i);
      td.style.height = '3px';
      td.style.backgroundColor = '#CCCCCC';
      td.setAttribute('mycolor', '#999999');

      td.onmouseover = new Function('showImage(' + i + ',true)');

      tr.appendChild(td);
      e.appendChild(tr);
    }

    // De "play" button
    tr = document.createElement('tr');
    td = document.createElement('td');
    a = document.createElement('a');
    a.setAttribute('href', '#');
    a.onclick = function() { stopAnimate(); startAnimate(); return false; };
    img = document.createElement('img');
    img.setAttribute('src', '/common_images/buttons/button-play.gif');
    img.setAttribute('border', '0');
    img.setAttribute('width', '15');
    img.setAttribute('height', '15');
    img.setAttribute('title', 'Afspelen');
    a.appendChild(img);
    td.appendChild(a);
    tr.appendChild(td);
    e.appendChild(tr);  
  }

  $(document).ready(function() { initModelImg(); resizeImgTable(); fillTimeLine(); preLoad(); });

