// requires that the prototype library be loaded already //
window.onload = onWindowLoad;

// path to the logging backend application //
var cc_logURL = "http://www.atkuniforms.com/tracker/tracker.php";


// use this string for grouping more than one area //
var cc_sectionName = 'ATKUNIFORM';

function onWindowLoad() {
  // add onclick event handlers to all <a> and <area> tags //
  var cc_anchors = document.getElementsByTagName('a'); 
  var cc_areas = document.getElementsByTagName('area');
  var cc_a = new Array();
  
  // combine arrays
for (var i=0; i<cc_areas.length; i++) {
  cc_a.push(cc_areas[i]);
}
for (var i=0; i<cc_anchors.length; i++) {
  cc_a.push(cc_anchors[i]);
}
  
  // detect browser properties //
  for (var i=0; i<cc_a.length; i++) {
    j = cc_a[i];
    
    // track only links in certain areas
    // if ( inID(j,"maincontent") || inID(j,"navbardiv") || inID(j,"sidebar") ) {
    if (true) { // all links
    
    if ( j.addEventListener ) { // MozFF //
      j.addEventListener('click',cc_clickHandler,true);   
    } else if ( j.attachEvent ) { // IE //
      j.attachEvent('onclick',cc_clickHandler);   
    }
    
    }
  }
}

function cc_clickHandler(e) {
  // gather data to post //
  var d = new Date();    // current date
  var curURL = window.location;    // current URL
  
  // detect browser properties //
  if ( e.currentTarget ) { // MozFF //
    var linkURL = getHREF(e.currentTarget);    // target URL
    var linkText = getInnerText(e.currentTarget);    // link text
  } else if ( e.srcElement ) { // IE //
    var linkURL = getHREF(e.srcElement);    // target URL
    var linkText = getInnerText(e.srcElement);    // link text
  }
  
  // HTTP request string //
  var reqStr = 'date=' + d.valueOf() + '&curURL=' + encodeURIComponent(curURL) + '&linkURL=' + encodeURIComponent(linkURL) + '&linkText=' + encodeURIComponent(linkText) + '&sectionName=' + cc_sectionName;
  
  // Ajax request //
  var r = new Ajax.Request(
    cc_logURL,
    {
      method: 'get',
      parameters: reqStr,
      onSuccess: showResult,
      onFailure: showResult,
      on404: showResult
    }
  );
  
  // block the link from being loaded //
  if ( e.preventDefault ) { // MozFF //
    e.preventDefault();
  } else if ( e.returnValue ) { // IE //
    e.returnValue = false;
  }

  // go after a short delay to allow Ajax to complete //
  window.setTimeout('execRedirect("'+linkURL+'")',100);
}

function execRedirect(url) {
  window.location = url;
}

function showResult(obj) {
  //alert(obj.responseText);
}

function inClass(el,search) {
  // recursively checks parent nodes for a particular class name
  var p = el.parentNode;
  classRegex = new RegExp (search, "gi");
  
  if (el.className.search(classRegex) != -1) {
    return true; // found it
  } else if (p == document) {
    return false; // reached the top without finding it
  } else {
    return inClass(p,search); // not here, check the parent
  }  
}

function inID(el,search) {
  // recursively checks parent nodes for a particular id name
  var p = el.parentNode;
  idRegex = new RegExp (search, "gi");
  
  if (el.id.search(idRegex) != -1) {
    return true; // found it
  } else if (p == document) {
    return false; // reached the top without finding it
  } else {
    return inID(p,search); // not here, check the parent
  }  
}

// these functions have some wierdness. IE and Moz/FF are handling things
// differently, and it's not really resolved here, though it does work.
// file under ugly hacks.
function getInnerText(el) {
  if ( el.tagName == "IMG" ) {  // IE image links //
    if (el.getAttribute('alt')) {
      return "(image) "+el.getAttribute('alt');
    } else {
      return "(image) "+el.src;
    }
  } else if (el.tagName == "AREA") {
    return "(area) "+el.getAttribute('alt');
  } else if ( el.firstChild.tagName == "IMG" ) { // MozFF image links //
    if (el.firstChild.getAttribute('alt')) {
      return "(image) "+el.firstChild.getAttribute('alt');
    } else {
      return "(image) "+el.firstChild.src;
    }
  } else { // text links //
    return el.firstChild.nodeValue;
  }
}

function getHREF(el) {
  // For image links in IE, el is the image, for some reason
  if ( el.tagName == "IMG" ) {
    return el.parentNode.href;
  } else {
    return el.href;
  }
}

