/**
 * @author alexanderfried
 */
//time in millisec = duration
var time = 1;
//difference
var difference = 8;
var minSize = 0;
var maxSizePerEl = 21;//pro element
var activeElement = "";

/**maximises the menu without time delay
 *
 * @param {Object} id
 */
function activeMenu(menu){
    var id = "item" + menu;
    shrinkOthers(id);
}


/**
 * resizes the menu. if the size is already maximum then resize to the minumum size (and vice versa).
 */
function XresizeMenu(id){
    var sizeInt = getHeight(id);
    document.getElementById(id).style.display = "block";
    
    //depending on the size of the menu, either shrink or enlarge the menu
    if (sizeInt > 40) 
        shrinkMenu(id);
     else 
        enlargeMenu(id);
    
    shrinkOthers(id);
}

var showitem1=0;
var showitem2=0;
var showitem3=0;

var redrawtimer=null;

function redraw_real()
{
    if (showitem1==1) enlargeMenu("item1"); else shrinkMenu("item1");
    if (showitem2==1) enlargeMenu("item2"); else shrinkMenu("item2");
    if (showitem3==1) enlargeMenu("item3"); else shrinkMenu("item3");
    if (showitem1+showitem2+showitem3==0) document.getElementById("navdiv_under").style.display="block";
    else document.getElementById("navdiv_under").style.display="none";
}

function redraw()
{
    if (redrawtimer) clearTimeout(redrawtimer);
    redrawtimer=setTimeout(redraw_real,300);
}

/* deactivate menu "id" */
function deactivateMenu(id)
{
    if (id=='item1') showitem1=0; 
    if (id=='item2') showitem2=0;
    if (id=='item3') showitem3=0;
    redraw();
}

/* activate menu "id", shrink the others */
function activateMenu(id)
{
    if (id=='item1') { showitem1=1; showitem2=0; showitem3=0; }
    if (id=='item2') { showitem1=0; showitem2=1; showitem3=0; }
    if (id=='item3') { showitem1=0; showitem2=0; showitem3=1; }
    redraw();
}

/**
 * shrinks the menu to the normal size.
 */
function shrinkMenu(id){
    var sizeInt = getHeight(id);

    sizeInt = sizeInt - difference;
    if (sizeInt < minSize) 
        sizeInt = minSize;
    
    document.getElementById(id).style.height = sizeInt + "px";
    
    //recursive call of method with time delay
    if (sizeInt > minSize) {
        window.setTimeout("shrinkMenu('" + id + "');", time);
    }
    else document.getElementById(id).style.display = "none";
}

function setActive(id){
    activeElement = id;
    shrinkOthers(id);
}

function showdebug(text) {
    document.getElementById("debug").textContent=text;
}

/**
 * enlarges the menu to the maximum size.
 */
function enlargeMenu(id){
    var num = 0;

    //anzahl der sub elemente ermitteln
    var obj = document.getElementById(id);
    var myObjColl = getElementsByClassName('subSpan', 'span', obj);
    num = myObjColl.length;
    
    var maxSize = maxSizePerEl * num;
    var sizeInt = getHeight(id);

    if (sizeInt < maxSize) {
    	sizeInt = parseInt(sizeInt) + difference;
    	obj.style.display = "block";
    	obj.style.height = sizeInt + "px";
        window.setTimeout("enlargeMenu('" + id + "');", time);
    }
    else {
    	obj.style.height = ""; // redraw needed for firefox
    }
}


/**
 * Returns the height of the element without px suffix
 * @param {Object} id identity of the element
 */
function getHeight(id){
    var size = document.getElementById(id).style.height;
    var sizeInt;
    
    if (size != "") {
        //if size is set, get size from that value
        var sizeArray = new String(size).split("px");
        sizeInt = sizeArray[0];
        
    }
    else {
    
        sizeInt = document.getElementById(id).offsetHeight;
    }
    
    
    return parseInt(sizeInt);
}

function getElementsByClassName(strClass, strTag, objContElm){
    strTag = strTag || "*";
    objContElm = objContElm || document;
    var objColl = objContElm.getElementsByTagName(strTag);
    if (!objColl.length && strTag == "*" && objContElm.all) 
        objColl = objContElm.all;
    var arr = new Array();
    var delim = strClass.indexOf('|') != -1 ? '|' : ' ';
    var arrClass = strClass.split(delim);
    for (var i = 0, j = objColl.length; i < j; i++) {
        var arrObjClass = objColl[i].className.split(' ');
        if (delim == ' ' && arrClass.length > arrObjClass.length) 
            continue;
        var c = 0;
        comparisonLoop: for (var k = 0, l = arrObjClass.length; k < l; k++) {
            for (var m = 0, n = arrClass.length; m < n; m++) {
                if (arrClass[m] == arrObjClass[k]) 
                    c++;
                if ((delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
                    arr.push(objColl[i]);
                    break comparisonLoop;
                }
            }
        }
    }
    return arr;
}

// To cover IE 5.0's lack of the push method
Array.prototype.push = function(value){
    this[this.length] = value;
}
