
function showBandwidthPreferencesPopup(popUpId, e) {
    var popupWidth = $("#"+popUpId).css("width");
    if(parseInt(popupWidth) > 0)
        openBandwidthPreferencesPopup(popUpId,-1*parseInt(popupWidth),0,e);
    else
        openBandwidthPreferencesPopup(popUpId,-410,0,e);
}

function showBottomAlignedBandwidthPreferencesPopup(popUpId, e) {
    var popupWidth = $("#"+popUpId).css("width");
    var popupHeight = $("#"+popUpId).css("height");
    if(parseInt(popupWidth) > 0 && parseInt(popupHeight) > 0)
        openBandwidthPreferencesPopup(popUpId,-1*parseInt(popupWidth),-1*parseInt(popupHeight),e);
    else
        openBandwidthPreferencesPopup(popUpId,-410,-135,e);
}

function loadBandwidthPreferencesPopup(popUpContentUrl, popUpId){
    var popup = document.getElementById(popUpId);
    $.ajax({
        url: popUpContentUrl,
        type:"GET",
        success: function(data, textStatus){
            popup.innerHTML = data;
        }
    });
}

function openBandwidthPreferencesPopup(popUpId, offX, offY, e){
    var eTarget = e.target ? e.target : e.srcElement;
    var myBandwidthPopup = document.getElementById(popUpId);
    x = e.clientX + offX;
    y = e.clientY + offY;
    var alignment = alignPopup(x>0 ? x : 0, y, myBandwidthPopup.clientHeight, myBandwidthPopup.style.width);
    bwPopUp.show(popUpId, alignment[0],alignment[1]);
}

function closeBandwidthPreferencesPopup(){
    bwPopUp.hide();
}

/**
* new popup menu
**/
var bwPopUp = {
  timer: null,
  activeMenuID: null,
  offX: 0,   // horizontal offset
  offY: 0,   // vertical offset
  show: function(id, xPos, yPos) {
  	clearTimeout(this.timer);
    var mnu = document.getElementById? document.getElementById(id): null;
    if (!mnu) return;
    this.activeMenuID = "bandwidthPopUp";
    this.position(mnu,xPos,yPos);
    // Log CM element when user opens bwPopup
    cmCreatePageElementTag("ChangePopup", "UserBitrateDisplay");
  },

  hide: function() {
    if (bwPopUp.timer) clearTimeout(bwPopUp.timer);
    if (this.activeMenuID && document.getElementById)
      this.timer = setTimeout("document.getElementById('bandwidthPopUp').style.display = 'none';activeMenu = '';", 200);
  },

  position: function(mnu, xPos, yPos) {
	mnu.style.left = xPos + "px";
	mnu.style.top = yPos + "px";
    this.timer = setTimeout("document.getElementById('bandwidthPopUp').style.display = 'block'", 20);
  }
};
/**
* end new popup menu
**/

function httpGet(url){
    $.ajax({
        url: url,
        type:"GET",
        error: function(data, textStatus){
            updateMyConnectionSpeed('Unknown');    
        }
    });
}

function cancelBandwidthDetect() {
    cancelBandwidthDetection();
    updateMyConnectionSpeed('Unknown');
    $("input:radio#radioButtonExtreme").removeAttr("disabled");
    $("input:radio#radioButtonPremium").removeAttr("disabled");
    $("input:radio#radioButtonHigh").removeAttr("disabled");
    $("input:radio#radioButtonMedium").removeAttr("disabled");
    $("input:radio#radioButtonLow").removeAttr("disabled");
    $("div#connectionPreferencesInput").css("opacity", "").css("filter", "");
    $("a#cancelLink").remove();
}

$(function() {
    $("a#changeBandwidthLink").click(function() {
        if($("#detectingConnectionSpeed").length > 0){
            $("div#connectionPreferencesInput").css("opacity", ".4").css("filter", "alpha(opacity=40)");
            $("div#connectionPreferencesInput").attr("class", "opacityFilter40");
            $("input:radio#radioButtonExtreme").attr("disabled", "disabled");
            $("input:radio#radioButtonPremium").attr("disabled", "disabled");
            $("input:radio#radioButtonHigh").attr("disabled", "disabled");
            $("input:radio#radioButtonMedium").attr("disabled", "disabled");
            $("input:radio#radioButtonLow").attr("disabled", "disabled");
        }
    });
});

$(function() {
    $(document).ready(function() {
        if($("#detectingConnectionSpeed").length > 0){
            setTimeout("onDetectingTimeout();", 30000);
        }
    });
});

function onDetectingTimeout() {
	if(!isBandwidthDetected()) {
		$("input:radio#radioButtonExtreme").removeAttr("disabled");
		$("input:radio#radioButtonPremium").removeAttr("disabled");
		$("input:radio#radioButtonHigh").removeAttr("disabled");
		$("input:radio#radioButtonMedium").removeAttr("disabled");
		$("input:radio#radioButtonLow").removeAttr("disabled");
		$("div#connectionPreferencesInput").css("opacity", "").css("filter", "");
		updateMyConnectionSpeed('Unknown');
        cmCreatePageElementTag("_Unknown", "UserBandwidth Auto-Detected");
    }
}

function toggleBandwidthPreferences(enable) {
	if(enable) {
		$("input:radio#radioButtonExtreme").removeAttr("disabled");
		$("input:radio#radioButtonPremium").removeAttr("disabled");
		$("input:radio#radioButtonHigh").removeAttr("disabled");
		$("input:radio#radioButtonMedium").removeAttr("disabled");
		$("input:radio#radioButtonLow").removeAttr("disabled");
		$("div#connectionPreferencesInput").css("opacity", "").css("filter", "");
    }else{
        $("div#connectionPreferencesInput").css("opacity", ".4").css("filter", "alpha(opacity=40)");
        $("div#connectionPreferencesInput").attr("class", "opacityFilter40");
        $("input:radio#radioButtonExtreme").attr("disabled", "disabled");
        $("input:radio#radioButtonPremium").attr("disabled", "disabled");
        $("input:radio#radioButtonHigh").attr("disabled", "disabled");
        $("input:radio#radioButtonMedium").attr("disabled", "disabled");
        $("input:radio#radioButtonLow").attr("disabled", "disabled");
    }
}
