﻿var EventCache = [];
var iFr;
var iframeList = document.getElementsByTagName("iframe");
var anchorList = document.getElementsByTagName("A");
function AddEvent(obj, evType, fn, useCapture) {
    useCapture=true;
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, useCapture);
	} 
    else if (obj.attachEvent) {
		obj.attachEvent('on'+evType, fn);
	}
	EventCache.push([obj, evType, fn, useCapture]);
}
function RemoveEvent(obj, evType, fn, useCapture) {
    if (useCapture==undefined) {
        useCapture=true;
    }
	if (obj.removeEventListener) {
      obj.removeEventListener(evType, fn, useCapture);
    } 
    else if (obj.detachEvent) {
      obj.detachEvent('on' + evType, fn);
    }
}
function Unload() {
    for (var i = 0; i < EventCache.length; i++) {
      RemoveEvent.apply(this, EventCache[i]);
      EventCache[i][0] = null;
    }
    EventCache = false;	
}
function LogAdSenseI() {
    var pc = "" + escape(window.status.substring(6));
    var adLoc = iFr.src.indexOf("?");
    var adSrc = iFr.src.substring(adLoc+1);
    var target = new Image();
    //var uniqueid = new Date(new Date().getTime());
    var uniqueid = new Date().getTime();
    target.id = uniqueid;
    target.name = uniqueid;
    target.src = '/AddClick.aspx?referrer=' + document.referrer + '&originalurl=' + OriginalURL + '&suffix=' + uniqueid + '&pc=' + pc + '&' + adSrc;
    var slowdown = new Image(100,100);
    slowdown.id = uniqueid;
    slowdown.name = uniqueid;
    slowdown.src = '/images/shared/blank.gif?'+uniqueid;
    //nap(1000);
}  
function nap(m) {
    var snooze = new Date(new Date().getTime() + m);
    while (new Date() < snooze) {
        //napping
    }
}
for (var i = 0; i < iframeList.length; i++) { 
    if(iframeList[i].src.indexOf('googlesyndication') > -1) { 
        if (iframeList[i].addEventListener) {
            //window.addEventListener('beforeunload', LogAdSenseI, false);
            //AddEvent(window,'beforeunload',LogAdSenseI,false);
            AddEvent(iframeList[i],'focus',LogAdSenseI,false);
        } 
        else if (iframeList[i].attachEvent) {
            iframeList[i].attachEvent('onfocus', LogAdSenseI);
        } 
        iFr = iframeList[i];
    }
}
if (anchorList.length > 0) {
  for (var i=0;i<anchorList.length;i++) {
    for (var att in anchorList[i]) {
      if (att.toLowerCase() == "href") {
        if (anchorList[i].getAttribute(att).indexOf('googlesyndication') > -1) {
          anchorList[i].onclick = function () {
                                                var href = this.getAttribute("href");
                                                if (href != '') {
                                                    var str1 = href.substring(href.indexOf("&adurl")+7);
                                                    var pc=str1.substring(0,str1.indexOf("&client"));
                                                    var adLoc=href.indexOf("?");
                                                    var adSrc=href.substring(adLoc+1);
                                                    var target = new Image();
                                                    //var uniqueid = new Date(new Date().getTime());
                                                    var uniqueid = new Date().getTime();
                                                    target.id = uniqueid;
                                                    target.name = uniqueid;
                                                    target.src = '/AddClick.aspx?referrer=' + document.referrer + '&originalurl=' + OriginalURL + '&suffix=' + uniqueid + '&pc=' + pc + '&' + adSrc;
                                                    var slowdown = new Image(100,100);
                                                    slowdown.id = uniqueid;
                                                    slowdown.name = uniqueid;
                                                    slowdown.src = '/images/shared/blank.gif?'+uniqueid;
                                                    //nap(1000);
                                                }
                                              }
        }
      }
    }
  }
}
AddEvent(window,'unload',Unload, false);
