/*
 * Functions for Maria Vogel's web site
 *
 * Copyright 2006 by Ben Siemerink.
 */

function humansonly(id, text, link)
{
  element=document.getElementById(id);
  if (element==undefined) return;

  if (text!=null)
  {
    t='';
    for (i in text) t+=String.fromCharCode(193-text[i]);
    element.innerHTML=t;
  }

  if (link!=null && element.href)
  {
    l='';
    for (i in link) l+=String.fromCharCode(193-link[i]);
    element.href=l;
  }
}


function focusfield(id)
{
  document.getElementById(id).focus();
}



function maxlen(textarea, maxlength)
{
  text_id=document.getElementById(textarea);
  if (text_id==undefined) return;

  curlength=document.getElementById(textarea).value.length;
  if (curlength==undefined) return;

  hint_id=document.getElementById(textarea+'_hint');
  if (hint_id==undefined) return;

  if (curlength<=maxlength)
  {
    hint_id.innerHTML='Maximum '+maxlength+' characters; '+(maxlength-curlength)+' left';
    hint_id.className='hint_ok';
  }
  else
  {
    hint_id.innerHTML='Maximum '+maxlength+' characters; '+(curlength-maxlength)+' will be truncated';
    hint_id.className='hint_truncate';
  }
}



/*
 * Intelligent prefetch object for images
 * Loads the images one by one.
 */
function debug() { return; }

function Prefetch(imagelist) {
  this._fetched=function() { // {{{2
    debug('_fetched_ok');
    this.fetched=true;
    this.parent._fetch();
  }

  this._fetch=function() // {{{2
  {
    // Avoid stack overflows in Internet Explorer
    setTimeout("__Prefetch_this_object.__fetch();", 0);
  }

  this.__fetch=function() // {{{2
  {
    var i=0;
    while (i<this.imagelist.length && this.imagelist[i].fetched) i++;
    if (i<this.imagelist.length && !this.imagelist[i].fetched)
      this.imagelist[i].src=this.imagelist[i].imageurl;
    debug('done _fetch');
  }

  // Build list of Image objects
  this.imagelist=new Array();
  for (i in imagelist) {
    var img=new Image();
    img.index=Number(i)+1;
    img.parent=this;
    img.imageurl=imagelist[i];
    img.fetched=false;
    img.error=false;
    img.onload=this._fetched;
    img.onerror=this._fetched;
    this.imagelist.push(img);
  }

  __Prefetch_this_object=this;	// for timers; IE does not have the global variables in the window iterable

  // Start downloading the images, one by one
  this._fetch();
  debug('done init');
}

function toggle_workdetail()
{
  e=document.getElementById('workdetail');
  if ('fade_value' in e && e.fade_value>0) fade('workdetail', 0, 1000, -300, true);
  else fade('workdetail', 0, 1000, 300, true);
}

