(
	function()
	{
		function getPar(w)
		{
			//Creo l oggetto p che contiene una serie di parametri riferiti al player
			var p = {counter: 0, country: 'it', 'id': -1, bgc: '000000', 'width': 498, 'height': 374, nostream: false, autoplay: false, stretch: true, audio: false, datasource: null, dict: {}, share: null, site: 'life', playlist: null, pl_prop: null, ads_on: false, ads_channels: null, kiosk: false};
			mapP(w, 'dadanet_video_counter', 'counter', p);
			w['dadanet_video_counter'] = p['counter']+1;
			
			if(w['dadanet_video_params'])
			{
				var ap = w['dadanet_video_params'][p['counter']];
				if(ap)
				{
					mapP(ap, 'width', 'width', p);
					mapP(ap, 'height', 'height', p);
					mapP(ap, 'id', 'id', p);
					mapP(ap, 'bgc', 'bgc', p);
					mapP(ap, 'country', 'country', p);
					mapP(ap, 'stretch', 'stretch', p);
					mapP(ap, 'audio', 'audio', p);
					mapP(ap, 'site', 'site', p);
				}
			}
			else
			{
				mapP(w, 'dadanet_video_width', 'width', p);
				mapP(w, 'dadanet_video_height', 'height', p);
				mapP(w, 'dadanet_video_id', 'id', p);
				mapP(w, 'dadanet_video_bgcolor', 'bgc', p);
				mapP(w, 'dadanet_video_country', 'country', p);
				mapP(w, 'dadanet_video_nostream', 'nostream', p);
				mapP(w, 'dadanet_video_autoplay', 'autoplay', p);
				mapP(w, 'dadanet_video_datasource', 'datasource', p);
				mapP(w, 'dadanet_video_dictionary', 'dict', p);
				mapP(w, 'dadanet_video_stretch', 'stretch', p);
				mapP(w, 'dadanet_video_share', 'share', p);
				mapP(w, 'dadanet_video_onlyaudio', 'audio', p);
				mapP(w, 'dadanet_video_site', 'site', p);
				mapP(w, 'dadanet_video_list', 'playlist', p);
				mapP(w, 'dadanet_video_prop', 'pl_prop', p);
				mapP(w, 'dadanet_video_ads', 'ads_on', p);
				mapP(w, 'dadanet_video_adschannels', 'ads_channels', p);
				mapP(w, 'dadanet_video_kiosk', 'kiosk', p);
			}

			//console.debug("dadanet_video_id = "+p['id']);
			//console.debug("counter = "+p['counter']);
			
			return(p);
		}

		function chkDom(w, p)
		{
			var h = w.location.hostname;
			var pt = w.location.pathname;

			if(/dada.net$/.test(h))
			{
				if(/^(..(\.www)?|www?)\.dada\.net$/.test(h) || /life?\.dada\.net$/.test(h))
				{
					if(pt == "/videoplayer/popupvideo.php")
					{
						//p['pl_prop'] = {i:[],f:[],p:[],ps:[],n:[]};
						var s = String(w.location.search);
						var r = s.substr(1,s.length-1).split("&");
						for(var i=0; i<r.length; i++)
						{
							s = r[i].split("=");
							if(s[0] == "id" || s[0] == "bgc" || s[0] == "splinder" || s[0] == "nostream" || s[0] == "audio" || s[0] == "stretch" || s[0] == "site" || s[0] == "playlist")
								p[s[0]] = s[1];
/*							
							if(s[0] == "urlVid")
								p['pl_prop'].f = s[1];
							
							if(s[0] == "nameVid")
								p['pl_prop'].l = s[1];
							
							if(s[0] == "id")
								p['pl_prop'].i = s[1];
*/
						}
					}

					if(/^dada_(video|audio)_/.test(w.name))
					{
						p['width'] = '100%';
						p['height'] = '100%';
					}

					if(w.name != "dada_video_popup") {
						p['uri'] = gURI(p, false);
					//Mia modifica: p['autoplay'] = true;
						//p['autoplay'] = true;
					}
					else
					{
						p['stretch'] = true;
						p['autoplay'] = true;
					}

					return(false);
				}
				else
				{
					return(true);
				}
			}
			else if(/splinder.com$/.test(h))
			{
				p['splinder'] = true;
				return(true);
			}
			else
			{
				return(true);
			}
		}

		function mapP(w, s, d, p)
		{
			var n = null;
			if(w[s] != n)
			{
				p[d] = w[s];
				w[s] = n;
			}
		}

		function mkIframe(d, p)
		{
			d.write( '<iframe name="dada_video_frame'+p['counter']+'" width="' + p['width'] + '" height="' + p['height'] + '" frameborder="0" src="' + gURI(p, true) + '" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no">');
			d.write( "</iframe>" );
		}

		function mkPlayer(w, p)
		{
			var pr = "";

			if (p['id'] != -1)
			{
				if(p['splinder'] == 1)
					pr = "vidpath=http://files.splinder.com/"+p['id'];
				else
				{
					if(p['pl_prop'] && p['pl_prop'][p['id']])
					{
						pr = "d=0";
					}
					else
					{
						if(p['audio'])
						{
							pr = "vidpath=http://flv.dada.net/audio/"+p['id']+".flv";
						}
						else {
							if(p['stretch'])
								pr = "vidpath=http://flv.dada.net/video/"+p['id']+".flv&imgpath=http://flv.dada.net/video/preview/"+p['id']+".jpg";
							else
								pr = "vidpath=http://flv.dada.net/video/"+p['id']+".flv&imgpath=http://flv.dada.net/video/previewthumb/"+p['id']+".jpg";
						}
					}
				}
			}

			pr += "&sticherColor=0x"+p['bgc'];

			if(p['uri'])
				pr += '&urlP=1';

			if(p['autoplay'])
				pr += '&ap=1';

			if(p['nostream'])
				pr += '&ns=1';

			if(p['audio'])
			{
				//TODO: verificare se vanno annullati altri parametri
				pr += '&audio=1';

				switch(p['site'])
				{
					default:
						if(p['playlist'])
							pr += "&skin=/swf/multi_skin_dada.swf";
						else
							pr += "&skin=/swf/skin_dada.swf";
						break;
				}
			}
			else
			{
				if(p['splinder'] == 1)
					pr += "&skin=/swf/skin_splinder.swf";
				else
				{
					switch(p['site'])
					{
						case 'splinder':
							pr += "&skin=/swf/skin_splinder.swf";
							break;
						case 'blogo':
							pr += "&skin=/swf/skin_blogo.swf";
							break;
						case 'motime':
							pr += "&skin=/swf/skin_motime.swf";
							break;
						case 'max':
							pr += "&skin=/swf/skin_max.swf";
							break;
						default:
							if(p['playlist'])
								pr += "&skin=/swf/multi_skin_dada.swf";
							else
								pr += "&skin=/swf/skin_dada.swf";
							break;
					}
				}

				if(p['stretch'])
					pr += '&st=1';
	
				if(p['datasource'])
					pr += '&ds=1';
				
				if(typeof(p['share']) == 'function')
					pr += '&sv=1';
				
				if(p['ads_on'])
					pr += '&gads=1';
			}
			pr += '&counter='+p['counter'];
			//console.debug("pr = "+pr);

			if(p['kiosk'])
			{
				AC_FL_RunContent(
					'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
					'pluginspage','http://www.macromedia.com/go/getflashplayer',
					'id', 'dada_video_player_'+p['counter'],
					'name', 'dada_video_player_'+p['counter'],
					'allowFullScreen', 'true',
					'width',p['width'],
					'height',p['height'],
					'align','middle',
					'quality','high',
					'bgcolor','#000000',
					'menu','false',
					'allowscriptaccess','always',
					'swliveconnect', 'true',
					"flashvars", pr,
					'src','/swf/videoplayer.swf',
					'movie','/swf/videoplayer.swf' );
			}
			else
			{
				AC_FL_RunContent(
					'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
					'pluginspage','http://www.macromedia.com/go/getflashplayer',
					'id', 'dada_video_player_'+p['counter'],
					'name', 'dada_video_player_'+p['counter'],
					'width',p['width'],
					'height',p['height'],
					'align','middle',
					'quality','high',
					'bgcolor','#000000',
					'wmode','opaque',
					'menu','false',
					'allowscriptaccess','always',
					'swliveconnect', 'true',
					"flashvars", pr,
					'src','/swf/videoplayer.swf',
					'movie','/swf/videoplayer.swf' );
			}
		}

		function gURI(p,f)
		{
			var r = 'http://'+p['country']+'.dada.net/videoplayer/popupvideo.php?id='+p['id']+'&nostream=1';
			if(p['splinder'])
				r += "&splinder=1";
			if(p['site'])
				r += "&site="+p['site'];  
			if(p['playlist'])
				r += "&playlist="+p['playlist'];
			if(p['stretch'])
				r += "&stretch=1";
			if(p['audio'])
				r += "&audio=1";
			if(f)
				r += "&bgc="+p['bgc'];
			return(r);
		}

		function mkObj(w, p)
		{
			var pid = (p['counter']);
			
			//console.debug("Player Make %s",pid);

			if(w['__dada_video_component_'+pid])
				return;

			var c = w['__dada_video_component_'+pid] = {
				_tld: 'it',
				_vid: '-1',
				_w: 498,
				_h: 374,
				_ds: null,
				_shareF: null,
				_playlist: null,
				_pl_prop: null,
				_ads_channels: null,
				_fakeDiv: null,
				_pid: -1,
				_dk: {},
				_init: function() {
					//console.debug("Player Init %s",this._pid);
					this._videoplayer = null;
					var pl_name = 'dada_video_player_'+this._pid;
					
					if(document[pl_name])
					{
						this._videoplayer = document[pl_name];
					}
					else if(window[pl_name])
					{
						this._videoplayer = window[pl_name];
					}
					//console.debug(this._playlist);
					/*
					if(window['_dadanet'])
					{
						window['_dadanet'].overlayWatch(this);
					}
					*/
					return((this._playlist && this._playlist.length > 0) ?  this._playlist : [this._vid]);
				},
				_makeFakeDiv: function() {
					if(this._fakeDiv == null)
					{
						var h = this._videoplayer.offsetHeight;
						var w = this._videoplayer.offsetWidth;
						this._fakeDiv = Builder.node('div',{style: 'width:'+w+'px; height:'+h+'px; background-color: #000; display: none'}, [' ']);
						var tgt = $('dada_video_player_'+this._pid);
						tgt.parentNode.insertBefore(this._fakeDiv, tgt);
					}
				},
				handleEvent: function(objEvt) {
					this._makeFakeDiv();
					switch(objEvt.type)
					{
						case 'overlayBegin':
							Element.hide(this._videoplayer);
							Element.show(this._fakeDiv);
							break;
						case 'overlayEnd':
							Element.show(this._videoplayer);
							Element.hide(this._fakeDiv);
							break;
					}
				},
				isInitialized: function() {
					return(typeof(this._videoplayer) != 'undefined');
				},
				loadVideo: function(fid) {
					this._vid = parseInt(fid);
					try
					{
						this._videoplayer.loadVideo(this._getVidProp());
					}
					catch(err)
					{
						console.debug(this._videoplayer);
						console.debug(this._getFilename());
						console.debug(err);
					}
				},
				_getUri: function() {
					//console.debug(this._vid);
					var res = 'http://'+this._tld+'.dada.net/videoplayer/popupvideo.php?id='+this._vid; 
					if(this._splinder)
						res += "&splinder=1";
					if(this._stretch)
						res += "&stretch=1";
					if(this._site)
						res += "&site="+this._site;
					if(this._playlist)
						res += "&playlist="+this._playlist;
					/*
					if(this._urlVid)
						res += "&urlVid="+this._urlVid;
					if(this._nameVid)
						res += "&nameVid="+this._nameVid;
					*/
					return(res);
				},
				_getAdsChannels: function() {
					return(this._ads_channels);
				},
				_getAllProp: function() {
					return(this._pl_prop);
				},
				_getVidProp: function(nid) {
					var r = this.__getVidProp(nid);
					//console.debug("dadanet_video_id = "+this._vid);
					//console.debug(r);
					return r;
				},
				__getVidProp: function(nid) {
					var id = (nid) ? nid : this._vid;
					if (!this._pl_prop && !this._pl_prop[id]) {
						// FIXME when nginx url works
						if (this._audio) return ['http://flv.dada.net/audio/'+nid+'.flv',"","",""];
						return ['http://flv.dada.net/video/'+nid+'.flv',"http://flv.dada.net/video/preview/"+nid+".jpg","http://flv.dada.net/video/previewthumb/"+nid+".jpg",""];
					}
					var o = this._pl_prop[id];

					// FIXME when nginx url works
					//console.debug('flv = '+this._pl_prop.f[idx]);
					//return [this._pl_prop.f[idx],this._pl_prop.p[idx],this._pl_prop.ps[idx],this._pl_prop.n[idx]];
					//return ["http://flv.dada.net/ngx/video/flv/86/95/0945257.flv",this._pl_prop.p[idx],this._pl_prop.ps[idx],this._pl_prop.n[idx]];
					return [o['flv'],o['thumb'],o['jpg'],o['title']];
				},
				_createFilename: function(nid) {
					if (this._audio) return 'http://flv.dada.net/audio/'+nid+'.flv';
					return 'http://flv.dada.net/video/'+nid+'.flv';
				},
				_getFilename: function(nid) {
					//alert('test1 '+nid);
					var idx = 0; var id = (nid) ? nid : this._vid;
					//alert('test2 '+this._pl_prop.i.length);
					for(var i=0; i<this._pl_prop.i.length; i++) {
						if (this._pl_prop.i[i] == id) {
							idx = i;
							break;
						}
					}
					//console.debug('flv = '+this._pl_prop.f[idx]);
					return "http://flv.dada.net/ngx/video/flv/51/b2/0881159.flv?ppp&ts=0720151514";
					//return this._pl_prop.f[idx];
					//return('http://flv.dada.net/video/'+(nid ? nid : this._vid)+'.flv');
				},
				getAsString: function() {
					var r = '<iframe name="dada_video_frame'+Math.floor(Math.random()*1000);
					r += '" width="' + this._w + '" height="' + this._h;
					r += '" frameborder="0" src="' + this._getUri() + '&nostream=1&bgc=000000';
					r += '" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no"></iframe>';
/*
					var r = '<scr'+'ipt type="text/javascript">if(typeof(dadanet_video_params) != "object" || !( dadanet_video_params instanceof Array)) dadanet_video_params = new Array();dadanet_video_params.push( { width: '+this._w;
					r += ', height: '+this._h;
					r += ', id: '+this._vid;
					r += ', bgc: "000000", country: "'+this._tld;
					r += '"} );</scr'+'ipt><scr'+'ipt type="text/javascript" src="http://js.dada.net/life/videoplayer.js"></scr'+'ipt>';
*/
					return(r);
				},
				openPopUp: function(par) {
					var aPar = par.split(",");
					this._vid = aPar[0];
					/*
					this._urlVid = aPar[1];
					this._nameVid = aPar[2];
					*/
					this._playlist = false;
					//console.debug(aPar);
					var w = window.open(this._getUri(),'dada_video_popup','resizable=yes,toolbar=no,scrollbars=no,location=no,menubar=no,width='+screen.width+',height='+screen.height);
					w.focus();
				},
				closePopUp: function() {
					window.close();
				},
				setParams: function(p) {
					this._tld = p['country'];
					this._vid = p['id'];
					this._w = p['width'];
					this._h = p['height'];
					this._splinder = p['splinder'];
					this._dk = p['dict'];
					this._shareF = p['share'];
					this._site = p['site'];
					this._stretch = p['stretch'];
					this._playlist = p['playlist'];
					this._pl_prop = p['pl_prop'];
					this._audio = p['audio'];
					this._pid = p['counter'];
					this._ads_channels = p['ads_channels'];

					if(this._pl_prop)
					{
						for(var i in this._pl_prop)
						{
							this._pl_prop[i]['flv'] = this._createFilename(i);
						}
					}
				},
				bindDataSource: function(ds) {
					if(this._ds)
						this._ds.delListener("onDataChange", this);
					this._ds = ds;
					this._ds.addListener("onDataChange", this);
				},
				onDataChange: function(objEvt) {
					if(this._videoplayer)
						this._videoplayer.onDataChange(objEvt);
				},
				_getLength: function() {
					if(this._ds)
						return(this._ds.getLength());
					else
						return(0);
				},
				_getItemAt: function(id) {
					if(this._ds)
						return(this._ds.getItemAt(id));
					else
						return(null);
				},
				_getMetaData: function() {
					if(this._ds)
						return(this._ds.getMetaData());
					else
						return(null);
				},
				_getDictionary: function() {
					return(this._dk);
				},
				_shareVideo: function() {
					if(typeof(this._shareF) == 'function')
						this._shareF();
				},
				_debug: function(str) {
					if(_JSDebug)
						console.debug("Video %s: %s",this._pid,str);
				}
			}
			
			// the first player is the one referred by old code
			if (pid == 0) w['__dada_video_component'] = c;
			
			c.setParams(p);

			if(p['datasource'] !== null && typeof(p['datasource']) == 'object')
			 c.bindDataSource(p['datasource']);
		}

		function A()
		{
			var d = document;
			var w = window;
			var p = getPar(w);

			if(chkDom(w, p))
			{
				mkIframe(d, p);
			}
			else
			{
				mkPlayer(w, p);
				mkObj(w, p);
			}
		}

		A();
	}
) ()
