﻿var ContentHeight;
var oldContentHeight = 200;
var TimeToSlide = 950.0;
var extraHeight = 34; //beacuse I'm using margin:-10px(top) -24px(Bottom)

var openAccordion = '';

function runAccordion(index, height) {

    if (height == undefined) {
        ContentHeigh = 200;
    }else{
        ContentHeight = height;
    }
    var nID = "Accordion" + index + "Content";
    if (openAccordion == nID)
        nID = '';
  
    setTimeout("animate(" + new Date().getTime() + "," + TimeToSlide + ",'"
      + openAccordion + "','" + nID + "')", 33);

    openAccordion = nID;
      
}

function animate(lastTick, timeLeft, closingId, openingId) {
    var curTick = new Date().getTime();
    var elapsedTicks = curTick - lastTick;
    
    var opening = (openingId == '') ? null : document.getElementById(openingId);
    var closing = (closingId == '') ? null : document.getElementById(closingId);

    if (timeLeft <= elapsedTicks) {
        if (opening != null)
            opening.style.height = ContentHeight + 'px';       
        if (closing != null) {
            closing.style.display = 'none';
            closing.style.height = '0px';
        }
        oldContentHeight = ContentHeight;  
        return;
    }
    
    timeLeft -= elapsedTicks;
    var newClosedHeight = Math.round((timeLeft / TimeToSlide) * ContentHeight);
    var fadeOut = (timeLeft / TimeToSlide);
    var fadeIn = (TimeToSlide / timeLeft) / 10
  
    if (opening != null) {
        if (opening.style.display != 'block')
            opening.style.display = 'block';
        opening.style.height = (ContentHeight - newClosedHeight) + 'px';
        opening.style.opacity = fadeIn ;
    }

    if (closing != null) {
        closing.style.height = newClosedHeight + extraHeight + 'px';
        closing.style.opacity = fadeOut;
    }
        setTimeout("animate(" + curTick + "," + timeLeft + ",'" + closingId + "','" + openingId + "')", 33);
    

}
