function _EmediateLib(positions, parameters) {
  this.BASE_INTERSTITIAL_URL = 'http://www.videogame.it/arturo/static/adv_interstitial_vg.html';
  this.BASE_IFRAME_URL = '/arturo/static/emediate.html';
  this.FLASH_VERSION = '10';
  this.INTERSTITIAL_CU = null;
  this.CU_LIST = new Array();
  this.TAG_LIST = new Array();
  this.EAS_URL = null;
  this.EAS_INTERSTITIAL_URL = null;
  this.EAS_PARAMS = parameters;
  this.ENABLE_DUMMY_CALLS = '';

  this.isMobile = function() {
	  var n = navigator.userAgent.toLowerCase();
	  if (n.indexOf('mobile') != -1 || n.indexOf('googlebot') != -1) return true;
	  if(n.match(/iPhone/i) || n.match(/iPod/i) || n.match(/iPad/i) || n.match(/Android/i) || n.match(/webOS/i) || n.match(/BlackBerry/i) || n.match(/Nokia/i)){
		  return true;
	  }
	  return false;
  }

  this.insertDummyAd = function(cu, url)	{
  	  if (!url && !cu) return;
	  var el = window.top.document.createElement('iframe');
	  el.style.position = 'absolute';
	  el.style.bottom = '0px'; el.style.left = '0px';
	  el.style.width = '1px'; el.style.height = '1px';
	  el.style.border = "0px";
	  if (typeof el.allowTransparency != "undefined") el.allowTransparency='yes';
	  if (typeof el.frameBorder != "undefined") el.frameBorder='0';
	  if (typeof el.scrolling != "undefined") el.scrolling = 'no';
	  el.className = 'emediate_dummy';
	  if (!url) url = 'http://eas8.emediate.eu/eas?cu='+cu+';cre=mu;from=ads';
	  el.src = url;
	  el.onload = function() { window.top.document.body.removeChild(this); }
	  window.top.document.body.appendChild(el);
	}
  window.EAS_DUMMY_AD = this.insertDummyAd;

  window.EAS_LOG_EMPTY_AD = function(cu, pos)	{
	var url = window.location.href;
	var eas_url = (window.EAS) ? window.EAS.EAS_URL : '';
	window.jQuery.get("/a/logemptyad/?url="+escape(url)+"&cu="+cu+'&pos='+pos+'&eas_url='+escape(eas_url));
  }

  this.loadRemoteJs = function(scriptUrl, callback) {
	var script = document.createElement('script');
	script.async = "async";
	script.type = "text/javascript";
	script.src = scriptUrl;
	if (callback) {
	  script.onload = script.onreadystatechange = function(_, isAbort) {
		if (!script.readyState || /loaded|complete/.test(script.readyState)) {
			// Handle memory leak in IE
			script.onload = script.onreadystatechange = null;
			// Remove the script
			if (script.parentNode) { script.parentNode.removeChild(script); }
			script = undefined;
			if (!isAbort) { window.jQuery().ready(callback); }
		}
	  };
	}
	var s = document.getElementsByTagName('script')[0];
	s.parentNode.insertBefore(script, s);
  }

	/* funzioni di callback */
  this.callbackAdsLoaded = function() {
	//debugger;
	if (window.EAS_DISABLE_ADV) return;

	// prima verifica che non ci sia un interstitial
	if (window.EAS.INTERSTITIAL_CU) {
	  try {
		var camp = eval("EAS_found_camp_"+window.EAS.INTERSTITIAL_CU);
		var cre = eval("EAS_found_cre_"+window.EAS.INTERSTITIAL_CU);
		if (camp && document.cookie.indexOf("EAS_GRACE_INTERSTITIAL=") == -1) {
			window.location.href = window.EAS.BASE_INTERSTITIAL_URL+'?cu='+window.EAS.INTERSTITIAL_CU+'&camp='+camp+'&no='+cre+'&next='+escape(window.location.href);
			window.EAS_INTERSTITIAL_REDIRECTING = true;
			return false;
		} else {
		  if (window.EAS && window.EAS.ENABLE_DUMMY_CALLS.indexOf('interstitial') != -1) {
			window.EAS_DUMMY_AD(window.EAS.INTERSTITIAL_CU);
		  }
		}
	  } catch (e) {
		  if (window.EAS && window.EAS.ENABLE_DUMMY_CALLS.indexOf('interstitial') != -1) {
			window.EAS_DUMMY_AD(window.EAS.INTERSTITIAL_CU);
		  }
	  }
	}


	var tagList = window.EAS.TAG_LIST;
	var cuList = window.EAS.CU_LIST;
	window.jQuery().ready(function() {
	  for (var i = 0; i < tagList.length; i++) {
		var tagName = tagList[i];
		var cu = cuList[i];
		try {
		  var camp = eval("EAS_found_camp_"+cu);
		  var cre = eval("EAS_found_cre_"+cu);
		  if ((!cre || !camp) && tagName == 'leaderboard') { window.EAS_LOG_EMPTY_AD(cu, tagName); }
		  //if (!camp || !cre) window.EAS_DUMMY_AD(cu);
		} catch (e) {
		  var el = document.getElementById("adv_"+tagName);
		  if (el) el.style.display = 'none';
		  if (tagName == 'leaderboard') { window.EAS_LOG_EMPTY_AD(cu, tagName); }
		  //window.EAS_DUMMY_AD(cu);
		  continue;
		}
		// registra comunque anche le impression vuote.
		if (!camp && window.EAS && window.EAS.ENABLE_DUMMY_CALLS.indexOf(tagName) != -1) { window.EAS_DUMMY_AD(cu); }
		if (tagName == 'skin') {
			if (!camp) continue;
			var adUrl = 'http://eas8.emediate.eu/eas?cu='+cu+';camp='+camp+';no='+cre+';cre=mu;target=_blank;';
			window.EAS.loadRemoteJs(adUrl);
			continue;
		}
		else if (tagName == 'overlayer') {
		  if (!camp) continue;
		  var el = document.createElement('iframe');
		  el.className = 'emediate invisible';
		  el.name = 'emediate_frame_'+cu;
		  el.id = 'emediate_frame_'+cu;
		  el.scrolling = 'no'; el.frameborder = 0;
		  el.marginwidth = 0; el.marginheight = 0;
		  el.style.backgroundColor = 'transparent';
		  el.style.position = "absolute"; el.style.visibility = "hidden";
		  el.style.left = "0px"; el.style.bottom = "0px";
		  el.width = 1; el.height = 1;
		  if (typeof(el.allowTransparency) != 'undefined') el.allowTransparency = true;
		  if (typeof(el.frameBorder) != 'undefined') el.frameBorder = "no";
		  if (el.backgroundColor) el.backgroundColor = 'transparent';
		  if (el.style.backgroundColor) el.style.backgroundColor = 'transparent';
		  el.src = '/arturo/static/emediate.html#'+cu+'-'+camp+'-'+cre+'-'+window.EAS.EAS_PARAMS;
		  document.body.appendChild(el);
		  // hack per ie
		  if (el.outerHTML) el.outerHTML = el.outerHTML;
		  continue;
		}
		var el = document.getElementById("adv_"+tagName);
		if (!el) continue;
		if (!camp && el.style) { el.style.display = "none"; continue; }
		var w = window.jQuery(el).width(); var h = window.jQuery(el).height();
		if (w <= 10 || h <= 10) { w = eval("EAS_found_width_"+cu); h = eval("EAS_found_height_"+cu); }
		el.innerHTML = '<iframe src="'+window.EAS.BASE_IFRAME_URL+'#'+cu+'-'+camp+'-'+cre+'-'+window.EAS.EAS_PARAMS+'" width="'+w+'" height="'+h+'" class="emediate" id="emediate_'+cu+'" name="emediate_'+cu+'" scrolling="no" frameborder="0" marginheight="0" marginwidth="0" allowtransparency="true"></iframe>';

	  }
	});
  }

  // La funzione principale di inizializzazione
  this.initPageAdv = function(positions, parameters) {
    if (window.EAS_DISABLE_ADV  ||!positions || positions == "disabled" || positions == "none") return;
	if ((this.isMobile && this.isMobile()) || (window.EAS && window.EAS.isMobile())) {
	  if (window.jQuery) { window.jQuery(".emediate").css("display", "none"); }
	  return;
	}
	for (var key in positions) {
	  if (key == 'interstitial') {
		this.INTERSTITIAL_CU = positions[key];
	  } else {
		this.TAG_LIST.push(key);
		this.CU_LIST.push(positions[key]);
	  }
	}

	var EAS_server = (location.protocol == "https:") ? "https://eas8.emediate.eu" : "http://eas8.emediate.eu";
	var EAS_ord=new Date().getTime();
	this.EAS_URL = EAS_server + "/eas?EASformat=nocontent&EAScus=" + this.CU_LIST.join(",") + "&ord=" + EAS_ord + "&EASflash="+this.FLASH_VERSION;
	if (parameters) this.EAS_URL += "&" + parameters;
	if (location.hostname && (location.hostname.indexOf('dev2.') != -1 || location.hostname.indexOf('dev.') != -1)) {
	  this.EAS_URL += "&kw1=adpreview"
	}
	if (this.INTERSTITIAL_CU) {
	  this.EAS_URL = this.EAS_URL.replace("EAScus=","EAScus="+this.INTERSTITIAL_CU+",");
	}
	this.loadRemoteJs(this.EAS_URL, this.callbackAdsLoaded);
  }

  window.EAS_RESIZE_CU = function(name, width, height) {
	var el = document.getElementById(name);
	if (!el) return;
	el.width = width; el.height = height;
	var el2 = el.parentNode;
	if (el2.id == 'adv_halfpage' && height < 600) {
	  $.getJSON("/arturo/static-spool/box_filmreview.json", function(fr) {
		var html = '<div id="box_filmreview">';
		html += '<h4 class="list300" style="text-align: right; padding-right: 130px; background: url(/arturo/static/vg/box_filmreview.png) no-repeat top center; cursor: pointer;" onclick="window.open(\'http://www.film-review.it\');">FILM AL CINEMA BY</h4>';
		html += '<ul class="list300" id="box_filmreview_list">';
		for (i = 0; i < 3; i++) {
		  var u = 'window.open(\''+fr[i]["url"]+'\');';
		  html += '<li class="onclick" onclick="'+u+'"><img src="'+fr[i]["image"]+'" class="clip" width="65" height="90"><h5>'+fr[i]["title"]+'</h5><h6>'+fr[i]["genere"]+'</h6>dal '+fr[i]["data"]+'<p>'+fr[i]["subtitle"]+'</p><hr></li>';
		}
		window.jQuery(el2).append(html+'</ul></div>');
		if (document.getElementById('adv_halfpage_title')) {
		  document.getElementById('adv_halfpage_title').style.display = "none";
		}
	  });
	}
	else if (el2.className.indexOf('emediate') != -1) {
	  el2.width = width; el2.height = height;
	}

  }

  window.EAS_AD_HIDE = function(cu) {
	var el = window.frameElement || document.getElementById("adv_"+cu);
	el.style.visibility = "hidden";
  }

  window.EAS_AD_CLOSE = function(cu) {
	var el = window.frameElement || document.getElementById("adv_"+cu);
	el.style.display = "none";
  }

  window.EAS_EXPAND_AD = function(width, height, cu) {
	var el = window.frameElement || document.getElementById("adv_"+cu);
	if (!el) alert('debug');
	var el2 = el.parentNode.style;
	//el2.position = "relative";
	el2.backgroundColor = "transparent";
	el2.overflow = "visible";
	//el2.width = width+"px";
	//el2.height = height+"px";
	el.style.position = "absolute";
	el.style.zIndex = 1000;
	el.style.width = width+"px"; el.style.height = height+"px";
	//top.jQuery(el).animate({'width': width, 'height': height });
  }

  window.EAS_SHRINK_AD = function(width, height, cu) {
	var el = (cu) ? document.getElementById("adv_"+cu) : window.frameElement;
	if (!el) alert('debug');
	var el2 = el.parentNode.style;
	top.jQuery(el).animate({'width': parseInt(el2.width.replace("px","")), 'height': parseInt(el2.height.replace("px","")) });
  }

  if (positions) {
	this.initPageAdv(positions, parameters);
  }
}

function EAS_init(cus, positions) {
  if (window.EAS_DISABLE_ADV) return;
  window.EAS = new _EmediateLib(cus, positions);
}

function EAS_insert_ad(cu, forceJS) {
  if (!window.EAS_place_ad)
	document.writeln('<scr'+'ipt type="text/javascript" src="http://eas8.emediate.eu/EAS_tag.1.0.js"></scr'+'ipt>');
  if (forceJS) forceJS = 'js=y;';
  document.writeln('<scr'+'ipt type="text/javascript" src="http://eas8.emediate.eu/eas?cu='+cu+';cre=mu;'+forceJS+'target=_blank"></scr'+'ipt>');
}

function EAS_insert_interstitial(cu, parameters) {
  var EAS_server = (location.protocol == "https:") ? "https://eas8.emediate.eu" : "http://eas8.emediate.eu";
  var EAS_ord=new Date().getTime();
  var url = EAS_server + "/eas?EASformat=nocontent&EAScus="+cu+"&ord="+EAS_ord+"&EASflash=10";
  if (parameters) url += "&" + parameters;
  document.writeln('<scr'+'ipt type="text/javascript" src="'+url+'"></scr'+'ipt>');

  document.write('<scr'+'ipt type="text/javascript">');
  document.write('if (EAS_found_camp_'+cu+') {');
  document.write('  window.location.href = "http://www.videogame.it/arturo/static/adv_interstitial_videogame.html?cu='+cu+'&camp="+EAS_found_camp_'+cu+'+"&no="+EAS_found_cre_'+cu+'+"&next='+escape(window.location.href)+'";');
  document.write('  window.EAS_DISABLE_ADV = true;');
  document.write('}');
  document.write('</scr'+'ipt>');
}

function EAS_SKIN_CLICK(el) {
  window.open(window.EAS_SKIN_LINK, 'EASSKIN');
  //window.location.href=window.EAS_SKIN_LINK;
  return;
}
/* Fine libreria emediate */


/* configurazione */
window.EMEDIATE_VG_CONFIG = {
  'MAGAZINE': { interstitial: 6689, skin: 6688, leaderboard: 6683, halfpage: 6684, boxlink300x100: 6686, strip: 6685, overlayer: 6687, boxlink468x60: 14113 },
  'HOMEPAGE': { interstitial: 6681, skin: 6680, leaderboard: 6675, maxistrillo: 6682, halfpage: 6676, strip: 6677, boxlink300x100: 6678, overlayer: 6679, boxlink468x60: 14113 },
  'FORUM': { interstitial: 6641, skin: 6640, leaderboard: 6633, halfpage: 6634, boxlink300x100: 6637, strip: 6635, overlayer: 6638, boxlink468x60: 14113 },
  'WEBGAME': { interstitial: 6673, skin: 6672, leaderboard: 6667, halfpage: 6668, boxlink300x100: 6670, strip: 6669, overlayer: 6671, maxistrillo: 6674, boxlink468x60: 14113 },
  'GALLERY': { interstitial: 13330, skin: 13334, leaderboard: 13331, halfpage: 13329, boxlink300x100: 13328, strip: 13335, overlayer: 13333, boxlink468x60: 14113 },
  'PUBLIREDAZIONALE': { }
}

window.EMEDIATE_TPC_CONFIG = {
  'DEFAULT': { interstitial: 8173, skin: 8175, overlayer: 8174, leaderboard: 8168, strip: 8171, mpu1: 8169, mpu2: 8170, utilink1: 8176, utilink2: 8177, utilink3: 8178, utilink4: 8179 },
  'HOMEPAGE': { interstitial: 8159, skin: 8161, overlayer: 8160, leaderboard: 8154, strip: 8157, mpu1: 8155, mpu2: 8156, utilink1: 8162, utilink2: 8163, utilink3: 8164, utilink4: 8165 },
  'DIARIO': { interstitial: 8201, skin: 8203, overlayer: 8202, leaderboard: 8196, strip: 8199, mpu1: 8197, mpu2: 8198, mpu3: 8392, autopromo300x180: 8356, utilink1: 8204, utilink2: 8205, utilink3: 8206, utilink4: 8207, skyscraper: 8200, boxlink160x70: 8211 },
  'DIARIHOME': { interstitial: 8187, skin: 8189, overlayer: 8188, leaderboard: 8182, strip: 8185, mpu1: 8183, mpu2: 8184, boxlink240x35: 8210, utilink1: 8190, utilink2: 8191, utilink3: 8192, utilink4: 8193 },

  'MAGAZINE': { interstitial: 8231, skin: 8233, overlayer: 8232, leaderboard: 8226, strip: 8229, mpu1: 8227, mpu2: 8228, mpu3: 8394, autopromo300x180: 8356, utilink1: 8234, utilink2: 8235, utilink3: 8236, utilink4: 8237, skyscraper: 8230 },
  'MAGAZINEHOME': { interstitial: 8217, skin: 8219, overlayer: 8218, leaderboard: 8212, strip: 8215, skyscraper: 8216, mpu1: 8213, mpu2: 8214, utilink1: 8220, utilink2: 8221, utilink3: 8222, utilink4: 8223 },

  'TAMTAM': { interstitial: 8259, skin: 8261, overlayer: 8260, leaderboard: 8254, strip: 8257, mpu1: 8255, mpu2: 8256, mpu3: 8396, autopromo300x180: 8356, utilink1: 8262, utilink2: 8263, utilink3: 8264, utilink4: 8265, skyscraper: 8258 },
  'TAMTAMHOME': { interstitial: 8245, skin: 8247, overlayer: 8246, leaderboard: 8240, strip: 8243, skyscraper: 8244, mpu1: 8241, mpu2: 8242, utilink1: 8248, utilink2: 8249, utilink3: 8250, utilink4: 8251 },

  'COMMUNITY': { interstitial: 8273, skin: 8275, overlayer: 8274, leaderboard: 8268, strip: 8271, mpu1: 8269, mpu2: 8270, skyscraper: 8272, utilink1: 8276, utilink2: 8277, utilink3: 8278, utilink4: 8279, boxlink300x180: 8401 },
  'FOTO': { interstitial: 8287, skin: 8289, overlayer: 8288, leaderboard: 8282, strip: 8285, mpu1: 8283, mpu2: 8284, utilink1: 8290, utilink2: 8291, utilink3: 8292, utilink4: 8293 },
  'VIDEO': { interstitial: 8301, skin: 8303, overlayer: 8302, leaderboard: 8296, strip: 8299, mpu1: 8297, mpu2: 8298, utilink1: 8304, utilink2: 8305, utilink3: 8306, utilink4: 8307 },

  'IOCISONOSTATO': { interstitial: 8378, skin: 8380, overlayer: 8379, leaderboard: 8373, strip: 8376, mpu1: 8374, mpu2: 8375, utilink1: 8381, utilink2: 8382, utilink3: 8383, utilink4: 8384 },

  'PUBLIREDAZIONALE': { leaderboard: 8168, mpu1: 8169, mpu2: 8170 },
  'SHOP': { leaderboard: 8391 },
  'EUROPCAR': { leaderboard: 8390 },
  'ISRAELE': { leaderboard: 8352 },
  'FIAT16': { leaderboard: 11346, mpu1: 11365 },
  'RODANOALPI': { leaderboard: 12442 }
}

