var collector_url = 'http://plusjamaisseul.net/htsrv/adsense.php';

// Detected browser type:
var browserType;
if( window.addEventListener )
	browserType = 'FF';
else if( window.attachEvent )
	browserType = 'IE';
else
	browserType = '??';

// The clicked iframe:
var iframe_Obj;

// For Firefox:
var inAdFrame = false;


// 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);
}
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( browserType == 'FF' )
	{	// Firefox: we cannot access the iFrame, so we'll try to be smart with the mouse position:
     window.addEventListener('mouseover', onMoveOutFF, true);
     window.addEventListener('unload', FFTrapClick, false);
	}

	// Note: (IE7) this will not work a second time if the user goes "back" to this page and clicks a second ad.
	var iframes = document.getElementsByTagName( 'iframe' );

	for( var i = 0; i < iframes.length; i++ )
	{	// For each iframe:
		if( iframes[i].src.indexOf('googlesyndication.com') > -1 )
		{	// This is an Adsense iframe...

			if(document.layers)
			{ // ?
				iframes[i].captureEvents(Events.ONFOCUS);
			}

			if( browserType == 'FF' )
		  {
		     iframes[i].addEventListener('mouseover', onMoveOverFF, true);
		  }
		  else
		  {
				iframes[i].onfocus = TrapClick;
			}
		}
	}

}

/**
 * Firefox; We're inside of the frame.
 */
function onMoveOverFF(e)
{
	inAdFrame = true;
	iframe_Obj = this;
}

/**
 * Firefox; We're outside of the frame.
 */
function onMoveOutFF(e)
{
	/*
	if( inAdFrame )
	{
	  alert( "OUT" );
	}
	*/
	inAdFrame = false;
}

/**
 * Firefox; We're leaving the page.
 */
function FFTrapClick(e)
{
	if( inAdFrame )
	{
	  trackAdsense();
	}
}

// Other browsers:
function TrapClick()
{
	iframe_Obj = event.srcElement;
	trackAdsense();
}


function trackAdsense()
{
	// encodeURIComponent not available everywhere. escape() should be good enough.
	var request = collector_url +
		'?from=' + escape(self.location) +
		'&title=' + escape(document.title) +
		'&ref=' + escape(document.referrer) +
		'&dest=' + escape(window.status) +
		'&browser=' + escape(browserType) +
		'&format=' + escape(get_Adsense_var( iframe_Obj, 'format')) +
		'&channel=' + escape(get_Adsense_var( iframe_Obj, 'channel')) +
		'&colbord=' + escape(get_Adsense_var( iframe_Obj, 'color_border')) +
		'&colbg=' + escape(get_Adsense_var( iframe_Obj, 'color_bg')) +
		'&collink=' + escape(get_Adsense_var( iframe_Obj, 'color_link')) +
		'&colurl=' + escape(get_Adsense_var( iframe_Obj, 'color_url')) +
		'&coltext=' + escape(get_Adsense_var( iframe_Obj, 'color_text'));

	// alert(request);

  var data = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try
	{
		data = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			data = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			data = false;
		}
	}
	@end @*/

	/*
	if( !data && typeof XMLHttpRequest != 'undefined')
	{
		data = new XMLHttpRequest();
	}

	if(data)
	{
	  //alert( "AJ sending" );
		// TODO: make sure it sends everytime
	  data.open("GET", request, false);
		data.send(null);
	}
	*/

	adsense_log_url_image = new Image();
  adsense_log_url_image.src = request;


  //alert( "ok" );
}

function get_Adsense_var( iframeObj, name )
{
	var dc = iframeObj.src;
	var prefix = name + "=";
	var begin = dc.indexOf("&" + prefix);
	if (begin == -1)
	{
	  begin = dc.indexOf("?" + prefix);
	  if (begin == -1) return null;
	}
	else
	{
	  begin += 1;
	}
	var end = dc.indexOf("&", begin);
	if (end == -1)
	  end = dc.length;

	return unescape(dc.substring(begin + prefix.length, end));
}
