//revised 4-24-07 mbc
iFrioBug = {};

iFrioBug.init = function(){
    this.debug=false;
    this.IE = (document.all == null) ? false : true;
    this.lastGoogleAd;
    this.FirePageHasAdSense = false;
    this.adsenseTrackerUnit = null;
    this.adsenseTrackerActive = false;
    this.adsenseClickedAd = "";
    this.analyticsTag = '/asclick';
    this.Account = _avnacct;

     //main
    var res=window.screen.width+"X"+window.screen.height;
    var ref=document.referrer;
    var pub=document.URL;
    var heartbeat= new Image();
    
   
    heartbeat.src="http://www.iFrio.com/DataCatcher/heartbeat.ashx?channel=heartbeat&event=load&account="
        +_avnacct+"&screen="+res+"&ref="+ref+"&pub="+pub;
    
    iFrioBug.addEvent(window,'load',iFrioBug.loadEvent, false); 
              
    }
iFrioBug.AdSenseLog = function(format, channel, theAd)
    {
    var bug = new Image();
    var AdType="GG";
    if(theAd == null || theAd == undefined) theAd = "?";
    var res=window.screen.width+"X"+window.screen.height;
    bug.src = 'http://www.iFrio.com/DataCatcher/Collect2.ashx?ref=' + 
        document.referrer + '&AdType='+AdType+'&url=' 
        + theAd + '&format=' + format +'&channel='+channel 
        +"&account="+_avnacct+"&pub="+document.URL
        +"&event=iframe&screen="+res;
    }

iFrioBug.AdSenseEvent = function(e)
    {
    var eTarget = e.target || window.event.srcElement; //IE or Moz
    var theAd = eTarget.href ;
    var format = iFrioBug.queryString('format',eTarget.src);
    var channel = iFrioBug.queryString('channel',eTarget.src);
    iFrioBug.AdSenseLog(format, channel, theAd);
    }

iFrioBug.queryString =function(item,source)
    {
    var itemLoc=source.indexOf(item);
    var newSrc=source.substring(itemLoc);
    var endLoc=newSrc.indexOf("&");
    var lstSrc=newSrc.substring(0,endLoc);
    var itm=lstSrc.substring(item.length+1);
    return itm;
    }
iFrioBug.ready = function()
    {
   
    if (iFrioBug.IE){
        return (document.readyState == "complete" || document.readyState == "loaded");
        }
    else // MOZ
        {
        return true;
        }
    }   
iFrioBug.loadEvent = function(e)
    {
    // is the page ready ?
    if (!iFrioBug.ready())
        {
        setTimeout('iFrioBug.loadEvent()', 200);
        return;
        }
   
    var elements = document.getElementsByTagName("iframe");
    for (var i = 0; i < elements.length; i++)
        {
        if(elements[i].src.indexOf('googlesyndication.com') > -1)
            {
            iFrioBug.lastGoogleAd = elements[i];
            if (iFrioBug.IE)
                {  
                iFrioBug.addEvent(iFrioBug.lastGoogleAd, 'focus', iFrioBug.AdSenseEvent, false);
                }
            else // firefox
                {
                iFrioBug.FirePageHasAdSense = true;
                iFrioBug.lastGoogleAd.onmouseover = iFrioBug.adsenseTrackerActivate;
                iFrioBug.lastGoogleAd.onmouseout = iFrioBug.adsenseTrackerDeActivate;
                }
            }
        }
    iFrioBug.addListeners();
    if (!iFrioBug.IE && iFrioBug.FirePageHasAdSense) window.addEventListener('beforeunload', iFrioBug.adsenseTrackerCheckClick, false);
    }
// -------------- TRACKING Firefox Google Ads

// when moving mouse out of adsense frame, deactivate click tracking...
iFrioBug.adsenseTrackerDeActivate = function(e) {
	iFrioBug.adsenseTrackerUnit = null;
	iFrioBug.adsenseTrackerActive = false;
}
// when moving mouse to adsense frame, activate click tracking for current ad unit...
iFrioBug.adsenseTrackerActivate = function(e) {
	iFrioBug.adsenseTrackerUnit = e.target;
	iFrioBug.adsenseTrackerActive = true;
}
// check if it's our click
iFrioBug.adsenseTrackerCheckClick = function(e) {
	if (iFrioBug.adsenseTrackerActive) {
		var reportedUrl;

		// check for ad Url/AdLinks text (is disabled in Firefox by default)
		if (window.status) {
		    iFrioBug.adsenseClickedAd = window.status;
			if (iFrioBug.adsenseClickedAd != '') {
			    reportedUrl += '/'+ escape(iFrioBug.adsenseClickedAd);
			}
		}
        var format = iFrioBug.queryString('format', iFrioBug.adsenseTrackerUnit.src);
        var channel = iFrioBug.queryString('channel', iFrioBug.adsenseTrackerUnit.src);
        var eTarget = e.target || window.event.srcElement; //IE or Moz
        var theAd = eTarget.href;
        if (theAd == null || theAd == undefined) theAd = "Google";
		// this could be replaced with other tracking sw...
		iFrioBug.AdSenseLog(format, channel, theAd);
	}
}
// -----------------------------------------
iFrioBug.addListeners = function()
    {
    //set up listeners for hyperlinks
    var elements;
    elements = document.getElementsByTagName("a");
    for (var i = 0; i < elements.length; i++)
        {
        iFrioBug.addEvent(elements[i],'click',iFrioBug.clickEvent,false);
        }
    }

iFrioBug.clickEvent = function (e)
    {
    var eTarget = e.target || window.event.srcElement; //IE or Moz

    var el; 
    var AdType="UNK";
    var channel="";
    
    var theAd = eTarget.href ;
    var rec = new Image();
    var res = window.screen.width+"X"+window.screen.height;
    rec.src = 'http://www.iFrio.com/DataCatcher/Collect2.ashx?ref=' 
        + document.referrer + '&AdType='+AdType+'&url=' 
        +theAd + '&channel='+channel +"&account="+_avnacct
        +"&pub="+document.URL+"&event=click&screen="+res;
    }

iFrioBug.addEvent = function(elm, evType, fn, useCapture)
    {
    if (elm.addEventListener)
        {
        elm.addEventListener(evType, fn, useCapture); 
        return true; 
        }
    else if (elm.attachEvent)
        {
        var r = elm.attachEvent('on' + evType, fn); 
        return r; 
        }
    else
        {
        elm['on' + evType] = fn;
        }
    }
iFrioBug.init();


        

