
/* Dom Manipulation Functions
 * 2007-10 Tim Igoe (tim@jellymedia.com)
 *
 *--------------------------------------------------------------------------*/

var thedom = new Object();

thedom.find = function(what, start)
{
  if (!start)
    start = document;

  var idSelector = /^#([A-Za-z0-9-]+)$/;
  var match = idSelector.exec(what);

  if (match && match[1])
    return start.getElementById(match[1]);

  var tagSelector = /^([A-Za-z]+)/;
  match = tagSelector.exec(what);

  if (match && match[1])
    return start.getElementsByTagName(match[1]);

  var classSelector = /^.([A-Za-z0-9-]+)/;
  match = classSelector.exec(what);
  if (match && match[1])
  {
    var elements = start.getElementsByTagName('*');
    var elementsWith = [];
    var className;
    for (i = 0; i < elements.length; i++)
    {
      className = elements[i].getAttribute('class') || elements[i].getAttribute('className');

      if (className && className.search(match[1]) != -1)
        elementsWith.push(elements[i]);
    }
    return elementsWith;
  }

  return null;
}



var effects = new Object();

effects.fps = 25;

effects.slide = function(element, newHeight, newWidth, time, callback)
{
  var el = thedom.find('#' + element);

  if(el == null)
    return;

  el.style.display = 'block';

  if (newHeight == -2)
  {
    var oldHeight = el.offsetHeight;
    el.style.height = null;
    newHeight = el.offsetHeight;
    if (newHeight != oldHeight)
      el.style.height = 0;
  }

  if (newWidth == -2)
  {
    el.style.width = '100%';
    newWidth = el.offsetWidth;
    el.style.width = 0;
  }

  var curWidth = el.offsetWidth;
  var curHeight = el.offsetHeight;

  var totalFrames = 1;
  if(time > 0)
    totalFrames = time / effects.fps;

  var frameWidth = newWidth - curWidth;
  if(frameWidth != 0)
    frameWidth /= totalFrames;

  var frameHeight = newHeight - curHeight;
  if(frameHeight != 0)
    frameHeight /= totalFrames;
  
  effects.move(element, curHeight, newHeight, frameHeight, curWidth, newWidth, frameWidth, callback);
}

effects.move = function(element, curHeight, newHeight, frameHeight, curWidth, newWidth, frameWidth, callback)
{
  var el = thedom.find('#' + element);

  curWidth = effects.calcUnit(curWidth, newWidth, frameWidth);
  curHeight = effects.calcUnit(curHeight, newHeight, frameHeight);

  if (newWidth != -1)
    el.style.width = Math.round(curWidth) + 'px';
  
  if (newHeight != -1)
    el.style.height = Math.round(curHeight) + 'px';

  if(curHeight == newHeight && curWidth == newWidth)
  {
    if(callback != null)
      callback();
    return;
  }

  setTimeout( 'effects.move("' + element + '",' + curHeight + ',' + newHeight + ',' + frameHeight + ',' + curWidth + ',' + newWidth + ',' + frameWidth + ',' + callback + ')', effects.fps);
}

effects.convertColour = function(col)
{
  var rgb = [parseInt(col.substring(0,2),16), parseInt(col.substring(2,4),16), parseInt(col.substring(4,6),16)];
  return rgb;
}

effects.fade = function(element, what, start, end, time)
{
  var el = thedom.find('#' + element);

  if (el == null)
    return;


  var totalFrames = 1;
  if(time > 0)
    totalFrames = time / effects.fps;

  var startColour = this.convertColour(start);
  var endColour = this.convertColour(end);

  var r, g, b;
  r = parseInt((startColour[0] - endColour[0]) / totalFrames);
  g = parseInt((startColour[1] - endColour[1]) / totalFrames);
  b = parseInt((startColour[2] - endColour[2]) / totalFrames);

  setTimeout( 'effects.animateColour("' + element + '", "' + what + '", ' + startColour[0] + ', ' + startColour[1] + ', ' + startColour[2] + ', ' + end + ', ' + r + ', ' + g + ', ' + b + ')', effects.fps);
}

effects.animateColour = function(element, what, startR, startG, startB, end, r, g, b)
{
  var el = thedom.find('#' + element);

  if (el == null)
    return;

  var currentR, currentG, currentB;

  currentR = parseInt(startR - r);
  currentG = parseInt(startG - g);
  currentB = parseInt(startB - b);

  var endColour = this.convertColour(end);

  var newColour = 'rgb(' + currentR + ',' + currentG + ',' + currentB + ')';

  if (what == 'background')
    el.style.backgroundColor = newColour;
  else if (what == 'border')
    el.style.borderColor = newColour;
  else
    el.style.color = newColour;

  if (currentR == endColour[0] && currentG == endColour[1] && currentB == endColour[2])
    return null;

  setTimeout( 'effects.animateColour("' + element + '", "' + what + '",' + currentR + ',' + currentG + ',' + currentB + ',' + end + ', ' + r + ', ' + g + ', ' + b + ')', effects.fps);
}

effects.calcUnit = function(cur, tar, rate)
{
  if (rate == 0 || cur == tar)
    return tar;

  cur += rate;

  if ((rate > 0 && cur >= tar) || (rate < 0 && cur <= tar))
    return tar;

  return cur;
}

// Inteface Object
var gui = new Object();

gui.modaliframe = function(id, url, onclose)
{
  if (this.modalID)
    this.hideDialog();
  gui.modalID = id;
  gui.close = onclose;

  // Create the dialog window
  var dialog = document.createElement("div");
  dialog.className = 'dialog';

  // Create close button
  var closeButton = document.createElement("div");
  closeButton.dialog = dialog;
  closeButton.onclick = gui.hideDialog;
  closeButton.className = 'close';
  dialog.appendChild(closeButton);

  var iframe = document.createElement("iframe");
  iframe.frameBorder = 0;
  iframe.src = url;
  dialog.appendChild(iframe);

  // Create modal layer
  dialog.modal = document.createElement("div");
  dialog.modal.className = 'modal';
  dialog.modal.appendChild(dialog);
  dialog.modal.id = id;

  document.body.appendChild(dialog.modal);

  return false;
}

gui.hideDialog = function ()
{
  if (gui.modalID)
  {
    var el = document.getElementById(gui.modalID)
    document.body.removeChild(el);
    gui.modalID = null;

    if (gui.close)
    {
      eval(gui.close);
      gui.close = null;
    }
  }
}
