//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 playAction(bitrate, setBandwidthUrl, streamingLink, format, playerType) {
    // must set bitrate - but if we are not logged in yet this will throw an exception.
    if (getDetectedKbps() != bitrate) {
        setBandwidth(setBandwidthUrl, bitrate);
	    setDetectedKbps(bitrate);
    }

    var playVodMovieFlowState = queryFlowController(streamingLink);

    if(playVodMovieFlowState.isFinalStepReachable) {
    	deliverStream(format, playerType, streamingLink);
		
		//google analytics tracking
		var playType = streamingLink.indexOf('sceneId');
		playType > -1 ? playType = 'Play-scene' : playType = 'Play-movie';
		if(playerType == 'NATIVE'){playerType = 'WMP'};
		_gaq.push(['_trackEvent', 'Video', playType, playerType, bitrate]);
		//end google analytics tracking

	} else {
    	// Puts us into the play vod flow
		location.href = streamingLink;
	}
}


function adaptivePlayAction(setBandwidthUrl, streamingLink, isHls) {
    var playVodMovieFlowState = queryFlowController(streamingLink);
    if(playVodMovieFlowState.isFinalStepReachable) {
		if (isHls) {
			window.location.href = streamingLink;
		}
		else {
			window.open(streamingLink, '_tmp', 'menubar=0,height=700,width=1000,left=0,location=0,resizable=1,scrollbars=0,status=0,toolbar=0,top=0');
		}

		//google analytics tracking
		var playType = streamingLink.indexOf('sceneId');
		playType > -1 ? playType = 'Play-scene' : playType = 'Play-movie';
		_gaq.push(['_trackEvent', 'Video', playType, 'ADAPTIVE', '0']);
		//end google analytics tracking

	} else {
    	// Puts us into the play vod flow
		location.href = streamingLink;
	}
}


// Old method to be removed
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;
	}
}

// Old method to be removed
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;
	}
}



