﻿var _cnSpeed = 0;
var _timerStart;

//  Called on page load to determine if we have a fast-enough connection to 
//  load the flash.
function LoadFlash() {
    //  Check to see if we have a saved cookie for connection speed.
    _cnSpeed = GetCookie("cnSpeed");
    
    if(_cnSpeed != null) {
        WriteFlashTag();
        return;
    }
    
    //  If we have no cookie we call the server to test the connection speed to 
    //  see if we should load the flash.
    TimeConnectionSpeed();
}

//  Request a chunk of data from the server and see how long it takes to get 
//  it back.
function TimeConnectionSpeed() {
    _timerStart = new Date().getTime();
    DoSyncCallback("SendTimingData", "PostSendTimingData");
}

//  Callback return.
function PostSendTimingData(rvalue) {
    var timerEnd = new Date().getTime();
    _cnSpeed = (Math.floor((((rvalue.length * 8) / ((timerEnd - _timerStart) / 1000)) / 1024) * 10) / 10);
    
    //  Save off a cookie so we only fetch the chunk of data the first time the
    //  page loads.  No sense in loading 20k of data every time they hit the 
    //  home page.
    SaveCookie("cnSpeed", _cnSpeed, 2);
    WriteFlashTag();
}

//  Dynamically load the flash <embed> tag.
function WriteFlashTag() {
    if(_cnSpeed < 225.0)
        return;
        
    //  If there is no flash player installed on the client, then also do 
    //  nothing, gracefully.
    if(DetectFlashVer(7, 0, 0) == false)
        return;
        
    pnlBanner.innerHTML = "<embed src='flash/WebHeader2.swf' width='100%' height='180' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer'" +
        "type='application/x-shockwave-flash' wmode='transparent' style='z-index: -5000;'></embed>";
}
