
if (typeof MVD == 'undefined') {
	var MVD = {};
}

MVD.Publicidad = function() {
	var ad_list = [];
	var curr = 0;
	var oldWrite = null;
	var run = false;
	var wmodeRe = /(.*)\>wmode=\"transparent\"/;
	var scriptRe = /\"\+\"/g;
	var explTagRe = /[\s\S]*<EXPLORER>([\s\S]*)<\/EXPLORER>[\s\S]*<NOEXPLORER>([\s\S]*)<\/NOEXPLORER>[\s\S]*/mg;

	var appendScript = function(src) {
		var script = document.createElement("script");
		script.type = 'text/javascript';
		script.src = src;
		document.body.appendChild(script);
	}
	var appendPub = function(id) {
		appendScript('http://publicidad.montevideo.com.uy/abm.aspx?z=' + id);
	}

	var isExplorer = function() {
		return (navigator.userAgent.indexOf("MSIE") != -1);
	}

	var insertHTML = function(id, html) {
		if (html) {
			var e = document.getElementById("pub_" + id);
			if(e) {
				html = html.replace(wmodeRe, '$1 wmode="transparent">');
				html = html.replace(scriptRe, "");
				var match = explTagRe.exec(html);
				if(match) {
					html = match[isExplorer() ? 1 : 2];
				}
				e.innerHTML = html;
			}
		}
	}

	return {
		add: function(id) {
			ad_list.push(id);
			document.write('<div id="pub_' + id + '"></div>');
		},

		load: function() {
			if (!run && (ad_list.length>0)) {
				run = true;
				appendPub(ad_list[0]);

				oldWrite = document.write;
				document.write = function(html) {
					insertHTML(ad_list[curr], html);
					curr ++;
					if (curr < ad_list.length) {
						appendPub(ad_list[curr]);
					} else {
						document.write = oldWrite;
						MVD.Publicidad = null;
					}
				}
			}
		}
	}
} ();


