﻿
  /****************************************************** 
 Keylinx Scrolling Layers (c) 2008 Keylinx
 This is the base functions for the ScrollLayer Modules 
 in kXServer. 
 *******************************************************/
kXscrollObjs = {};
kXscrollObj.speed = 100;
function kXscrollObj(wnId, lyrId, cntId) {
    this.id = wnId;
    kXscrollObjs[this.id] = this;
    this.animString = "kXscrollObjs." + this.id;
    this.load(lyrId, cntId);
};
kXscrollObj.loadLayer = function(wnId, id, cntId) {
    if (kXscrollObjs[wnId])
        kXscrollObjs[wnId].load(id, cntId);
};
kXscrollObj.prototype.load = function(lyrId, cntId) {
    if (!document.getElementById)
        return;
    var wndo, lyr;
    if (this.lyrId) {
        lyr = document.getElementById(this.lyrId);
        lyr.style.visibility = "hidden";
    }
    lyr = document.getElementById(lyrId);
    wndo = document.getElementById(this.id);
    lyr.style.top = this.y = 0;
    lyr.style.left = this.x = 0;
    this.maxY = (lyr.offsetHeight - wndo.offsetHeight > 0) ? lyr.offsetHeight - wndo.offsetHeight : 0;
    this.wd = cntId ? document.getElementById(cntId).offsetWidth : lyr.offsetWidth;
    this.maxX = (this.wd - wndo.offsetWidth > 0) ? this.wd - wndo.offsetWidth : 0;
    this.lyrId = lyrId;
    lyr.style.visibility = "visible";
    this.on_load();
    this.ready = true;
};
kXscrollObj.prototype.on_load = function() {
};
kXscrollObj.prototype.shiftTo = function(lyr, x, y) {
    if (!lyr.style)
        return;
    lyr.style.left = (this.x = x) + "px";
    lyr.style.top = (this.y = y) + "px";
};
kXscrollObj.GeckoTableBugFix = function() {
    var ua = navigator.userAgent;
    if (ua.indexOf("Gecko") > -1
         && ua.indexOf("Firefox") == -1
         && ua.indexOf("Safari") == -1
         && ua.indexOf("Konqueror") == -1) {
        kXscrollObj.hold = [];
        for (var i = 0; arguments[i]; i++) {
            if (kXscrollObjs[arguments[i]]) {
                var wndo = document.getElementById(arguments[i]);
                var holderId = wndo.parentNode.id;
                var holder = document.getElementById(holderId);
                document.body.appendChild(holder.removeChild(wndo));
                wndo.style.zIndex = 1000;
                var pos = getPageOffsets(holder);
                wndo.style.left = pos.x + "px";
                wndo.style.top = pos.y + "px";
                kXscrollObj.hold[i] = [arguments[i], holderId];
            }
        }
        window.addEventListener("resize", kXscrollObj.rePositionGecko, true);
    }
};
kXscrollObj.rePositionGecko = function() {
    if (kXscrollObj.hold) {
        for (var i = 0; kXscrollObj.hold[i]; i++) {
            var wndo = document.getElementById(kXscrollObj.hold[i][0]);
            var holder = document.getElementById(kXscrollObj.hold[i][1]);
            var pos = getPageOffsets(holder);
            wndo.style.left = pos.x + "px";
            wndo.style.top = pos.y + "px";
        }
    }
};
function getPageOffsets(el) {
    var left = el.offsetLeft;
    var top = el.offsetTop;
    if (el.offsetParent
         && el.offsetParent.clientLeft
         || el.offsetParent.clientTop) {
        left += el.offsetParent.clientLeft;
        top += el.offsetParent.clientTop;
    }
    while (el = el.offsetParent) {
        left += el.offsetLeft;
        top += el.offsetTop;
    }
    return { x: left, y: top };
};
kXscrollObj.scrollBy = function(wnId, x, y, dur) {
    if (kXscrollObjs[wnId]) kXscrollObjs[wnId].glideBy(x, y, dur);
}
kXscrollObj.scrollTo = function(wnId, x, y, dur) {
    if (kXscrollObjs[wnId]) kXscrollObjs[wnId].glideTo(x, y, dur);
}
kXscrollObj.prototype.glideBy = function(dx, dy, dur) {
    if (!document.getElementById || this.sliding) return;
    this.slideDur = dur || kXscrollObj.slideDur;
    this.destX = this.destY = this.distX = this.distY = 0;
    this.lyr = document.getElementById(this.lyrId);
    this.startX = this.x; this.startY = this.y;
    if (dy < 0) this.distY = (this.startY + dy >= -this.maxY) ? dy : -(this.startY + this.maxY);
    else if (dy > 0) this.distY = (this.startY + dy <= 0) ? dy : -this.startY;
    if (dx < 0) this.distX = (this.startX + dx >= -this.maxX) ? dx : -(this.startX + this.maxX);
    else if (dx > 0) this.distX = (this.startX + dx <= 0) ? dx : -this.startX;
    this.destX = this.startX + this.distX; this.destY = this.startY + this.distY;
    if (dy != 0) {
        if ((Math.abs(this.y) == Math.abs(this.maxY)) && (Math.abs(this.y) == Math.abs(this.destY))) {
            this.glideTo(0, 0, this.slideDur); return;
        }
        else if ((this.y == 0) && (this.destY == 0)) {
            this.glideTo(0, this.maxY, this.slideDur); return;
        }
    }
    else {
        if ((Math.abs(this.x) == Math.abs(this.maxX)) && (Math.abs(this.x) == Math.abs(this.destX))) {
            this.glideTo(0, 0, this.slideDur); return;
        }
        else if ((this.x == 0) && (this.destX == 0)) {
            this.glideTo(this.maxX, 0, this.slideDur); return;
        }
    }
    this.slideTo(this.destX, this.destY);
}
kXscrollObj.prototype.glideTo = function(destX, destY, dur) {
    if (!document.getElementById || this.sliding) return;
    this.slideDur = dur || kXscrollObj.slideDur;
    this.lyr = document.getElementById(this.lyrId);
    this.startX = this.x; this.startY = this.y;
    this.destX = -Math.max(Math.min(destX, this.maxX), 0);
    this.destY = -Math.max(Math.min(destY, this.maxY), 0);
    this.distY = this.destY - this.startY;
    this.distX = this.destX - this.startX;
    this.slideTo(this.destX, this.destY);
}
kXscrollObj.prototype.slideTo = function(destX, destY) {
    this.per = Math.PI / (2 * this.slideDur); this.sliding = true;
    this.slideStart = (new Date()).getTime();
    this.aniTimer = setInterval(this.animString + ".doSlide()", 10);
    this.on_slide_start(this.startX, this.startY);
}
kXscrollObj.prototype.doSlide = function() {
    var elapsed = (new Date()).getTime() - this.slideStart;
    if (elapsed < this.slideDur) {
        var x = this.startX + this.distX * Math.sin(this.per * elapsed);
        var y = this.startY + this.distY * Math.sin(this.per * elapsed);
        this.shiftTo(this.lyr, x, y); this.on_slide(x, y);
    } else {
        clearInterval(this.aniTimer); this.sliding = false;
        this.shiftTo(this.lyr, this.destX, this.destY);
        this.lyr = null; this.on_slide_end(this.destX, this.destY);
    }
}
kXscrollObj.prototype.on_slide_start = function() { }
kXscrollObj.prototype.on_slide = function() { }
kXscrollObj.prototype.on_slide_end = function() { } 
 

