﻿/* Object Height Expansion and Reduction */
/* Copyright (c) 2010 FoXSE Ltd */

var ROLL_AMOUNT_PX_DEFAULT = 5;
var ROLL_TIMER_INT = 15;

/* incrementally change the height of an object */
function rollObject(obj, startHeight, finalHeight, finalFunc, direction) {
    rollObject(obj, startHeight, finalHeight, finalFunc, direction, null);
}

/* incrementally change the height of an object, and specify the roll amount */
function rollObject(obj, startHeight, finalHeight, finalFunc, direction, rollAmountPx) {

    /* check for id */
    if (null == obj || null == obj.id) {
        alert("Cannot resize an object with a null ID.");
    }

    /* object */
    var nro = new clsRollingObject();
    nro.currentHeight = startHeight;
    nro.finalObjHeight = finalHeight;
    nro.finFunc = finalFunc;
    nro.rollDir = direction;
    
    if (rollAmountPx) {
        nro.rollAmountPx = rollAmountPx;
    } else {
        nro.rollAmountPx = ROLL_AMOUNT_PX_DEFAULT;
    }
    
    /* two way relationship */
    nro.parentId = obj.id;
    obj.rolling_object = nro;
	
    /* set direction */
    if (nro.rollDir == "+") {
        nro.rollVal = nro.rollAmountPx;
    } else {
        nro.rollVal = -nro.rollAmountPx;
    }
    
    /* start */
    rollLoop(nro.parentId);
}

/* called at interval */
function rollLoop(ro_id) {
    try {
        var rollingObj = document.getElementById(ro_id);
        var nro = rollingObj.rolling_object;
        
        rollingObj.style.height = nro.currentHeight + "px";
    	
        /* check if roll complete */
        var rollComplete;
    	
        if (nro.rollDir == "+") {
            rollComplete = (nro.currentHeight >= nro.finalObjHeight);
        } else {
            rollComplete = (nro.currentHeight <= nro.finalObjHeight);
        }
    	
        /* end or iterate */
        if (rollComplete) {
            if (null != nro.finFunc) {
                setTimeout(nro.finFunc, 1);
            }
            
        } else {
            var nextPanelHeight = nro.currentHeight + nro.rollVal;
            
            /* do not exceed min or max */
            if (nextPanelHeight < 0) {
                nextPanelHeight = 0;
            }

            if (nro.rollDir == "+") {
                /* don't grow too much */
                if (nextPanelHeight > nro.finalObjHeight) {
                    nextPanelHeight = nro.finalObjHeight;
                }
            } else {
                /* don't shrink too much */
                if (nextPanelHeight < nro.finalObjHeight) {
                    nextPanelHeight = nro.finalObjHeight;
                }
            }
            
            nro.currentHeight = nextPanelHeight;
            
            /* set-up for callback */
            rollingObj.rolling_object = nro;
            setTimeout("rollLoop('" + ro_id + "')", ROLL_TIMER_INT);
        }
        
    } catch (ex) {
        wrapEx("error in rollLoop, ro_id:" + ro_id, ex);
    }
}

function clsRollingObject() {
    /* properties */
    this.currentHeight = null;
    this.finalObjHeight = null;
    this.finFunc = null;
    this.rollVal = null;
    this.rollDir = null;
    this.parentId = null;
    this.rollAmountPx = null;
}
