﻿// Copyright (C) 2007 XtractPro (Data Extraction Magazine)
// http://www.xtractpro.com, support@xtractpro.com
//
// This code is provided AS IS, with NO WARRANTY expressed or implied.
// Any use of this free open source code is at your own risk.
//
// You are hereby granted the right to use it and change it,
// provided that you acknowledge XtractPro somewhere in your
// source files, documentation, web site and application.

// current animated docking panel content element
var crtDockContent = null;

function ToggleDockingPanel(ctrlID)
{
	var content = document.getElementById(ctrlID + "_content");
	var chevron = document.getElementById(ctrlID + "_chevron");
	if (typeof(content) != "undefined" && typeof(chevron) != "undefined")
	{
        var expand = (content.style.display=="none");
        content.style.display = (expand ? "block" : "none");
        ToggleDockingPanelChevron(chevron);
	}
}
 
function ToggleDockingPanelChevron(chevron)
{
    var src = chevron.src.toString();
    if (src.indexOf("to-top")>0)
        chevron.src = src.split("to-top").join("to-bottom");
    else if (src.indexOf("to-bottom")>0)
        chevron.src = src.split("to-bottom").join("to-top");
    else if (src.indexOf("to-left")>0)
        chevron.src = src.split("to-left").join("to-right");
    else if (src.indexOf("to-right")>0)
        chevron.src = src.split("to-right").join("to-left");

    return (src.indexOf("to-top")>0 || src.indexOf("to-bottom")>0);
}

function ToggleDockingPanel4(ctrlID, group)
{
	var content = document.getElementById(ctrlID + "_content");
	var chevron = document.getElementById(ctrlID + "_chevron");
	if (typeof(content) != "undefined" && typeof(chevron) != "undefined")
	{
        var expand = (content.style.display=="none");
        if (expand)
            CollapseAllBut(ctrlID, group);
        content.style.display = (expand ? "block" : "none");
        ToggleDockingPanelChevron(chevron);
	}
}

function ToggleDockingPanel3(ctrlID, expand)
{
	var content = document.getElementById(ctrlID + "_content");
	var chevron = document.getElementById(ctrlID + "_chevron");
	if (typeof(content) != "undefined" && typeof(chevron) != "undefined"
	    && (expand && content.style.display=="none"
        || !expand && content.style.display!="none"))
	{
        content.style.display = (expand ? "block" : "none");
        ToggleDockingPanelChevron(chevron);
	}
}

function CollapseAllBut(ctrlID, group)
{
    if (group==null || group.length==0)
        return;
        
    var groups = document.getElementsByTagName("input");
    for (var i=0; i<groups.length; i++)
        if (groups[i].type=="hidden"
            && groups[i].value=="docking_group_" + group
            && groups[i].parentNode.id!=ctrlID)
            ToggleDockingPanel3(groups[i].parentNode.id, false);
}

function ToggleDockingPanel2(ctrlID, group, interval, step)
{
	var content = document.getElementById(ctrlID + "_content");
	var chevron = document.getElementById(ctrlID + "_chevron");
    
    // wait for another animated expand/collapse action to end
    if (typeof(content) != "undefined"
        && typeof(chevron) != "undefined"
        && crtDockContent == null)
    {
        crtDockContent = content;
        var expand = (content.style.display=="none");
        if (expand)
        {
            CollapseAllBut(ctrlID, group);
            content.style.display = "block";
        }
        
        var vert = ToggleDockingPanelChevron(chevron);
        var max_height = (vert ? content.offsetHeight : content.offsetWidth);
        var step_height = step + (expand ? 0 : -max_height);

        // schedule first animated collapse/expand event
        if (vert)
            content.style.height = Math.abs(step_height) + "px";
        else
            content.style.width = Math.abs(step_height) + "px";
        setTimeout("ToggleDockingPanelAnimation("
            + interval + "," + step + "," + max_height + ","
            + step_height + "," + vert + ")", interval);
    }
}
function ToggleDockingPanelAnimation(interval, step, max_height, step_height, vert)
{
    if (crtDockContent==null)
        return;
    
    var step_height_abs = Math.abs(step_height);

    // schedule next animated collapse/expand event
    if (step_height_abs>=step && step_height_abs<=(max_height-step))
    {
        step_height += step;
        if (vert)
            crtDockContent.style.height = Math.abs(step_height) + "px";
        else
            crtDockContent.style.width = Math.abs(step_height) + "px";
        setTimeout("ToggleDockingPanelAnimation("
            + interval + "," + step + "," + max_height + ","
            + step_height + "," + vert + ")", interval);
    }
    // animated expand/collapse done
    else
    {
        if (step_height_abs<step)
            crtDockContent.style.display = "none";
        if (vert)
            crtDockContent.style.height = "";
        else
            crtDockContent.style.width = "";
        crtDockContent = null;
    }
}

