//============================================
// Wayback Common JS Library
//============================================

var WB_wombat_archivePrefix;
var WB_wombat_origHost;

function WB_wombat_Init(archivePrefix, origHost)
{
  WB_wombat_archivePrefix = archivePrefix;
  WB_wombat_origHost = origHost;
}
	
function WB_StripPort(str)
{
  var hostWithPort = str.match(/^http:\/\/[\w\d@.-]+:\d+/);
  if (hostWithPort) {
     var hostName = hostWithPort[0].substr(0, hostWithPort[0].lastIndexOf(':'));
     return hostName + str.substr(hostWithPort[0].length);
  }

  return str;
}

function WB_IsHostUrl(str)
{
  // Good guess that's its a hostname
  if (str.indexOf("www.") == 0) {
    return true;
  }
  
  // hostname:port (port required)
  var matches = str.match(/^[\w-]+(\.[\w-_]+)+(:\d+)(\/|$)/);
  if (matches && (matches[0].length < 64)) {
    return true;
  }
  
  // ip:port
  matches = str.match(/^\d+\.\d+\.\d+\.\d+(:\d+)?(\/|$)/);
  if (matches && (matches[0].length < 64)) {
    return true;
  }

  return false;
}

function WB_RewriteUrl(url)
{
  var httpPrefix = "http://";

  // If not dealing with a string, just return it
  if (!url || (typeof url) != "string") {
    return url;
  }
  
  // If starts with prefix, no rewriting needed
  if (url.indexOf(WB_wombat_archivePrefix) == 0) {
    return url;
  }
  
  // If server relative url, add prefix and original host
  if (url.charAt(0) == "/") {
    return WB_wombat_archivePrefix + WB_wombat_origHost + url;
  }
  
  // If full url starting with http://, add prefix
  if (url.indexOf(httpPrefix) == 0) {
    return WB_wombat_archivePrefix + url;
  }
  
  // May or may not be a hostname, call function to determine
  // If it is, add the prefix and make sure port is removed
  if (WB_IsHostUrl(url)) {
    return WB_wombat_archivePrefix + httpPrefix + url;
  }

  return url;
}

function WB_CopyObjectFields(obj)
{
  var newObj = {};
  
  for (prop in obj) {
    if ((typeof obj[prop]) != "function") {
      newObj[prop] = obj[prop];
    }
  }
  
  return newObj;
}
  
function WB_CopyLocationObj(loc)
{
  var newLoc = WB_CopyObjectFields(loc);
  
  newLoc._origLoc = loc;
  newLoc._origHref = loc.toString();
  
  // Rewrite replace and assign functions
  newLoc.replace = function(url) { this._origLoc.replace(WB_RewriteUrl(url)); }
  newLoc.assign = function(url) { this._origLoc.assign(WB_RewriteUrl(url)); }
  newLoc.reload = loc.reload;
  newLoc.toString = function() { return this.href; }
  
  return newLoc;
}


// Location objects
var WB_wombat_self_location = WB_CopyLocationObj(window.self.location);
var WB_wombat_top_location = ((window.self.location != window.top.location) ? WB_CopyLocationObj(window.top.location) : WB_wombat_self_location);
var WB_wombat_opener_location = (window.opener ? WB_CopyLocationObj(window.opener.location) : null);

function WB_wombat_checkLocationChange(wbLoc, isTop)
{
  var locType = (typeof wbLoc);
	
  // String has been assigned to location, so assign it
  if (locType == "string") {
    if (isTop) {
	  window.top.location = WB_RewriteUrl(wbLoc);
    } else {
      window.location = WB_RewriteUrl(wbLoc);
	}

  } else if (locType == "object") {
    if (wbLoc.href) {
      if (!wbLoc._origHref || (wbLoc._origHref != wbLoc.href)) {
        if (isTop) {
          window.top.location.href = WB_RewriteUrl(wbLoc.href);
        } else {
    	  window.location.href = WB_RewriteUrl(wbLoc.href);
        }				
      }
    }
  }
}

function WB_wombat_checkLocations()
{
  WB_wombat_checkLocationChange(WB_wombat_self_location, false);
  
  if (WB_wombat_self_location != WB_wombat_top_location) {
    WB_wombat_checkLocationChange(WB_wombat_top_location, true);
  }
}

// Check quickly after page load
setTimeout(WB_wombat_checkLocations, 100);
setTimeout(WB_wombat_checkLocations, 1000);

// Check periodically every few seconds
setInterval(WB_wombat_checkLocations, 10000);
