if (!window.__thankthis__) {

// static data
var thankthis_site_key = "1c75e0db389df551638b0741d7c1d581aebf881d470322a6212646f8f99d30b5";
var thankthis_stat_url = 'http://www.thankthis.com/ajax/stat/';
var thankthis_overlay_url = 'http://www.thankthis.com/overlay/';
var thankthis_overlay_blank_url = 'http://www.thankthis.com/black.html';
var thankthis_xd_recv_url = 'http://www.thankthis.com/page/xd_receiver.html';
var thankthis_image_base = 'http://www.thankthis.com/static/img';

// closuare
(function() {

// include utility
// add Array.indexOf
if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function(elt /*, from*/) {
    var len = this.length >>> 0;

    var from = Number(arguments[1]) || 0;
    from = (from < 0) ? Math.ceil(from) : Math.floor(from);
    if (from < 0) from += len;

    for (; from < len; from++) {
      if (from in this && this[from] === elt)
        return from;
    }
    return -1;
  };
}

// hitch obj to func so that func sees obj as 'this'
var hitch = function(func, obj) {
  obj = obj || window;
  if (arguments.length > 2) {
    var _arg = Array.prototype.slice.call(arguments, 2);
    return function() {
      var arg = Array.prototype.slice.call(arguments);
      Array.prototype.unshift.apply(arg, _arg);
      return func.apply(obj, arg);
    }
  } else {
    return function() {
      return func.apply(obj, arguments)
    }
  }
};

// detect user-agent, user-agent version, platform
var isOpera, isIE, isWebKit, isMobileWebKit, isFireFox, isMac, isWin, isLnx, isX11;
var userAgent = window.navigator ? window.navigator.userAgent : null;
var platform = window.navigator ? window.navigator.platform : "";
(function() {
  isFireFox= isMobileWebKit = isWebKit = isIE = isOpera = false;
  if (userAgent) {
    var b = window.navigator;
    isOpera = (userAgent.indexOf("Opera")==0);
    isIE = !isOpera && (userAgent.indexOf("MSIE") != -1);
    isMobileWebKit = (isWebKit = !isOpera && userAgent.indexOf("WebKit") != -1) && (userAgent.indexOf("Mobile") != -1);
    isFireFox = !isOpera && !isWebKit && b.product=="Gecko"
  }

  isMac = platform.indexOf("Mac") != -1;
  isWin = platform.indexOf("Win") != -1;
  isLnx = platform.indexOf("Linux") != -1;
  isX11 = !!window.navigator && (window.navigator.appVersion || "").indexOf("X11") != -1;
})();
var userAgentVersion = (function() {
  var ver = "", rx;
  if (isOpera && window.opera) {
    ver = window.opera.version;
    ver = (typeof ver == "function") ? ver() : ver;
  } else {
    if (isFireFox) {
      rx = /rv\:([^\);]+)(\)|;)/;
    } else if (isIE) {
      rx = /MSIE\s+([^\);]+)(\)|;)/;
    } else if(isWebKit) {
      rx = /WebKit\/(\S+)/;
    }
    if (rx) {
      ver = (ver = rx.exec(userAgent)) ? ver[1] : "";
    }
  }
  return ver;
})();

var _isUserAgentVersion = {};
var isUserAgentVersion = function(v) {
  return _isUserAgentVersion[v] || (_isUserAgentVersion[v] = parseUserAgentVersion(userAgentVersion, v) >= 0);
};

var parseUserAgentVersion = function(a, b) {
  for (var c = 0, d = String(a).replace(/^[\s\xa0]+|[\s\xa0]+$/g,"").split("."), e = String(b).replace(/^[\s\xa0]+|[\s\xa0]+$/g,"").split("."), g = Math.max(d.length,e.length), f = 0; (c == 0) && (f < g); f++) {
    var j = d[f] || "", k = e[f] || "", l = new RegExp("(\\d*)(\\D*)","g"), m = new RegExp("(\\d*)(\\D*)","g");
    do {
      var h = l.exec(j) || ["","",""], i = m.exec(k) || ["","",""];
      if ((h[0].length == 0) && (i[0].length == 0)) { break; }
      c = (h[1].length == 0) ? 0 : parseInt(h[1],10);
      var n = (i[1].length == 0) ? 0 : parseInt(i[1],10);
      c = cmp(c, n) || cmp(h[2].length == 0, i[2].length==0) || cmp(h[2], i[2]);
    } while (c == 0);
  }
  return c;
};

var cmp = function(a, b) {
  if (a < b) {
    return -1;
  } else if (a > b) {
    return 1;
  }
  return 0;
};

// select elements by tag name and/or classname
var selectElements = function(tagName, className, domroot) {
  domroot = domroot || document;
  tagName = (tagName && (tagName != "*")) ? tagName.toUpperCase() : "";
  if (domroot.querySelectorAll && (tagName || className) && (!isWebKit || (document.compatMode == "CSS1Compat") || isUserAgentVersion("528"))) {
    className = tagName + (className ? "." + className : "");
    return domroot.querySelectorAll(className)
  }
  if (className && domroot.getElementsByClassName) {
    var a = domroot.getElementsByClassName(className);
    if (tagName) {
      var d = {};
      for (var e = 0, g = 0, f; f = a[g]; g++) {
        if (tagName == f.nodeName) {
          d[e++] = f;
        }
      }
      d.length = e;
      return d;
    } else {
      return a;
    }
  }
  a = domroot.getElementsByTagName(tagName || "*");
  if (className) {
    d = {};
    for (g = e = 0; f = a[g]; g++) {
      var b = f.className;
      if (typeof b.split=="function" && (b.split(/\s+/).indexOf(className) >= 0)) {
        d[e++]=f;
      }
    }
    d.length=e;
    return d;
  } else {
    return a;
  }
};

// add function as object (at any level) in root
var declare_object = function(objname, func, root) {
  _objname = objname.split(".");
  root = root || window;
  !(_objname[0] in root) && root.execScript && root.execScript("var "+_objname[0]);
  for (var d; _objname.length && (d = _objname.shift());) {
    if (!_objname.length && func !== undefined) {
      root[d] = func;
    } else {
      root = root[d] ? root[d] : (root[d] = {});
    }
  }
};

// create DOM element with name
var createNamedElement = function(tag, name) {
  var element = null;

  // try the IE way; this fails on standards-compliant browsers
  try {
    element = document.createElement('<'+tag+' name="'+name+'">');
  } catch (e) { }
  if (!element || element.nodeName != tag.toUpperCase()) {
    // non-IE browser; use canonical method to create an element and set name
    element = document.createElement(tag);
    element.name = name;
 }
 return element;
};


// include button class
var _button_config = {
  'normal-count': {
    html:'<span id="${id}" class="${class}"></span>',
    baseClassName:'thank-counter',
    displayCount: true
  },
  'small-count': {
    html:'<span id="${id}" class="${class}"></span>',
    baseClassName:'thank-counter-long',
    displayCount: true
  }
};

var _button_CSS =
"a.thankthis-button, a.thankthis-button:visited, a.thankthis-button:hover {\n"+
"  text-decoration: none;\n"+
"  margin: 0;\n"+
"  padding: 0;\n"+
"  cursor: pointer;\n"+
"}\n"+
"a.thankthis-button img {\n"+
"  border: 0;\n"+
"  margin: 0;\n"+
"  padding: 0;\n"+
"}\n"+
"a.thankthis-button .thank-counter {\n"+
"  background: transparent url("+thankthis_image_base+"/thankthis-counter.${imgSuffix}) repeat;\n"+
"  display: inline-block;\n"+
"  text-align:center;\n"+
"  margin: 0;\n"+
"  padding: 0;\n"+
"  width:50px;\n"+
"  height:60px;\n"+
"  color:#555;\n"+
"  font-family:sans-serif;\n"+
"  text-decoration: none;\n"+
"  font-size:20px;\n"+
"  line-height: 26px;\n"+
"  font-weight:bold;\n"+
"}\n"+
"a.thankthis-button .thank-counter .thank-count-100 {font-size: 17px;}\n"+
"a.thankthis-button .thank-counter .thank-count-1000 {font-size: 15px;}\n"+
"a.thankthis-button .thank-counter .thank-count-10000 {font-size: 13px;}\n"+
"a.thankthis-button .thank-counter .thank-count-100000 {font-size: 10px;}\n"+
"a.thankthis-button .thank-counter .thank-count-1000000 {font-size: 9px;}\n"+
"a.thankthis-button .thank-counter-long {\n"+
"  background: transparent url("+thankthis_image_base+"/thankthis-counter-long.${imgSuffix}) repeat;\n"+
"  display: inline-block;\n"+
"  text-align:center;\n"+
"  margin: 0;\n"+
"  padding: 0 0 0 58px;\n"+
"  width:36px;\n"+
"  line-height: 18px;\n"+
"  height:18px;\n"+
"  color:#555;\n"+
"  font-family:sans-serif;\n"+
"  text-decoration: none;\n"+
"  font-size:12px;\n"+
"  font-weight:bold;\n"+
"}\n"+
"a.thankthis-button .thank-counter-long .thank-count-100 {font-size: 12px;}\n"+
"a.thankthis-button .thank-counter-long .thank-count-1000 {font-size: 12px;}\n"+
"a.thankthis-button .thank-counter-long .thank-count-10000 {font-size: 10px;}\n"+
"a.thankthis-button .thank-counter-long .thank-count-100000 {font-size: 9px;}\n"+
"a.thankthis-button .thank-counter-long .thank-count-1000000 {font-size: 8px;}\n"+
"#thankthis-backdrop { width:100%; height:100%; position:fixed; top:0; left:0; z-index:65536; background-color:#333; filter:alpha(opacity=80); opacity:0.8; }\n"+
"#thankthis-overlay { border:0; width:600px; height:400px; position:fixed; left:50%; top:50%; margin-left:-300px; margin-top:-200px; z-index:65537; }\n"+
"#thankthis-overlay-iframe { width:600px; height:400px; overflow:hidden; z-index:65538; border:0; background-color:black; }\n"+
"#thankthis-overlay-close { position:absolute; bottom:5px; right:5px; height:16px; line-height:16px;font-family:sans-serif; color:gray; margin:0;padding:0; float:left; z-index:65539; cursor:pointer; }\n"+
"#thankthis-overlay-close a { text-decoration:none; font-size:14px; color:gray; }\n"+
"#thankthis-overlay-close a:hover { color:#ff7f1f; }";

// class definition
var _button = function(processor, element) {
  this.processor = processor;
  this.id = 'thankthis-' + String(Math.round(2147483647*Math.random()));
  this.element = element;
  this.buttonStyle = element.getAttribute('data-button-style') || 'normal-count';
  this.url = element.getAttribute('data-url') || document.location.href;
  this.title = element.getAttribute('data-title') || document.title;
  this.count = -1;
  this.key = -1;
  this.counted = false;
  this.config = _button_config[this.buttonStyle];
  if (this.config == null) {
    throw 'Unknown button type: ' + this.buttonStyle;
  }
};

// method definition
_button.prototype.onClick = function() {
  this.processor.click(this);
  return false;
};

_button.prototype.click = function() {
  if (!this.counted) {
    this.setCount(this.count + 1);
    this.counted = true;
  }
};

_button.prototype.getClassName = function() {
  // for localization
  return this.config.baseClassName;
};

_button.prototype.getHTML = function() {
  var html = this.config.html;
  html = html.replace(/\$\{id\}/gim, this.id);
  var className = this.getClassName();
  html = html.replace(/\$\{class\}/gim, className);

  return html;
};

_button.prototype.setKey = function(key) {
  this.key = key;
};

_button.prototype.setCount = function(cnt) {
  this.count = cnt;
  var btn = document.getElementById(this.id);
  if (btn) {
    for ( ; btn.hasChildNodes(); ) {
      btn.removeChild(btn.lastChild);
    }
    var c = 'thank-count';

    if (cnt > 1000000) {
        c += '-1000000';
    } else if (cnt > 100000) {
        c += '-100000';
    } else if (cnt > 10000) {
        c += '-10000';
    } else if (cnt > 1000) {
        c += '-1000';
    } else if (cnt > 100) {
        c += '-100';
    }

    btn.innerHTML = '<span class="'+c+'">'+cnt+'</span>';
  }
};

_button.prototype.render = function() {
  this.element.style.textDecoration = 'none';
  this.element.innerHTML = this.getHTML();
  this.element.onclick = hitch(this.onClick, this);
  this.config.displayCount && this.processor.getCount(this);
};

// include processor class
// class definition
var _processor = function() {
  this.intervalId = 0;
  this.isIE7DomReady = false;
  this.buttons = [];
  this.buttonsByUrl = {};
  this.countRequests = {};
  this.overlay = false;
  this.backdrop = false;
  this.infocus = false;

  // remember current overflow setting
  if (isIE && (document.compatMode == 'CSS1Compat') && (!document.documentMode || (document.documentMode < 8))) {
    this._overflow = document.getElementsByTagName('html')[0].style.overflow;
  } else {
    this._overflow = document.body.style.overflow;
  }
};

// method definition
_processor.prototype.isDomReady = function() {
  return (isIE && !(isUserAgentVersion(8) || (isUserAgentVersion(7) && document.documentMode))) ? this.isIE7DomReady : true;
};

_processor.prototype.onDomReady = function(force) {
  if (force || this.isDomReady()) {
    var els = selectElements("a", "thankthis-button"); // get all A element which has "thankthis-button" class
    for (var i = 0; i < els.length; i++) { // iterate over elements
      var el = els[i];
      if (el && !el.tt_rendered) {
        el.tt_rendered = true;
        el.style.textDecoration = "none";
        try {
          var btn = new _button(this, el); // instantiate TT button class
          this.buttonsByUrl[btn.url] = this.buttonsByUrl[btn.url] || [];
          this.buttonsByUrl[btn.url].push(btn);
          this.buttons.push(btn);
          btn.render()
        } catch (e) {
          // window.console && window.console.log(e);
        }
      }
    }
    this.sendCountRequests();
  }
};

_processor.prototype.onLoad = function() {
  window.clearInterval(this.intervalId);
  this.isIE7DomReady = true;
  this.onDomReady();
};

_processor.prototype.getCount = function(btn) {
  this.countRequests[btn.url] = btn.title;
};

_processor.prototype.sendCountRequests = function() {
  var qstr = "";
  for (var url in this.countRequests) {
    var title = this.countRequests[url];
    var p = encodeURIComponent(url) +'='+encodeURIComponent(title);
    if (qstr.length + p.length > 1900) {
      this.sendRequest(qstr);
      qstr = "";
    }
    qstr += (qstr ? "&" : "?") + p;
  }
  this.sendRequest(qstr);
  this.countRequests = {};
};

_processor.prototype.click = function(btn) {
  var btns = this.buttonsByUrl[btn.url] || [];
  for (var i = 0; i < btns.length; i++) {
    btns[i].click();
  }
  this.showOverlay(btn);
};

_processor.prototype.setCount = function(stats) {
  for (var url in stats) {
    var stat = stats[url];
    for (var btns = this.buttonsByUrl[url] || [], i = 0; i < btns.length; i++) {
      btns[i].setCount(stat.count);
      btns[i].setKey(stat.key);
    }
  }
};

_processor.prototype.sendRequest = function(qstr) {
  if (qstr) {
    var url = window.thankthis_stat_url + window.thankthis_site_key + qstr;
    var el = document.createElement("script");
    el.src = url;
    document.body.appendChild(el);
  }
};

_processor.prototype.addCSS = function(){
  var csstext = _button_CSS;
  csstext = (isIE && !(isUserAgentVersion(8) || isUserAgentVersion(7) && document.documentMode)) ? csstext.replace(/\$\{imgSuffix\}/gim,"gif") : csstext.replace(/\$\{imgSuffix\}/gim,"png");
  var el = document.createElement("style");
  el.type="text/css";
  if (el.styleSheet) {
    el.styleSheet.cssText = csstext;
  } else {
    el.appendChild(document.createTextNode(csstext));
  }
  document.getElementsByTagName("head")[0].appendChild(el);
};

_processor.prototype.addOverlay = function() {
  var backdrop = this.backdrop = document.createElement('div');
  backdrop.id = 'thankthis-backdrop';
  backdrop.style.display = 'none';
  document.body.appendChild(backdrop);

  var overlay = this.overlay = document.createElement('div');
  overlay.id = 'thankthis-overlay';
  overlay.style.display = 'none';

  var close = document.createElement('div');
  close.id = 'thankthis-overlay-close';
  close.innerHTML = '<a href="javascript:void(0);">[ x ]</a>';
  close.onclick = hitch(this.hideOverlay, this);
  overlay.appendChild(close);

  var iframe = createNamedElement('iframe', 'ttoverlay'); // see utility.js
  iframe.id = 'thankthis-overlay-iframe';
  iframe.marginheight = 0;
  iframe.marginwidth = 0;
  iframe.frameBorder = 0;
  iframe.src = window.thankthis_overlay_blank_url; // fix white page flicker; preload blank black pg
  overlay.appendChild(iframe);

  document.body.appendChild(overlay);

  var iframeWrapper = document.createElement('div');
  iframeWrapper.style.display = 'none';

  var xd_recv = this.xd_receiver = document.createElement('iframe');
  xd_recv.height = 1;
  xd_recv.width = 1;
  xd_recv.frameBorder = 0;
  iframeWrapper.appendChild(xd_recv);

  document.body.appendChild(iframeWrapper);
};

_processor.prototype.showOverlay = function(btn) {
  if (this.backdrop && this.overlay) {
    if (isIE && ((document.compatMode == 'BackCompat') || (!document.documentMode || (document.documentMode < 8)))) {
      var _body = (document.compatMode == 'BackCompat') ? document.body : document.documentElement;
      var _sctop = _body.scrollTop;
      var _iheight = _body.clientHeight;
      var _iwidth = _body.clientWidth;
      this.backdrop.style.top = _sctop + 'px';
      this.backdrop.style.height = _iheight + 'px';
      this.backdrop.style.width = _iwidth + 'px';
      this.overlay.style.top = _sctop + Math.floor(_iheight / 2);
      this.backdrop.style.position = 'absolute';
      this.overlay.style.position = 'absolute';
    }

    if (isIE && (document.compatMode == 'CSS1Compat') && (!document.documentMode || (document.documentMode < 8))) {
      document.getElementsByTagName('html')[0].style.overflow = 'hidden';
    } else {
      document.body.style.overflow = 'hidden';
    }

    this.backdrop.style.display = '';
    this.overlay.style.display = '';
    this.overlay.lastChild.src = window.thankthis_overlay_url + btn.key;
    this.infocus = btn;
  }
};

_processor.prototype.hideOverlay = function() {
  if (this.backdrop && this.overlay) {
    this.backdrop.style.display = 'none';
    this.overlay.style.display = 'none';
    //this.overlay.lastChild.src = window.thankthis_overlay_blank_url; // fix residue flicker; preload black blank pg
    this.xd_receiver.src = window.thankthis_xd_recv_url;

    if (isIE && (document.compatMode == 'CSS1Compat') && (!document.documentMode || (document.documentMode < 8))) {
      document.getElementsByTagName('html')[0].style.overflow = this._overflow
    } else {
      document.body.style.overflow = this._overflow
    }

    if (this.infocus) {
      var btn = this.infocus;
      this.getCount(btn);
      this.sendCountRequests();
      this.infocus = false;
    }
  }
};

_processor.prototype.start = function() {
  var a = hitch(this.onLoad, this), b = hitch(this.onLoad, this);
  window.setTimeout(a, 0);
  this.intervalId = window.setInterval(a, 800);
  window.addEventListener ? window.addEventListener("load", b, false) : window.attachEvent("onload", b);
};

if (!window.thankthis_processor) {
  window.thankthis_processor = new _processor(); // see processor.js
  window.thankthis_processor.addCSS();
  window.thankthis_processor.addOverlay();
  window.thankthis_processor.start();
}

// declare callback function - server will call thankthis_set_count
declare_object("thankthis_set_count", function(a) { window.thankthis_processor.setCount(a); });

})();  // end of closure

// set loaded flag
window.__thankthis__ = true;
};
