var req;
var MQSAUrl = "http://" + document.location.host + "/video/VideoService/TokenQsa.htmx?";//"/video/VideoService/mqsa.htmx?";
var clipQueue;
var _baseUrlToken = "rtmpe://ntteurope.fcod.llnwd.net/a1225/r1/mp4:";
var checkDone=false;
var baseURL = 'http://' + document.location.host;
if (baseURL.indexOf('frontend')>-1)
	_OrderURL = setOrderLanguage(document.location.host,'dev');
else if (baseURL.indexOf('staging')>-1)
	_OrderURL = setOrderLanguage(document.location.host,'stag');
else
	_OrderURL = setOrderLanguage(document.location.host,'prod');//'https://secure.video.uefa.com/Selfcare/default.aspx';

function setOrderLanguage(path,environment)
{
	var url = path.substring(0,path.indexOf("."));
	if (environment == 'dev'){
		switch (url)
		{
			case "it" : return "http://frontend.uefa.deltatre.it:21001/op/Lang=IT/";
			case "fr" : return "http://frontend.uefa.deltatre.it:21001/op/Lang=FR/";
			case "de" : return "http://frontend.uefa.deltatre.it:21001/op/Lang=DE/";
			case "es" : return "http://frontend.uefa.deltatre.it:21001/op/Lang=ES/";
			case "pt" : return "http://frontend.uefa.deltatre.it:21001/op/Lang=PT/";
			default: return "http://frontend.uefa.deltatre.it:21001/op/Lang=EN/";
		}	
	}
	else if (environment == 'stag'){
		switch (url)
		{
			case "it" : return "http://orderpath.static.dr.infra.uefa.com/op/Lang=IT/";
			case "fr" : return "http://orderpath.static.dr.infra.uefa.com/op/Lang=FR/";
			case "de" : return "http://orderpath.static.dr.infra.uefa.com/op/Lang=DE/";
			case "es" : return "http://orderpath.static.dr.infra.uefa.com/op/Lang=ES/";
			case "pt" : return "http://orderpath.static.dr.infra.uefa.com/op/Lang=PT/";
			default: return "http://orderpath.static.dr.infra.uefa.com/op/Lang=EN/";
		}	
	}
	else if(environment == 'prod'){
		//return "https://order.uefa.com/op/Lang=EN/";
		switch (url)
		{
			case "it" : return "https://order.uefa.com/op/Lang=IT/";
			case "fr" : return "https://order.uefa.com/op/Lang=FR/";
			case "de" : return "https://order.uefa.com/op/Lang=DE/";
			case "es" : return "https://order.uefa.com/op/Lang=ES/";
			case "pt" : return "https://order.uefa.com/op/Lang=PT/";
			default: return "https://order.uefa.com/op/Lang=EN/";
		}		
	}
	//return "http://template.orderpath.infra.uefa.com:21001/op/Lang=EN/";
}

function handleXmlResponse(vodRequest)
{
	var itemsResult = new mediaItemsResponse(vodRequest.responseXML);
	if(itemsResult.GlobalErrorCode=='500'){
		returnURL = 'http://' + document.location.host + '/video/login.html';
		document.location.href=_OrderURL + "Replay/login.html?r=" + returnURL;
	}	
	else{
		HandleQSAResponse(itemsResult);
	}

}

function getSwf(name)
{
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[name];
	} else {
		return document[name];
	}	
}

function HandleQSAResponse(items)
{
	if(items.Success)
	{
		if(items.isAuthorized())
		{
			if (items.getnewWin() == 'false')
			{
				var tokenUrl = items.urlToPlay();
				var newsId = items.newsId();
				var newsTitle = items.newsTitle();
				tokenUrl = tokenUrl.replace("rtmpe://ntteurope.fcod.llnwd.net/a1225/r1/",_baseUrlToken).replace("&amp;","&");
				
				getSwf("VideoPlayer").playThis(tokenUrl,newsId,newsTitle);
				
				var myid = items.getEntityID();
				$$('span.inplay').each(function(e){e.removeClassName('inplay').addClassName('played')});
				$(myid).addClassName('inplay');

			}
			else
			{
				fnOpenWindow(unescape(items.getReturnURL()), 'VideoService', 'no', 'no', 'no', 'no', 'no', 'no', 'no', 848, 610, '', ''); 
			}		
		}
		else
		{
			if (items.getnewWin() == 'false')
			{
				document.location.href = unescape(items.getBuyURL());
			}
			else
			{
				fnOpenWindow(unescape(items.getBuyURL()), 'VideoService', 'no', 'no', 'no', 'no', 'no', 'no', 'no', 848, 610, '', '');
			}
		}
	}
	else
	{
		if(items.getItemErrorCode()=='100' || items.getItemErrorCode()=='110' || items.getItemErrorCode()=='111' || items.getItemErrorCode()=='120' || items.getItemErrorCode()=='130' || items.getItemErrorCode()=='502')
		{
			//display error
			if (items.getnewWin() == 'false') {
				document.location.href = unescape(items.getErrorURL() + items.getItemErrorMessage()); 		
			}
			else
			{
				fnOpenWindow(unescape(items.getErrorURL() + items.getItemErrorMessage()), 'VideoService', 'no', 'no', 'no', 'no', 'no', 'no', 'no', 848, 610, '', ''); 
			}						
		}
		else
		{
			//session expired send to login page return to match page
			if (items.getnewWin() == 'false') {
				document.location.href = unescape(items.getLoginURL()); 
			}
			else
			{
				fnOpenWindow(unescape(items.getLoginURL()), 'VideoService', 'no', 'no', 'no', 'no', 'no', 'no', 'no', 848, 610, '', ''); 
			}						
		}	
	}
}

//**** checks user status and if logged in and not already authorised runs QSA for a particular item (manID)
function ChkItem(competitionID,season,matchday,matchID,mediaURL,manID,lang,newwin,newsid,newstitle)
{
	var _BaseURL = baseURL;
	var competitionName;
	competitionName = CompetitionName(competitionID);
	//if (isNumeric(competitionID)) { 
	//	competitionName = CompetitionName(competitionID);
	//}
	//else { 
	//	competitionName = competitionID;	
	//}
	var MatchdayOrRound = SetMatchdayRound(competitionName);

	var mediakindID = matchID;
	var LoginEntitleCheckURL = escape(_OrderURL + 'Replay/login.html');
	//var URLparam = 'MediaURL=' + mediaURL;
	
	var rURL = "";
	var errorURL = escape(_BaseURL + '/Video/Live/Block.html?msgid=')
	var loginURL = LoginEntitleCheckURL;
	if (document.location.search !='')
		//rURL = document.location.href.toLowerCase() + "&MediaURL=" + mediaURL;
		rURL = document.location.href.toLowerCase() + "&item=" + manID;
	else
		//rURL = document.location.href.toLowerCase() + "?MediaURL=" + mediaURL;
		rURL = document.location.href.toLowerCase() + "?item=" + manID;
	var myUrl = document.URL.toLowerCase();
	if (competitionID == '1' || competitionID == '14' || competitionID == '9'){
		var mdorround = myUrl.substring(myUrl.indexOf("/matchday=") + 10, myUrl.indexOf("/match="));
	}
	else{
		var mdorround = myUrl.substring(myUrl.indexOf("/round=") + 7,myUrl.indexOf("/match="));
	}
	var match = myUrl.substring(myUrl.indexOf("/match=") + 7,myUrl.indexOf("/vod_popupflash.html"));
	var returnURL = "";
	//check if the user have selected another match using an ajax call, if so set the correct URL to play
	if (((matchday != mdorround) || (matchID != match)) && matchday != ""){
		
		if (competitionID == '1' || competitionID == '14' || competitionID == '9'){
			rURL = rURL.replace("/matchday="+mdorround,"/matchday="+matchday).replace("/match="+match,"/match="+matchID);
			returnURL = encodeURIComponent(rURL);
		}
		else{
			returnURL = encodeURIComponent(rURL.replace("/round="+mdorround,"/round="+matchday).replace("/match="+match,"/match="+matchID));
		}
		
	}
	else{
		returnURL = encodeURIComponent(rURL);
	}
	var buyURL = escape(_OrderURL + "Replay/confirmSelection/index.html?" + "i=" + manID + "&c=" + competitionID + "&s=" + season + "&m=" + matchID + "&r=" + returnURL);
	var item = new mediaItem(_AccountID,'uvs',manID,returnURL,buyURL,loginURL,errorURL,newwin,newsid,newstitle);
	
	if (readCookie('manSession') == null) {
		var lang = "";
		lang = document.location.host.substring(0,document.location.host.indexOf("."));
		if(lang.length > 2){lang = "en";}
		if(document.location.search.indexOf("item=") > -1){
			var newURL = document.location.search.substring(document.location.search.indexOf("r=")+2,document.location.search.indexOf("item="));
		}
		else {
			var newURL = document.location.search.substring(document.location.search.indexOf("r=")+2,document.location.search.length);
		}
		var context= encodeURIComponent(document.URL.toLowerCase());
		/*
		if (newURL == 'hp')
			context = encodeURIComponent('http://' + ((lang == 'en')? '': lang + '.') + 'video.uefa.com/video/geoindex.html');
		else if (newURL == 'vod')
			context = encodeURIComponent('http://' + ((lang == 'en')? '': lang + '.') + 'video.uefa.com/video/vod/competitions/geoindex.html');
		*/
		if (newwin == 'false'){
			document.location.href= unescape(LoginEntitleCheckURL) + "?i=" + manID + "&c=" + competitionID + "&s=" + season + "&m=" + matchID + "&retUrl=" + newURL + "&context=" + context + "&r=" + returnURL;
		}
		else{
			fnOpenWindow(unescape(LoginEntitleCheckURL) + "?i=" + manID + "&c=" + competitionID + "&s=" + season + "&m=" + matchID + "&retUrl=" + newURL + "&context=" + context + "&r=" + returnURL, 'VideoService', 'no', 'no', 'no', 'no', 'no', 'no', 'no', 848, 610, '', ''); 
		}
	} 
	else {
		//no entitlement cookie so run QSA
		item.QueryMAN(readCookieParam('manSession','SessionId'),readCookieParam('manSession','Ticket'),handleXmlResponse);		
	} 
} 