var changeimage_fileprefix = 'fileadmin/templates/main/img/picmenu/';
var changeimage_duration = 4000; /* ms */

var changeimage_imgno = 1;
var changeimage_max = 0;
var changeimage_state = "run";
var changeimage_timeout;

function changeimage_next() {
  if (changeimage_state == "run")
  {
    if (changeimage_imgno == changeimage_max) {
      changeimage_imgno = 1;
    } else {
      changeimage_imgno++
    };
    changeimage_hideall();
    document.getElementById('timg'+changeimage_imgno).style.visibility = 'visible';
    changeimage_timeout = window.setTimeout("changeimage_next()", changeimage_duration);
  }
}

function changeimage_init() {
  for (i=1; i<=27; i++) {
    timg = document.getElementById('timg'+i);
    if (timg && timg.hasChildNodes()) changeimage_max++; 
    else break;
  } 
  if (changeimage_max > 1) changeimage_generatemenu();
  changeimage_timeout = window.setTimeout("changeimage_next()", changeimage_duration);
}

function changeimage_hideall() {
  for (i=1; i<=27; i++) {
    img =  document.getElementById('timg'+i);
    if (img) img.style.visibility = 'hidden';
  }
}

function changeimage_onmouseover(no) {
  window.clearTimeout(changeimage_timeout);
  changeimage_state = 'stop';
  changeimage_imgno = no;
  changeimage_hideall();
  document.getElementById("timg"+no).style.visibility = 'visible';
}

function changeimage_onmouseout() {
  changeimage_state = 'run';
  changeimage_timeout = window.setTimeout("changeimage_next()", changeimage_duration);
}

function changeimage_generatemenu() {
  table       = document.createElement('table');
  cellpadding = document.createAttribute('cellpadding');
  cellspacing = document.createAttribute('cellspacing');
  border      = document.createAttribute('border');
  cellpadding.nodeValue = 0;
  cellspacing.nodeValue = 0;
  border.nodeValue      = 0;
  table.setAttributeNode(cellpadding);
  table.setAttributeNode(cellspacing);
  table.setAttributeNode(border);

  tbody = document.createElement('tbody');

  for (imgno = 1, row = 0; row < 2; row++) {
    tr = document.createElement('tr');
    for (col = 0; col < 3; imgno++, col++) {
      td = document.createElement('td');

      a    = document.createElement('a');
      href        = document.createAttribute('href');
      onmouseover = document.createAttribute('onmouseover');
      onmouseout  = document.createAttribute('onmouseout');
      href.nodeValue        = '#';
      onmouseover.nodeValue = 'changeimage_onmouseover('+imgno+');';
      onmouseout.nodeValue  = 'changeimage_onmouseout();';
      a.setAttributeNode(href);
      a.setAttributeNode(onmouseover);
      a.setAttributeNode(onmouseout);

      img    = document.createElement('img');
      src    = document.createAttribute('src');
      width  = document.createAttribute('width');
      height = document.createAttribute('height');
      alt    = document.createAttribute('alt');
      src.nodeValue    = changeimage_fileprefix + imgno + '.gif';
      width.nodeValue  = 25;
      height.nodeValue = 19;
      alt.nodeValue    = 'Picture ' + imgno;

      img.setAttributeNode(src);
      img.setAttributeNode(width);
      img.setAttributeNode(height);
      img.setAttributeNode(alt);

      a.appendChild(img);
      td.appendChild(a);
      if (document.getElementById('timg'+imgno).hasChildNodes()) {
        tr.appendChild(td);
        document.getElementById('limg'+imgno).style.visibility = 'visible';
      } else {
        tr.appendChild(document.createElement('td'));
      }
    }
    tbody.appendChild(tr);
  }
  table.appendChild(tbody);
  //document.getElementById('changeimage_menu').appendChild(table);
}
