sbdt      = window.sbdt || {};

// ------------------------------------------
// A reusable pattern replacement.
// sbdt.patchLocator extends this behavior.
//
// For usage example, see 
// sbdt.patchLocator.execute(rootHTMLNode);

sbdt.patternLocator = {};

sbdt.patternLocator.getInstance = function() {
  function F() {}
  F.prototype = sbdt.patternLocator;
  return new F();
}

/**
 * Returns true if a text node matches the regular expression pattern.
 */
sbdt.patternLocator.test = function (node, pattern)
{
  if (node.nodeType == 3) {
    // 3 == TextNode
    var text = node.data;
    return (text && pattern.test(text));
  }
  return false;
}

/**
 * Processes a single text node.
 */
sbdt.patternLocator.process = function (m, pattern, patternConverter)
{
  var text = m.data;
  if (text) {
    var result;
    var startPos = 0;
    var outputArr = [];
    
    // collect data
    while ((result = pattern.exec(text)) != null) {
      var index = result.index;
      var match = result[0];
      
      // console.log("match " + match);
      // console.log("index " + index);
      var fragment = text.substr(startPos, result.index - startPos);
      outputArr.push(document.createTextNode(fragment));
      var convertedText = patternConverter(match);
      if (convertedText) {
        outputArr.push(convertedText);
      }
      
      startPos = result.index + match.length;
    }
    if (startPos != 0) {
      var fragment = text.substr(startPos);
      outputArr.push(document.createTextNode(fragment));
    }
    var outputLength = outputArr.length;
    if (outputLength > 0) {
      var parentNode = m.parentNode;
      for (var i = 0; i < outputLength; ++i) {
        if (i + 1 == outputLength) {
          parentNode.replaceChild(outputArr[i], m);
        } else {
          parentNode.insertBefore(outputArr[i], m);
        }
      }
    }
  }
}

/**
 * Replaces all occurrences of "patch 012345..." text and replace
 * it with a link.
 */
sbdt.patternLocator.execute = function (n, doneFunc)
{
  // we should not block UI for more than 500ms.
  var maxDuration = 500;
  
  var nodesFound = [];
  var itr = sbdt.domIterator;
  itr.setNode(n);
  var processedCount = 0;
  
  // the regular expression used for searching
  // cannot be used for prcessing
  // because the "g" flag makes the regular expression stateful.
  var patternRegExp = new RegExp(this.pattern, "i");
  var beginTime = new Date();
  var that = this;
  
  // an anonymous function that can be executed many times.
  // in each iteration, it will start from where it left off.
  (function() {
    var currentTime = new Date();
    
    // search phase.
    while (itr.hasNext()) {
      var node = itr.next();
      if (that.test(node, patternRegExp)) {
        nodesFound.push(node);
      }
      if (maxDuration < (new Date() - currentTime)) {
        setTimeout(arguments.callee, 10);
        return;
      }
    }
    
    // replacement phase.
    while (processedCount < nodesFound.length) {
      var replaceRegExp = new RegExp(that.pattern, "ig");
      that.process(nodesFound[processedCount++], replaceRegExp, that.patternConverter);
      
      if (maxDuration < (new Date() - currentTime)) {
        setTimeout(arguments.callee, 10);
        return;
      }
    }

    if (doneFunc) {
      doneFunc();
      // sbdt.log("Took " + (new Date()- beginTime));
    }
  })();
}

// ------------------------------------------
// A DOM iterator.

/**
 * Simulates the DOM tree as a flat iterator.
 */
sbdt.domIterator = {};
sbdt.domIterator.setNode = function(n) {
  this.m_root = n;
  this.m_curr = n;
}

/**
 * Returns true if there are more elements.
 */
sbdt.domIterator.hasNext = function() {
  return (this.m_curr != null);
}

/**
 * Performs preorder traversal on the DOM tree.
 * returns all the nodes found in document order.
 */
sbdt.domIterator.next = function() {
  var result = this.m_curr;
  
  var curr = this.m_curr;
  if (curr) {
    if (curr.childNodes.length > 0) {
      // should go down.
      this.m_curr = curr.childNodes.item(0);
    } else if (curr.nextSibling) {
      // should go across.
      this.m_curr = curr.nextSibling;
    } else {
      // should go up
      var tmp = curr;
      while (tmp) {
        if (tmp == this.m_root) {
          this.m_curr = null;
          break;
        } else if (tmp.nextSibling) {
          this.m_curr = tmp.nextSibling;
          break;
        } else {
          tmp = tmp.parentNode;
        }
      }
    }
  }
  return result;
}

sbdt.locationLocator = sbdt.patternLocator.getInstance();

sbdt.locationLocator.execute = function (n)
{
  this.patternConverter = function (text) {
    var link = document.createElement("a");
    link.href= "/location";
    link.innerHTML = text;
    return link;
  }
  this.pattern = "Rains Hacienda Commons";
  sbdt.patternLocator.execute.call(this, n);
  this.pattern = "Ford Center";
  sbdt.patternLocator.execute.call(this, n);
  this.pattern = "The Axe & Palm";
  sbdt.patternLocator.execute.call(this, n);
  this.pattern = "Graduate Community Center";
  sbdt.patternLocator.execute.call(this, n);
  this.pattern = "Old Union Clubhouse Ballroom";
  sbdt.patternLocator.execute.call(this, n);
  this.pattern = "Arrillaga Family Sports Center";
  sbdt.patternLocator.execute.call(this, n);
  this.pattern = "Bechtel International Center";
  sbdt.patternLocator.execute.call(this, n);
}

$(document).ready(function(){
  $('a[href$="/wp-login.php"]').html('Administrator Log in')
});
$(document).ready(sbdt.locationLocator.execute(document.body));

// Google Analytics start
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
try {
var pageTracker = _gat._getTracker("UA-1280491-3");
pageTracker._trackPageview();
} catch(err) {}
// Google Analytics end 


