﻿var divArray = new Array();

var mouseItemOver = function (element)
{
    setElementHi(element);
    element.style.cursor='pointer';
}

var mouseItemOut = function (element)
{
    setElementLo(element);
    element.style.cursor='default';
}

function setElementHi(element)
{
    element.style.backgroundImage="url(../files/sys-gra-gradientGreen.png/$file/gradientGreen.png)";
    element.style.backgroundRepeat="repeat-x";
    element.style.backgroundColor='#DAEEE6';
}

function setElementLo(element)
{
    if(!divArray[element.id].active && !divArray[element.id].clip.active)
    {
        element.style.backgroundColor='#FFFFFF';
    }
    else
    {
        element.style.backgroundColor='#DAEEE6';
    }
    element.style.cursor='default';
    element.style.backgroundImage="";
}

var setClicked = function (element)
{
    if(divArray[element.id].active)
    {
        setPlus(element.firstChild);
		element.style.color="#000000"
    }
    else
    {
        setMinus(element.firstChild);
		if(!isParamInURL("OpenDocument"))
		{
	        element.style.backgroundColor='#DAEEE6';
	        element.style.backgroundImage="";
	        element.style.visibility = "visible";
			element.style.color="#088A55";
		}
    }
    if(document.getElementById("ct_"+element.id) == null && !isParamInURL("OpenDocument"))
        disableOtherNodes(divArray[element.id].parent, divArray[element.id].level, element.id);
    divArray[element.id].active = !divArray[element.id].active;
}

var disableOtherNodes = function (cid, level, id)
{
    var container = document.getElementById("ct_"+cid);
    for(var i=0; i < container.childNodes.length; i++)
    {
        var node = container.childNodes[i];
        if(node.nodeType == 1 && node.className == "level"+level && node.id != id)
        {
            if(document.getElementById("ct_"+node.id) == null)
            {
                if(divArray[node.id].active)
                {
                    divArray[node.id].active = false;
                    node.style.backgroundColor = "#ffffff";
					node.style.color="#000000";
                }
            }
        }
    }
}

var synchronizeDivs = function ()
{
    var a = new Array();
    for(var d in divArray)
    {
        a.push(d);
    }
    for(var i=a.length-1; i > 0; i--)
    {
        var div = divArray[a[i]];
        if(div.active)
            if(div.container != null && document.getElementById("ct_"+a[i]) != null)
            {
                if(!divArray[a[i]].clip.active)
                {
                    document.getElementById(a[i]).style.height = document.getElementById("ct_"+a[i]).offsetHeight-11 + "px";
                    
                }
            }
    }
    for(var i=0; i < a.length; i++)
    {
        var div = divArray[a[i]];
        if(div.active)
            if(div.container != null && document.getElementById("ct_"+a[i]) != null)
            {
                if(div.clip.active)
                {
                    div.clip.right = document.getElementById("ct_"+a[i]).offsetWidth;
                }
                setHeight(div.id, div.id);
                if(div.level != 2)
                {
                    document.getElementById("ct_"+a[i]).style.top = document.getElementById(a[i]).offsetTop-1 + "px";
                }
                else
                {
                    document.getElementById("ct_"+a[i]).style.top = document.getElementById(a[i]).offsetTop + "px";
                }
            }
    }
    
}

var setHeight = function (id, or)
{
    if(id != 'top')
    {
        if(id == or && divArray[id].clip.bottom-12 > 0)
        {
			document.getElementById(id).style.height = divArray[id].clip.bottom-12+"px";
        }
        else
        {
            if(divArray[id].active)
            {
                document.getElementById(id).style.height = document.getElementById("ct_"+id).offsetHeight-12+"px";
            }
        }
        setHeight(divArray[id].parent, or);
    }
}

var setDisabled = function (e)
{
    if(e.className != "container")
    {
        setPlus(e.firstChild);
        e.style.backgroundColor='#FFFFFF';
        e.style.backgroundImage="";
        e.style.height="auto";
		e.style.color="#000000";
        e.style.display = "none";
        divArray[e.id].active = false;
        divArray[e.id].clip.bottom = -1;
        divArray[e.id].clip.right = -1;
        divArray[e.id].opacity = 1;
        if(document.getElementById("ct_"+e.id) != null)
            e.onclick = function () {mouseItemClickOpen(this)};
        synchronizeDivs();
    }
}


var setMinus = function (e)
{
    if(e.className=="divImageContainer")
    {
        e.style.backgroundImage = "url(../files/sys-gra-img_minus_satt.jpg/$file/img_minus_satt.jpg)";
    }
}

var setPlus = function (e)
{
    if(e.className=="divImageContainer")
    {
        e.style.backgroundImage = "url(../files/sys-gra-img_plus_satt.jpg/$file/img_plus_satt.jpg)";
    }
}

var mouseItemClickOpen = function (e)
{
    var container = document.getElementById("ct_"+e.id);
    var height=0;
    var i =0;
    container.style.position = "absolute";
    container.style.top = document.getElementById(e.id).offsetTop-1+"px";
    container.style.left = document.getElementById(e.id).offsetLeft+document.getElementById(e.id).offsetWidth+"px";
    for(i=0; i < container.childNodes.length; i++)
    {
        var node = container.childNodes[i];
        if(node.nodeType == 1)
        {
            node.style.display = "block";
            node.style.visibility = "visible";
            height += node.offsetHeight;
        }
    }
    divArray[e.id].textHeight = e.offsetHeight;
    divArray[e.id].height = height;
	if(!isParamInURL("OpenDocument"))
		beginClip(e.id);
    setClicked(e);
    synchronizeDivs();
}

var expandTimeInMs = 100;
var stepFrequencyInMs = 100;
var numberOfSteps = expandTimeInMs/stepFrequencyInMs;

var beginClip = function (id)
{
    if(divArray[id].clip.right == -1)
    {
        divArray[id].clip.active = true;
        divArray[id].clip.bottom = divArray[id].textHeight;
        divArray[id].clip.right = document.getElementById("ct_"+divArray[id].id).offsetWidth;
        divArray[id].opacity = 0;
        divArray[id].clip.type = "start";
        document.getElementById(id).onclick ="";
		if(document.getElementById(id).className == 'level1' && document.getElementById(parseInt(id)+1) != null)
		{
			document.getElementById(id).style.marginBottom="20px";
			document.getElementById(id).style.borderBottom="solid 1px #D0D0D0";
		}
    }
    document.getElementById("ct_"+divArray[id].id).style.clip="rect(0px "+divArray[id].clip.right+"px "+divArray[id].clip.bottom+"px 0px)";
    document.getElementById("ct_"+divArray[id].id).style.opacity = divArray[id].opacity/100;
    document.getElementById("ct_"+divArray[id].id).style.filter = "alpha(opacity="+divArray[id].opacity+", finishopacity=0)";
    if(divArray[id].clip.bottom < divArray[id].height)
    {
        var height = divArray[id].clip.bottom;
        var change = divArray[id].height - height;
        if(change > 5)
        {
            divArray[id].opacity = Math.ceil(100 * height / divArray[id].height);
            divArray[id].clip.bottom = height+Math.ceil((change/4));
        }
        else
        {
            divArray[id].clip.bottom = divArray[id].height;
            divArray[id].opacity = 100;
        }
        divArray[id].timer = setTimeout("beginClip("+id+")",stepFrequencyInMs);
    }
    else
    {
        clearTimeout(divArray[id].timer);
        divArray[id].clip.right = -1;
        divArray[id].clip.bottom = -1;
        divArray[id].opacity = 0;
        divArray[id].clip.active = false;
        document.getElementById("ct_"+divArray[id].id).style.filter = null;
        document.getElementById("ct_"+divArray[id].id).style.clip = "rect(0px 500px 10000px 0px)";
        document.getElementById("ct_"+divArray[id].id).style.opacity = "";
        document.getElementById(id).onclick = function () {mouseItemClickClose(this)};
        return true;
    }
	synchronizeDivs();
}

var endClip = function (id)
{
    if(divArray[id].clip.right == -1)
    {
        divArray[id].clip.bottom = divArray[id].height;
        divArray[id].clip.right = document.getElementById("ct_"+divArray[id].id).offsetWidth;
        divArray[id].opacity = 100;
        divArray[id].clip.active = true;
        divArray[id].clip.type = "end";
        document.getElementById(id).onclick ="";
		if(document.getElementById(id).className == 'level1' && document.getElementById(parseInt(id)+1) != null)
		{
			document.getElementById(id).style.marginBottom="0px";
			document.getElementById(id).style.borderBottom="none";
		}
    }
    document.getElementById("ct_"+divArray[id].id).style.clip="rect(0px "+divArray[id].clip.right+"px "+divArray[id].clip.bottom+"px 0px)";
    document.getElementById("ct_"+divArray[id].id).style.opacity = divArray[id].opacity/100;
    document.getElementById("ct_"+divArray[id].id).style.filter = "alpha(opacity="+divArray[id].opacity+", finishopacity=0)";
    if(divArray[id].clip.bottom > 5)
    {   
        var height = divArray[id].clip.bottom;
        var change = -10 - height;
        divArray[id].opacity = Math.ceil(100 * height / divArray[id].height);
        divArray[id].clip.bottom = height+Math.ceil((change/4));
        if(height > divArray[id].textHeight)
        {
            document.getElementById(id).style.height = divArray[id].clip.bottom+"px";
        }
        else
        {
            document.getElementById(id).style.height = (divArray[id].textHeight-10)+"px";
        }
        divArray[id].timer = setTimeout("endClip("+id+")",stepFrequencyInMs);
    }
    else
    {
        divArray[id].clip.right = -1;
        divArray[id].clip.bottom = -1;
        divArray[id].clip.active = false;
        divArray[id].opacity = 0;
        clearTimeout(divArray[id].timer);
        var container = document.getElementById("ct_"+id);
        disableAllNodes(container);
        document.getElementById(id).onclick = function () {mouseItemClickOpen(this)};
        document.getElementById("ct_"+divArray[id].id).style.filter = null;
        document.getElementById("ct_"+divArray[id].id).style.clip = "rect(0px 500px 10000px 0px)";
        document.getElementById("ct_"+divArray[id].id).style.opacity = "";
        //mouseItemOut(document.getElementById(divArray[id].id));
        document.getElementById(divArray[id].id).style.backgroundColor='#FFFFFF';
        for(var i=0; i < container.childNodes.length; i++)
        {
            var node = container.childNodes[i];
            if(node.nodeType == 1)
            {
                node.style.display = "none";
                node.style.visibility = "hidden";
            }
        }
        return true;
    }
	synchronizeDivs();
}

var mouseItemClickClose = function (e)
{
    endClip(e.id);
    setClicked(e);
}

var disableAllNodes = function (e)
{
    if (e.childNodes != null) {
        for(var i=0; i < e.childNodes.length; i++)
        {
            var node = e.childNodes[i];
            if(node.nodeType == 1)
            {
                setDisabled(node);
                if((document.getElementById("ct_"+node.id) != null))
                   disableAllNodes(document.getElementById("ct_"+node.id)); 
            }
        }
    }
}
