var timeout = 3;
var hostName = '';
var login_id = '';

var htmlTags = new Array('TABLE', 'DIV', 'SPAN', 'STRONG', 'P', 'B', 'I', 'U', 'LI', 'FONT', 'STYLE');

function setTimeout(t)			{ timeout = (t >= 3) ? t : 3; }
function setHostname(h)		{ hostName = h; }
function setPublisherID(id)	{ login_id = id; }


function cleanUrl(u)
{
	var url = u;
	var i = url.indexOf('#');
	if (i >= 0) url = url.substring(0, i);
	i = url.indexOf('?');
	if (i >= 0) url = url.substring(0, i);
	return url;
}


function is_normal_link(href)
{
	return href && (href.substring(0,7).toLowerCase() == 'http://' || href.substring(0,8).toLowerCase() == 'https://')
}

function getLinkByConstructor(link)
{
	if (link.constructor == '[HTMLAnchorElement]')
		return link;

	if (link.constructor == '[HTMLImageElement]')
		return link.parentNode;

	if (link.constructor == '[HTMLSpanElement]' || link.constructor == '[HTMLFontElement]')
		return getLinkByConstructor(link.parentNode);

	return null;
}


function loadLink(target, href)
{
	if (!target) target = '_self';

	// first processes predefined targets
	if(target == '_self')
	{
		// loads to the current frame/window
		document.location=href;
		return true;
	} else if (target == '_parent')
	{
		// loads to parent frame/window
		window.parent.location = href;
		return true;
	} else if (target == '_top')
	{
		// loads to the topmost window
		window.top.location = href;
		return true;
	} else if (target == '_blank')
	{
		// opens a blank window and loads the link there
		var w = window.open(href);
		w.focus();
		return true;
	}

	// check if the target is another frame in the same frameset
	if (window.parent)
	{
		var frm = window.parent.frames[target];
		if (frm)
		{
			frm.location = href;
			return true;
		}
	}

	var w = window.open(href);
	w.focus();
	return true;
}

function getLinkByTag(link)
{
	if (link.tagName == 'IMG')
		return link.parentElement;

	for (var i=0; i<htmlTags.length; i++)
		if(link.tagName == htmlTags[i])
			return getLinkByTag(link.parentNode);

	if (link.href)
		return link;

	return null;
}


function activateAndRedirect(link)
{
	var href	= link.href;
	
	if(hostName != '' && document.cookie.indexOf('mmGIC=ON') < 0)
	{ 
		var now = new Date();
		var uaid = now.getTime() % Math.floor(8640000 - Math.random()*1000);
		href = hostName + '/ad/id=' + login_id + '&opt=hnj&cv=210&uid=' + uaid + '&/&url=' + link.href;

		now.setTime(now.getTime() + timeout*60000);
		document.cookie = 'mmGIC=ON; path=/; expires=' + now.toGMTString();
	}

	loadLink(link.target, href);
	return false;
}


function insertInterstitial(e)
{
	var link = document.all ? getLinkByTag(event.srcElement) : getLinkByConstructor(e.target);

	if (link && is_normal_link(link.href) && cleanUrl(link.href) != cleanUrl(location.href))
	{
		activateAndRedirect(link);
		return false;
	}

	return true;
}
 
if (!document.all)
	document.captureEvents(Event.CLICK);

document.onclick = insertInterstitial;
