//check the state of a FlowExecutionQueryable FlowController on the server
function queryFlowController(serverAddr) {
	var flowExecutionQueryResult = {
		isFinalStepReachable: false
	};

	$.ajax({
		type: "POST",
		async: false,
		data: "",
		dataType: "json",
		url: serverAddr+"&flowQueryMode=1",
		success: function(response) {
			flowExecutionQueryResult.isFinalStepReachable = response.isFinalStepReachable == 'true';
		}
	});

	return flowExecutionQueryResult;
}


function deliverStream(format, playerType, streamingLink) {
	if(cmCreateConversionEventTag) {
		cmCreateConversionEventTag(format+playerType+":LAUNCH", '2', 'Usage Conversion', '10');
	}
	if(playerType == "TMP") {
		window.open(streamingLink, '_tmp', 'menubar=0,height=700,width=1000,left=0,location=0,resizable=1,scrollbars=0,status=0,toolbar=0,top=0');
	}
	else {
		location.href = streamingLink;
	}
}


function envokePlayAction(popUpId, event, bitrates, bandwidthSelected, format, playerType, streamingLink) {
	var playVodMovieFlowState = queryFlowController(streamingLink);

	if(playVodMovieFlowState.isFinalStepReachable) {
		
		if(isBandwidthDetected()) {
			
			deliverStream(format, playerType, streamingLink);
			
		} else {
			
			showBitratePopup(popUpId, event, bitrates, bandwidthSelected, format, playerType, streamingLink);
		}
	}
	else {
		// Puts us into the play vod flow
		location.href = streamingLink;
	}
}

function envokeBitratePopup(popUpId, event, bitrates, bandwidthSelected, format, playerType, streamingLink) {
	var playVodMovieFlowState = queryFlowController(streamingLink);

	if(playVodMovieFlowState.isFinalStepReachable) {
		
		showBitratePopup(popUpId, event, bitrates, bandwidthSelected, format, playerType, streamingLink);
		
	} else {
		// Puts us into the play vod flow		
		location.href = streamingLink;
	}
}
