﻿var bandwidthSpeed = new Array()

//********************************
function checkBandwidth(){
    
    var flashDetect = document.getElementById("nav")
    currentKbps = new Number(flashDetect.GetVariable("connectionKbps"))

    if (currentKbps != 0 && currentKbps !=-1 && !isNaN(currentKbps)){
        
        
        bandwidthSpeed.push(currentKbps)


        if (bandwidthSpeed.length > 2) {
            var total = 0
            var minSpeed = bandwidthSpeed[0]
            var factorLength = 0
            
            for (i=0;i<bandwidthSpeed.length;i++){                                                
                if (bandwidthSpeed[i] < minSpeed) minSpeed = bandwidthSpeed[i]
            }
            

            for (i=0;i<bandwidthSpeed.length;i++){                
                if (bandwidthSpeed[i] <= minSpeed * 3){
                    total += bandwidthSpeed[i]
                    factorLength++                                        
                }                    
            }            
            
            var avgKbps = parseInt(total / factorLength)
            
            bandwidthDetect(avgKbps)
        }else{
            window.setTimeout("checkBandwidth()", 1500)
        }		        
    }else{
        window.setTimeout("checkBandwidth()", 1500)
    }        
}

//********************************
function bandwidthDetect(bandwidthKbps) {
    var speed = document.getElementById('__SPEED').value;    
    var videoName = document.getElementById('__NAME').value;

    var mBandwidthKbps = ""



    mBandwidthKbps = bandwidthKbps


    if(speed == ""){

        if(bandwidthKbps>500) speed = "500";
        if(bandwidthKbps<500) speed = "300";
        if(bandwidthKbps<300) speed = "200";
        if(bandwidthKbps<200) speed = "100";
        if(bandwidthKbps<100) speed = "056";
        if(bandwidthKbps<56)  speed = "000";
    }
     
    var pickup_href = document.getElementById("__PATH").value
    location.href = pickup_href + '?auto=1&speed=' + speed + '&name=' + videoName + '&kbps=' + bandwidthKbps;
}

