function initPlayers(xmlurl, logoURL, adCampaingID, isConi) {
	var atv_playerURL = "../include/atv_player3.swf", atv_w = 980, atv_h = 470;
	if (isConi) {
		atv_playerURL = "../include/atv_player3coni.swf";
		atv_w = 725; atv_h = 530;
	}
	// init aTVPlayer
	swfobject.embedSWF(atv_playerURL, "aTVPlayer", atv_w, atv_h, "10.0.0", false,
		{xml: xmlurl, debug: 0},
		{bgcolor: "#FFFFFF", allowFullScreen: "false", menu: "false", allowScriptAccess: "always", wmode: "opaque"}
	);

	// init flowPlayer
	var flowPlayerConfig = {
		buffering : false,
		key: '#$550f177797f8ee23350',
		onLoad : function() { flowPlayerReady(); },
		canvas: {
			backgroundColor: '#000000'
			//backgroundGradient: [0.3, 0],
			//borderRadius: 10
		},
		screen: {
			height: 270, //324,
			top: 0
		},
		logo: {
			url: logoURL,
//			linkUrl: 'http://www.atleticomtv.it/',
			displayTime: 0,
			opacity: 0.75,
			bottom: 30,
			right: 10,
			fullscreenOnly: false
		},
		plugins: {
			controls: {
				url: '../include/flowplayer.controls-3.2.5.swf',
				// display properties
				bottom:0,
				height:20,
				zIndex:1,
				backgroundColor: '#333333',
				backgroundGradient: 'low',

				// controlbar-specific configuration
				fontColor: '#ffffff',
				timeFontColor: '#333333',
				autoHide: 'never',

				// which buttons are visible and which are not?
				play: true,
				volume: true,
				mute: false,
				time: false,
				stop: false,
				playlist: false,
				fullscreen: true,
				scrubber: true
			},
			rtmp: {	// Latest RTMP plugin, good for Influxis
				url: '../include/flowplayer.rtmp-3.2.3.swf'
			},
			uif: {	// Stream clip plugin
				videoCompleteCallback: "",
				url: '../include/flowplayer.uif.swf', //?'+Math.random(),
				//test calls
//				preroll: escape('http://ad.de.doubleclick.net/ad/atleticom.it.smartclip/as3_16.9_rotation;sz=400x320;dcmt=text/xml;ord=[random]?'),
//				midroll: escape('http://ad.de.doubleclick.net/ad/atleticom.it.smartclip/as3_16.9;sz=400x299;dcmt=text/xml;ord=[random]?'),
//				postroll: escape('http://ad.de.doubleclick.net/ad/atleticom.it.smartclip/as3_16.9;sz=400x300;dcmt=text/xml;ord=[random]?'),
//				overlay: escape('http://ad.de.doubleclick.net/ad/atleticom.it.smartclip/as3_16.9;sz=300x50;dcmt=text/xml;ord=[random]?'),
				//
				preroll: escape('http://ad-emea.doubleclick.net/ad/'+adCampaingID+'.smartclip/;sz=400x320;dcmt=text/xml;ord=[random]?'),
				midroll: escape('http://ad-emea.doubleclick.net/ad/'+adCampaingID+'.smartclip/;sz=400x299;dcmt=text/xml;ord=[random]?'),
				postroll: escape('http://ad-emea.doubleclick.net/ad/'+adCampaingID+'.smartclip/;sz=400x300;dcmt=text/xml;ord=[random]?'),
				overlay: escape('http://ad-emea.doubleclick.net/ad/'+adCampaingID+'.smartclip/;sz=300x50;dcmt=text/xml;ord=[random]?'),
				ad_facade_url: 'uif.facade.jpg',
				ad_facade_duration: '3',
				midroll_video_length: '10',
				overlay_delay: '2',
				ad_countdown: true,
				ad_countdown_position: 'top',
//				ad_countdown_text: 'Il tuo video partirà tra: [time] secondi - Caricamento: [percent]',
				ad_countdown_text: 'Il tuo video partirà tra: [time] secondi',
				ad_countdown_text_color: '0xffffff',
				ad_countdown_color: '0xF60000',
				ad_countdown_opacity: 0.75,
				always_randomize_cachebuster: true
			}
		}
	};

	if (adCampaingID == false) { flowPlayerConfig.plugins.uif = null; }

	$f("flowPlayer", {src: "../include/flowplayer.commercial-3.2.7.swf", version: [10, 0], wmode: 'opaque'}, flowPlayerConfig);
}

// handle flowPlayer ready event: required as aTVPlayer and flowPlayer loading is asynchronous
isFlowPlayerReady = false;
flowPlayerReady_videoToPlay = "";
function flowPlayerReady() {
	isFlowPlayerReady = true;
	if (flowPlayerReady_videoToPlay != "") playVideo(flowPlayerReady_videoToPlay);
}

// bridge function called from aTVPlayer to play videos into flowPlayer
function playVideo(v, id, vTitle) {
	// console.log({v:v,id:id,vTitle:vTitle});
	if (!isFlowPlayerReady) {
		flowPlayerReady_videoToPlay = v;
		return;
	}

	// common clip attributes
	var clip = {
		bufferLength: 0,
		metaData: false
	};

	if (v.indexOf('rtmp://') == 0) { // an rtmp stream (Influxis like)
		var parts = parseVideoUrl(v);
		clip.provider = 'rtmp';
		clip.netConnectionUrl = parts.path;
		if (parts.format != "") clip.url = parts.format+':'+parts.name;
		else clip.url = parts.name;
//	} else if ( *** how do WeStream urls look like? *** ) {
//		still not implemented
	} else { // last attempt: let FlowPlayer play the video url as is (finger crossed!)
		clip.url = v;
	}

	$f().play(clip);
	// console.log({clip: clip, id: id, vTitle: vTitle});
	setShareVideo(id, vTitle);
}

function parseVideoUrl(u){
	var r = {path: "", name: "", format: ""};

	// find file name
	var i = u.lastIndexOf('/');
	if (i == -1) r.name = u;
	else {
		r.name = u.slice(i+1);
		r.path = u.slice(0,i);
	}

	// find format
	i = r.name.lastIndexOf('.');
	if (i != -1) {
		r.format = r.name.slice(i+1);
		r.name = r.name.slice(0,i);
	}

	return r;
}

// let share button share the current video (with its title)
var basePageURL = location.protocol + '//' + location.host + location.pathname;
var fullVideoUrl = basePageURL;
var basePageTitle = document.title;
var cnt = 1;
function setShareVideo(id, vTitle) {
	var u = basePageURL + "?id=" + id;
	if (id == undefined) u = basePageURL;
	var t = basePageTitle + " - " + vTitle;
	if (t == undefined) u = basePageTitle;
	var bid = "addThisButton"+(cnt++);

	// replace the current share button with a new empty <a> with a brand new id
	var htmlCode = '<a id="'+bid+'"><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a>';
	var linkBtnCode = '&nbsp;&nbsp;&nbsp;<a href="javascript:copyLink();"><img src="/include/copylink.gif" width="58" height="16" alt="Copy Link" style="border:0"/></a>';
	document.getElementById('addThisButtons').innerHTML = htmlCode+linkBtnCode;

	// add the share js to the new <a>
	addthis.button('#'+bid, {
		// configurationObject
		data_track_clickback: false
	}, {
		// sharingObject
		url: u,
		title: t
	});
	fullVideoUrl = u;
}

function copyLink() {
	window.prompt("Copia il link qui sotto", fullVideoUrl);
	return false;
}
