﻿// Lymph-Man Script...

// Define Namespace...

if (!WillCurson) var WillCurson = {};
if (!WillCurson.LymphMan) WillCurson.LymphMan = {};

//--------------------------------------------------------------------------------

WillCurson.LymphMan = function()
{
    // Globals...

    // Animation Poller...
    this.animatePoll = null;

    // Data Array..
    this.data = [];

    // Total Animation Length In Milliseconds...
    this.ANIM_LENGTH = null;

    // Flags...
    // ----------

    // Browser Is Internet Explorer...
    this.isIE8 = false;

    // ----------
};

//--------------------------------------------------------------------------------

WillCurson.LymphMan.prototype.init = function(data, intervalArg)
{
    // Initialise...

    try
    {
        // Set Data Array...
        this.data = data;

        // Start Animation...
        this.animatePoll = setInterval(intervalArg, 20);
    }
    catch (e)
    {
        alert("LymphMan.init Error: " + e.message);
    }
};

//--------------------------------------------------------------------------------

WillCurson.LymphMan.prototype.mouseOver = function(node, divID)
{
    // Mouse Over...

    try
    {
        // Get Nodes Data Array Index...
        var index = this.getNodeIndex(divID);
    
        // Set Cursor...
        node.style.cursor = "pointer";

        // Set Background Colour....
        node.style.backgroundColor = this.data[index][5];

        // Set Fading In...
        this.data[index][1] = true;

        // Set Start Opacity...
        this.data[index][2] = parseFloat(document.getElementById(this.data[index][0]).style.opacity) * 100;

        // Set Start Time...
        this.data[index][3] = new Date().getTime();
    }
    catch (e)
    {
        alert("LymphMan.mouseOver Error: " + e.message);
    }
};

//--------------------------------------------------------------------------------

WillCurson.LymphMan.prototype.mouseOut = function(node, divID)
{
    // Mouse Out...

    try
    {
        // Get Nodes Data Array Index...
        var index = this.getNodeIndex(divID);

        // Set Cursor...
        node.style.cursor = "default";

        // Set Background Colour....
        node.style.backgroundColor = this.data[index][4];

        // Set Fading In...
        this.data[index][1] = false;

        // Set Start Opacity...
        this.data[index][2] = parseFloat(document.getElementById(this.data[index][0]).style.opacity) * 100;

        // Set Start Time...
        this.data[index][3] = new Date().getTime();
    }
    catch (e) {
        alert("LymphMan.mouseOut Error: " + e.message);
    }
};

//--------------------------------------------------------------------------------

WillCurson.LymphMan.prototype.animate = function()
{
    // Animate...

    try
    {
        // Cycle Through Data Array...

        var index = 0;

        /*
        
        Lymph-Man Layer Data Array Entry...
        
        data[index][0] -> Div Client ID...
        data[index][1] -> Fade In Boolean Flag...
        data[index][2] -> Starting Opacity...
        data[index][3] -> Start Time...
        data[index][4] -> Label Colour...
        data[index][5] -> Label Highlight Colour...

        */

        for (index = 0; index < this.data.length; index++)
        {
            // Set Node To Nav Ribbon Link Div..
            var node = document.getElementById(this.data[index][0]);

            // Ensure Node Is Not Null...

            if (node != null)
            {
                // Get The Nav Wheel Links Current Opacity...

                var nodeOpacity = parseFloat(document.getElementById(this.data[index][0]).style.opacity) * 100;

                //alert(nodeOpacity);
                
                if (this.data[index][1] && (parseInt(nodeOpacity) < 100))
                {
                    // (Is Fading In And Node Opacity Is Less Than 100)...
                    // Fading In...

                    // Calculate Frequency...
                    var freq = Math.PI / (2 * this.ANIM_LENGTH);

                    // Elapsed Time...
                    var elapsedTime = new Date().getTime() - this.data[index][3];

                    // f Value...
                    var f = Math.abs(Math.sin(elapsedTime * freq));

                    // Mamimum Displacement Distance...
                    var disp = 100 - this.data[index][2];

                    // Calculate Opacity...
                    var opacity = Math.round(f * disp + parseInt(this.data[index][2]))

                    // Set Opacity...
                    document.getElementById(this.data[index][0]).style.opacity = (opacity / 100);

                    if (this.isIE8)
                    {
                        document.getElementById(this.data[index][0]).style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + opacity + ")";
                    }
                }
                else if (!this.data[index][1] && (parseInt(nodeOpacity) > 0))
                {
                    // (Is Not Fading In And Node Opacity Is Greater Than 0)...
                    // Fading Out...

                    // Calculate Frequency...
                    var freq = Math.PI / (2 * this.ANIM_LENGTH);

                    // Elapsed Time...
                    var elapsedTime = new Date().getTime() - this.data[index][3];

                    // f Value...
                    var f = Math.abs(Math.sin(elapsedTime * freq));

                    // Mamimum Displacement Distance...
                    var disp = 0 - this.data[index][2];

                    // Calculate Opacity...
                    var opacity = Math.round(f * disp + parseInt(this.data[index][2]))

                    // Set Opacity...
                    document.getElementById(this.data[index][0]).style.opacity = (opacity / 100);

                    if (this.isIE8)
                    {
                        document.getElementById(this.data[index][0]).style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + opacity + ")";
                    }
                }
            }
        }
    }
    catch (e)
    {
        alert("LymphMan.animate Error: " + e.message);
    }
};

//--------------------------------------------------------------------------------

WillCurson.LymphMan.prototype.getNodeIndex = function(nodeID)
{
    // Find Next Node ID Array Array...

    try
    {
        var index = 0;

        // Find The Index Which Corrisponds To The Node ID...

        for (index = 0; index < this.data.length; index++)
        {
            if (this.data[index][0] == nodeID)
            {
                return index;

                break;
            }
        }

        return null;
    }
    catch (e)
    {
        alert("LymphMan.getNodeIndex Error: " + e.message);
    }
};

//--------------------------------------------------------------------------------


