﻿/* в итоге получили: 
для IE:
	отлавливаем клики по области iframe (независимо от того покидаем данную страницу или нет)
для FF: 
	отлавливаем клики по области iframe только в случаях нажатия на ссылку мышью AND
	если при этом выгружается активный докимент  AND  курсор после загрузки  хотя бы временно находился на теле документа (любые фреймы перехватывают курсор)
*/

//var FRM_source = 'googlesyndication.com';
var FRM_source = 'googleads.g.doubleclick.net'

function as_click(Btype) {
	var frmSrcStr = new String(window.document.getElementById('click_fixer').src); 
	var last_pos = frmSrcStr.length;
	
	if (frmSrcStr.indexOf('?') > -1){
		last_pos = frmSrcStr.indexOf('?');
	}
	var new_frmSrc = frmSrcStr.substring(0,last_pos) + '?from_url='+ window.location.pathname;
	/* if browser is FF */
	if (Btype == 'FF'){
		var req = new XMLHttpRequest();
		req.open('GET', new_frmSrc, false); 
		req.send(null);
		
		while(req.status != 200){
		  		//dump(req.responseText);
		}
		
	}
	/* if browser is IE */
	
	if (Btype == 'IE'){
		window.document.getElementById('click_fixer').src = new_frmSrc;
		window.top.focus();
	}
	
}

window.onload = function () { window.focus(); }

// incredibly funky onload add-event scripting, for all browsers

		 if(typeof window.addEventListener != 'undefined')
		 {
		 	//.. gecko, safari, konqueror and standard
		 	window.addEventListener('load', adsense_init, false);
		 }
		 else if(typeof document.addEventListener != 'undefined')
		 {
		 	//.. opera 7
		 	document.addEventListener('load', adsense_init, false);
		 }
		 else if(typeof window.attachEvent != 'undefined')
		 {
		 	//.. win/ie
		 	window.attachEvent('onload', adsense_init);
		 }

		 //** remove this condition to degrade older browsers
		 else
		 {
		 	//.. mac/ie5 and anything else that gets this far

		 	//if there's an existing onload function
		 	if(typeof window.onload == 'function')
		 	{
		 		//store it
		 		var existing = onload;

		 		//add new onload handler
		 		window.onload = function()
		 		{
		 			//call existing onload function
		 			existing();

		 			//call adsense_init onload function
		 			adsense_init();
		 		};
		 	}
		 	else
		 	{
		 		//setup onload function
		 		window.onload = adsense_init;
		 	}
		 }
function adsense_init () {

	if (document.all) {  //ie

		var el = document.getElementsByTagName("iframe");
	
		for(var i = 0; i < el.length; i++) {
			if(el[i].src.indexOf(FRM_source) > -1) {
				as_clickIE =  function (){		as_click('IE');	}
				el[i].attachEvent("onfocus", as_clickIE);
			}
		}
	
	} else {   // firefox
		window.addEventListener('beforeunload', doPageExit, false);
		window.addEventListener('mousemove', getMouse, true);
	
	}
		
}

//for firefox
var px;
var py;

// this correct for FF 
function getMouse(e) {
	px=e.pageX;
	py=e.pageY;
}

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 srcStr = new String(ad[i].src);
		if (srcStr.indexOf(FRM_source) > -1){
			//alert(srcStr.toString());
			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));
			
			var inFrameX = (px >= parseInt(adLeft)-30 && px <= (parseInt(adLeft) + parseInt(ad[i].width) +20));
			var inFrameY = (py >= parseInt(adTop)-20 && py <= (parseInt(adTop) + parseInt(ad[i].height)+20));
			
			if (inFrameY && inFrameX ) {
				var s = new String(ad[i].src);
//				alert(s.substring(7,20) +'::  x:'+ inFrameX + ' y:' + inFrameY + '\n\r frame:\n\r \tleft: ' + adLeft + '\t width: '+ad[i].width+'\n\r \t top: ' + adTop +'\t height: '+ ad[i].height + '\r\n mouse x:'+px +' y: '+ py);
				as_click('FF');
			
			}
			
		}
		
	}
	
}