﻿var bobFlash = null;
var bobActionFlash = null;
var bobFirstAction = 0;
var bobDoingAction = false;
var BobLastAction = '';
var playBobAnimationTimer = null;
var bobsRandomActions = new Array("fix_tie", "switch_legs", "fix_glasses", "sign_completely", "sign_never", "yawn", "whistle", "cough", "points_left", "thinking", "coupons", "services", "savings-bubble", "bills", "samples");
var bobActionRandom;
var bobMouseActionRandom;
var bob_EnterDiv = "";
var bob_ActionDiv = "";
var BOB_ENTER = "Enter";
var bobActionFlashLoaded = false;
var PageFocus = true;

function StartContactAction()
{
    bobFlash.TGotoFrame("Bob", 0);
    bobFlash.TGotoLabel("Bob", "BobEnter");
    bobFlash.TPlay("Bob");
}

function StopContactAction()
{
    bobFlash.TGotoFrame("Bob", 0);
    bobFlash.TGotoLabel("Bob", "BobEnter");
    bobFlash.TStopPlay("Bob");
}
//Intro animation function on homepage

function textlinksBack()
{
    document.getElementById("BobDIV").style.zIndex = 3;
}
function textlinksFront()
{
    document.getElementById("BobDIV").style.zIndex = 1;
}
function Savingsbubble()
{
    document.getElementById("verticlas-links").style.zIndex = 5;
}
function CancelSavingsbubble()
{
    document.getElementById("verticlas-links").style.zIndex = 2;
}

//drop down list function on homepage
function startList()
{
    if (document.all && document.getElementById)
    {
        navRoot = document.getElementById("nav");
        for (i = 0; i < navRoot.childNodes.length; i++)
        {
            node = navRoot.childNodes[i];
            if (node.nodeName == "LI")
            {
                node.onmouseover = function ()
                {
                    this.className += " over";
                }
                node.onmouseout = function ()
                {
                    this.className = this.className.replace(" over", "");
                }
            }
        }
    }
}
window.onload = startList;

//End drop down list function on homepage

function InitBobObject()
{
    bobDoingAction = false;

    if (!bobFlash && (BrowserDetectName == FIREROX_BROWSER || BrowserDetectName == CHROME_BROWSER))
    {
        bobFlash = document.getElementById("BobMovie");
        bobActionFlash = document.getElementById("BobAction");
    }
    else if (!bobFlash && BrowserDetectName == "Mozila")
    {
        bobFlash = document.getElementById("BobMovie");
        bobActionFlash = document.getElementById("BobAction");
    }
    else
    {
        bobFlash = document.getElementById("BobMovieIE");
        bobActionFlash = document.getElementById("BobActionIE");
    }

    if (!bobFlash)
    {
        ChangeBobFlash();
        //SetFooter();
        BobActionFinish();
    }

    SetBobEyeRolling();
}

//To stop intro animation when page is out of focus
function onWindowBlur()
{
    PageFocus = false;

    bobDoingAction = false;

    if (bobFirstAction == 0)
        ChangeBobFlash();

    SetBobTimers();
}
//End

function onWindowFocus()
{
    PageFocus = true;
    SetBobTimers();

    //ContactPage
    if (document.getElementById('BobContactDiv'))
        StartContactAction()
}

function SetWindowFocus()
{
    if (BrowserDetectName != CHROME_BROWSER)
    {
        PageFocus = document.hasFocus();
    }
    return PageFocus;
}

function PlayBobAnimation(animationName, randomAction)
{
    if (animationName == BOB_ENTER) // No need to start animation for Enter command for the homepage
        return;

    if (bobActionRandom)
        clearTimeout(bobActionRandom);

    if (bobMouseActionRandom)
        clearTimeout(bobMouseActionRandom);

    if (!bobFlash && !bobActionFlash)
    {
        InitBobObject();
    }

    if (animationName == BOB_ENTER || randomAction == true)
        playBobAnimationTimer = setTimeout("SetBobFlashAnimation('" + animationName + "')", 1500);
    else
        playBobAnimationTimer = setTimeout("SetBobFlashAnimation('" + animationName + "')", 500);
}

function CanelBobAnimation()
{
    document.getElementById("BobDIV").style.zIndex = 1;
    if (playBobAnimationTimer)
        clearTimeout(playBobAnimationTimer);

    BobLastAction = "";

    if (bobActionFlash && bobActionFlashLoaded)
    {
        try
        {
            bobActionFlash.TGotoFrame("Bob/BobActions", 0);
        }

        catch (Error)
        {
        }

        BobActionFinish();
    }
}

function SetBobFlashAnimation(animationName)
{
    try
    {
        if (!bobDoingAction && animationName == BOB_ENTER)
        {
            bobDoingAction = true;
            bobFlash.TGotoFrame("Bob", 0);
            bobFlash.TGotoLabel("Bob", "BobEnter");
            bobFlash.TPlay("Bob");
            BobLastAction = animationName;
        }

        else if (bobFirstAction == 1 && BobLastAction != animationName)
        {
            document.getElementById("BobDIV").style.zIndex = 2;
            bobDoingAction = true;
            bobActionFlash.TGotoLabel("Bob", "BobActions");
            bobActionFlash.TGotoLabel("Bob/BobActions", animationName);
            bobActionFlash.TPlay("Bob/BobActions");
            BobLastAction = animationName;
        }
    }
    catch (err)
    {
        bobDoingAction = false;
        bobFlash = null;
        setTimeout("SetBobFlashAnimation('" + animationName + "')", 500);
    }
}

function ChangeBobFlash()
{
    BobLastAction = "";
    bobFirstAction = 1;

    if (document.getElementById("BobActionDIV") != null)
    {
        document.getElementById("BobActionDIV").style.display = "block";
    }
    //SetFooter();

    if (BrowserDetectName == EXPLORER_BROWSER)
        FinishLoadActions();
}

function BobActionFinish()
{
    bobDoingAction = false;
    document.getElementById("BobDIV").style.zIndex = 1;

    if (bobFirstAction == 0)
        ChangeBobFlash();

    SetBobTimers();
}

function SetBobTimers()
{
    if (bobActionRandom)
        clearTimeout(bobActionRandom);

    if (bobMouseActionRandom)
        clearTimeout(bobMouseActionRandom);

    if (SetWindowFocus())
    {
        bobActionRandom = setTimeout("SetBobDoAnyAction()", 15000);
        bobMouseActionRandom = setTimeout("SetBobDoAnyActionMouse()", 5000);
    }
}

function SetBobDoAnyAction()
{
    if (SetWindowFocus())
    {
        var randomNum;
        randomNum = Math.floor(Math.random() * bobsRandomActions.length + 1);
        PlayBobAnimation(bobsRandomActions[randomNum], true);
    }
}

function SetBobDoAnyActionMouse()
{
    if (SetWindowFocus())
    {
        var randomNum;
        randomNum = Math.floor(Math.random() * bobsRandomActions.length + 1);
        PlayBobAnimation(bobsRandomActions[randomNum], true);
    }
}

function SetBobEyeRolling()
{
    document.onmousemove = function ()
    {
        if (bobFlash)
        {
            if (bobMouseActionRandom && !bobDoingAction)
            {
                clearTimeout(bobMouseActionRandom);
                bobMouseActionRandom = setTimeout("SetBobDoAnyActionMouse()", 5000);
            }
        }
    }
}

function FinishLoadActions()
{
    bobActionFlashLoaded = true;
    if (document.getElementById("BobEnterDIV"))
        document.getElementById("BobEnterDIV").style.display = "none";

    SetBobTimers();
}
