/***********************************************************************
 *                                                                     *
 * Copyright (C) 2003-2009 iWebGate Technology Ltd & Charlie Gargett   *
 * Author: Charlie Gargett                                             *
 *                                                                     *
 ***********************************************************************
 *                                                                     *
 * This software  product and related  source code is  licenced by the *
 * owner and copyright holder,  iWebGate Technology Ltd, Perth Western *
 * Australia 6000, AUSTRALIA.                                          *
 *                                                                     *
 * All intellectual property rights  in this software product and user *
 * documentation are owned by  iWebGate Technology Ltd,  PERTH Western *
 * Australia  6000,   AUSTRALIA,   and  are  protected  by  applicable *
 * Australian Copyright laws and international treaty provisions.  The *
 * owner retains all rights not expressly granted.                     *
 *                                                                     *
 * All javascript source code exposed by this and related websites via *
 * direct  HTML  embedding,  in-direct  linking  through  reference to *
 * external  source  files  or  by  any  other  means  of  exposure of *
 * javascript  routines made by this software product may  be used for *
 * the  explicit purpose of research into  the development  of similar *
 * routines but may not be copied, modified, disassembled, decompiled, *
 * reverse  engineered, transfered as  is or as a copy,  modification, *
 * merged portion,  in whole or in part, except as expressly  provided *
 * for by the owner or copyright holder in writing.                    *
 *                                                                     *
 ***********************************************************************/

function findPagePosition(elem) {
    var p = elem
    var x = 0
    var y = 0

    while (p) {
        x += p.offsetLeft
        y += p.offsetTop
        p = p.offsetParent
    }
    return {'x':x,'y':y}
}

function Anime() {
    this.slide = slide;
    this.rollout = rollout;
    this.rollin = rollin;
    this.fadein = fadein;
    this.fadeout = fadeout;

    var working = false
    
    function slide(obj, options) {
        if(!options['speed']) options['speed'] = 10;
        if(!options['delay']) options['delay'] = 10;

        switch(options['side']) {
            case 'left':
                var posX = parseInt(obj.style.left)
                obj.style.left = posX+"px"
                if(parseInt(options['speed'])>=0) {
                    if(posX >= (parseInt(options['toX'])-parseInt(options['speed']))) {
                        obj.style.left = parseInt(options['toX'])+"px"
                        var hit = true
                    } else obj.style.left = (parseInt(obj.style.left)+parseInt(options['speed']))+"px"
                } else {
                    if(posX <= (parseInt(options['toX'])+parseInt(options['speed']))) {
                        obj.style.left = parseInt(options['toX'])+"px"
                        var hit = true
                    } else obj.style.left = (parseInt(obj.style.left)+parseInt(options['speed']))+"px"
                }
                break;
            case 'right':
                var posX = parseInt(obj.style.right)
                obj.style.right = posX+"px"
                if(parseInt(options['speed'])>=0) {
                    if(posX >= (parseInt(options['toX'])-parseInt(options['speed']))) {
                        obj.style.right = parseInt(options['toX'])+"px"
                        var hit = true
                    } else obj.style.right = (parseInt(obj.style.right)+parseInt(options['speed']))+"px"
                } else {
                    if(posX <= (parseInt(options['toX'])-parseInt(options['speed']))) {
                        obj.style.right = parseInt(options['toX'])+"px"
                        var hit = true
                    } else obj.style.right = (parseInt(obj.style.right)+parseInt(options['speed']))+"px"
                }
                break;
            case 'top':
                var posX = parseInt(obj.style.top)
                obj.style.top = posX+"px"
                if(parseInt(options['speed'])>=0) {
                    if(posX >= (parseInt(options['toX'])-parseInt(options['speed']))) {
                        obj.style.top = parseInt(options['toX'])+"px"
                        var hit = true
                    } else obj.style.top = (parseInt(obj.style.top)+parseInt(options['speed']))+"px"
                } else {
                    if(posX <= (parseInt(options['toX'])+parseInt(options['speed']))) {
                        obj.style.top = parseInt(options['toX'])+"px"
                        var hit = true
                    } else obj.style.top = (parseInt(obj.style.top)+parseInt(options['speed']))+"px"
                }
                break;
            case 'bottom':
                var posX = parseInt(obj.style.bottom)
                obj.style.bottom = posX+"px"
                if(parseInt(options['speed'])>=0) {
                    if(posX >= (parseInt(options['toX'])-parseInt(options['speed']))) {
                        obj.style.bottom = parseInt(options['toX'])+"px"
                        var hit = true
                    } else obj.style.bottom = (parseInt(obj.style.bottom)+parseInt(options['speed']))+"px"
                } else {
                    if(posX <= (parseInt(options['toX'])-parseInt(options['speed']))) {
                        obj.style.bottom = parseInt(options['toX'])+"px"
                        var hit = true
                    } else obj.style.bottom = (parseInt(obj.style.bottom)+parseInt(options['speed']))+"px"
                }
                break;
        }

        if(!hit) setTimeout(function() {slide(obj,options)}, options['delay'])
        else {
            if(typeof options['oncomplete']) {
                eval(options['oncomplete'])
            }
            if(options['close']) {
                if(navigator.appVersion.indexOf('MSIE') == -1) obj.parentNode.removeChild(obj)
                else obj.removeNode(true)
            }
        }
    }

    function rollout(obj, options) {
        if(!options['speed']) options['speed'] = 10;
        if(!options['delay']) options['delay'] = 10;
        if(!options['screenx']) options['screenx'] = 0;
        if(!options['screeny']) options['screeny'] = 0;
        if(!options['origwidth']) options['origwidth'] = parseInt(obj.style.width);
        if(!options['origheight']) options['origheight'] = parseInt(obj.style.height);
        if(!options['direction']) options['direction'] = 'down';

        if(!options['callback'] && obj.working) return
        obj.working = true

        obj.style.position = 'absolute'
        switch(options['direction']) {
            case 'left':
            case 'down':
                obj.style.top = options['screeny']
                if(options['screenx']>=0) obj.style.left = options['screenx']
                else obj.style.right = Math.abs(options['screenx'])
                obj.style.bottom = null
                if(options['screenx']>=0) obj.style.right = null
                else obj.style.left = null
                break;

            case 'up':
                obj.style.top = null
                obj.style.left = options['screenx']
                obj.style.bottom = options['screeny']
                obj.style.right = null
                break;

            case 'right':
                obj.style.top = options['screeny']
                obj.style.left = null
                obj.style.bottom = null
                obj.style.right = options['screenx']
                break;
        }

        if(!options['callback']) {
            options['callback'] = true

            obj.style.display = 'none'
            obj.style.overflow = 'hidden'
            if(options['direction'] == 'down' || options['direction'] == 'up') obj.style.height = '0px'
            else obj.style.width = '0px'
        } else {
            obj.style.display = ''
            if(options['direction'] == 'down' || options['direction'] == 'up') {
                if(parseInt(obj.style.height) >= options['origheight']) {
                    obj.style.height = options['origheight']+"px"
                    obj.working = null
                    return
                } else obj.style.height = (parseInt(obj.style.height)+options['speed'])+"px"
            } else {
                if(parseInt(obj.style.width) >= options['origwidth']) {
                    obj.style.width = options['origwidth']+"px"
                    obj.working = null
                    return
                } else obj.style.width = (parseInt(obj.style.width)+options['speed'])+"px"
            }
        }
        setTimeout(function() {rollout(obj,options)}, options['delay'])
    }

    function rollin(obj, options) {
        if(!options['speed']) options['speed'] = 10;
        if(!options['delay']) options['delay'] = 10;
        if(!options['direction']) options['direction'] = 'up';

        obj.style.position = 'absolute'
        if(!options['origwidth']) options['origwidth'] = obj.style.width
        if(!options['origheight']) options['origheight'] = obj.style.height

        if(!options['callback'] && obj.working) return 
        options['callback'] = true
        
        if(options['direction'] == 'down' || options['direction'] == 'up') {
            if(parseInt(obj.style.height) <= options['speed']) {
                obj.style.display = 'none'
                obj.style.width = options['origwidth']
                obj.style.height = options['origheight']
                if(options['remove']) obj.parentNode.removeChild(obj)
                obj.working = null
                return
            } else obj.style.height = (parseInt(obj.style.height)-options['speed'])+"px"
        } else {
            if(parseInt(obj.style.width) <= options['speed']) {
                obj.style.display = 'none'
                obj.style.width = options['origwidth']
                obj.style.height = options['origheight']
                if(options['remove']) obj.parentNode.removeChild(obj)
                obj.working = null
                return
            } else obj.style.width = (parseInt(obj.style.width)-options['speed'])+"px"
        }
        setTimeout(function() {rollin(obj,options)}, options['delay'])
    }
    
    function fadein(obj, options) {
        if(!options['speed']) options['speed'] = 10
        if(!options['delay']) options['delay'] = 10
        if(!options['callback'] && obj.working) return

        if(!options['callback']) {
            options['callback'] = true
            options['opacity'] = 0
        }

        if(options['opacity'] >= (100-options['speed'])) {
            obj.style.opacity = 1
            obj.style.filter = 'alpha(opacity=100)'
            obj.working = null
            return
        } else {
            options['opacity'] += options['speed']
            obj.style.opacity = (options['opacity']/100)
            obj.style.filter = 'alpha(opacity='+options['opacity']+')'
            options['callback'] = true
            obj.working = true
        }
        setTimeout(function() {fadein(obj, options)}, options['delay'])
    }

    function fadeout(obj, options) {
        if(!options['speed']) options['speed'] = 10
        if(!options['delay']) options['delay'] = 10
        if(!options['callback'] && obj.working) return

        if(!options['callback']) {
            options['callback'] = true
            options['opacity'] = 100
        }

        if(options['opacity'] <= options['speed']) {
            obj.style.opacity = 0
            obj.style.filter = 'alpha(opacity=0)'
            obj.working = null
            return
        } else {
            options['opacity'] -= options['speed']
            obj.style.opacity = (options['opacity']/100)
            obj.style.filter = "alpha(opacity="+options['opacity']+")"
            options['callback'] = true
            obj.working = true
        }
        setTimeout(function() {fadeout(obj, options)}, options['delay'])
    }
}
