// Definition of DhtmlObject
function DhtmlObject (objectId, varName) {
        // Create Object
        if (document.all) {  // MSIE
                this.dhtmlObject = document.all[objectId].style;
                this.dhtmlString = 'document.all[\"' + objectId+'\"].style';
                this.dhtmlType = "msie";
        }
        else {
                if (document.layers) { // NN until 4.7
                        this.dhtmlObject = document.layers[objectId];
                        this.dhtmlString = 'document.layers[\"' + objectId+'\"]';
                        this.dhtmlType = "nn";
                }
                else {
                        if (document.getElementById) {  // NN 6
                                this.dhtmlObject = document.getElementById(objectId).style;
                                this.dhtmlString = 'document.getElementById(\"' + objectId+'\").style';
                                this.dhtmlType = "n6";
                        }
                        else {
                                this.dhtmlObject = "";
                                this.dhtmlString = '';
                                this.dhtmlType = "off";
                        }
                }
        }
        this.timeoutId = null;
        this.fixedFlag=0;
        var varName = varName;
        var animationStep = 0;
        var animationLoops = 0;
        var animationArray = new Array();
        // Access Layer

        function visibility(value) {
                this.dhtmlObject.visibility = value;
        }

        function top(value) {
                (value) ? this.dhtmlObject.top = value: value = this.dhtmlObject.top;
                return value;
        }

        function left(value) {
                (value) ? this.dhtmlObject.left = value: value = this.dhtmlObject.left;
                return value;
        }

        function zIndex(value) {
                (value) ? this.dhtmlObject.zIndex = value: value = this.dhtmlObject.zIndex;
                return value;
        }

        function clip(y1,x1,y2,x2,fixedFlag) {
                //alert (x1);
                this.clipRight = x1;
                this.clipTop = y1;
                this.clipLeft = x2;
                this.clipBottom = y2;
                this.clipRight2 = x1;
                this.clipTop2 = y1;
                this.clipLeft2 = x2;
                this.clipBottom2 = y2;
                this.fixedFlag = fixedFlag;
                if (this.dhtmlType == 'nn') {
                        this.dhtmlObject.clip.right = x1;
                        this.dhtmlObject.clip.top = y1;
                        this.dhtmlObject.clip.left = x2;
                        this.dhtmlObject.clip.bottom = y2;
                }
                else {
                        this.dhtmlObject.clip = "rect(" + y1 + " " + x1 + " " + y2 + " " + x2 + ")";
                }

        }

        function moveClip(deltaX,deltaY,valueX,valueY) {
                this.clipTop += deltaY;
                this.clipBottom += deltaY;
                this.clipRight += deltaX;
                this.clipLeft += deltaX;
                this.dhtmlObject.left = valueX;
                this.dhtmlObject.top = valueY;
                if (this.dhtmlType == 'nn') {
                        this.dhtmlObject.clip.right = this.clipRight;
                        this.dhtmlObject.clip.top   = this.clipTop;
                        this.dhtmlObject.clip.left   = this.clipLeft;
                        this.dhtmlObject.clip.bottom   = this.clipBottom;
                }
                else {
                        //alert (deltaX + ':::' +deltaY);
                        //alert ("rect(" + this.clipTop + " " + this.clipRight + " " + this.clipBottom + " " + this.clipLeft +")");
                        this.dhtmlObject.clip = "rect(" + this.clipTop + " " + this.clipRight + " " + this.clipBottom + " " + this.clipLeft +")";
                }

        }

        function fixPosition(valueX , valueY) {
                //alert (valueX + ' - ' + valueY);
        }

        function pos (valueX , valueY) {
                //alert (valueX + ' - ' + valueY);
                this.dhtmlObject.left = valueX;
                this.dhtmlObject.top = valueY;
        }

        function animationStart (animationProperties,animation) {
                if (!animation) {
                        if (animationProperties['animationType']=='circle') {circle(this,animationProperties)};
                        if (animationProperties['animationType']=='randomPath') {randomPath(this,animationProperties)};
                        if (animationProperties['animationType']=='linePath') {linePath(this,animationProperties)};

                }
                else {
                        animationArray = animation;
                }
                animationStep = 0;
                animationLoops = (animationProperties['totalLoops']) ? parseInt(animationProperties['totalLoops']) : 1;
                animateObject(this.dhtmlString);
        }

        // linePath
        function linePath (thisObject,animationProperties) {
                var startX = (animationProperties['startX']) ? parseInt(animationProperties['startX']) : thisObject.left();
                var startY = (animationProperties['startY']) ? parseInt(animationProperties['startY']) : thisObject.top();
                var stopX = (animationProperties['stopX']) ? parseInt(animationProperties['stopX']) : thisObject.left();
                var stopY = (animationProperties['stopY']) ? parseInt(animationProperties['stopY']) : thisObject.top();

                var steps = (animationProperties['steps']) ? parseInt(animationProperties['steps']) : 1;
                var totalTime = (animationProperties['totalTime']) ? parseInt (animationProperties['totalTime']*1000) : 10000;
                stepTime = parseInt(totalTime/steps);
                stepX = parseInt((stopX-startX)/steps);
                stepY = parseInt((stopY-startY)/steps);
                positionX = startX;
                positionY = startY;
                //alert (positionX + ' - ' +positionY);
                stepCounter = 0;
                for (var i=0; i<steps; i++) {
                        animationArray[i] = new Array (positionX, positionY, stepTime);
                        positionX += stepX;
                        positionY += stepY;
                }
        }


        // randomPath
        function randomPath (thisObject,animationProperties) {
                var pathLength = (animationProperties['pathLength']) ? parseInt(animationProperties['pathLength']) : 10;
                var pathLengthX = (animationProperties['pathLengthX']) ? parseInt(animationProperties['pathLengthX']) : pathLength;
                var pathLengthY = (animationProperties['pathLengthY']) ? parseInt(animationProperties['pathLengthY']) : pathLength;
                var flowX = (animationProperties['flowX']) ? parseInt(animationProperties['flowX']) : 0;
                var flowY = (animationProperties['flowY']) ? parseInt(animationProperties['flowY']) : 0;

                var positionX = (animationProperties['positionX']) ? parseInt(animationProperties['positionX']) : thisObject.left();
                var positionY = (animationProperties['positionY']) ? parseInt(animationProperties['positionY']) : thisObject.top();
                var steps = (animationProperties['steps']) ? parseInt(animationProperties['steps']) : 1;
                var pathTime = (animationProperties['pathTime']) ? parseInt (animationProperties['pathTime']*1000) : 0;
                var stepTime = (animationProperties['totalTime']) ? parseInt (animationProperties['totalTime']/steps*1000) : 100;
                stepCounter = 0;
                //alert (steps + ' - ' + pathLengthX + ' - ' + pathLengthY+ ' - ' + positionX + ' - ' + positionY);
                // create AniationArray
                var feedback = "randomPath:\n";
                for (var i=0; i<steps; i++) {
                        positionX = parseInt (positionX + (0.5-Math.random())*pathLengthX - flowX );
                        positionY = parseInt (positionY + (0.5-Math.random())*pathLengthY - flowY);
                        //alert (valueY + ' - ' +positionX + ' - ' + positionY + ' - ' + stepTime);
                        var stepTime = (pathTime) ? parseInt(Math.random() * pathTime) : stepTime;
                        animationArray[i] = new Array (positionX, positionY, stepTime);
                        //feedback += positionX + " - " +  positionY + " - " + stepTime + "\n";
                }
                //alert (feedback);
        }

        // circle
        function circle (thisObject,animationProperties) {
                var radius =(animationProperties['radius']) ? parseInt(animationProperties['radius']) : 100;
                var radiusX =(animationProperties['radiusX']) ? parseInt(animationProperties['radiusX']) : radius;
                var radiusY =(animationProperties['radiusY']) ? parseInt(animationProperties['radiusY']) : radius;
                var valueX =(animationProperties['valueX']) ? parseInt(animationProperties['valueX']) : thisObject.top();
                var valueY =(animationProperties['valueY']) ? parseInt(animationProperties['valueY']) : thisObject.left();
                var steps =(animationProperties['steps']) ? parseInt(animationProperties['steps']) : 1;
                var stepTime = (animationProperties['totalTime']) ? parseInt (animationProperties['totalTime']/steps*1000) : 100;
                var startDegree = (animationProperties['startDegree']) ? parseInt (animationProperties['startDegree']) : 0;
                var stepDegree = 2*Math.PI/steps;
                var degree = startDegree/360*Math.PI;
                stepCounter = 0;
                //alert (radiusX + ' - ' + radiusY+ ' - ' + valueX + ' - ' + valueY);
                // create AniationArray
                for (var i=0; i<steps; i++) {
                        positionX = parseInt (valueX + Math.cos(degree)*radiusX);
                        positionY = parseInt (valueY + Math.sin(degree)*radiusY);
                        animationArray[i] = new Array (positionX, positionY, stepTime);
                        degree += stepDegree;
                }
        }


        // central loop with timeout for animations
        function animateObject (animationString) {
                if (this.timeoutId) {clearTimeout (this.timeoutId);}
                //alert (animationStep +'-'+this.fixedFlag);
                if ((this.fixedFlag) && (animationStep>0)) {
                        var deltaX = animationArray[animationStep-1][0] - animationArray[animationStep][0];
                        var deltaY = animationArray[animationStep-1][1] - animationArray[animationStep][1];
                        //alert(deltaX +'-'+deltaY);
                        this.moveClip(deltaX,deltaY,animationArray[animationStep][0],animationArray[animationStep][1]);
                }
                else {
                        eval(animationString+'.left = ' + animationArray[animationStep][0]);
                        eval(animationString+'.top = ' + animationArray[animationStep][1]);
                }

                animationStep ++;
                if (animationArray.length>animationStep) {
                        this.timeoutId = setTimeout(varName + '.animateObject(\'' + animationString + '\')' , animationArray[animationStep][2]);
                }
                else {  // loop finished
                        animationLoops -= 1;
                        if (animationLoops > 0) {
                                animationStep = 0;
                                this.clipRight = this.clipRight2;
                                this.clipTop = this.clipTop2;
                                this.clipLeft = this.clipLeft2;
                                this.clipBottom = this.clipBottom2;
                                this.timeoutId = setTimeout(varName + '.animateObject(\'' + animationString + '\')' , animationArray[animationStep][2]);
                        }
                        else {
                                if (this.timeoutId) {clearTimeout (this.timeoutId);}
                                animationArray = new Array();
                        }
                }
        }
        this.animateObject = animateObject;
        this.pos = pos;
        this.fixPosition = fixPosition;
        this.left = left;
        this.top = top;
        this.clip = clip;
        this.moveClip = moveClip;
        this.zIndex = zIndex;
        this.visibility = visibility;
        this.animationStart = animationStart;
}
