/**
 * Morph-effects
 *
 * @changes 2005-11-07 / marc / Created
 *
 * @copyright Copyright © 2005 / CHILIBYTES Böhm & Welte GbR / www.chilibytes.com
 * @author marc
 * @version 0.0.1
 * @package Javascript
 * @subpackage Morph
 *
 */

/**
 * @var array Stores the this-object for many instances
 *
 * @access private
 */
var _globalThisA = new Array();

/**
 * Constructor
 */
function CB_Morph() {
    this.constructor = CB_Morph;
    // Global
    this._threadRunning = false;
    this._envObj = new CB_Environment();
    this._userAgent = this._envObj.CurrentClientBrowser();
    // Fading
    this._fadeSettings = new Array();
    this._fadeSettings["timeout"] = null;
    this._fadeSettings["objId"] = null;
    this._fadeSettings["startOp"] = null;
    this._fadeSettings["endOp"] = null;
    this._fadeSettings["stepOp"] = null;
    this._fadeSettings["timeStep"] = null;
    // Scaling
    this._scaleSettings = new Array();
    this._scaleSettings["objId"] = null;
    this._scaleSettings["newWidth"] = null;
    this._scaleSettings["newHeight"] = null;
    this._scaleSettings["timeStep"] = null;
    this._scaleSettings["timeout"] = null;
    this._scaleSettings["scaleStep"] = null;
}

/**
 * Fades a object from a start opacity-value to a end value with the specified timestep
 *
 * @param string objId The html object-id of the fade-object
 * @param int startOpacity Startvalue of the opacity in percent (between 0 and 100)
 * @param int endOpacity Endvalue of the opacity in percent (between 0 and 100)
 * @param int timeStep Time of the timeout
 */
CB_Morph.prototype.Fade = function( objId, startOpacity, endOpacity, timeStep ) {
   	_globalThisA[objId] = this;
    this._fadeSettings["objId"] = objId;
    this._fadeSettings["endOp"] = endOpacity;
    if ( this._fadeSettings["startOp"] == null || this._fadeSettings["startOp"] == this._fadeSettings["endOp"] ) {
        this._fadeSettings["startOp"] = startOpacity;
    }
    this._fadeSettings["timeStep"] = parseInt( timeStep );
    this._fadeSettings["stepOp"] = 5;
    this._threadRunning = true;
    this._fadeSettings["timeout"] = window.setTimeout( "_globalThisA['" + objId + "']._FadeThread()", 0 );
}


/**
 *
 */
CB_Morph.prototype._FadeThread = function() {
    var objId = this._fadeSettings["objId"];
    var obj = document.getElementById( objId );
    var step = this._fadeSettings["stepOp"];
    _globalThisA[objId] = this;
    if ( obj ) {
    	if ( obj.tagName == "IMG" && !obj.complete ) {
   			this._fadeSettings["timeout"] = window.setTimeout( "_globalThisA['" + objId + "']._FadeThread()", 5 );
    	}
    	else if ( this._fadeSettings["startOp"] < 0 || this._fadeSettings["startOp"] > 100 ||
               this._fadeSettings["endOp"] < 0 || this._fadeSettings["endOp"] > 100 )
          {
              alert( "Error: Opacity have to be between 0 and 100" );
          }
          else if ( this._fadeSettings["startOp"] != this._fadeSettings["endOp"] ) {
            if ( this._fadeSettings["startOp"] < this._fadeSettings["endOp"] ) {
                if ( ( this._fadeSettings["startOp"] + step ) > this._fadeSettings["endOp"] ) {
                    step = 1;
                }
                this._fadeSettings["startOp"] += step;
            }
            else {
                if ( ( this._fadeSettings["startOp"] - step ) < this._fadeSettings["endOp"] ) {
                    step = 1;
                }
                this._fadeSettings["startOp"] -= step;
            }
            this.OpacitySet( obj, this._fadeSettings["startOp"] );
            this._fadeSettings["timeout"] = window.setTimeout( "_globalThisA['" + objId + "']._FadeThread()", this._fadeSettings["timeStep"] );
        }
        else {
	        this._threadRunning = false;
        }
    }
    else {
        alert( "Error: Object '" + objId + "' not found!" );
    }
}

/**
 *
 */
CB_Morph.prototype.OpacitySet = function( obj, opacity ) {
    var opPerc = parseFloat( opacity / 100 );
    if ( this._userAgent & CB_ENVIRONMENT_BROWSER_IDENT_GECKO ||
		 this._userAgent & CB_ENVIRONMENT_BROWSER_IDENT_IE ||
		 this._userAgent & CB_ENVIRONMENT_BROWSER_IDENT_KHTML )
	{
	    obj.style.filter = "Alpha( opacity=" + opacity + " )";
	    obj.style.opacity = opPerc;
	    obj.style.MozOpacity = opPerc;
	    obj.style.KhtmlOpacity = opPerc;
	}
	else {
		if ( opacity > 50 ) obj.style.visibility = "visible";
		else obj.style.visibility = "hidden";
	}
}

/**
 *
 */
CB_Morph.prototype.IsRunning = function() {
	return this._threadRunning;
}

/**
 *
 */
CB_Morph.prototype.Scale = function( objId, newWidth, newHeight, timeStep ) {
    _globalThisA[objId] = this;
    this._scaleSettings["objId"] = objId;
    this._scaleSettings["newWidth"] = parseInt( newWidth );
    this._scaleSettings["newHeight"] = parseInt( newHeight );
    this._scaleSettings["timeStep"] = parseInt( timeStep );
    this._scaleSettings["scaleStep"] = 15;
    this._threadRunning = true;
    this._scaleSettings["timeout"] = window.setTimeout( "_globalThisA['" + objId + "']._ScaleThread()", 0 );
}

/**
 *
 */
CB_Morph.prototype._ScaleThread = function() {
    var objId = this._scaleSettings["objId"];
    var obj = document.getElementById( objId );
    var step = this._scaleSettings["scaleStep"];
    var objH = null;
    var objW = null;
    _globalThisA[objId] = this;
    if ( obj ) {
        objW = obj.offsetWidth;
        objH = obj.offsetHeight;
        if ( objW != this._scaleSettings["newWidth"] || objH != this._scaleSettings["newHeight"] ) {
	        // Width
        	if ( objW < this._scaleSettings["newWidth"] ) {
        		if ( ( objW + step ) > this._scaleSettings["newWidth"] ) step = 1;
        		objW += step;
        	}
        	else if ( objW > this._scaleSettings["newWidth"] ) {
				if ( ( objW - step ) < this._scaleSettings["newWidth"] ) step = 1;
        		objW -= step;
        	}
        	obj.style.width = objW + "px";
            // Height
        	if ( objH < this._scaleSettings["newHeight"] ) {
        		if ( ( objH + step ) > this._scaleSettings["newHeight"] ) step = 1;
        		objH += step;
        	}
        	else if ( objH > this._scaleSettings["newHeight"] ) {
				if ( ( objH - step ) < this._scaleSettings["newHeight"] ) step = 1;
        		objH -= step;
        	}
        	obj.style.height = objH + "px";
        	// Restart
            this._scaleSettings["timeout"] = window.setTimeout( "_globalThisA['" + objId + "']._ScaleThread()", this._scaleSettings["timeStep"] );
        }
        else {
	        this._threadRunning = false;
        }
    }
}