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 useHLS(){
	var name = 'isHls';
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if (results == null) {
		return "";
	}
	else {
		return decodeURIComponent(results[1].replace(/\+/g, " "));
	}	
}

function playActionAdaptive(bandwidthUrl, adaptiveMSSSLink, adaptiveFlashLink, adaptiveHLSLink) {
    // Which action do we use
    var value = getPlayerChoiceCookie();
	
	if (useHLS()=='true') {
		adaptivePlayAction(bandwidthUrl, adaptiveHLSLink, true);	
	}
	else {
		if (value == "winmedia") {
			adaptivePlayAction(bandwidthUrl, adaptiveFlashLink, false);
		}
		else {
			adaptivePlayAction(bandwidthUrl, adaptiveMSSSLink, false);
		}
	}
}

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);
            if (typeof updateMyConnectionSpeed == "function") updateMyConnectionSpeed(response.bandwidth);
		}
	});
}

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')});
    }

});


