var adsrc = '';
var isOverIFrame = false;

function ad_click() {
    if(isOverIFrame) {
		var ad_target_url = '';
		ad_target_url = escape(window.status);
		adlogger_url_image = new Image();

		adsrc = this.src;
		adlogger_url_image.src = adlogger_loc + '/track/ppc_click_logger.php?ch_id=' + channel_id + '&targ=' + ad_target_url + '&disp=' + escape(document.location.href) + '&src=' + escape(adsrc);
	}
}

// Inicio Nuevo Cogigo

function processMouseOut() {
	isOverIFrame = false;
	top.focus();
}

function processMouseOver() {
	isOverIFrame = true;
}

function attachOnloadEvent(func, obj) {
	if(typeof window.addEventListener != 'undefined') {
		window.addEventListener('load', func, false);
	} else if (typeof document.addEventListener != 'undefined') {
		document.addEventListener('load', func, false);
	} else if (typeof window.attachEvent != 'undefined') {
		window.attachEvent('onload', func);
	} else {
		if (typeof window.onload == 'function') {
			var oldonload = onload;
			window.onload = function() {
				oldonload();
				func();
			};
		} else {
			window.onload = func;
		}
	}
}

function adsense_init () {

	var el = document.getElementsByTagName("iframe");
	
	if (el.length > 0) {
	
		for(var i = 0; i < el.length; i++) {
		
		    
			if(el[i].src.indexOf('doubleclick.net') > -1) {
				el[i].onmouseover = processMouseOver;
				el[i].onmouseout = processMouseOut;
			} else if(el[i].src.indexOf('overture.com') > -1) {
				el[i].onmouseover = processMouseOver;
				el[i].onmouseout = processMouseOut;
			}
		}
	} else {
		window.onunload = doPageExit;
		window.addEventListener('mousemove', getMouse, true);
	}
	
	if (typeof window.attachEvent != 'undefined') {
		top.attachEvent('onblur', ad_click);
	}
	else if (typeof window.addEventListener != 'undefined') {
		top.addEventListener('blur', ad_click, false);
	}
	
}

attachOnloadEvent(adsense_init);

var px;
var py;

function getMouse(e) {
	px=e.pageX;
	py=e.clientY;
}

function findY(obj) {
	var y = 0;
	while (obj) {
		y += obj.offsetTop; obj = obj.offsetParent;
	}
	return(y);
}

function findX(obj) {
	var x = 0;
	while (obj) {
		x += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return(x);
}

function doPageExit(e) {
	ad = document.getElementsByTagName("iframe");

	for (i=0; i<ad.length; i++) {
		var adLeft = findX(ad[i]);
		var adTop = findY(ad[i]);
		var inFrameX = (px > (adLeft - 10) && px < (parseInt(adLeft) + parseInt(ad[i].width) + 15));
		var inFrameY = (py > (adTop - 10) && py < (parseInt(adTop) + parseInt(ad[i].height) + 10));
		if (inFrameY && inFrameX) {
			adsrc = ad[i].src;
			ad_click();
		}
	}
}