




function showQuestion(id) {
    if ((window.fadingQuestion)
            && (window.fadingQuestion.inFading)
            && (window.fadingQuestion.inFading == true)) {
        window.fadingQuestion.inFading = false;
        window.fadingQuestion.idForShow = id;
        
        return;
    }
    
    var cont = $('MainSecQContainer');
    
    if (!cont) {
        return;
    }
    
    var selected = $(id);
    
    if (!selected) {
        return;
    }
    
    if (!selected.id) {
        return;
    }
    
    var current = $('MainSecQContainer').getElementsByClassName('selected');
    
    if (current.length < 1) {
        return;
    }
    
    current = current[0];
    
    if (!current.id) {
        return;
    }
    
    if (current.id == selected.id) {
        return;
    }
    
    var ev = {
        cont : cont,
        selected : selected,
        current : current,
        step : 5,
        refresh : 20,
        use : null,
        canceling : false
    }
    
    
    if (window.movingQuestion) {
        if (!ev.cont || !ev.selected || !ev.current) {
            return;
        }
        
        if (window.movingQuestion.selected.id == id) {
            return;
        }
        
        
        window.movingQuestion.canceling = true;
        window.movingQuestion.use = ev;
        return;
    }
    
    calculateAnimation(ev);
    
    prepareQuestion(ev);
    
    setTimeout(function() {
            moveQuestion(ev);
        }, ev.refresh);
}

function calculateAnimation(ev) {
    // nombre de pas pour faire apparaitre la nouvelle div
    var newH = ev.selected.getVisualBox().height;
    
    var currentH = ev.cont.getVisualBox().height;
    
    
    ev.contHeightMethod = null;
    ev.contHeightValue = null;
    
    if (newH < currentH) {
        ev.contHeightMethod = '-';
        ev.contHeightValue = newH;
        
    } else if (currentH < newH) {
        ev.contHeightMethod = '+';
        ev.contHeightValue = newH;
        
    }
    
    ev.contHeightValue = newH;
    
    ev.stepNumber = Math.ceil(newH / ev.step);
    
    ev.opacityStep = (100 / ev.stepNumber);
    ev.startOpa = 100;
}

function prepareQuestion(ev) {
    
    ev.selected.style.top = (-1 * ev.contHeightValue) + 'px';
    ev.selected.style.zIndex = 3;
    ev.selected.setOpacityStyle(0);
    ev.selected.className = ev.selected.className.addWord('selected');
    
    ev.current.style.zIndex = 2;
    ev.current.style.top = 0;
    //ev.current.setOpacityStyle(100);
    ev.current.className = ev.current.className.removeWord('selected');
    
    
    ev.currentStep = 0;
    
    window.movingQuestion = ev;
    
}


function moveQuestion(ev) {
    if (ev.canceling == true) {
        //finishMoveQuestion(ev);
        
        //ev.selected.style.top = 0;
        ev.selected.style.zIndex = 3;
        //ev.selected.setOpacityStyle(100);
        
        ev.current.style.zIndex = 2;
        ev.current.style.top = '-5000px';
        ev.current.setOpacityStyle(0);
        
        var newEv = window.movingQuestion.use;
        
        var tmp = window.movingQuestion.startOpa;
        
        calculateAnimation(newEv);
        
        newEv.selected.style.top = (-1 * newEv.contHeightValue) + 'px';
        newEv.selected.style.zIndex = 3;
        newEv.selected.setOpacityStyle(0);
        newEv.selected.className = newEv.selected.className.addWord('selected');
        
        newEv.current.style.zIndex = 2;
        //newEv.current.style.top = 0;
        //newEv.current.setOpacityStyle(100);
        newEv.current.className = newEv.current.className.removeWord('selected');
        
        
        newEv.currentStep = 0;
        
        window.movingQuestion = newEv;
        
        
        setTimeout(function() {
                moveQuestion(newEv);
            }, ev.refresh);
        
        return;
    }
    
    
    if (ev.stepNumber < ev.currentStep) {
        finishMoveQuestion(ev);
        return;
    }
    
    
    var top = Number.parse.integer(ev.selected.style.top);
    var opa = Number.parse.integer(ev.currentStep * ev.opacityStep);
    
    top = ((top + ev.step) < 0) ? (top + ev.step) : 0;
    
    ev.selected.style.top = top + 'px';
    
    ev.selected.setOpacityStyle(opa);
    
    ev.current.setOpacityStyle(ev.current.getOpacityStyle() - ev.opacityStep);
    
    ev.startOpa = ev.startOpa - opa;
    
    var nh = null;
    if (ev.contHeightMethod == '-') {
        nh = ev.cont.getMyStyle('height');
        nh = Number.parse.integer(nh);
        nh = nh - ev.step;
        
        
        if (nh < ev.contHeightValue) {
            nh = ev.contHeightValue;
            ev.contHeightMethod = null;
        }
        
        
        ev.cont.style.height = nh + 'px';
        
    } else if (ev.contHeightMethod == '+') {
        nh = ev.cont.getMyStyle('height');
        nh = Number.parse.integer(nh);
        
        nh = nh + ev.step;
        
        if (ev.contHeightValue < nh) {
            nh = ev.contHeightValue;
            ev.contHeightMethod = null;
        }
        
        
        ev.cont.style.height = nh + 'px';
    }
    
    
    
    ev.currentStep++;
    
    setTimeout(function() {
            moveQuestion(ev);
        }, ev.refresh);
}



function finishMoveQuestion(ev) {

    window.movingQuestion = null;
    
    ev.selected.style.top = 0;
    ev.selected.style.zIndex = 3;
    ev.selected.setOpacityStyle(100);
    
    ev.current.style.top = '-5000px';
    ev.current.style.zIndex = 2;
    ev.current.setOpacityStyle(0);
    
    ev.selected.className = ev.selected.className.addWord('selected');
    ev.current.className = ev.current.className.removeWord('selected');
    
    
    if (ev.contHeightValue) {
        ev.cont.style.height = ev.contHeightValue + 'px';
    }
    
}




function fadeFirstQuestion(id) {
    window.fadingQuestion = {
            inFading : true
        }
    var ev = {
        current : $('MainSecQContainer').getElementsByClassName('selectedForFade')[0],
        step : 2,
        refresh : 5
    }
    
    
    ev.opaNumberOfStep = Math.ceil(100 / 5);
    ev.currentOpa = 0;
    
    ev.current.style.top = 0;
    ev.current.setOpacityStyle(0);
    
    setTimeout(function() {
            performFadeFirstQuestion(ev);
        }, ev.refresh);
}

function performFadeFirstQuestion(ev) {
    if (window.fadingQuestion.inFading == false) {
        
        if (window.fadingQuestion.idForShow) {
            var nid = window.fadingQuestion.idForShow;
            window.fadingQuestion = null;
            ev.current.className = ev.current.className.addWord('selected');
            showQuestion(nid);
        }
        
        return;
    }
    
    if (100 < ev.currentOpa) {
        
        ev.current.setOpacityStyle(100);
        ev.current.className = ev.current.className.addWord('selected');
        window.fadingQuestion.inFading = false;
        window.fadingQuestion = null;
        return;
        
    }
    
    ev.currentOpa = ev.currentOpa + ev.step;
    ev.current.setOpacityStyle(ev.currentOpa);
    
    
    setTimeout(function() {
            performFadeFirstQuestion(ev);
        }, ev.refresh);
}

window.appendLoadEvent(function() {
        fadeFirstQuestion('SecQContainer0');
    });










