/** Newspaper ***********************************************************/

var newsItems = new Array();
var newsMeta = new Array();

var npContent = null; // = 160x142px
var npItems = new Array();
var npPaused = false;
var npNextIdx = 0;

function initNewspaper()
{
  npContent = document.getElementById("newspaperContent");
  if (npContent == null) return;
  var y = 5;
  while (y < 142)
  {
    var item = npAddItem(200); // out of sight
    var h = item.offsetHeight;
    item.style.top = y+"px";
    y += h+10;
  }
  setTimeout("setInterval('npScroller()',50)",5*1000);
}

function npScroller()
{
  if (npPaused) return;
  var y = 0;
  for (var n=0; n<npItems.length; ++n)
  {
    y = npItems[n].offsetTop-1;
    npItems[n].style.top = y+"px";
  }
  y += npItems[npItems.length-1].offsetHeight+10;
  if (y < 142) npAddItem(y);
  if (npItems[0].offsetTop+npItems[0].offsetHeight <= 0)
  {
    npContent.removeChild(npItems[0]);
    npItems.shift();
  }
}

function npAddItem(y)
{
  var item = document.createElement("div");
  item.style.position = "absolute";
  item.style.top = y+"px";
  var text = newsItems[npNextIdx];
  if (newsMeta[npNextIdx] != null)
  {
    item.style.cursor = "pointer";
    item.setAttribute("npID",newsMeta[npNextIdx]);
    item.onmouseover = function() { npOver(this) }
    item.onmouseout = function() { npOut(this) }
    item.onclick = function() { npClick(this) }
    text += " - <a href='?id="+newsMeta[npNextIdx]+"'>meer</a>";
  }
  else
    item.style.cursor = "default";
  if (newsItems[npNextIdx] == null)
    item.style.height = "100px"; // h*0.7 ongeveer
  else
    item.innerHTML = text;
  npContent.appendChild(item);
  npItems.push(item);
  if (++npNextIdx >= newsItems.length) npNextIdx = 0;
  return item;
}

function npOver(item)
{
  item.className = "npClickableItemHover";
}

function npOut(item)
{
  item.className = null;
}

function npClick(item)
{
  location.href = "?id="+item.getAttribute("npID");
}

function npStop()
{
  npPaused = true;
}

function npStart()
{
  npPaused = false;
}

/** Post-it *************************************************************/

var postitItems = new Array();
var postitMeta = new Array();

var piHeader = null;
var piContent = null;
var piNextIdx = 0;
var piCnt = 0;

function initPostit()
{
  piHeader = document.getElementById("postitHeader");
  piContent = document.getElementById("postitContent");
  if (piHeader == null || piContent == null) return;
  piNext();
  piCnt = 1000;
  setInterval("piNext()",100);
}

function piNext()
{
  if (piCnt == 0)
  {
    piHeader.innerHTML = postitItems[piNextIdx*2];
    var text = postitItems[piNextIdx*2+1];
    piOut();
    if (postitMeta[piNextIdx] != null)
    {
      text += " - <a href='"+postitMeta[piNextIdx]+"'>meer</a>";
      piHeader.style.cursor = "pointer";
      piContent.style.cursor = "pointer";
      piContent.onmouseover = piHeader.onmouseover = piOver;
      piContent.onmouseout = piHeader.onmouseout = piOut;
      piContent.onclick = piHeader.onclick = piClick;
    }
    else
    {
      piHeader.style.cursor = "default";
      piContent.style.cursor = "default";
      piHeader.onmouseover = piHeader.onmouseout = piHeader.onclick = null;
      piContent.onmouseover = piContent.onmouseout = piContent.onclick = null;
    }
    piContent.innerHTML = text;
    if ((++piNextIdx)*2 >= postitItems.length) piNextIdx = 0;
  }
  else if (piCnt < 1000)
  {
    if (piHeader.style.display == "none")
      piContent.style.display = piHeader.style.display = "";
    else
      piContent.style.display = piHeader.style.display = "none";
  }
  else if (piCnt > 7000)
  {
    piCnt = 0;
    return;
  }
  else
    piContent.style.display = piHeader.style.display = "";
  piCnt += 100;
}

function piOver()
{
  piContent.className = piHeader.className = "piClickableItemHover";
}

function piOut()
{
  piContent.className = piHeader.className = null;
}

function piClick()
{
  location.href = postitMeta[(piNextIdx-1)%postitMeta.length];
}

/** Photos **************************************************************/

var photos = new Array();
var photoMeta = new Array(); // id,h

var phContent = null; // = 200px x 100%
var phItems = new Array();
var phPaused = false;
var phNextIdx = 0;

function initPhotos()
{
  phContent = document.getElementById("photos");
  if (phContent == null) return;
  var y = 0;
  while (y < phContent.offsetHeight)
  {
    var item = phAddItem(phContent.offsetHeight); // out of sight
    var h = item.offsetHeight;
    item.style.top = y+"px";
    y += h+1;
  }
  setTimeout("setInterval('phScroller()',50)",5*1000);
}

function phScroller()
{
  if (phPaused) return;
  var y = 0;
  for (var n=0; n<phItems.length; ++n)
  {
    y = phItems[n].offsetTop-1;
    phItems[n].style.top = y+"px";
  }
  y += phItems[phItems.length-1].offsetHeight+1;
  if (y < phContent.offsetHeight) phAddItem(y);
  if (phItems[0].offsetTop+phItems[0].offsetHeight <= 0)
  {
    phContent.removeChild(phItems[0]);
    phItems.shift();
  }
}

function phAddItem(y)
{
  var item = document.createElement("div");
  item.style.position = "absolute";
  item.style.top = y+"px";
  item.style.cursor = "pointer";
  item.setAttribute("phID",photoMeta[phNextIdx*2]);
  item.onclick = function() { phClick(this) }
  var img = document.createElement("img");
  img.src = photos[phNextIdx];
  img.style.width = "200px";
  img.style.height = photoMeta[phNextIdx*2+1]+"px";
  item.appendChild(img);
  phContent.appendChild(item);
  phItems.push(item);
  if (++phNextIdx >= photos.length) phNextIdx = 0;
  return item;
}

function phClick(item)
{
  location.href = "?id="+item.getAttribute("phID");
}

function phStop()
{
  phPaused = true;
}

function phStart()
{
  phPaused = false;
}
