<!-- hide script
/*
*   General purpose function to strip the size unit from
*   style position/size value if present and convert
*   to a number value.
*/
function convertSize(size) {
    var px = size.indexOf("p");
    var pct = size.indexOf("%");
    var em = size.indexOf("em");
    var pos = 0;
    
    if (px > 0) {
        pos = px;
    } else if (pct > 0) {
        pos = pct;
    } else if (em > 0) {
        pos = em;
    } else {
        return Number(size);
    }

    return Number(size.substring(0, pos));
}

/*
*   search for possible units.
*/
function findUnit(size) {
    if (size.indexOf("p") > 0) {
        return "px";
    } else if (size.indexOf("em") > 0) {
        return "em";
    } else if (size.indexOf("%") > 0) {
        return "%";
    }
    
    return "";
}

/*
*   Storage for position
*/
function Point(x,y) {
    this.x = x;
    this.y = y;
}

/*
*   object:     Animator
*
*   Enables animation of HTML elements (mostly DIVs) using the setTimeout method of the
*   Window object associated with this Animator. The create Animator object is added as
*   reference named 'animator'. This is needed for the setTimeout method.
*
*   The object using the animator object can subscribe itself as callback. For this to
*   work it should implement the following function:  function animationCompleted(element, effect)
*/
function Animator(win) {
    // store references
    this.window = win;
    this.window.animator = this;
    
    // effects available
    this.EFFECT_Shade           = 1;
    this.EFFECT_Unshade         = 2;
    this.EFFECT_Shrink          = 3;
    this.EFFECT_Grow            = 4;
    this.EFFECT_Move            = 5;
    
    // direction of animation
    this.DIR_Left               = 0x10;
    this.DIR_Top                = 0x20;
    this.DIR_Right              = 0x40;
    this.DIR_Bottom             = 0x80;
    
    // current animated object
    this.element = null;
    this.effect  = 0;
    this.destpos = null;

    // loop variables
    this.startTop        = 0;
    this.stopTop         = 0;
    this.incrementTop    = 0;
    this.startLeft       = 0;
    this.stopLeft        = 0;
    this.incrementLeft   = 0;
    this.startWidth      = 0;
    this.stopWidth       = 0;
    this.incrementWidth  = 0;
    this.startHeight     = 0;
    this.stopHeight      = 0;
    this.incrementHeight = 0;
    
    // animation properties
    this.animateDelay    = 10;      // time between animation frames
    this.animateCycles   = 60;      // number of frames for animation

    this.callbackObject  = null;

    // internal animation functions
    this.handleSizing = function(value, start, stop) {
        if ( start == stop ) {
            return 0;
        }
        
        if (stop < start) {
            if ( value > stop ) {
                return 1;
            } else {
                this.callbackComplete();
                return 2;
            }
        } else if ( stop > start ) {
            if ( value < stop ) {
                return 1;
            } else {
                this.callbackComplete();
                return 2;
            }
        }
    }
    
    this.animateCB = function() {
        if ( this.element == null ) {
            return;
        }
        
        // new dimensions
        var top    = convertSize( this.element.style.top    ) + this.incrementTop;
        var left   = convertSize( this.element.style.left   ) + this.incrementLeft;
        var width  = convertSize( this.element.style.width  ) + this.incrementWidth;
        var height = convertSize( this.element.style.height ) + this.incrementHeight;

        // handle new dimensions
        if ( this.handleSizing( top,    this.startTop,    this.stopTop ) == 2 ) { return; }
        if ( this.handleSizing( left,   this.startLeft,   this.stopLeft ) == 2 ) { return; }
        if ( this.handleSizing( width,  this.startWidth,  this.stopWidth ) == 2 ) { return; }
        if ( this.handleSizing( height, this.startHeight, this.stopHeight ) == 2 ) { return; }
        this.window.setTimeout("animator.animateCB()", this.animateDelay);
        
        this.element.style.top    = top    + this.element.topUnit;
        this.element.style.left   = left   + this.element.leftUnit;
        this.element.style.width  = width  + this.element.widthUnit;
        this.element.style.height = height + this.element.heightUnit;
    }
    
    this.shade = function() {
        // determine loop values
        if ((this.effect & this.DIR_Left) > 0) {
            this.stopWidth = 1;
            this.incrementWidth = -Number(this.element.originalWidth / this.animateCycles);
        } else if((this.effect & this.DIR_Right) > 0) {
            this.stopWidth = 1;
            this.incrementWidth = -Number(this.element.originalWidth / this.animateCycles);
            
            this.stopLeft  = this.element.originalLeft+this.element.originalWidth;
            this.incrementLeft = -this.incrementWidth;
        }
        
        if ((this.effect & this.DIR_Top) > 0) {
            this.stopHeight = 1;
            this.incrementHeight = -Number(this.element.originalHeight / this.animateCycles);
        } else if((this.effect & this.DIR_Bottom) > 0) {
            this.stopHeight = 1;
            this.incrementHeight = -Number(this.element.originalHeight / this.animateCycles);

            this.stopTop  = this.element.originalTop+this.element.originalHeight;
            this.incrementTop = -this.incrementHeight;
        }
        
        this.animateCB();
    }

    this.unshade = function() {
        // determine loop values
        if ((this.effect & this.DIR_Left) > 0) {
            this.startWidth = 0;
            this.incrementWidth = Number(this.element.originalWidth / this.animateCycles);
        }
        
        if ((this.effect & this.DIR_Top) > 0) {
            this.startHeight = 0;
            this.incrementHeight = Number(this.element.originalHeight / this.animateCycles);
        }
        
        this.animateCB();
    }
    
    this.shrink = function() {
        // determine loop values
        if ((this.effect & this.DIR_Left) > 0) {
            this.stopWidth = 1;
            this.incrementWidth = -Number(this.element.originalWidth / this.animateCycles);
            
            this.stopLeft  = this.startLeft + Number(this.element.originalWidth / 2);
            this.incrementLeft  = -this.incrementWidth;
            this.incrementWidth = this.incrementWidth * 2;
        }
        
        if ((this.effect & this.DIR_Top) > 0) {
            this.stopHeight = 1;
            this.incrementHeight = -Number(this.element.originalHeight / this.animateCycles);

            
            this.stopTop = this.startTop + Number(this.element.originalHeight / 2);
            this.incrementTop    = -this.incrementHeight;
            this.incrementHeight = this.incrementHeight * 2;
        }
        
        this.animateCB();
    }
    
    this.grow = function() {
        // determine loop values
        if ((this.effect & this.DIR_Left) > 0) {
            this.startWidth = 0;
            this.incrementWidth = Number(this.element.originalWidth / this.animateCycles);
            
            this.startLeft  = this.stopLeft + Number(this.element.originalWidth / 2);
            this.incrementLeft  = -this.incrementWidth;
            this.incrementWidth = this.incrementWidth * 2;
        }
        
        if ((this.effect & this.DIR_Top) > 0) {
            this.startHeight = 0;
            this.incrementHeight = Number(this.element.originalHeight / this.animateCycles);

            
            this.startTop = this.stopTop + Number(this.element.originalHeight / 2);
            this.incrementTop    = -this.incrementHeight;
            this.incrementHeight = this.incrementHeight * 2;
        }
        
        this.animateCB();
    }
    
    this.move = function() {
        // determine loop values
        this.startTop      = convertSize( this.element.style.top );
        this.stopTop       = this.destpos.y;
        this.incrementTop  = Number( (this.stopTop - this.startTop) / this.animateCycles );
        
        this.startLeft     = convertSize( this.element.style.left );
        this.stopLeft      = this.destpos.x;
        this.incrementLeft = Number( (this.stopLeft - this.startLeft) / this.animateCycles );
        
        this.startWidth      = this.element.originalWidth;
        this.stopWidth       = this.startWidth;
        this.incrementWidth  = 0;
        
        this.startHeight     = this.element.originalHeight;
        this.stopHeight      = this.startHeight;
        this.incrementHeight = 0;
        
        this.animateCB();
    }

    this.initLoopValues = function() {
        // initialize loop values
        this.startTop        = this.element.originalTop;
        this.stopTop         = this.startTop;
        this.incrementTop    = 0;
        
        this.startLeft       = this.element.originalLeft;
        this.stopLeft        = this.startLeft;
        this.incrementLeft   = 0;
        
        this.startWidth      = this.element.originalWidth;
        this.stopWidth       = this.startWidth;
        this.incrementWidth  = 0;
        
        this.startHeight     = this.element.originalHeight;
        this.stopHeight      = this.startHeight;
        this.incrementHeight = 0;
    }
    
    // the anitmation function
    this.animate = function() {
        this.element = arguments[0];
        this.effect  = arguments[1];
        
        // execute proper effect function
        if ((this.effect & 0xf) == this.EFFECT_Shade) {
            this.initLoopValues();
            this.shade();
        } else if ((this.effect & 0xf) == this.EFFECT_Unshade) {
            this.initLoopValues();
            this.unshade();
        } else if ((this.effect & 0xf) == this.EFFECT_Shrink) {
            this.initLoopValues();
            this.shrink();
        } else if ((this.effect & 0xf) == this.EFFECT_Grow) {
            this.initLoopValues();
            this.grow();
        } else if ((this.effect & 0xf) == this.EFFECT_Move) {
            this.destpos = arguments[2];
            this.move();
        }
    }
    
    // helper functions
    this.prepareAnimateElement = function(el) {
        // set orginal values
        if (el != null) {
            el.originalTop    = convertSize(el.style.top);
            el.originalLeft   = convertSize(el.style.left);
            el.originalWidth  = convertSize(el.style.width);
            el.originalHeight = convertSize(el.style.height);
            el.topUnit      = findUnit(el.style.top);
            el.leftUnit     = findUnit(el.style.left);
            el.widthUnit    = findUnit(el.style.width);
            el.heightUnit   = findUnit(el.style.height);
        }
    }
    
    this.setDelay = function(delay) {
        this.animateDelay = delay;
    }
    
    this.setCycles = function(cycles) {
        this.animateCycles = cycles;
    }
    
    this.setCompleteCallback = function(callObj) {
        this.callbackObject = callObj;
    }
    
    this.callbackComplete = function() {
        this.element.style.top    = this.stopTop+this.element.topUnit;
        this.element.style.left   = this.stopLeft+this.element.leftUnit;
        this.element.style.width  = this.stopWidth+this.element.widthUnit;
        this.element.style.height = this.stopHeight+this.element.heightUnit;
        
        if (this.callbackObject != null) {
            var el = this.element;
            var ef = this.effect;
            var cb = this.callbackObject;

            this.element = null;
            this.effect = 0;
            this.callbackObject = null;
            cb.animationCompleted(el, ef);
        } else {
            this.element = null;
            this.effect  = 0;
        }
    }
}

// end of script -->

