var anim_speed = 1000;
var imagecache = new Array();
var anim_idx = -1;
var anim_on = false;
var anim_timer = null;
var anim_maxloops = 5;
var anim_loop = 0;
var dt = new Date();
var playimg1 = "http://www.silkeborg-vejret.dk/vejrudsigt/gfs/media-playback-start.png";
var playimg2 = "http://www.silkeborg-vejret.dk/vejrudsigt/gfs/media-playback-pause.png";
var actColor = "#333";
var inActColor = "#4F94CD";


function settextcolor(name,col) {
  if(document.getElementById)
    document.getElementById(name).style.color = col;
  else { 
    document.all[name].style.color = col;
  }
}

function setimage(name,value) {
  if(document.getElementById) {
    img = document.getElementById(name); 
    img.src = value;
  }
  else {
    document.images[name].src = value;
  }
}

function setAnimSpeed(percentage) {
  anim_speed = percentage*1000;
}

function anim_faster(over) {
  if(anim_speed>600)
    anim_speed = anim_speed - 200;
  anim_show_speed(anim_speed);
}

function anim_slower(over) {
  if(anim_speed<5000)
    anim_speed = anim_speed + 200;
  anim_show_speed(anim_speed);
}

function returnObjById(id) {
  if (document.getElementById)
    var returnVar = document.getElementById(id);
  else if (document.all)
    var returnVar = document.all[id];
  else if (document.layers)
    var returnVar = document.layers[id];
  return returnVar;
}

function str_repeat(i, m) { for (var o = []; m > 0; o[--m] = i); return(o.join('')); }

function sprintf () {
  var i = 0, a, f = arguments[i++], o = [], m, p, c, x;
  while (f) {
    if (m = /^[^\x25]+/.exec(f)) o.push(m[0]);
    else if (m = /^\x25{2}/.exec(f)) o.push('%');
    else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
      if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) throw("Too few arguments.");
      if (/[^s]/.test(m[7]) && (typeof(a) != 'number'))
        throw("Expecting number but found " + typeof(a));
      switch (m[7]) {
        case 'b': a = a.toString(2); break;
        case 'c': a = String.fromCharCode(a); break;
        case 'd': a = parseInt(a); break;
        case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
        case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
        case 'o': a = a.toString(8); break;
        case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
        case 'u': a = Math.abs(a); break;
        case 'x': a = a.toString(16); break;
        case 'X': a = a.toString(16).toUpperCase(); break;
      }
      a = (/[def]/.test(m[7]) && m[2] && a > 0 ? '+' + a : a);
      c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
      x = m[5] - String(a).length;
      p = m[5] ? str_repeat(c, x) : '';
      o.push(m[4] ? a + p : p + a);
    }
    else throw ("Huh ?!");
    f = f.substring(m[0].length);
  }
  return o.join('');
}

function anim_show_speed(speed) {
  var elem = returnObjById("anim_speed_box");
  var multip = sprintf("%01.1fs", speed/1000);
  elem.innerHTML = multip;
  
}

function handleMousewheel(node, callback) {
  if (node.addEventListener) {
    node.addEventListener("DOMMouseScroll", callback, false);
    node.addEventListener("mousewheel", callback, false);
  }
  else {
    node.onmousewheel = callback;
  }
}

function mouseWheelCallback(e) {
  if (!e) 
    e = window.event;
  if ( e.wheelDelta <= 0 || e.detail > 0) {
    if(anim_timer) 
      clearTimeout(anim_timer); 
    anim_on = false; 
    anim_image((anim_idx+1)%anim_count,0);
  }
  else {
    if(anim_timer) 
      clearTimeout(anim_timer); 
    anim_on = false; 
    if (anim_idx==0) 
      anim_image(anim_count-1);
    else 
      anim_image((anim_idx-1)%anim_count,0);
  } 
  setimage("anim_img_play",playimg1);
  return cancelEvent(e);
}

function cancelEvent(e) {
  e = e ? e : window.event;
  if(e.stopPropagation)
    e.stopPropagation();
  if(e.preventDefault)
    e.preventDefault();
  e.cancelBubble = true;
  e.cancel = true;
  e.returnValue = false;
  return false;
}

function handleMouseClick(node, callback) {
  if (node.addEventListener) {
    node.addEventListener("DOMClick", callback, false);
    node.addEventListener("click", callback, false);
  } 
  else {
    node.onclick = callback;
  }
}

function mouseClickCallback(e) {
  if(anim_timer) 
    clearTimeout(anim_timer);
  if (anim_on){ 
    anim_on=false; 
    setimage("anim_img_play",playimg1);
  }
  else 
    anim_play();
  return true;
}

function anim_preload() { 
  var names = new Array(); 
  names = names.concat(anim_images_anim_anim); 
  names.sort(); 
  var n = imagecache.length; 
  var lastname = '';
  for(var i=0; i<names.length; i++) 
    if(names[i]!=lastname) { 
      lastname = names[i]; 
      imagecache[n]=new Image(); 
      imagecache[n].src = lastname; 
      n++; 
    }
}

function anim_play(over) { 
  if(!anim_on) { 
    anim_on = true; 
    anim_timer = setTimeout("anim_next()",anim_speed); 
    anim_loop = 0;
  } 
  setimage("anim_img_play",playimg2);
}

function anim_playpause(over) { 
  if(!anim_on) { 
    anim_on = true; 
    anim_timer = setTimeout("anim_next()",anim_speed); 
    anim_loop = 0;
    setimage("anim_img_play",playimg2);
  } else {
    if(anim_timer) 
      clearTimeout(anim_timer); 
    anim_on = false; 
    setimage("anim_img_play",playimg1);
  }
}

function anim_stop(over) { 
  if(anim_timer) 
    clearTimeout(anim_timer); 
  anim_on = false; 
  setimage("anim_img_play",playimg1);
}

function anim_stop2(over) { 
  if(anim_timer) 
    clearTimeout(anim_timer); 
  anim_on = false; 
  setimage("anim_img_play",playimg1);
  anim_image(0,1); 
}

function anim_image(idx,over) { 
  if(idx==anim_idx) 
    return; 
  if(over) { 
    setimage("anim_img_play",playimg1); 
    if(anim_timer) 
      clearTimeout(anim_timer); 
    anim_on = false; 
  } 
  if(anim_idx>=0) { 
    settextcolor("anim_txt_time_"+anim_idx, inActColor); 
  } 
  settextcolor("anim_txt_time_"+idx, actColor); 
  setimage("anim_image_anim_anim", anim_images_anim_anim[idx]); 
  anim_idx = idx;
}

function anim_next() {
  if(!anim_on)
    return;
  anim_image((anim_idx+1)%anim_count,0);
  if(anim_idx+1==anim_count)
    if(++anim_loop > anim_maxloops) {
      anim_stop(); 
      return;
    }
  var nextdelay = anim_speed;
  if(anim_idx+1==anim_count)
    var nextdelay = anim_speed/1000 * 2000;
  anim_timer = setTimeout("anim_next()",nextdelay);
}

function anim_start() {
  if(anim_timer)
    clearTimeout(anim_timer);
  anim_on = false;
  anim_preload();
  anim_image(0,1);
  //setTimeout("anim_play(1)",2000);
  if (document.getElementById('anim_image_anim_anim'))
    handleMousewheel(document.getElementById('anim_image_anim_anim'), mouseWheelCallback);
  if (document.getElementById('animtimer'))
    handleMousewheel(document.getElementById('animtimer'), mouseWheelCallback);
  if (document.getElementById('anim_image_anim_anim'))
    handleMouseClick(document.getElementById('anim_image_anim_anim'), mouseClickCallback);
}
