DOMAIN_PATTERN_REGEXP = new RegExp( Ingenta.Links.DOMAIN_PATTERN );
OFFSET_TYPE = "minutes";
OFFSET_AMOUNT = 10;

function twoDigits(i) {
   if (i<10){
      i = '0' + i
   }
   return i
}

function embedLinks() {
   
   var processor = new Ingenta.Links.LinkProcessor(
      Ingenta.Links.domainMatcher,
      function(anchor, href) {
         var urlParts = new DecomposedUrl(href);
         var newUrl = new UrlRewriter(urlParts);
         var recombinedUrl = newUrl.trustedIngentaConnectLink();
         anchor.href = recombinedUrl;
      }
   );
   
   processor.process();
}

/*
 * global vars for setting up timestamps
 */
var now = new Date();
var timeStamp = new CreateTimestamp(now, OFFSET_AMOUNT, OFFSET_TYPE);

/*
 * Creates an offsetted timestamp
 */
function CreateTimestamp (date, amount, type) {
   if ( amount != 0 ) {
      this.millis = date.getTime();
      if ( type == "hours" ) {
         this.millis = this.millis + amount * 60 * 60 * 1000;
      }
      else if ( type == "minutes" ) {
         this.millis = this.millis + amount * 60 * 1000;
      }
      else if ( type == "seconds" ) {
         this.millis = this.millis + amount * 1000;
      }
      date.setTime( this.millis );
   }
   this.output = '' + date.getUTCFullYear() + twoDigits(date.getUTCMonth() + 1)
         + twoDigits(date.getUTCDate()) + twoDigits(date.getUTCHours())
         + twoDigits(date.getUTCMinutes()) + twoDigits(date.getUTCSeconds());
}

/*
 * decomposes a url into address, an object containing the url parameters and
 * the anchor component
 */
DecomposedUrl = function (url) {
   this.wholeUrl = url;
}

/*
 * returns the string in the url after the ?
 */
DecomposedUrl.prototype.queryString = function() {
   if ( this.wholeUrl.indexOf('?') != -1 ) {
      return this.wholeUrl.split('?').pop();
   }
   return false;
}

/*
 * returns the string before the ? in the url
 */
DecomposedUrl.prototype.address = function() {
   if ( this.wholeUrl.indexOf('?') != -1 ) {
      return this.wholeUrl.split('?').shift();
   }
   return this.wholeUrl.split('#').shift();
}

/*
 * returns the pathInfo in the url
 */
DecomposedUrl.prototype.pathInfo = function() {
   return this.address().replace( DOMAIN_PATTERN_REGEXP, "/" );
}

/*
 * returns the string after a # in the url or false if it is not present;
 */
DecomposedUrl.prototype.pageAnchor = function() {
   if (this.queryString() && this.queryString().match('#')) {
      return this.queryString().split('#').pop();
   }
   if ( this.wholeUrl.indexOf('#') != -1 ) {
      return this.wholeUrl.split('#').pop();
   }
   return false;
}

/*
 * returns an object containing the name, value pairs from the url parameters
 */
DecomposedUrl.prototype.urlParameters = function() {
   if ( this.queryString() ) {
      var paramsObj = new Object();
      var queryArray = this.queryString().split('#').shift().split('&');
      var kvPair;
      for (var i = 0; i < queryArray.length; i++) {
         kvPair = queryArray[i].split('=');
         paramsObj[kvPair[0]] = kvPair[1];
      }
      return paramsObj;
   }
}

/*
 * an object for rewriting a decomposed url in order to add trusted links
 */
UrlRewriter = function (decomposedUrl) {
   this.decomposedUrl = decomposedUrl;
}

/*
 * mungs the data into a trusted url
 */
UrlRewriter.prototype.trustedIngentaConnectLink = function () {
   var baseString = 'originator=' + originator + '&identity=' + identity
               + '&timestamp=' + timeStamp.output;
   if (window.username) {
      baseString = baseString + '&username=' + username;
   }
   var beforeHashString = this.decomposedUrl.pathInfo()
                   + '?' + baseString + '&token=' + token;
   var hashString = MD5(beforeHashString);
   var returnedUrl = this.decomposedUrl.address() + '?' + baseString
               + '&signature=' + hashString;
   for ( var index in this.decomposedUrl.urlParameters()) {
      returnedUrl = returnedUrl + '&' + index + '='
               + this.decomposedUrl.urlParameters()[index];
   }
   if (this.decomposedUrl.pageAnchor()) {
      returnedUrl = returnedUrl + '#' + this.decomposedUrl.pageAnchor();
   }
   return returnedUrl;
}

addLoadEvent(embedLinks);