function xnLog(text)
{
  //xnDebug2Div(text);
  //var dDiv = document.getElementById('debugDiv_xxxx');
  //dDiv.innerHTML += text + '<br>';
//  if (window.console != undefined)
//  {
//    window.console.log(text);
//  }
  //document.body.innerHTML += (text + "<br>");
}

dBox = {};
dBox.config = {};
dBox.config.delay = 3000;
dBox.config.divId = 'dedication';
dBox.config.controls_divId = 'dedicationControls';

dBox.paused = false;

dBox.dedications = {};
dBox.dedications.items = {};
dBox.dedications.count = 0;

dBox.current = {};
dBox.current.objTimeout = 0;
dBox.current.timeoutValue = {};
//dBox.current.date = {}; //date will not be used yet.... could be memory overflows
dBox.current.index = {};

// slideshow flow controls
dBox.firstStart = function()
{
  if (dBox.dedications.count > 0)
  {
    var index, i, dedicationDiv, divHeight, maxHeight;
    
    dedicationDiv = document.getElementById(dBox.config.divId);
    dedicationDiv.style.visibility = 'hidden';
    dedicationDiv.style.height = '';
    
    maxHeight = 0;
    maxWidth = 400; // 370
    for (i=0; i<dBox.dedications.count; i++)
    {
      index = i;
      dedicationDiv.innerHTML = dBox.dedications[index];
      dBox.ctl.highlight(index);
      
      divHeight = dedicationDiv.clientHeight;
      maxHeight = Math.max(maxHeight, divHeight);
      
      divWidth = dedicationDiv.clientWidth;
      maxWidth = Math.max(maxWidth, divWidth);
    }
    
    dedicationDiv.style.visibility = 'visible';
    dedicationDiv.style.height = maxHeight + 'px';
    dedicationDiv.style.width = maxWidth + 'px';
  }
}

// start from first
dBox.start = function()
{
  dBox.current.index = 0;
  if (dBox.dedications.count > 0)
  {
    dBox.show(dBox.current.index);
  }
}

dBox.stop = function()
{
  if (dBox.current.objTimeout)
  {
    window.clearTimeout(dBox.current.objTimeout);
  }
}

dBox.pause = function(control)
{
  dBox.stop();
  dBox.paused = !dBox.paused;
  dBox.resume();
}

// resume slideshow timer
dBox.resume = function()
{
  if (!dBox.paused)
  {
    //optimization.. if we have less then two dedications we do not have to switch to next
    if (dBox.dedications.count >= 2)
    {
      dBox.current.objTimeout = window.setTimeout('dBox.next()', dBox.config.delay);
      xnLog('timeout(' + dBox.config.delay + ') started');
    }
  }
}

dBox.step = function(step)
{
  dBox.current.index += step;
  dBox.current.index = dBox.current.index % dBox.dedications.count;
  if (dBox.current.index < 0) {dBox.current.index += dBox.dedications.count;};
  dBox.show(dBox.current.index);
}

dBox.next = function()
{
  xnLog('next!');
  dBox.step(1);
  xnLog('next done!');
  return false;
}
dBox.previous = function(){dBox.step(-1); return false;};

// items
dBox.show = function(index)
{
  dBox.stop();
  xnLog('show(' +index+ ')');
  var dedicationDiv = document.getElementById(dBox.config.divId);
  xnLog(dBox.dedications[index]);
  dedicationDiv.innerHTML = dBox.dedications[index];
  dBox.ctl.highlight(index);
  dBox.paused = false;
  dBox.resume();
  return false;
}

dBox.add = function(dedication)
{
  /*
  dedication.dedicationFor
  dedication.names
  dedication.dedicationBy
  dedication.text
  */
  
  // !!! for now dedication will not be an object.. it will be just html
  // later maybe better to add object handling
  dBox.dedications[dBox.dedications.count] = dedication;
  dBox.dedications.count++;
}

dBox.clear = function()
{
  dBox.stop();
  dBox.dedications = {};
  dBox.dedications.count = 0;
}

// controls visual
dBox.ctl = {};
dBox.ctl.showByClick = function()
{
  dBox.show(this.dedicationIndex);
  return false;
}

dBox.ctl.ppByClick = function()
{
  //dBox.show(this.dedicationIndex);
  dBox.pause();
  //this.innerHTML = (dBox.paused)? '[ > ]' : '[ II ]';
  this.className = (dBox.paused)? 'dBoxPlay' : 'dBoxPause';
  return false;
}


dBox.ctl.redraw = function()
{
  dBox.ctl.hide();
  
  var cont = document.getElementById(dBox.config.controls_divId);
  cont.innerHTML = '';
  
  if (dBox.dedications.count > 1)
  {
    var itemNum, textDiv;
    
    textDiv = document.createElement('div');
    textDiv.className = 'clear';
    cont.appendChild(textDiv);
    
    var oPrev = document.createElement('a');  
    //oPrev.innerHTML = '[ << ]';
    oPrev.href = '#previous';
    oPrev.onclick = dBox.previous;
    oPrev.className = 'dBoxPrev';
    cont.appendChild(oPrev);
    
    var oPP = document.createElement('a');  
    //oPP.innerHTML = '[ II ]';
    oPP.href = '#pause';
    oPP.onclick = dBox.ctl.ppByClick;
    oPP.className = 'dBoxPause';
    cont.appendChild(oPP);
    
    dBox.ctl.list = new Array();
    for (var i=1; i<=dBox.dedications.count; i++)
    {
      //textDiv = document.createElement('div');
      itemNum = document.createElement('a');
//      itemNum.innerHTML = '[ ' + i + ' ]';
      itemNum.innerHTML = "<table class='dBoxNum dBoxNumTable'><tr><td valign='middle' align='center'>" + i + "</td></tr></table>";
      itemNum.href = '#dedication' + i;
      itemNum.id = 'dBox_ctl_num' + i;
      itemNum.dedicationIndex = (i-1);
      itemNum.onclick = dBox.ctl.showByClick;
      itemNum.className = 'dBoxNum';
      //textDiv.appendChild(itemNum);
      
      //cont.appendChild(textDiv);
      cont.appendChild(itemNum);
      dBox.ctl.list[dBox.ctl.list.length] = itemNum;
      delete(itemNum);
      //delete(textDiv);
    }
    
    var oNext = document.createElement('a');
    //oNext.innerHTML = '[ >> ]';
    oNext.href = '#next';
    oNext.onclick = dBox.next;
    oNext.className = 'dBoxNext';
    cont.appendChild(oNext);
    
    textDiv = document.createElement('div');
    textDiv.className = 'clear';
    cont.appendChild(textDiv);
    
    dBox.ctl.show();
  }
}

dBox.ctl.hide = function()
{
  var cont = document.getElementById(dBox.config.controls_divId);
  cont.style.dispay = 'none';
}

dBox.ctl.show = function()
{
  var cont = document.getElementById(dBox.config.controls_divId);
  cont.style.dispay = '';
}

dBox.ctl.highlight = function(index)
{
  if (dBox.ctl.list != undefined)
  {
  var c = dBox.ctl.list.length;
  for (var i=0; i<c; i++)
  {
    dBox.ctl.list[i].className = (i==index)? 'dBoxNumSelected' : 'dBoxNum';
  }
  }
}
