/*
Contextual Tracker
http://ContextualTracker.wiadomosc.info/

Copyright (C) 2006 Tobiasz 'gosciu' Cudnik
gosc@wiadomosc.info

You can use ContextualTracker free of charge.
Distribution and code reuse NOT permitted.
*/

setTimeout('ct_adsenseRun()', 500);

function ct_adsenseRun()
{
	var ad = document.getElementsByTagName("iframe");
	var scripts = document.getElementsByTagName('script');
	var scriptsCount = 0;

	for ( var i=0; i<scripts.length; i++)
	{
		if ( typeof scripts[ i ].src != 'undefined' && scripts[ i ].src.indexOf('http://pagead2.googlesyndication.com') > -1 ) {
			scriptsCount++;
		}
	}

	var j = 1;

	for ( var i = 0; i < ad.length; i++ ) {
		if ( ad[i].src.indexOf('googlesyndication.com') > -1 ) {
			if ( j <= ct_sysFrameCount('adsense') ) continue;

			ct_trackFrame( ad[i], 'adsense', 'ct_adsenseClick' );
			j++;
		}
	}

	if ( scriptsCount != ct_sysFrameCount('adsense') )
		setTimeout('ct_adsenseRun()', 500);
}

function ct_adsenseClick( frameObj, pos )
{
	var data = new Object();

	if ( ct_location != self.location ) {	// posiblity of page redirection with mouse over adsense frame (no click)
		ct_cancelClick( frameObj );
		ct_debug('cancelling click - location missmatch');
		return;
	}

	// link unit ?
	if ( ct_status && ct_getURLvar( frameObj.src, 'format').match(/ads_al/) ) {
		ct_debug("adlinks detected");
		data['ad_url'] = window.status;
	}
	else if ( ct_status ) {
		if ( ct_status.substr(0,36) == 'http://services.google.com/feedback/' ) {
			ct_cancelClick( frameObj );
			return;
		}

		var match = ct_status.match(/(\S+)$/);

		if ( match != null )
			data['ad_url'] = 'http://'+match[1];
	}
	else if ( ct_statusWorking ) {	// posiblity of clicking on blank space of iframe
		ct_cancelClick( frameObj );
		ct_debug('cancelling click - window.status missing, but working');
		return;
	}

	data['sys'] = "adsense";
	data['src_title'] = document.title;
	data['ad_pos'] = pos;
	data['src_url'] = ct_location;
	data['channel'] = ct_getURLvar( frameObj.src, 'channel');
	data['ad_size'] = frameObj.width+"x"+frameObj.height;
	data['browser'] = ct_browser();

	ct_storeClick( data );
}