var PLAYER_TYPE_NONE = "none";
var PLAYER_TYPE_FLEX = "flex";
var PLAYER_TYPE_SILVERLIGHT = "silverlight";
var PLAYER_TYPE_ALL = "all";

var advSequenceXML = null;
var adagioCallStartTime = null;
var adagioCallEndTime = null;

function GetPlaylist()
{
	return xmlVideoMetadata;
}


function CreatePlayer(xmlVideoMetadata) {
	
	var essenceType = GetVideoEssencePlayerType(xmlVideoMetadata);
	var clientType = GetClientSupportedPlayerType();
	
	if ((essenceType == PLAYER_TYPE_FLEX || essenceType == PLAYER_TYPE_ALL) 
		&& (clientType == PLAYER_TYPE_FLEX || clientType == PLAYER_TYPE_ALL)) {
		// flash client is supported
		CreateFXPlayer();
	
	} else {
		// essenza e installazione sul client non corrispondono
		if (essenceType == PLAYER_TYPE_FLEX || essenceType == PLAYER_TYPE_ALL) {
			// content is flash
			GetFlashPlayer();
		}
	}
}


function GetVideoEssencePlayerType(xmlVideoMetadata) {
	var type = PLAYER_TYPE_NONE;
	
	var pos = xmlVideoMetadata.indexOf("<essence ");
	var typepos, typestr;
	
	while (pos >= 0) {
		typepos = xmlVideoMetadata.indexOf("type=", pos) + 6;
		typestr = xmlVideoMetadata.substr(typepos);
		if (typestr.indexOf("video-flv") == 0 || typestr.indexOf("video-f4v") == 0) {
			if (type == PLAYER_TYPE_SILVERLIGHT) {
				type = PLAYER_TYPE_ALL;
				break;
			} else type = PLAYER_TYPE_FLEX;
		} else if (typestr.indexOf("video-wmv") == 0 || typestr.indexOf("stream-smooth") == 0) {
			if (type == PLAYER_TYPE_FLEX) {
				type = PLAYER_TYPE_ALL;
				break;
			}
			else type = PLAYER_TYPE_SILVERLIGHT;
		}
		
		pos = xmlVideoMetadata.indexOf("<essence ", typepos);
	}
	
	
	return type;
}

function GetClientSupportedPlayerType() {
	var type = PLAYER_TYPE_NONE;
	
	if ( DetectFlashVer(10, 0, 0) ) {
		type = PLAYER_TYPE_FLEX;
	}

/*	if (Silverlight.isInstalled("3.0")) {
		if (type == PLAYER_TYPE_FLEX) {
			type = PLAYER_TYPE_ALL;
		}
		else type = PLAYER_TYPE_SILVERLIGHT;
	}
	*/
	
	return type;
}

function playerEventsDispatcher(eventCode, p1, p2, p3, p4) {
	// lock page function when adv is running and unlock when is finished
	if ((eventCode == 15 && p2 != "content") ||
		(eventCode == 13))
	{
		// one adv has started
		lockPage(true);
	}
	else if ((eventCode == 7) ||
		(eventCode == 14))
	{
		// one adv has stopped
		lockPage(false);
	}
	else if (eventCode == 15 && p2 == "content")
	{
		// main video has started
		lockPage(false);
	}
	
	// dispatch to nielsen script
	if (gg1) gg1.ggPM(eventCode, p1, p2, p3, p4);
}


