﻿// Navigation Block Script...

// Define Namespace...

if (!WillCurson) var WillCurson = {};
if (!WillCurson.NavBlock) WillCurson.NavBlock = {};

//--------------------------------------------------------------------------------

WillCurson.NavBlock = 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.NavBlock.prototype.init = function(data, intervalArg)
{
    // Mouse Over...

    try
    {
        // Set Data Array...
        this.data = data;

        // Start Animation...
        this.animatePoll = setInterval(intervalArg, 20);
    }
    catch (e)
    {
        alert("NavBlock.init Error: " + e.message);
    }
};

//--------------------------------------------------------------------------------

WillCurson.NavBlock.prototype.mouseOver = function(node, imageURL)
{
    // Mouse Over...

    try
    {
        // Set Cursor...
        node.style.cursor = "pointer";

        // Get Nodes Data Array Index...
        var index = this.getNodeIndex(node.id);
        
        // Set Image....
        
        if (this.isIE8)
        {
            document.getElementById(this.data[index][5]).style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imageURL + "', sizingMethod='scale')";
        }
        else
        {
            document.getElementById(this.data[index][5]).src = imageURL;
        }

        // Set Fading In...
        this.data[index][1] = true;

        // Set Start Opacity...
        this.data[index][3] = parseFloat(document.getElementById(this.data[index][2]).style.opacity) * 100;

        // Set Start Time...
        this.data[index][4] = new Date().getTime();
    }
    catch (e)
    {
        alert("NavBlock.mouseOver Error: " + e.message);
    }
};

//--------------------------------------------------------------------------------

WillCurson.NavBlock.prototype.mouseOut = function(node, imageURL)
{
    // Mouse Out...

    try
    {
        // Set Cursor...
        node.style.cursor = "default";

        // Get Nodes Data Array Index...
        var index = this.getNodeIndex(node.id);
            
        // Set Image...
            
        if (this.isIE8)
        {
            document.getElementById(this.data[index][5]).style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imageURL + "', sizingMethod='scale')";
        }
        else
        {
            document.getElementById(this.data[index][5]).src = imageURL;
        }

        // Set Fading In...
        this.data[index][1] = false;

        // Set Start Opacity...
        this.data[index][3] = parseFloat(document.getElementById(this.data[index][2]).style.opacity) * 100;

        // Set Start Time...
        this.data[index][4] = new Date().getTime();
    }
    catch (e)
    {
        alert("NavBlock.mouseOut Error: " + e.message);
    }
};

//--------------------------------------------------------------------------------

WillCurson.NavBlock.prototype.animate = function()
{
    // Animate...

    try
    {
        // Cycle Through Data Array...

        var index = 0;

        /*
        
        Nav Ribbon Link Data Array Entry...
        
        data[index][0] -> Mask Image Client ID...
        data[index][1] -> Fade In Boolean Flag...
        data[index][2] -> Label Container Div Client ID...
        data[index][3] -> Starting Opacity...
        data[index][4] -> Start Time...
        data[index][5] -> Main Image Icon Client ID...

        */

        for (index = 0; index < this.data.length; index++)
        {
            // Set Node To Nav Ribbon Link Div..
            var node = document.getElementById(this.data[index][0]).parentNode.parentNode;

            // Ensure Node Is Not Null...

            if (node != null)
            {
                // Get The Nav Block Links Current Opacity...

                var nodeOpacity = parseFloat(document.getElementById(this.data[index][2]).style.opacity) * 100;
                
                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][4];

                    // f Value...
                    var f = Math.abs(Math.sin(elapsedTime * freq));

                    // Mamimum Displacement Distance...
                    var disp = 100 - this.data[index][3];

                    // Calculate Opacity...
                    var opacity = Math.round(f * disp + parseInt(this.data[index][3]))

                    // Set Opacity...
                    document.getElementById(this.data[index][2]).style.opacity = (opacity / 100);

                    if (this.isIE8)
                    {
                        document.getElementById(this.data[index][2]).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][4];

                    // f Value...
                    var f = Math.abs(Math.sin(elapsedTime * freq));

                    // Mamimum Displacement Distance...
                    var disp = 0 - this.data[index][3];

                    // Calculate Opacity...
                    var opacity = Math.round(f * disp + parseInt(this.data[index][3]))

                    // Set Opacity...
                    document.getElementById(this.data[index][2]).style.opacity = (opacity / 100);

                    if (this.isIE8)
                    {
                        document.getElementById(this.data[index][2]).style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + opacity + ")";
                    }
                }
            }
        }
    }
    catch (e)
    {
        alert("NavBlock.animate Error: " + e.message);
    }
};

//--------------------------------------------------------------------------------

WillCurson.NavBlock.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("NavBlock.getNodeIndex Error: " + e.message);
    }
};

//--------------------------------------------------------------------------------

