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;

document.write('<script src="' + "/PolymediaShowAnalytics/tracing/SessionGenerator?rf=varjs&rnd=" + +(Math.floor(Math.random() * 1000)) + '" ><\/script>');

function GetPlaylist()
{
	return xmlVideoMetadata;
}

function GetAdvSequence()
{
	if (advSequenceXML) {
		//adagioCallEndTime = new Date();
		return advSequenceXML;
	}
	else return null;
}

function GetAdagioResponseTime() {
	return adagioCallEndTime.getTime() - adagioCallStartTime.getTime();
}

function adagioWriteCookiePlayer() {
	document.getElementById('ad-loader').contentWindow.adagioWriteCookiePlayer();
}

function CreatePlayer() {  
	var essenceType = GetVideoEssencePlayerType();
	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 if ((essenceType == PLAYER_TYPE_SILVERLIGHT || essenceType == PLAYER_TYPE_ALL) 
		&& (clientType == PLAYER_TYPE_SILVERLIGHT || clientType == PLAYER_TYPE_ALL)) {
		detectSLPlugins();		
	} else {
		// essenza e installazione sul client non corrispondono
		if (essenceType == PLAYER_TYPE_FLEX || essenceType == PLAYER_TYPE_ALL) {
			// content is flash
			GetFlashPlayer();
		} else if (essenceType == PLAYER_TYPE_SILVERLIGHT) {
			// content is silverlight			
			detectSLPlugins();			
		}
	}
}

function CreateLivePlayer() {
	if (Silverlight.isInstalled("4.0")) {
		CreateSLPlayer(true);
	} else {
		CreateSLPlayer(true);
	}
}

function GetVideoEssencePlayerType() {
	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("4.0")) {
		if (type == PLAYER_TYPE_FLEX) {
			type = PLAYER_TYPE_ALL;
		}
		else type = PLAYER_TYPE_SILVERLIGHT;
	}
	
	return type;
}

function playerEventsDispatcher(eventCode, p1, p2, p3, p4) {
	
	// dispatch to nielsen script
	try {
		if (gg1) gg1.ggPM(eventCode, p1, p2, p3, p4);
	} catch(error) {}
	
	// 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);
	}
}

function GetRelatedObject()
{
	return jsonVideoCorrelati;
}


function detectSLPlugins() {
    var createSPL = true;
    if( navigator.plugins && navigator.plugins.length > 0 ) {
		var num_of_plugins = navigator.plugins.length;

		for (var i=0; i < num_of_plugins; i++) {

		   var list_number=i+1;
		   if( navigator.plugins[i].name == "Silverlight Plug-In"){		   
			   if((navigator.plugins[i].filename.indexOf("moon") > 0)){				  
				  createSPL = false;
			   } 
		   }
		 }			
    } else {
	   if ((navigator.appVersion.indexOf("X11")!=-1) || (navigator.appVersion.indexOf("Linux")!=-1)) {	      
		   createSPL = false;
	   } 
    }
	
	if(createSPL){
		CreateSLPlayer(false);	
	}else{
	    a="<div id='conteinerslplstatic' style='width:700px;height:327px;background-image:url(/res/img/sl/bgslcont.gif);'><div id='plslstatic' style='width:346px;height:195px;background-image:url(/res/img/sl/bgplayer.jpg);float:left;margin-top:60px;margin-left:5px;'><a href='javascript:Silverlight.getSilverlight(\"\");' style='text-decoration: none;'><img src='"+urlimg+"' width='239' height='179' alt='Get Microsoft Silverlight' style='border-style: none'/></a></div><div style='margin-top:60px;padding-left:2px;margin-right:5px;float:right;width:342px;'><p style='font-size:16px;font-weight:bold;'>Per vedere questo video aggiorna il tuo browser con il plug-in Silverlight di Microsoft<br>E' gratuito, il download e l'installazione richiede solo un minuto</p><br><a href='javascript:Silverlight.getSilverlight(\"\");' style='text-decoration: none;'><img src='http://go2.microsoft.com/fwlink/?LinkID=108181' alt='Get Microsoft Silverlight' style='border-style: none'/></a></div></div>"+tracer;
	
		document.getElementById('box-player').innerHTML=a;
	}
}
