/* - - - - - - - - - - - - - - - - - - - - - - -
 JavaScript
 25 mai 2010 22:49:37
 HAPedit 3.1.11.111
 - - - - - - - - - - - - - - - - - - - - - - - */

JSN.clipTable = function () {
    this.top    = 0;
    this.right  = 0;
    this.bottom = 0;
    this.left   = 0;
}
JSN.clipTable.prototype.attachTo = function (obj) {
    obj.clipTable = this;
    this.master = obj;
}
JSN.clipTable.prototype.toStyle = function(obj) {
    if (!obj)
       if (this.master)
          obj = this.master;
    if (obj)
       obj.style.clip = "rect(" + this.top + "px " + this.right + "px " + this.bottom + "px " + this.left + "px)";
}
JSN.clipTable.prototype.clone = function () {
    with (newclip = new JSN.clipTable) {
         top    = this.top;
         right  = this.right;
         bottom = this.bottom;
         left   = this.left;
    }
    return newclip;
}


JSFX.Clasor.prototype.setSlice = function(slicesCount, sliceDirection, options) {
    var thisObj = this;
    var clip = new Animator({duration: this.settings[this.page].frameTime, transition: Animator.tx.easeInOut});

    clip.extension = Animator.applyDefaults({
         resetOnPlay: false,
         resetOnStop: false
    }, options);
    clip.extension.slicesCount = slicesCount;
    switch (sliceDirection) {
        case "vertical":
             clip.extension.sliceDirection = "offsetHeight";
             clip.extension.sliceFrom = "top";
             clip.extension.sliceTo   = "bottom";
             break;
        case "orizontal":
        default:
             clip.extension.sliceDirection = "offsetWidth";
             clip.extension.sliceFrom = "left";
             clip.extension.sliceTo   = "right";
    }

    clip.resetOnPlay = function(state) {
        if (this.extension.newImg) return;

        this.extension.slices = [];

//    anim.sliceImg = function() {
        var standardclip = new JSN.clipTable();
        with (standardclip) {
             right  = thisObj.img.offsetWidth;
             bottom = thisObj.img.offsetHeight;
        }
        with (this.extension) {
             for (var i=slicesCount-1; i>=0; i--) {
                 this.extension.refer = this.cloneImg(thisObj);

                 var newclip = standardclip.clone();
                 newclip[sliceFrom] = Math.floor(thisObj.img[sliceDirection]*(i)/slicesCount);
                 newclip[sliceTo]   = Math.floor(thisObj.img[sliceDirection]*(i+1)/slicesCount)-1;
                 newclip.attachTo(newImg);
                 newclip.toStyle();

                 slices[i] = newImg;
             }
        }
//    }

// defineste subiectele animatiei
           this.addSubject(function(state) {
                with (clip.extension) {
                     for (var i=0; i<slicesCount; i++) {
                         var newclip = slices[i].clipTable.clone();
                         var delta = parseInt((newclip[sliceTo] - newclip[sliceFrom]) * state);
                         newclip[sliceFrom] += delta;
                         newclip[sliceTo]   -= delta;
                         newclip.toStyle(slices[i]);
                     }
                }
                           });
    }

    clip.resetOnStop = function() {
        with (this.extension) {
             if (!newImg) return;

             for (var i=0; i<slicesCount; i++)
                 thisObj.el.removeChild(slices[i]);
             slices = null;
             newImg = null;
        }

        this.clearSubjects();
    };

    clip.jumpTo = clip.resetOnPlay;

    clip.options.onStart = function() {
        this.extension.sens = this.state < this.target? 1 : -1;
        with (this.extension) {
             if (resetOnPlay) this.resetOnPlay((1-sens)/2);

             for (var i=0; i<slicesCount; i++) {
                 slices[i].src = thisObj.img.src;
                 slices[i].clipTable.toStyle();
             }

             thisObj.nextFrame(sens);
        }
    }

    clip.options.onComplete = function() {
        this.extension.change = this.extension.sens;
        if (this.extension.resetOnStop) this.resetOnStop();
    };

    return clip;
}


JSFX.Clasor.prototype.setSliceAll = function(slicesCount, sliceDirection, options) {
    var thisObj = this;
    var clip = new Animator({duration: this.settings[this.page].frameTime, transition: Animator.tx.easeInOut});

    clip.extension = Animator.applyDefaults({
         resetOnPlay: false,
         resetOnStop: false
    }, options);
    clip.extension.slicesCount = slicesCount;
    switch (sliceDirection) {
        case "orizontal":
             clip.extension.sliceDirection = "offsetHeight";
             clip.extension.sliceFrom = "top";
             clip.extension.sliceTo   = "bottom";
             clip.extension.divSens   = "height";
             break;
        case "vertical":
        default:
             clip.extension.sliceDirection = "offsetWidth";
             clip.extension.sliceFrom = "left";
             clip.extension.sliceTo   = "right";
             clip.extension.divSens   = "width";
    }

    clip.extension.divBorderColor = "#dedede";

    clip.resetOnPlay = function(state) {
        if (this.extension.newImg) return;

        this.extension.slices = [];
        this.extension.divs = [];

//    anim.sliceImg = function() {
        var standardclip = new JSN.clipTable();
        with (standardclip) {
             right  = thisObj.img.offsetWidth;
             bottom = thisObj.img.offsetHeight;
        }
        with (this.extension) {
             for (var i=slicesCount-1; i>=0; i--) {
                 this.extension.refer = this.cloneImg(thisObj);

                 var newclip = standardclip.clone();
                 newclip[sliceFrom] = Math.floor(thisObj.img[sliceDirection]*(i)/slicesCount);
                 newclip[sliceTo]   = Math.floor(thisObj.img[sliceDirection]*(i+1)/slicesCount);
                 newclip.attachTo(newImg);
                 newclip.toStyle();

                 divs[i] = document.createElement("div");
                 with (divs[i].style) {
                      position = "absolute";
                      margin  = 0;
                      padding = 0;
                      width  = thisObj.img.offsetWidth+"px";
                      height = thisObj.img.offsetHeight+"px";
                      borderStyle = "solid";
                      borderColor = "transparent";
                      borderWidth = "1px 0 0 1px";
                      top  = refer.top +"px";
                      left = refer.left+"px";
                 }
                 divs[i].style[divSens]   = newclip[sliceTo] - newclip[sliceFrom]+"px";
                 divs[i].style[sliceFrom] = refer[sliceFrom] + newclip[sliceTo]+"px";
                 thisObj.el.appendChild(divs[i]);

                 slices[i] = newImg;
             }
        }
//    }

// defineste subiectele animatiei
        this.addSubject(function(state) {
             with (clip.extension) {
                  for (var i=0; i<slicesCount; i++) {
                      var newclip = slices[i].clipTable.clone();
                      var delta = (newclip[sliceTo] - newclip[sliceFrom]) * state / 2;
                      newclip[sliceFrom] += delta;
                      newclip[sliceTo]   -= delta;
                      newclip.toStyle(slices[i]);

                      divs[i].style[sliceFrom] = refer[sliceFrom] + newclip[sliceFrom]+"px";
                      divs[i].style[divSens]   = newclip[sliceTo] - newclip[sliceFrom]+"px";
                      divs[i].style.borderColor = divBorderColor;
                  }
             }
        });
    }

    clip.resetOnStop = function() {
        with (this.extension) {
             if (!newImg) return;

             for (var i=0; i<slicesCount; i++) {
                 thisObj.el.removeChild(slices[i]);
                 thisObj.el.removeChild(divs[i]);
             }
             slices = null;
             divs   = null;
             newImg = null;
        }

        this.clearSubjects();
    };

    clip.jumpTo = clip.resetOnPlay;

    clip.options.onStart = function() {
        this.extension.sens = this.state < this.target? 1 : -1;
        with (this.extension) {
             if (resetOnPlay) this.resetOnPlay((1-sens)/2);

             for (var i=0; i<slicesCount; i++) {
                 slices[i].src = thisObj.img.src;
                 slices[i].clipTable.toStyle();
                 divs[i].style[sliceFrom] = refer[sliceFrom] + newclip[sliceFrom]+"px";
             }

             thisObj.nextFrame(sens);
        }
    }

    clip.options.onComplete = function() {
        this.extension.change = this.extension.sens;
        for (var i=0; i<slicesCount; i++) {
            this.extension.divs[i].style.borderColor = "transparent";
        }
        if (this.extension.resetOnStop) this.resetOnStop();
    };

    return clip;
}


JSFX.Clasor.prototype.setSliceWait = function(slicesCount, sliceDirection, lostTime, options) {
    var thisObj = this;
    var clip = new Animator({duration: this.settings[this.page].frameTime, transition: Animator.tx.easeInOut});

    clip.extension = Animator.applyDefaults({
         resetOnPlay: false,
         resetOnStop: false
    }, options);
    clip.extension.slicesCount = slicesCount;
    switch (sliceDirection) {
        case "orizontal":
             clip.extension.sliceDirection = "offsetHeight";
             clip.extension.sliceFrom = "top";
             clip.extension.sliceTo   = "bottom";
             clip.extension.divSens   = "height";
             break;
        case "vertical":
        default:
             clip.extension.sliceDirection = "offsetWidth";
             clip.extension.sliceFrom = "left";
             clip.extension.sliceTo   = "right";
             clip.extension.divSens   = "width";
    }
    clip.extension.lostTime = lostTime;
    clip.extension.amplifier = 1 / (1 - clip.extension.lostTime);
    clip.extension.quanta = clip.extension.lostTime / (clip.extension.slicesCount - 1);

    clip.extension.divBorderColor = "#dedede";

    clip.resetOnPlay = function(state) {
        if (this.extension.newImg) return;

        this.extension.slices = [];
        this.extension.divs = [];

//    anim.sliceImg = function() {
        var standardclip = new JSN.clipTable();
        with (standardclip) {
             right  = thisObj.img.offsetWidth;
             bottom = thisObj.img.offsetHeight;
        }
        with (this.extension) {
             for (var i=slicesCount-1; i>=0; i--) {
                 this.extension.refer = this.cloneImg(thisObj);

                 var newclip = standardclip.clone();
                 newclip[sliceFrom] = Math.floor(thisObj.img[sliceDirection]*(i)/slicesCount);
                 newclip[sliceTo]   = Math.floor(thisObj.img[sliceDirection]*(i+1)/slicesCount);
                 newclip.attachTo(newImg);
                 newclip.toStyle();

                 divs[i] = document.createElement("div");
                 with (divs[i].style) {
                      position = "absolute";
                      margin  = 0;
                      padding = 0;
                      width  = thisObj.img.offsetWidth+"px";
                      height = thisObj.img.offsetHeight+"px";
                      borderStyle = "solid";
                      borderColor = "transparent";
                      borderWidth = "1px 0 0 1px";
                      top  = refer.top +"px";
                      left = refer.left+"px";
                 }
                 divs[i].style[divSens]   = newclip[sliceTo] - newclip[sliceFrom]+"px";
                 divs[i].style[sliceFrom] = refer[sliceFrom] + newclip[sliceTo]+"px";
                 thisObj.el.appendChild(divs[i]);

                 slices[i] = newImg;
             }
        }
//    }

// defineste subiectele animatiei
        this.addSubject(function(state) {
             with (clip.extension) {
                  for (var i=0; i<slicesCount; i++) {
                      var pos = (state - quanta * i) * amplifier;
                      if (pos>=0 && pos<1) {
                         var newclip = slices[i].clipTable.clone();
                         var delta = (newclip[sliceTo] - newclip[sliceFrom]) * pos / 2;
                         newclip[sliceFrom] += delta;
                         newclip[sliceTo]   -= delta;
                         newclip.toStyle(slices[i]);

                         divs[i].style[sliceFrom] = refer[sliceFrom] + newclip[sliceFrom]+"px";
                         divs[i].style[divSens]   = newclip[sliceTo] - newclip[sliceFrom]+"px";

                         divs[i].style.borderColor = divBorderColor;
                      } else
                         if (pos>=1 && divs[i].style.borderColor==divBorderColor) {
                            var newclip = slices[i].clipTable.clone();
                            newclip[sliceTo] = 0;
                            newclip.toStyle(slices[i]);

                            divs[i].style.borderColor = "transparent";
                         }
                  }
             }
        });
    }

    clip.resetOnStop = function() {
        with (this.extension) {
             if (!newImg) return;

             for (var i=0; i<slicesCount; i++) {
                 thisObj.el.removeChild(slices[i]);
                 thisObj.el.removeChild(divs[i]);
             }
             slices = null;
             divs   = null;
             newImg = null;
        }

        this.clearSubjects();
    };

    clip.jumpTo = clip.resetOnPlay;

    clip.options.onStart = function() {
        this.extension.sens = this.state < this.target? 1 : -1;
        with (this.extension) {
             if (resetOnPlay) this.resetOnPlay((1-sens)/2);

             for (var i=0; i<slicesCount; i++) {
                 slices[i].src = thisObj.img.src;
                 slices[i].clipTable.toStyle();
                 divs[i].style[sliceFrom] = refer[sliceFrom] + newclip[sliceFrom]+"px";
             }

             thisObj.nextFrame(sens);
        }
    }

    clip.options.onComplete = function() {
        this.extension.change = this.extension.sens;
        for (var i=0; i<slicesCount; i++) {
            this.extension.divs[i].style.borderColor = "transparent";
        }
        if (this.extension.resetOnStop) this.resetOnStop();
    };

    return clip;
}


