//main.js

window.onload = function ()
{
  home ();
}

//helper methods
function getById (id)
{
  return window.document.getElementById (id);
}

function setLogoutVisible (vis)
{
  if (vis)
    getById ("logout").innerHTML = "<a href=\"javascript:followLink('intern/logout')\" class=\"inBlueLink\">ausloggen</a>";
  else
    getById("logout").innerHTML = "";
}

//site-control methods
function home ()
{
  getById ("smallNaviDiv").style.display = 'none';
  getById ("bigNaviDiv").style.display = 'block';
  getById ("leftTd").style.width = '446px';
  getById ("rightTd").style.height = '431px';
  loadToRightTd ("neues");
}

function changeCat (cat, link)
{
  getById ("bigNaviDiv").style.display = 'none';
  getById ("smallNaviDiv").style.display = 'block';
  getById ("leftTd").style.width = '150px';
  getById ("rightTd").style.height = 'auto';

  getById ("navichor").style.backgroundColor = '';
  getById ("navibilder").style.backgroundColor = '';
  getById ("navijungblaeser").style.backgroundColor = '';
  getById ("naviintern").style.backgroundColor = '';
  getById ("navi"+cat).style.backgroundColor = '#efe';
}

function followLink (href)
{
  var link = href.split ('/', 2);
  if (link[0].indexOf('.') > 0)
  {
    document.href = href;
    return;
  }
  if (link.length > 1)
    changeCat (link[0]);
  else
    if (link[0] == 'index')
      home ();
    else
      changeCat ('none', href);

  loadToRightTd (href);
}

//animation methods
/*function shrinkLeftTD(size)
{
  size -= 10;
  if (size <= 100)
  {
    size = 100;
    getById ("rightTd").style.visibility = "visible";
  }
  else setTimeout ("shrinkLeftTD("+size+")", 10);
  getById ("leftTd").style.width = size+'px';
}

function growLeftTD (size)
{
  size += 10;
  if (size >= 434)
  {
    size = 434;
    getById ("smallNaviDiv").style.display = 'none';
    getById ("bigNaviDiv").style.display = '';
    getById ("rightTd").style.visibility = "visible";
  }
  else setTimeout ("growLeftTD("+size+")", 10);
  getById ("leftTd").style.width = size+'px';
}*/

//general control methods
function totop ()
{
  window.scroll (0, 0);
}

function submitForm (form)
{
  document.getElementById (form).submit();
}

//Caution: This method does not work with file upload, i.e. multipart-formdata!
function submitForm2 (formid)
{
  var form = document.getElementById (formid);
  var params = "";

  //add a pair to the parameter list
  function addParam (name, value)
  {
    params += (params.length>0 ? "&" : "")
      + escape(name) + "="
      + escape(value);
  }

  //search the form for data to send
  for (var i=0;i<form.elements.length;i++)
  {
    var elem = form.elements[i];
    if (!elem.name)
      continue;
    var elemType = elem.type.toLowerCase ();
    if (elemType == "text" ||
        elemType == "textarea" ||
        elemType == "password" ||
        elemType == "hidden" ||
        (elemType == "radio" && elem.checked))
      addParam (elem.name, elem.value);
    else if (elemType == "checkbox" && elem.checked)
      addParam (elem.name, (elem.value ? elem.value : "true"));
    else if (elemType.indexOf ("select") != -1)
      for (var j = 0; j < elem.options.length; j++)
      {
        var option = elem.options[j];
        if (option.selected)
          addParam (elem.name, option.value ? option.value : option.text);
      }
  }

  var action = "";
  for (var i=0;i<form.attributes.length;i++)
    if (form.attributes[i].name=="action")
      action = form.attributes[i].value;

  loadToRightTd (action, params);

  //don't let the browser submit the form:
  return false;
}

function openInfoBox (ib)
{
  hideInfoBox ();
  document.getElementById (ib).style.display="block";
  document
}

function hideInfoBox ()
{
  var divs = document.getElementsByTagName ("div");
  for (var i=0; i<divs.length; i++)
    if (divs[i].className == "infobox")
      divs[i].style.display = "none";
}

function collapseElement (id)
{
  var el = document.getElementById (id);
  if (el.style.display == "")
    el.style.display = "none";
  else
    el.style.display = "";
}

//special control methods
function showNewCatDiv (catsel)
{
  var ncd = document.getElementById ("newcat");
  if (catsel.value == "new")
    ncd.style.display = "";
  else
    ncd.style.display = "none";
}
