﻿// Accordion written by David Murdoch
// version 1.0
// October 2007
// uses Prototype's document.getElementsByClassName 

function accordion(){
    var panels = document.getElementsByClassName("panel");
    var continues = document.getElementsByClassName("continue");
    for (var i = 0; i < panels.length; i++){
        panels[i].setAttribute("title", panels[i].offsetHeight);
        panels[i].style.display="none";
        continues[i].style.display="";
    }
}

var tall = 0;
var currentPanel;
var currentFrom;


function show(who,from,direction){
    
    if (currentPanel != undefined){
        hide(currentPanel,currentFrom,"hide");
    }
    
    currentPanel = who;
    currentFrom = from;
    tall = document.getElementById(who).getAttribute("title");
    document.getElementById(who).style.height = "0px";
    document.getElementById(who).style.overflow = 'hidden';
    document.getElementById(who).style.display = "";
    doHeight(who,from,direction);
}

function hide(who,from,direction){
    doHeight(who,from,direction);
}

  function doHeight(who,from,direction){
    var speed;
    var offHeight = document.getElementById(who).offsetHeight;
    var curHeight = offHeight;
        
    if (direction == "show"){
        if (tall - offHeight > 100)
            speed = 50;
        else
            speed = 5;
    }
    else {
        if (offHeight > 100)
            speed = -50;
        else
            speed = -5;
    }
    
    if (direction != "show" && curHeight < 5) {
        document.getElementById(who).style.height = "0px";
        }
    else
        document.getElementById(who).style.height =  curHeight + speed + 'px';
    if (direction == "show"){
        if (document.getElementById(who).offsetHeight < tall){
             var myTime = window.setTimeout(function(who,from,direction) {return function() {doHeight(who,from,direction);}}(who,from,direction),10);
             
        }
        else {
            clearTimeout(myTime);
            
            if (from.attachEvent){
                from.onclick = function(e){hide(who,from,'hide')};
            }
            else {
                from.setAttribute('onclick', "hide('"+who+"',this,'hide')"); 
            }
            
            from.innerHTML = "hide story";
        }
    }
    else {
        if (document.getElementById(who).offsetHeight > 0){
             var myTime2 = window.setTimeout(function(who,from,direction) {return function() {doHeight(who,from,direction);}}(who,from,direction),10);
        }
        else {
            document.getElementById(who).style.height = "0px";
            clearTimeout(myTime2);
            if (from.attachEvent){
                from.onclick = function(e){show(who,from,'show')};
            }
            else {
                from.setAttribute('onclick', "show('"+who+"',this,'show')");
            }
            from.innerHTML = "continue reading...";
        }
    }
}