//script from Rebecca Murphey
//http://blog.rebeccamurphey.com/
//with modifications by Ted Lawless
(function($) {
  $('document').ready(function() {  
    $('a').each(function() {
      var $a = $(this);
      var href = $a.attr('href');
      //below - check to see if link is external
      //all proxied urls included
      if ( ((href.match(/^http/)) && (! href.match(document.domain))
	      && (! href.match(/^.*jy2lg7py6p.*/))) 
	      ||(href.match(/^http:\/\/0-/)) ) {
            //add external link to proxied urls
	    //this is for pdf and new window icons
	    $a.attr({ target: "_blank" })
	    //clean up urls
	    //first step - get rid of leading url characters
	    var re1 = /^http:\/\/www\.|^http:\/\/0\-|^http:\/\/|^https:\/\/www\.|^https:\/\/|/;
	    var ga_name = href.replace(re1, "");
	    //second step - remove proxy information
	    var re2 = /^(www\.|)(.*)\.pegasus\.law\.columbia\.edu(.*)/;
	    var ga_name = ga_name.replace(re2, "$3");
	    var title = $("td.bibInfoData strong")[0];
	    var title = $(title).text();
	    var track = '/eres/' + $a.text() + '/' + title;
	    var bibnum =(($("a#recordnum").attr('href')).replace(/.*(b[0-9]{6})/, "$1")).substring(0,7);
	    // if so, add the GA tracking code
	    $a.click(function() {
	       pageTracker._trackPageview('/ext/' + ga_name);
	       pageTracker._trackPageview('/exitbibs/' + bibnum);
	       pageTracker._trackPageview(track);
	       //for testing 
	       //console.debug("you justed clicked on " + track)
	    });
  }
  });
 });
})(jQuery);


