﻿function findAction(imgID)
{
    var src = document.getElementById(imgID).src;


    if (src.toString().match("expand"))
    {
        return "expand";
    }
    else
    {
        return "collapse";
    }
}

function expand(img, div, action)
{
    var expandImg = document.getElementById(img);

    var imgCollapse = document.getElementById('imgCollapse');

    CollapseAll('all');

    if (action == "collapse")
    {
        return;
    }

    var currentDiv = document.getElementById(div);
    if (currentDiv != null)
    {
        expandImg.src = imgCollapse.src;
        expandImg.alt = "Collapse this section";
        expandImg.title = "Collapse this section";
        currentDiv.style.display = "block";
    }

}

function CollapseAll(id)
{
    
    var strHidden = "none";
    var strExpand = "expandedDiv";
    var altExpand = "Expand this section";
    var srcExpand = "../images/expand.png";

    if (id == 'all' || id == 'divSupportAndShare')
    {
        var divSupport = document.getElementById('divSupportAndShare');
        divSupport.style.display = strHidden;
        
        var imgSupport = document.getElementById('imgExpandSupportAndShare');
        imgSupport.alt = altExpand;
        imgSupport.title = altExpand;
        imgSupport.src = srcExpand;

        var span = document.getElementById('spanMoreSupport');
        span.style.display = "inline";
    }

    if (id == 'all' || id == 'divBackgroundPapers')
    {
        var divPapers = document.getElementById('divBackgroundPapers');
        divPapers.style.display = strHidden;
        
        var imgPapers = document.getElementById('imgExpandBackgroundPapers');
        imgPapers.alt = altExpand;
        imgPapers.title = altExpand;
        imgPapers.src = srcExpand;
        
        var span = document.getElementById('spanMorePapers');
        span.style.display = "inline";
    }

    if (id == 'all' || id == 'divNewsAndForums')
    {
        var divNews = document.getElementById('divNewsAndForums');
        divNews.style.display = strHidden;
        
        var imgNews = document.getElementById('imgExpandNewsAndForums');
        imgNews.alt = altExpand;
        imgNews.title = altExpand;
        imgNews.src = srcExpand;
        
        var span = document.getElementById('spanMoreNews');
        span.style.display = "inline";
    }   

}


// mouseover code. (in progress)
//  pre-defined max heights.
var maxSupportHeight = 150;
var maxPapersHeight = 150;
var maxNewsHeight = 200;
var initialized = false;

function expandOnHover(img, div, action)
{
    CollapseAll('all');

    if (action == "collapse")
    { return; }

    if (!initialized)
    {
        InitializeMouseEventsOnChildren();
    }

    var currentDiv = document.getElementById(div);
    var currentImg = document.getElementById(img);
    var imgCollapse = document.getElementById('imgCollapse');
    
    if (currentDiv != null)
    {
        currentDiv.style.display = "block";

        if (currentDiv.id == "divSupportAndShare")
        {
            currentDiv.style.height = maxSupportHeight;

            var span = document.getElementById('spanMoreSupport');
            span.style.display = "none";
            if (stSupport != null)
            { clearTimeout(stSupport); }
        }
        else if (currentDiv.id == "divBackgroundPapers")
        {
            currentDiv.style.height = maxPapersHeight;
            
            var span = document.getElementById('spanMorePapers');
            span.style.display = "none";
            if (stPapers != null)
            { clearTimeout(stPapers); }
        }
        else if (currentDiv.id == "divNewsAndForums")
        {
            currentDiv.style.height = maxNewsHeight;

            var span = document.getElementById('spanMoreNews');
            span.style.display = "none";
            if (stNews != null)
            { clearTimeout(stNews); }
        }
        
        currentDiv.style.overflow = "auto"

        currentImg.src = imgCollapse.src;
        currentImg.alt = "Collapse this section";
        currentImg.title = "Collapse this section";
    }
}

var stSupport = null;
var stPapers = null;
var stNews = null;

function setCollapse(div)
{
    if (div == 'divSupportAndShare')
    {
        if (stSupport != null)
        {
            clearTimeout(stSupport);
        }
        stSupport = setTimeout("CollapseAll('divSupportAndShare');", 5000);
    }
    else if (div == 'divBackgroundPapers')
    {
        if (stPapers != null)
        {
            clearTimeout(stPapers);
        }
        stPapers = setTimeout("CollapseAll('divBackgroundPapers');", 5000);
    }
    else if (div == 'divNewsAndForums')
    {
        if (stNews != null)
        {
            clearTimeout(stNews);
        }
        stNews = setTimeout("CollapseAll('divNewsAndForums');", 5000);
    }
    else if (div == 'all')
    {
        //can't cancel this one.
        setTimeout("CollapseAll('all');", 5000);
    }

}

function InitializeMouseEventsOnChildren()
{
    ApplyMouseEventsToAllChildren('divMasterSupport');
    ApplyMouseEventsToAllChildren('divMasterPapers');
    ApplyMouseEventsToAllChildren('divMasterNews');

    initialized = true;
}

function ApplyMouseEventsToAllChildren(div)
{
    var currentDiv = document.getElementById(div);
    
    var children = currentDiv.getElementsByTagName("*");
   
    for (var i = 0; i < children.length; i++)
    {
        var child = children[i];

        child.onmouseover = currentDiv.onmouseover;
        //child.onmouseout = currentDiv.onmouseout;
    }
}