﻿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");
var bobActionRandom;
var bobMouseActionRandom;
var bob_EnterDiv = "";
var bob_ActionDiv = "";
var BOB_ENTER = "Enter";
var bobActionFlashLoaded= false;
var PageFocus = true;
var PageActiveElement;
var isStopAllBobActions = false;



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");
    
}


function InitBobObject()
{
    

    bobDoingAction = false;


    if (!bobActionFlash && BrowserDetectName == FIREROX_BROWSER)
    {
      
        bobActionFlash = document.getElementById("BobAction");

    }
    else if (!bobActionFlash && BrowserDetectName == "Mozilla") {
     
        bobActionFlash = document.getElementById("BobAction");
        
    }
    else {
    
     
        bobActionFlash = document.getElementById("BobActionIE");
        
    }
    
//    if(!bobFlash)
//    {
        ChangeBobFlash();
        SetFooter();
        BobActionFinish();
 //   }


     
     SetBobEyeRolling();

     
}

function StopAllBobActions()
{
    CanelBobAnimation();
    isStopAllBobActions = true;
}



var rrr = 0;
function onWindowBlur() {
    rrr++;
    //document.getElementById('spanTest').innerText = 'outfocus' + rrr;
    
    PageFocus = false;
    if (PageActiveElement != document.activeElement) 
    {
        PageActiveElement = document.activeElement;
		return;
	}

    
    if (bobActionRandom)
        clearTimeout(bobActionRandom);

    if (bobMouseActionRandom)
        clearTimeout(bobMouseActionRandom);

    //ContactPage
    if (document.getElementById('BobContactDiv'))
        StopContactAction()        
        
}


function onWindowFocus() {
    rrr++;
    //document.getElementById('spanTest').innerText = 'focus' + rrr;
    PageFocus = true;
    SetBobTimers();
    
    //ContactPage
    if (document.getElementById('BobContactDiv'))
        StartContactAction()      
}


function SetWindowFocus()
{
    PageFocus = document.hasFocus();
    
}



function PlayBobAnimation(animationName,randomAction) {


  
    
    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 = 0;
                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 (PageFocus) 
    {
        bobActionRandom = setTimeout("SetBobDoAnyAction()", 15000);
        bobMouseActionRandom = setTimeout("SetBobDoAnyActionMouse()", 5000);
    }
}

function SetBobDoAnyAction()
{
    if (PageFocus) 
    {
        var randomNum;
        randomNum = Math.floor(Math.random()* bobsRandomActions.length+1);
        PlayBobAnimation(bobsRandomActions[randomNum],true);
    }
}

function SetBobDoAnyActionMouse() 
{
    if (PageFocus) 
    {
        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);
            }
            
            //var e = arguments[0] || event;
            //var x = ((getOffset(bobFlash).left - e.clientX) * -1) - (420/3);
            //var y = ((getOffset(bobFlash).top - e.clientY) * -1) + (420/4);;
            //bobFlash.ReportMouseLoc(x ,y )
        }
    }
}


function FinishLoadActions() {

   
    bobActionFlashLoaded = true;
    if (document.getElementById("BobEnterDIV"))
        document.getElementById("BobEnterDIV").style.display = "none";
        
    SetBobTimers();
}

//function ExitDivBob()
//{   
   // if(document.getElementById("message_bg").style.display != "none")
       // clearInterval(randomNum);
  //}

//function CancelExitDivBob()
//{   
        
//}
