// add_urchin.js
// This script automatically adds the urchinTracker code from Google
// Analytics to each link in an html page, if that link points to a 
// file with one of the specified suffixes.
//
// To use this code, just add the following script at the end of each 
// html page, right after the Google Analytics code.
// <script src="add_urchin.js" type="text/javascript"></script>
// For an example, see http://www.cs.toronto.edu/~dross/
//
// Author: David Ross, July 2006.
// http://www.cs.toronto.edu/~dross/
// Comments and suggestions are welcome.

// This variable specifies the number of initial characters to skip 
// when converting a url to an identifier string for urchinTracker.
// For example, using the value of 25
// http://www.cs.toronto.edu/~dross/add_urchin.js becomes 
// /~dross/add_urchin.js
var url_skip = 25

// List of file types whose links should be tracked
var suffixes = Array(".ps.gz", ".pdf", ".jpg", ".avi", ".m",".mat",".tgz", ".zip");

if (document.getElementsByTagName) {
    var ahrefs = document.getElementsByTagName('a');
    for (var i=0; i<ahrefs.length;i++) {
	var has_suffix = false;
	for(var j=0; j<suffixes.length; j++) {
            var endpart = ahrefs[i].href.slice(-suffixes[j].length)
            if (endpart == suffixes[j]) {
              has_suffix = true;
              break;
            }
	}
	//alert(ahrefs[i].href + ' ' + has_suffix)
        if (has_suffix && !ahrefs[i].onclick) {
            ahrefs[i].onclick = function () { 
                var track = this.href + ''; 
                //alert('TESTING! ' + track.substring(url_skip)); 
                urchinTracker(track.substring(url_skip)); }
        }
    }
}

