// Get the element with the specified id and for
// each link found (a tag) highlight it differently
// and ensure it opens in a new window
function mark_external_links(id){
  var element = document.getElementById(id);

  if(element){
    var links = element.getElementsByTagName('a');

     for(i=0;i<links.length;i++){
      var link = links[i];

      // Only add highlight if link is to an external site
      // and is a http or https (so e.g. not mailto links)
      if(link.hostname != location.host && link.href.match(/^https*:\/\//)){
        var ext_icon = document.createElement('img');
        ext_icon.setAttribute('src', '/include/graphics/ext_link_16x16.gif');
        ext_icon.setAttribute('alt','External link');
        ext_icon.setAttribute('height','16');
        ext_icon.setAttribute('width','16');

	      link.appendChild(ext_icon);

        link.setAttribute('target','_new');
	      if(!link.title){
          link.setAttribute('title','Go to external site');
	      }
      }
    }
  }
}


// Sets tr_horizontal class if a table is found to
// have more than one <th> tag in the same tr tag, sets class
// to tr_vertical otherwise
function set_th_direction_class(id){
  var element = document.getElementById(id);

  // Get all tables in element
  var tables = element.getElementsByTagName('table');

  // For each table examine the tr tags
  for(i=0;i<tables.length;i++){
    var trs = tables[i].getElementsByTagName('tr');
 
    // If more than one th tags in the tr tag
    // set horizontal class on table, then continue
    // with next table, else set table as vertical style
    var ths = trs[i].getElementsByTagName('th');
    var th_vertical_count = 0;

    if(ths.length>1){
      tables[i].setAttribute('class', tables[i].className + ' tr_horizontal');
      continue
    }
    else if(ths.length==1){
      th_vertical_count++;
    }
    
    if(th_vertical_count>1){
      tables[i].setAttribute('class', tables[i].className + ' tr_vertical');
      continue
    }
  }
}

// Track links in analytics
document.onclick = function(event) {
 
    event = event || window.event;
    var target = event.target || event.srcElement,
        targetElement = target.tagName.toLowerCase();
 
    if (targetElement == "a") {
        var href = target.getAttribute("href"),
            urlHost = document.domain.replace(/^www\./i,""),
            urlPattern = "^(?:https?:)?\/\/(?:(?:www)\.)?" + urlHost + "\/?";
        if (href) {
            eventCheck(href,urlPattern);
        }    
    }
 
    function eventCheck(href,urlPattern){
        if ((href.match(/^https?\:/i)) && (!href.match(urlPattern))){
            if (href.match(/^.*\.(pdf|jpg|png|gif|zip|mp3|txt|doc|rar|js|py)$/i)) {
                _gaq.push(['_trackEvent', 'Download', 'click', href]);
            } else {
                _gaq.push(['_trackEvent', 'External', 'click', href]);
            }
        } else if (href.match(/^mailto\:/i)) {
            _gaq.push(['_trackEvent', 'Email', 'click', href.substr(7)]);
        } else if (href.match(/^.*\.(pdf|jpg|png|gif|zip|mp3|txt|doc|rar|js|py|tar\.gz|deb)$/i)) {
            _gaq.push(['_trackEvent', 'Download', 'click', href]);
        }
    }
};

window.onload=function(){
  if(document.getElementById('frontpage_content')) {
    var content_element='frontpage_content';
  }
  else {
    var content_element='content';
  }
  set_th_direction_class(content_element);
  mark_external_links(content_element);
}

