  matchSpace = /\s+/g;
  defined = function( x ) {
    try {
      if (typeof x != "undefined")
        return true;
    } catch( e ) {}
    return false;
  }

  elementOrId = function( element ) {
    if ( !element )
      return null;
    if ( typeof element == "string" )
      element = document.getElementById( element );
    return element;
  }

  hasClassName = function( element, className )	{
    if ( !element || !element.className || !className )
      return false;
    var classNames = element.className.split( matchSpace );
    for ( var i in classNames ) {
      if ( classNames[ i ] == className )
        return true;
    }
    return false;
  }

  getElementsByTagAndClassName = function( tagName, className, root ) {
    root = elementOrId( root );
    if ( !root )
      root = document;
    var allElements = root.getElementsByTagName( tagName );
    var elements = [];
    for ( var i = 0; i < allElements.length; i++ ) {
      var element = allElements[ i ];
      if ( !element )
        continue;
      if ( hasClassName( element, className ) )
        elements[ elements.length ] = element;
    }
    return elements;
  }

  getElementsByClassName = function( className, root ) {
    return getElementsByTagAndClassName( "*", className, root );
  }

  clamp = function( n, min, max ) {
    if( n < min ) n = min;
    if( n > max ) n = max;
    return n;
  }

  setOpacity = function( element, opacity ) {
    element = elementOrId( element );
    if( !element )
      return;
    opacity = clamp( opacity, 0, 1.0 );
    element.style.opacity = opacity;
    element.style.MozOpacity = opacity;
    try { 
      element.style.filter = "alpha( opacity: " + opacity * 100 + ")"; 
    } catch( e ) {}
  }

  Slideshow = function( element ) {
    this.element = element;
    var self = this;
    this.runClosure = function() { return self.run( null ); };
  }

  Slideshow.prototype.init = function()	{
    this.slideshow = elementOrId( this.element );
    this.slides = getElementsByClassName( "slide", this.slideshow );
    for( var i = 0; i < this.slides.length; i++ )
      this.slides[ i ].style.visibility = "hidden";
    this.currentSlide = 0;
  }

  Slideshow.prototype.run = function( inElement ) {
    var outElement = (this.currentSlide == null) ? 
      null : this.slides[ this.currentSlide ];
    // hack to punt firefox 1.0 into rendering correctly
    this.slideshow.style.visibility = "hidden";
    this.slideshow.style.visibility = "visible";
    if ( inElement )
      inElement.style.visibility = "visible";
    if ( outElement )
      outElement.style.visibility = "hidden";
  }

  Slideshow.prototype.next = function() {
    var next = this.currentSlide + 1;
    if (next == this.slides.length) { next = 0; }
    var nextElement = (this.currentSlide == null) ? null : this.slides[ next ];
    this.run(nextElement);
    this.currentSlide = next;
    getByID("layout").value = this.currentSlide + 1;
  }

  Slideshow.prototype.prev = function()	{
    var prev = this.currentSlide - 1;
    if (prev == -1) { prev = this.slides.length - 1; }
    var prevElement = (this.currentSlide == null) ? null : this.slides[ prev ];
    this.run(prevElement);
    this.currentSlide = prev;
    getByID("layout").value = this.currentSlide + 1;
  }

  Slideshow.prototype.debug = function( s ) {
    var e = document.getElementById( "debug" );
    e.innerHTML = s;
  }

