/////////////////////////////////////////////////////////////////////

var FloatLayers = new Array();
var FloatLayersByName = new Array();

function addFloatLayer(n, offX, offY, spd) { new FloatLayer(n, offX, offY, spd); }
function getFloatLayer(n) { return FloatLayersByName[n]; }
function alignFloatLayers() { for (var i = 0; i < FloatLayers.length; i++) FloatLayers[i].align(); }

function getXCoord(el) {
    x = 0;
    while (el) {
        x += el.offsetLeft;
        el = el.offsetParent;
    }
    return x;
}
function getYCoord(el) {
    y = 0;
    while (el) {
        y += el.offsetTop;
        el = el.offsetParent;
    }
    return y;
}

/////////////////////////////////////////////////////////////////////

FloatLayer.prototype.setFloatToTop = setTopFloater;
FloatLayer.prototype.setFloatToBottom = setBottomFloater;
FloatLayer.prototype.setFloatToLeft = setLeftFloater;
FloatLayer.prototype.setFloatToRight = setRightFloater;
FloatLayer.prototype.initialize = defineFloater;
FloatLayer.prototype.adjust = adjustFloater;
FloatLayer.prototype.align = alignFloater;

function FloatLayer(n, offX, offY, spd) {
    this.index = FloatLayers.length;

    FloatLayers.push(this);
    FloatLayersByName[n] = this;

    this.name = n;
    this.floatX = 0;
    this.floatY = 0;
    this.tm = null;
    this.steps = spd;
    this.alignHorizontal = (offX >= 0) ? leftFloater : rightFloater;
    this.alignVertical = (offY >= 0) ? topFloater : bottomFloater;
    this.ifloatX = 0;//  Math.abs(offX);
    this.ifloatY = Math.abs(offY);
}

/////////////////////////////////////////////////////////////////////

function defineFloater() {
    this.layer = document.getElementById(this.name);
    this.width = this.layer.offsetWidth;
    this.height = this.layer.offsetHeight;
    this.prevX = this.layer.offsetLeft;
    this.prevY = this.layer.offsetTop;
}

function adjustFloater() {
    this.tm = null;
    //this.layer = document.getElementById('floatlayer');
    if (this.layer == null) this.initialize();

    if (this.layer.style.position != 'absolute') return;

    //var dx = Math.abs(this.floatX - this.prevX);
    var dy = Math.abs(this.floatY - this.prevY);

    /*if (dx < this.steps / 2)
        cx = (dx >= 1) ? 1 : 0;
    else
        cx = Math.round(dx / this.steps);*/

    if (dy < this.steps / 2)
        cy = (dy >= 1) ? 1 : 0;
    else
        cy = Math.round(dy / this.steps);

    /*if (this.floatX > this.prevX)
        this.prevX += cx;
    else if (this.floatX < this.prevX)
        this.prevX -= cx;*/

    if (this.floatY > this.prevY)
        this.prevY += cy;
    else if (this.floatY < this.prevY)
        this.prevY -= cy;

    this.layer.style.left = 0;// this.prevX;
    this.layer.style.top = this.prevY;

    if (/*cx != 0 ||*/ cy != 0) {
        if (this.tm == null) this.tm = setTimeout('FloatLayers[' + this.index + '].adjust()', 50);
        //alignFloater();
    } else
        alignFloatLayers();
}

function setLeftFloater() { this.alignHorizontal = leftFloater; }
function setRightFloater() { this.alignHorizontal = rightFloater; }
function setTopFloater() { this.alignVertical = topFloater; }
function setBottomFloater() { this.alignVertical = bottomFloater; }

function leftFloater() {this.floatX = 0;/* document.body.scrollLeft + this.ifloatX; */}
function topFloater() {
    //this.floatY = document.body.scrollTop + this.ifloatY;
    //var scrOfX = 0, scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        this.floatY = window.pageYOffset;
        //scrOfX = window.pageXOffset;
    } else if (document.body && (/*document.body.scrollLeft ||*/ document.body.scrollTop)) {
        //DOM compliant
        this.floatY = document.body.scrollTop;
        //scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (/*document.documentElement.scrollLeft ||*/ document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        this.floatY = document.documentElement.scrollTop;
        //scrOfX = document.documentElement.scrollLeft;
    }

    //alert(this.floatY);

    //this.floatY += this.ifloatY;
}

function topFloaterRet() {
    var ret = document.body.scrollTop;

    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        ret = window.pageYOffset;
    } else if (document.body && (document.body.scrollTop)) {
        //DOM compliant
        ret = document.body.scrollTop;
    } else if (document.documentElement && (document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        ret = document.documentElement.scrollTop;
    }

    return ret;
}
function rightFloater() { this.floatX = document.body.scrollLeft + document.body.clientWidth - this.ifloatX - this.width; }
function bottomFloater() { this.floatY = document.body.scrollTop + document.body.clientHeight - this.ifloatY - this.height; }

function alignFloater() {
    if (this.layer == null) this.initialize();
    //this.alignHorizontal();
    this.alignVertical();

    if (this.floatY > (nDefaultTop + $('#floatlayer').height() + getUserListHeight(1) + getUserListHeight(2) + getUserListHeight(3))) {

        $('#floatlayer').css('top', this.floatY - $('#floatlayer').height());
        //adjustFloater();
    } else {
        $('#floatlayer').css('top', (226 + getUserListHeight(1) + getUserListHeight(2) + getUserListHeight(3)));
    }
    
    /*if (this.prevX != this.floatX || this.prevY != this.floatY) {
        if (this.tm == null) this.tm = setTimeout('FloatLayers[' + this.index + '].adjust()', 50);
    }*/
}

// -------------------------------------------------------------


// posun div s obrazkem v postranni liste nahoru nebo dolu, podle nastaveneho smeru 'direction'
function moveElement(direction) {
    var grp = $(".sidebarBoxList").children();
    var cnt = grp.length;

    var temp, x;
    var moved = false;
    for (var i = 0; i < cnt; i++) {
        if (grp[i].id == 'floatlayer' && !moved) {
            // vychozi pozice - pokud se nebude posouvat zadnym smerem
            x = i;

            // posun nahoru
            if (direction == 'up') {
                if (i > 1) {
                    x = i - 1;
                }
            } else {
            //posun dolu
                if (i < (cnt - 1)) {
                    x = i + 1;
                }
            }

            temp = grp[i];
            grp[i] = grp[x];
            grp[x] = temp;

            moved = true;
        }
    }

    $(grp).remove();
    $(".sidebarBoxList").append($(grp));
}

// zjisti jestli je div s obrazek auta posledni v rade (sloupci) v postranni liste
function isLastInBox() {
    var grp = $(".sidebarBoxList").children();
    var cnt = grp.length;
    var isLast = false;

    for (var i = 0; i < cnt; i++) {
        if (grp[i].id == 'floatlayer' && i == (cnt-1)) {isLast = true; }
    }

    return isLast;
}

function getUserListHeight(nr) {
    var totalHeight = 0;

    totalHeight += $('#userListHeader' + nr).outerHeight();

    if (!isHidden('userList' + nr)) {
        totalHeight += $('#userList' + nr).outerHeight();
    }

    return totalHeight;
}

function isHidden(elID) {
    return $('#'+elID).is(':hidden');
}

var lastScrollTop = 0;


function detach() {

    var st = $(document).scrollTop();
    var floaty = topFloaterRet();

    // box s obrazkem uz je na posledni pozici, zacnu floatovat
    if (isLastInBox() && this.layer == null && floaty > (210 + 155)) {

        //alert(floaty);

        nDefaultTop = 210 + $('#carHistory').height() + $('#commodityHistory').height() + $('#userCarList').height();

        // vytvorim instanci plovouciho objektu
        new FloatLayer('floatlayer', 0, nDefaultTop, 10);

        lay = document.getElementById('floatlayer');
        l = getXCoord(lay);
        t = getYCoord(lay);

        lay.style.position = 'absolute';
        lay.style.top = t;
        lay.style.left = 0; // l;

        getFloatLayer('floatlayer').initialize();
        alignFloatLayers();
    } else {
        // box s obrazkem je treba posunout / prohodit
        
        if (floaty > (210 + 155)) {

            var offs = $('#carHistory').position();

            // vyberu kterym smerem se bude posouvat
            if ((floaty + $('#floatlayer').height()) > lastScrollTop) {
                // downscroll code
                moveElement();
            } else {
                // upscroll code
                moveElement('up');
            }
            lastScrollTop = st;
        } else {
            $('#floatlayer').css('position', 'relative');
            $('#floatlayer').css('top', '0');
            // upscroll code
            moveElement('up');
        }

    }
}

