﻿function CF_CategoryListItemOver(CategoryItem) {
    var ChildElements = CategoryItem.getElementsByTagName("ul");
    for(var i=0; i<ChildElements.length; i++) {
        ChildElements[i].className = "CF_CategoryListMouseOver";
    }
}
function CF_CategoryListItemOut(CategoryItem) {
    var ChildElements = CategoryItem.getElementsByTagName("ul");
    for (var i = 0; i < ChildElements.length; i++) {
        ChildElements[i].className = "CF_CategoryList";
    }
}

var ShowingDescription = "";
var selectedRow;

function DisplayDescription(ShortDescriptionLabel) 
{
    var DescriptionElement = document.getElementById(ShortDescriptionLabel);
    if (ShowingDescription != "") {
        var OldDescriptionElement = document.getElementById(ShowingDescription);
        OldDescriptionElement.style.display = "none";
    }
    if(selectedRow != null) {
		selectedRow.className = "";
    }
    
    DescriptionElement.style.display = "block";
    selectedRow = document.getElementById("row" + ShortDescriptionLabel);
    if(selectedRow != null) {
		selectedRow.className = "SelectedItem";
    }
    ShowingDescription = ShortDescriptionLabel;
}

var Expanded = false;
var Expanding = false;
var ExpandTimer = 0;
var CollapseTimer = 0;

var scaleStep = 10;
var scaleMinHeight = 20;
var scaleMaxHeight = 180;
var scaleSpeed = 10;    //Delay between steps, larger steps increase speed, lower delay increases speed but increases load and increases smoothness
var scaleObjectId = "ScalingTable";
var scaleHiddenRowClass = "NotSelectedItem";

function ToggleExpand() {
    if (Expanded) {
        StartCollapse(false);
    } else {
        StartExpand(false);
    }
}

function StartExpand(useTimer) {
    if (ExpandTimer == 0 && !Expanded) {
        if (CollapseTimer != 0) {
            clearTimout(CollapseTimer);
            CollapseTimer = 0;
        } else {
            if (useTimer) {
                ExpandTimer = setTimeout('Expand()', 300);
            } else {
                Expand();
            }
        }
    }
}
function CancelExpand() {
    if (ExpandTimer != 0) {
        clearTimeout(ExpandTimer);
        ExpandTimer = 0;
    }
}
function Expand() 
{
    ExpandTimer = 0;
    Expanded = true;
    var Scalingdiv = document.getElementById(scaleObjectId);
    if (Scalingdiv != null) {
        ExpandAnimation((scaleMinHeight+scaleStep));
        var AllRows = Scalingdiv.getElementsByTagName("TR");
        for (var i = 0; i < AllRows.length; i++) {
            var Row = AllRows[i];
            if (Row.className == scaleHiddenRowClass) {
                Row.style.display = "block";
            }
        }
    } else {
        alert("Error, could not find table to scale");
    }
}
function ExpandAnimation(newHeight) {
    var Scalingdiv = document.getElementById(scaleObjectId);
    if (Scalingdiv != null) {
        Scalingdiv.style.height=newHeight+"px";
    }
    if (newHeight < scaleMaxHeight) {
        Expanding = true;
        setTimeout("ExpandAnimation(" + (newHeight + scaleStep) + ")", scaleSpeed);
    } else {
        Expanding = false;
    }
}

function StartCollapse(useTimer) {
    if (CollapseTimer == 0 && Expanded && !Expanding) {
        if (ExpandTimer != 0) {
            clearTimeout(ExpandTimer);
            ExpandTimer = 0;
        } else {
            if (useTimer) {
                CollapseTimer = setTimeout('Collapse()', 1000);
            } else {
                Collapse();
            }
        }
    }
}
function Collapse() {
    Expanded = false;
    CollapseTimer = 0;
    var Scalingdiv = document.getElementById(scaleObjectId);
    if (Scalingdiv != null) {
        CollapseAnimation((scaleMaxHeight-scaleStep));
        var AllRows = Scalingdiv.getElementsByTagName("TR");
        for (var i = 0; i < AllRows.length; i++) {
            var Row = AllRows[i];
            if (Row.className == scaleHiddenRowClass) {
                Row.style.display = "none";
            }
        }
    } else {
        alert("Error, could not find table to scale");
    }
}
function CollapseAnimation(newHeight) {
    var Scalingdiv = document.getElementById(scaleObjectId);
    var ParentElement = document.getElementById("CONTAINER");
    if (Scalingdiv != null) {
        Scalingdiv.style.height = newHeight + "px";
    }
    if (newHeight > scaleMinHeight) {
        setTimeout("CollapseAnimation(" + (newHeight - scaleStep) + ")", scaleSpeed);
    }
}





function toggleBoxSize(id, buttonId, collapsedHeight, expandedHeight, collapseScript, expandScript) {

	var boxContentDiv = document.getElementById(id);
	var expandButtonDiv = document.getElementById(buttonId);

	if(boxContentDiv.clientHeight <= collapsedHeight) {
		expandButtonDiv.className = 'expandCollapseButton expanded';
		expandBox(id, expandedHeight, expandScript);
	}
	else if (boxContentDiv.clientHeight >= expandedHeight) {
		expandButtonDiv.className = 'expandCollapseButton collapsed';
		collapseBox(id, collapsedHeight, collapseScript);
	}
	
}


function expandBox(id, targetHeight, expandScript) {
	
	var boxContentDiv = document.getElementById(id);
	var currentHeight = parseInt(boxContentDiv.style.height); //boxContentDiv.clientHeight;

	var nextHeight = currentHeight + ((targetHeight - currentHeight) / 2) ;
	nextHeight = Math.ceil(nextHeight);
	
	if(nextHeight > targetHeight) nextHeight = targetHeight;
	
	boxContentDiv.style.height = (nextHeight) + 'px';

	eval(expandScript);

	if(boxContentDiv.clientHeight < targetHeight) {
		setTimeout("expandBox('" + id + "', " + targetHeight + ", '" + expandScript + "')", 10);
	}

}

function collapseBox(id, targetHeight, collapseScript) {

	var boxContentDiv = document.getElementById(id);
	var currentHeight = parseInt(boxContentDiv.style.height); //  boxContentDiv.clientHeight;

	var nextHeight = targetHeight + ((currentHeight - targetHeight) / 4);
	nextHeight = Math.floor(nextHeight);
	
	if(nextHeight < targetHeight) nextHeight = targetHeight;

	boxContentDiv.style.height = (nextHeight) + 'px';

	eval(collapseScript);

	if(boxContentDiv.clientHeight > targetHeight) {
		setTimeout("collapseBox('" + id + "', " + targetHeight + ", '" + collapseScript + "')", 10);
	}
	
	
}