// preload css hover images
var img0 = new Image(); img0.src = "images/pdf-gold.gif";
var img1 = new Image(); img1.src = "images/source-gold.gif";

//alert(document.compatMode) /* rendering in "quirks mode" ? */

function $(id)
{
  return document.getElementById(id);
}

// show fontsize dialog
$('fontsize').style.display = "block";

// Cookie Handling

function getCookieValue (cookieName)
{
  var exp = new RegExp (escape(cookieName) + "=([^;]+)");
  if ( exp.test (document.cookie + ";") )
  {
    exp.exec (document.cookie + ";");
    return unescape(RegExp.$1);
  }
  else return false;
}

function testSessionCookie ()
{
  document.cookie ="testSessionCookie=Enabled";
  if ( getCookieValue("testSessionCookie") == "Enabled" )
    return true
  else
    return false;
}

function writeSessionCookie (cookieName, cookieValue)
{
  if ( testSessionCookie() == true)
  {
    document.cookie = escape(cookieName) + "=" + escape(cookieValue) + "; path=/";
    return true;
  }
  else return false;
}

if ( getCookieValue("fontsize") )
  setFontSize ( getCookieValue("fontsize") );


// Event handler
function addEventHandler(func)
{
  var oldonload = window.onload;
  if ( typeof window.onload != 'function' )
    window.onload = func;
  else
    window.onload = function() { if (oldonload) { oldonload(); } func(); };
}


addEventHandler(
  function()
  {
    $('p80L').onmouseover = function() { printFontControl(80, '#c00'); };
    $('p90L').onmouseover = function() { printFontControl(90, '#c00'); };
    $('p100L').onmouseover = function() { printFontControl(100, '#c00'); };
    $('p110L').onmouseover = function() { printFontControl(110, '#c00'); };
    $('p120L').onmouseover = function() { printFontControl(120, '#c00'); };
    $('p130L').onmouseover = function() { printFontControl(130, '#c00'); };

    $('p80L').onmouseout = resetFontControl;
    $('p90L').onmouseout = resetFontControl;
    $('p100L').onmouseout = resetFontControl;
    $('p110L').onmouseout = resetFontControl;
    $('p120L').onmouseout = resetFontControl;
    $('p130L').onmouseout = resetFontControl;

    $('p80L').onfocus = $('p80L').onmouseover;
    $('p90L').onfocus = $('p90L').onmouseover;
    $('p100L').onfocus = $('p100L').onmouseover;
    $('p110L').onfocus = $('p110L').onmouseover;
    $('p120L').onfocus = $('p120L').onmouseover;
    $('p130L').onfocus = $('p130L').onmouseover;

    $('p80L').onblur = resetFontControl;
    $('p90L').onblur = resetFontControl;
    $('p100L').onblur = resetFontControl;
    $('p110L').onblur = resetFontControl;
    $('p120L').onblur = resetFontControl;
    $('p130L').onblur = resetFontControl;

    $('p80L').onclick = function() { setFontSize('0.8em'); return false; };
    $('p90L').onclick = function() { setFontSize('0.9em'); return false; };
    $('p100L').onclick = function() { setFontSize('1.0em'); return false; };
    $('p110L').onclick = function() { setFontSize('1.1em'); return false; };
    $('p120L').onclick = function() { setFontSize('1.2em'); return false; };
    $('p130L').onclick = function() { setFontSize('1.3em'); return false; };

    $('minuszeichen').onclick = function() { decFontSize(); return false; };
    $('pluszeichen').onclick = function() { incFontSize(); return false; };
  }
);

// functions
function getFontSize()
{
  var size = $('main-wrapper').style.fontSize;
  if ( size.length == 0 )
    size = '1.0em';
  return size;
}

function printFontControl( prozent, color )
{
  $('prozent').firstChild.nodeValue = prozent + '%';
  $('prozent').style.color = color;

  for ( i=80; i <= prozent; i+=10 )
    $( "p" + i ).style.backgroundColor = color;
  for ( ; i <= 130; i+=10 )
    $( "p" + i ).style.backgroundColor = "#90979e";
}

function resetFontControl()
{
  var size = getFontSize();
  var prozent = Math.round(100 * size.replace(/em/, ''));
  printFontControl(prozent, '#225284');
}

function setFontSize( size )
{
  var prozent = Math.round(100 * size.replace(/em/, ''));

  if ( prozent > 130 )
  {
    prozent = 130;
    size = '1.3em';
  }

  if ( prozent < 80 )
  {
    prozent = 80;
    size = '0.8em';
  }

  $('main-wrapper').style.fontSize = size;
  printFontControl(prozent, '#225284');
  writeSessionCookie ("fontsize", size);
}

function incFontSize()
{
  size = getFontSize();
  size = parseFloat( size.replace(/em/, '') ) + 0.1;
  setFontSize( size + 'em');
}

function decFontSize()
{
  size = getFontSize();
  size = parseFloat( size.replace(/em/, '') ) - 0.1;
  setFontSize( size + 'em');
}