isNetscape = navigator.appName=="Netscape";
var iStartSection = -1;
var iSections = 4;
var aName = Array('news_nieuws', 'news_weer', 'news_file', 'news_flits');
var aMax  = Array(250, 250, 250, 250);
var aMin  = Array(1, 1, 1, 1);
var aStep = Array(2.6, 1.3, 0, 0);

//-----------------------------------------------------------------------------

function StartShowNews(iSection)
{
  if(iStartSection != iSection)
  {
    iStartSection = iSection;
    if(iNewsTimer == -1)
    {
      iNewsTimer = window.setInterval("ShowNews()", 50);
    }
    if(iStartSection == 0)
    {
      bScrollIgnore = true;
      if(!bScrollNews)
      {
        window.setTimeout("ScrollNews(false)", iScrollSpeed);
        bScrollNews = true;
      }
    }
    else if(bScrollNews)
    {
      bScrollNews = false;
    }
  }
}

//-----------------------------------------------------------------------------

function ShowNews()
{
  change = false;

  for(i=0; i<iSections; i++)
  {
    var el = document.getElementById(aName[i]);

    //Trace(aName[i]+': ' + el.offsetHeight + '=' + aMax[i]);

    if(el)
    {
      if(i == iStartSection)
      {
        el.style.display = '';
        if(el.offsetHeight < aMax[i])
        {
          aStep[i] = Math.min(3.14, aStep[i] + 0.1);
          a = (1-Math.cos(aStep[i]))/2;
          a = a*a;
          y = aMin[i] + aMax[i] * a;
          el.style.height  = Math.min(y, aMax[i])+"px";
          //el.style.height  = 5 + el.offsetHeight;
          change = true;
        }
      }
      else
      {
        if(el.offsetHeight > aMin[i])
        {
          aStep[i] = Math.max(0, aStep[i] - 0.1);
          a = (1-Math.cos(aStep[i]))/2;
          a = a*a;
          y = aMin[i] + aMax[i] * a;
          el.style.height  = Math.max(1, y)+"px";

          change = true;
        }
        else
        {
          el.style.display = 'none';
        }
      }
    }
  }
  if(!change)
  {
    window.clearInterval(iNewsTimer);
    iNewsTimer = -1;
  }
  //Trace('----');
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------


function StartScrollNews()
{
  if(bScrollNews && !bScrollStart)
  {
    if(!bScrollIgnore)
    {
      if(parseInt(navigator.appVersion)>3)
      {
        document.onmousemove = MouseMove;
        if(isNetscape)
        {
          document.captureEvents(Event.MOUSEMOVE);
        }
      }
    }
    bScrollIgnore = false;
  }
}

//-----------------------------------------------------------------------------

function StopScrollNews()
{
  StopNext();
  var el  = document.getElementById(aName[0]);
  if(el)
  {
    el.style.cursor = '';
  }
  if(parseInt(navigator.appVersion)>3)
  {
    document.onmousemove = null;
    if(isNetscape)
    {
      document.releaseEvents(Event.MOUSEMOVE);
    }
  }
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

iNewsTimer   = -1;
bScrollNews  = true;
bScrollIgnore= false;
bScrollStart = true;
iScrollSpeed = 2000;
iScrollDir   = 1;
iScrollDef   = 45;
window.setTimeout("ScrollNews(true)", iScrollSpeed);

//-----------------------------------------------------------------------------

function ScrollNews(Init)
{
  var el  = document.getElementById(aName[0]);
  var el1 = document.getElementById(aName[0]+'1');
  if(el && el1)
  {
    el.scrollTop += iScrollDir;
    if((iScrollDir > 0) &&
       (el.scrollTop >= el1.offsetHeight))
    {
      el.scrollTop -= el1.offsetHeight;
    }
    else if((iScrollDir < 0) &&
            (el.scrollTop == 0))
    {
      el.scrollTop = el1.offsetHeight;
    }
  }
  if(bScrollNews)
  {
    if(bScrollStart)
    {
      iScrollSpeed = Math.max(iScrollDef, iScrollSpeed/1.5);
      if(iScrollSpeed == iScrollDef)
      {
        bScrollStart = false;
      }
    }
    window.setTimeout("ScrollNews(false)", iScrollSpeed);
  }
}

//-----------------------------------------------------------------------------

function MouseMove(ev)
{
  if(isNetscape)
  {
    MouseX = ev.pageX; // pageX
    MouseY = ev.pageY;
  }
  else
  {
    MouseX = event.x;
    MouseY = event.y;
  }

  var el  = document.getElementById(aName[0]);
  if(el)
  {
    elp = el;
    elX = MouseX;
    do
    {
      elX -= elp.offsetLeft;
      elp  = elp.offsetParent;
    }
    while(elp);

    if((elX < 30) || (elX > 98))
    {
      iNextNext = (elX < 60) ? -1 : 1;
      el.style.cursor = (elX < 60) ? 'w-resize' : 'e-resize';
      StartNext();
    }
    else
    {
      StopNext();

      iScrollSpeed = (MouseY+document.body.scrollTop-260);
      if(iScrollSpeed < 0)
      {
        iScrollSpeed = -iScrollSpeed;
        iScrollDir = -1;
      }
      else
      {
        iScrollDir = 1;
      }
      el.style.cursor = (iScrollDir < 0) ? 'n-resize' : 's-resize';
      iScrollSpeed = Math.max(0, 100-iScrollSpeed);
    }
  }
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

iNextTimer   = -1;
iNextState   = 0;
iNextCount   = 0;
iNextColor   = 0x33;
iNextNext    = 1;
iNextCurrent = 0;
iNextMax     = 1;

//-----------------------------------------------------------------------------

function StartNext()
{
  if(iNextTimer == -1)
  {
    iNextTimer = window.setInterval("NextNews()", 50);
  }
}

//-----------------------------------------------------------------------------

function StopNext()
{
  if((iNextTimer != -1) && (iNextState == 0))
  {
    window.clearInterval(iNextTimer);
    iNextTimer = -1;
    iNextCount = 0;

    if(iStartSection <= 0)
    {
      bScrollIgnore = true;
      if(!bScrollNews)
      {
        window.setTimeout("ScrollNews(false)", iScrollSpeed);
        bScrollNews = true;
      }
    }
  }
}

//-----------------------------------------------------------------------------

function NextNews()
{
  var el  = document.getElementById(aName[0]);
  if(el)
  {
    switch(iNextState)
    {
      // Wait some
      case 0:
        if(iNextCount++ > 10)
        {
          iNextState = 1;
          bScrollNews = false;
        }
        break;

      // Dissolve text
      case 1:
        iNextColor += 15;

        if(iNextColor < 255)
        {
          col = decToHex(iNextColor);
          el.style.color = "#"+col+col+col;
        }
        else
        {
          iNextState++;
        }
        break;

      // Change text
      case 2:
        iNextCurrent += iNextNext;
        if(iNextCurrent == NewsText.length)
        {
          iNextCurrent = 0;
        }
        if(iNextCurrent < 0)
        {
          iNextCurrent = NewsText.length-1;
        }
        NewsNieuws1 = document.getElementById('news_nieuws1');
        NewsNieuws2 = document.getElementById('news_nieuws2');

        NewsNieuws1.innerHTML = NewsText[iNextCurrent];
        NewsNieuws2.innerHTML = NewsText[iNextCurrent];
        ScrollNews(false);
        iNextState++;
        break;

      // Resolve text
      case 3:
        iNextColor -= 15;

        if(iNextColor > 0x33)
        {
          col = decToHex(iNextColor);
          el.style.color = "#"+col+col+col;
        }
        else
        {
          el.style.color = '';
          iNextState++;
        }
        break;


      // Done
      default:
        iNextState = 0;
        StopNext();
        break;
    }
  }
}

//-----------------------------------------------------------------------------

function Trace(t)
{
  trace=document.getElementById('ad_text');

  trace.innerHTML = t + '<br>' + trace.innerHTML;
}

//-----------------------------------------------------------------------------

function decToHex(dec)
{
  var hexStr = "0123456789ABCDEF";
  var low = dec % 16;
  var high = (dec - low)/16;
  var hex = "" + hexStr.charAt(high) + hexStr.charAt(low);
  return hex;
}

//-----------------------------------------------------------------------------

function goNews(link)
{
  window.open(link, 'news', 'toolbar=no,status=no,scrollbars=yes,resizable=yes,width=750,height=500');
  return false;
}

