
// This is a stripped down version of (c)Glenn Jones code - April 06

function addLinkTracker()
{
	if (!document.getElementsByTagName) return false;
	
	linksElements = document.getElementsByTagName('a')
	for (var i = 0; i < linksElements.length; i++) 
	{
		addEvent(linksElements[i], 'mousedown', recordClick, false);
		// If a link does not have any id it is given one
		if (! linksElements[i].getAttribute('id') )
			linksElements[i].setAttribute('id',"link_" + i)
	}
}

function addEvent(elm, evType, fn, useCapture) 
{
  // cross-browser event handling for IE5+, NS6 and Mozilla By Scott Andrew 
  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;
  }
}

function recordClick(e)
{
	if (typeof e == 'undefined')
		var e = window.event;

	var source;
	if (typeof e.target != 'undefined') 
	{
		source = e.target;
	} else if (typeof e.srcElement != 'undefined') {
		source = e.srcElement;
	} else {
		return true;
	}

	if (source.nodeType == 3)
		source = source.parentNode;

	var id, record, target, url, label
	
	/*if( source.tagName == "IMG" )
	{
		if( source.parentNode.tagName == "A" )
		{
			id = source.parentNode.getAttribute('id');
			target = source.parentNode.getAttribute('href');
		}
		label = source.getAttribute("alt");
	}else{
		id = source.getAttribute('id');
		target = source.getAttribute('href');
		label = source.childNodes[0].nodeValue;
		record = source.getAttribute('record');
	}
	url = document.location.href;*/

	if( source.tagName == "IMG" )
	{
		id = source.parentNode.getAttribute('id');
		record = source.parentNode.getAttribute('record');
	}
	else
	{
		id = source.getAttribute('id');
		record = source.getAttribute('record');
	}

	try
	{
		//alert('id: ' + id);
		//alert('record: ' + record + ' - len: ' + record.length);
		if(record.length > 0)
		{
			var pars = '';	
			if (document.location.href.toLowerCase().indexOf("http://www.") >= 0) {
		        apiurl = "http://www.1stdirectory.com/addClick.aspx?id=" + id + "&record=" + record + "&rand=" + Math.random();
		    }
		    else {
		        apiurl = "http://1stdirectory.com/addClick.aspx?id=" + id + "&record=" + record + "&rand=" + Math.random();
		    }
			//apiurl = url + "/addClick.aspx?id=" + id + "&record=" + record + "&rand=" + Math.random();
			
			//alert('url: ' + apiurl);
			ajaxRequest = new Ajax.Request(apiurl, {method: 'get', parameters: pars, onComplete: passThrough});
		}
	}
	catch(e) {
	    //alert('test' + e);
	}
	
	return false;	
}

function passThrough( originalRequest )
{
	// Helps debug api errors
	//alert( originalRequest.responseText );
}

addEvent(window, 'load', addLinkTracker, false);
