<!--

// We use this to avoid the GC from collecting objects from loadXml before
// they have actually loaded their documents and processed them:

// TODO: Use the DOM. Don't print dirty HTML.

var requests = new Array();

function loadXml (href, process)
{
  var xmlDoc;
  var len = requests.length;

  if (document.implementation && document.implementation.createDocument)
    {
      xmlDoc = document.implementation.createDocument("", "", null);
      xmlDoc.onload = function () { removeRequest(xmlDoc); process(xmlDoc) };
    }
  else if (window.ActiveXObject)
    {
      xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.onreadystatechange = function () { if (xmlDoc.readyState == 4) { removeRequest(xmlDoc); process(xmlDoc); } };
    }
  else
    {
      alert('Your browser can\'t handle this script :( Can you try Firefox or IE?');
      return;
    }

  requests.push(xmlDoc);
  xmlDoc.load(href);
}

function removeRequest (element)
{
  var rd, wr;
  for (rd = 0, wr = 0; rd < requests.length; rd ++)
    if (requests[rd] != element)
      requests[wr ++] = requests[rd];
  while (wr < requests.len)
    requests.pop();
}


  function writeHTML(idl, shtml)
  {
    if(document.layers)
    {
      eval('document.layers.'+idl+'.document.write(shtml)')
      eval('document.layers.'+idl+'.document.close()')
    }
    else
    {
      if(document.all||document.getElementById)
        document.getElementById(idl).innerHTML=shtml
    }
  }

  function enableHTML(idl, value)
  {
    document.getElementById(idl).disabled = !value
    document.getElementById(idl).enabled = value
  }

// http://talideon.com/weblog/2005/02/detecting-broken-images-js.cfm
function IsImageOk(img)
{
    if (!img.complete)
        return false

    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0)
        return false;

    return true
}


// use tags_arr = Array('*') to enable all tags

function GalleryObject(var_name, div_name, xml_config, tags_arr)
{
  var images = new Array()
  var alt = new Array()
  var href = new Array();

  // XML configuration
  var prefix = ''

  var actual = 0
  var timer = null
  var max_delay = 5000
  var delay = 2000

  var has_image = false

  for (var i = 0; i < tags_arr.length; i++)
    tags_arr[i] = tags_arr[i].toLowerCase()

  function preload_images (xmlDoc)
  {
    var dirs = xmlDoc.getElementsByTagName('dir')

    var xmlConf = xmlDoc.getElementsByTagName('conf')[0]
    prefix = xmlConf.getAttribute('prefix')

    if (!dirs.length || !tags_arr.length)
      return

    var taginfo = new Array()
    var the_taginfo = xmlDoc.getElementsByTagName('taginfo')[0].getElementsByTagName('tag');

    for (var i = 0; i < the_taginfo.length; i++)
    {
      var tag = the_taginfo[i]
      var name = tag.getAttribute('name').toLowerCase()

      taginfo[name] = new Object()
      taginfo[name].href = tag.getAttribute('href')
    }

    for (var i = 0; i < dirs.length; i++)
    {
      var imgs = dirs[i].getElementsByTagName('img')

      for (var j = 0; j < imgs.length; j++)
      {
        var tags = imgs[j].getElementsByTagName('tag')

        // Check if the image has any tag we requested to show
        found = false

        for (var t = 0; !found && t < tags.length; t++)
          for (var tt = 0; !found && tt < tags_arr.length; tt++)
            if (tags[t].childNodes[0].nodeValue.toLowerCase() == tags_arr[tt] || tags_arr[tt] == '*')
              found = true

        if (!found)
          continue

        ahref = ''

        for (var t = 0; t < tags.length; t++)
        {
          var key = tags[t].childNodes[0].nodeValue.toLowerCase()
          if (taginfo[key] && (ahref = taginfo[key].href) != '')
            break
        }
         
        im      = new Image()
        alt_txt = imgs[j].getAttribute('alt')

        im.src  = prefix + '/' + dirs[i].getAttribute('name') + '/' + imgs[j].getAttribute('file')

        im.onload = new function()
        {
          images.push(im)
          alt.push(alt_txt)
          href.push(ahref)

          if (!has_image)
          {
            has_image = true
            show_controls()
            enable_disable_controls()
            aux_show_img(0)
          }
        }

      }
    }

    show_controls()
    enable_disable_controls()
  }  

  loadXml (xml_config, preload_images)


  function aux_show_img(i)
  {
    h = '<img src="' + images[i].src + '" />'
   
    if (href[i] != '')
    {
      h = '<a href="' + href[i] +'">' + h + '</a>'
    }

    writeHTML(div_name + '-frame', h)
    writeHTML(div_name + '-alt', alt[i])
  }

  function next ()
  {
    if (!images.length)
      return

    actual = (actual + 1) % images.length

    if (IsImageOk(images[actual]))
      aux_show_img(actual) 
  }

  function prev ()
  {
    if (!images.length)
      return

    actual = (actual + images.length - 1) % images.length

    if (IsImageOk(images[actual]))
      aux_show_img(actual) 
  }

  this.update = function()
  {
    next()
    timer = setTimeout(var_name + ".update()", delay)
  }

  function enable_disable_controls()
  {
    enableHTML(div_name + '-start', timer ? false : true)
    enableHTML(div_name + '-stop', timer ? true : false)
    enableHTML(div_name + '-next', timer ? false : true)
    enableHTML(div_name + '-prev', timer ? false : true)
    enableHTML(div_name + '-faster', timer ? true : false)
    enableHTML(div_name + '-slower', timer ? true : false)
  }


  this.stop = function ()
  {
    if (timer)
      clearTimeout(timer)
    timer = null

    enable_disable_controls()
  }

  this.start = function ()
  {
    if (timer == null)
      this.update()

     enable_disable_controls()
  }

  this.step_next = function ()
  {
    if (timer)
      stop()
    else
      next()
  }

  this.step_prev = function ()
  {
    if (timer)
      stop()
    else
      prev()
  }

  /* linear increments are anoying */ 

  this.faster = function ()
  {
    if (delay > 200)
      delay -= 100
  }

  this.slower = function()
  {
    if (delay < max_delay)
      delay += 100
  }

  function show_controls()
  {

   var h = '<form>[ ' +
           '<input id="' + div_name + '-start" value=" Start " onclick="' + var_name +  '.start()" type="button">' +
           '<input id="' + div_name + '-stop" value=" Stop " onclick="' + var_name + '.stop()" type="button"> | ' +
           '<input id="' + div_name + '-prev" value=" Prev " onclick="' + var_name + '.step_prev()" type="button">  ' +
           '<input id="' + div_name + '-next" value=" Next " onclick="' + var_name + '.step_next()" type="button"> | ' +
           '<input id="' + div_name + '-slower" value=" Slower " onclick="' + var_name + '.slower()" type="button"> ' +
           '<input id="' + div_name + '-faster" value=" Faster " onclick="' + var_name + '.faster()" type="button"> ]</form>'

   writeHTML(div_name + '-controls', h)
  }


}

-->
