﻿connectionSpeed = 0;
min_speed = 2;

function drawFSImageTag(fileLocation, fileSize, imgTagProperties) {

    start = (new Date()).getTime();
    // Record Start time of <img> load.

    loc = fileLocation + '?t=' + escape(start);
    // Append the Start time to the image url
    // to ensure the image is not in disk cache.
    //alert(loc);
    document.write('<img src="' + loc + '" ' + imgTagProperties + ' onload="connectionSpeed=computeConnectionSpeed(' + start + ',' + fileSize + ');">');
    // Write out the <img> tag.

    return;
}


function computeConnectionSpeed(start, fileSize) {
    end = (new Date()).getTime();
    connectSpeed = (Math.floor((((fileSize) / ((end - start) / 1000)) / 1024) * 10) / 10);
    //alert(connectSpeed);
    return connectSpeed;
}

jQuery(document).ready(function() {

});

function bwTest(srcUrl) {
    drawFSImageTag(srcUrl + '/bw_test.jpg', 15336, 'border=0 height=0  width=0 alt="Bandwidth Test"');
    return isFast();
}


function isFast() {
    if (connectionSpeed == 0) {
        window.setTimeout("isFast()", 100);

    } else {
        if (connectionSpeed > min_speed) {
            //alert(connectionSpeed);

            doFlash();
            //document.getElementById("flashcontent").style.display = 'block';


        } else {
            if (getQueryVariable('bw_test')) {
                doFlash();
                document.getElementById("flashcontent").style.display = 'block';
                return true;
            } else {
                //set the src path for the non-flash version
                //nocache_day = new Date();
                //nocache_id = nocache_day.getTime();
                var nocache_id = Math.round(100 * Math.random());
                if (!document.getElementById('av_callout')) {
                    if (document.getElementById('fr')) {
                        document.getElementById("bwcontent").innerHTML = "<p><strong>Connexion lente</strong></p><p>On dirait que votre connexion Internet est trop lente pour que vous puissiez interagir avec notre expert en ligne.</p><p><a href=\"http://www.futureshop.ca/informationcentre/FR/faqs.asp?logon=&langid=FR\" target=\"_blank\">Vous avez besoin d'aide?</a></p>";
                    } else {
                        document.getElementById("bwcontent").innerHTML = "<p><strong>Slow connection</strong></p><p>It looks like your internet connection is too slow for you to interact with our online expert.</p><p><a href=\"http://www.futureshop.ca/informationcentre/EN/faqs.asp?logon=&langid=EN\" target=\"_blank\">Need help?</a></p>";
                    }
                    document.getElementById("bwcontent").style.display = 'block';
                } else {
                    if (document.getElementById('callout_search')) {
                        document.getElementById('callout_search').style.display = 'block';
                    }
                }
                return false;
            }
        }

    }
}







