var IS_BANDWIDTH_DETECTED = false;
var IS_BANDWIDTH_DETECTION_CANCELLED = false;
var DETECTED_KBPS = "";

var WIN_MEDIA_SUPPORTED = true;
var SILVERLIGHT_SUPPORTED = true;
var IS_MACINTOSH = false;

function setWinMediaSupported(isSupported) {
    WIN_MEDIA_SUPPORTED = isSupported;
}

function setSilverlightSupported(isSupported) {
    SILVERLIGHT_SUPPORTED = isSupported;
}

function setIsMacintosh(isMacintosh) {
    IS_MACINTOSH = isMacintosh;
}

function playActionWin(bitrate, setBandwidthUrl, winStreamingLink, winPlayerType, tmpStreamingLink, tmpPlayerType, format) {
    // Which action do we use

    var value = getPlayerChoiceCookie();

    if (value == "winmedia") {
        playAction(bitrate, setBandwidthUrl, winStreamingLink, format, winPlayerType);
    } else {
        playAction(bitrate, setBandwidthUrl, tmpStreamingLink, format, tmpPlayerType);
    }
}


function setPlayerChoiceCookie(winSelected) {
    var value = "silverlight";

    if (winSelected) {
        value = "winmedia";
    }

    createCookie("aebnPlayerChoice", value, 1000);
}

function getPlayerChoiceCookie() {

    var value = readCookie("aebnPlayerChoice");

    if (value == null) {
        if (IS_MACINTOSH) {
            value = "silverlight";
        } else {
            value = "winmedia";
        }
    }

    if (!WIN_MEDIA_SUPPORTED) {
        value = "silverlight";
    } else if (!SILVERLIGHT_SUPPORTED) {
        value = "winmedia";
    }

    return value;
}

function updatePlayerChooseButtonState(winSelected) {

    setPlayerChoiceCookie(winSelected);

    // Need to make sure all of these radio buttons have the same value
    if (winSelected) {
        $('.playerChoiceWinMediaRadio').each(function(){$(this).attr('checked','checked')});
        $('.playerChoiceSilverlightRadio').each(function(){$(this).removeAttr('checked')});
    } else {
        $('.playerChoiceWinMediaRadio').each(function(){$(this).removeAttr('checked')});
        $('.playerChoiceSilverlightRadio').each(function(){$(this).attr('checked','checked')});
    }
}

function setBandwidth(setBandwidthUrl, kbps) {
	$.ajax({
		async: false,
		type: "POST",
		url: setBandwidthUrl,
		data: {bandwidth: kbps},
		dataType: "json",
		success: function(response) {
			setBandwidthDetected(true);
            setDetectedKbps(response.kbps);
            reportDetectedBandwidth(kbps);
            if (typeof updateMyConnectionSpeed == "function") updateMyConnectionSpeed(response.bandwidth);
		}
	});
}

function reportDetectedBandwidth(kbps) {
    var bwReportString = createbandwidthReportString(kbps);
    cmCreatePageElementTag(bwReportString, "UserBandwidth Auto-Detected");
    // cmPageTag for "Unknown" is located in bandwidthPreferencesPopup.js
}

function createbandwidthReportString(kbps) {
    var rounded = (Math.round(kbps / 100)) * 100;
    if (rounded > 100) {
        return (zeroPad((rounded - 100),6)) + "-" + zeroPad(rounded, 6);
    } else {
        return "000000-000100";
    }
}

function zeroPad(num, count) {
    var numZeropad = num + '';
    while(numZeropad.length < count) {
        numZeropad = "0" + numZeropad;
    }
    return numZeropad;
}

function setBandwidthDetected(value) {
	IS_BANDWIDTH_DETECTED = value && true;
}

function isBandwidthDetected() {
	
	return IS_BANDWIDTH_DETECTED;
}

function setDetectedKbps(value) {
	DETECTED_KBPS = value;
}

function getDetectedKbps() {
	return DETECTED_KBPS;
}

function cancelBandwidthDetection() {
	if(!isBandwidthDetected()) {
		IS_BANDWIDTH_DETECTION_CANCELLED = true;
	}
}

function isBandwidthDetectionCancelled () {
	return IS_BANDWIDTH_DETECTION_CANCELLED;
}

function isFlashInstalled() {
 return DetectFlashVer(9, 0, 0);
}

function getBandwidthClientEmbedCode() {
	var code = "";
	code = AC_FL_GetContent(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
			'width', '1',
			'height', '1',
			'src', '/staticFlash/BandwidthClient',
			'quality', 'high',
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'align', 'middle',
			'play', 'true',
			'loop', 'true',
			'scale', 'showall',
			'wmode', 'window',
			'devicefont', 'false',
			'id', 'BandwidthClient',
			'bgcolor', '#777777',
			'name', 'BandwidthClient',
			'menu', 'true',
			'allowFullScreen', 'false',
			'allowScriptAccess','sameDomain',
			'movie', '/staticFlash/BandwidthClient',
			'salign', ''
		); //end AC code
	return code;
}


 // we need to initialize which one is checked.
$(function(){
    var value = getPlayerChoiceCookie();

    // Need to make sure all of these radio buttons have the same value
    if (value == "winmedia") {
        $('.playerChoiceWinMediaRadio').each(function(){$(this).attr('checked','checked')});
        $('.playerChoiceSilverlightRadio').each(function(){$(this).removeAttr('checked')});
    } else {
        $('.playerChoiceWinMediaRadio').each(function(){$(this).removeAttr('checked')});
        $('.playerChoiceSilverlightRadio').each(function(){$(this).attr('checked','checked')});
    }

});

