document.write('<scr' + 'ipt type="text/javascript" src="http://www.google.com/jsapi"></sc' + 'ript>');

var GTG; 
if (GTG == undefined) {
  GTG = {};
}

if (!GTG.initialized) {
  GTG.getById = function(id) {
    element = document.getElementById(id);
    return element ? element : {};
  };

  GTG.register = function(element, name, callback) {
    if (element.addEventListener) {
      element.addEventListener(name, callback, false);
    } else if (element.attachEvent) {
      element.attachEvent(name, callback);
    }
  }

  GTG.unregister = function(element, name, callback) {
    if (element.addEventListener) {
      element.removeEventListener(name, callback, false);
    } else if (element.detachEvent) {
      element.detachEvent(name, callback);
    }
  }

  GTG.classRegexp = function(className) {
    return new RegExp("(^|\\s)" + className + "(\\s|$)");
  };

  GTG.hasClass = function(element, className) {
    var candidate = element.className; 
    var regex = GTG.classRegexp(className);
    return (candidate.length > 0 &&
      (candidate == className ||
       regex.test(elementClassName)));
  };

  GTG.addClass = function(element, className) {
    if (!GTG.hasClass(element, className)) {
      element.className += (element.className ? ' ' : '') + className;
    }
    return element;
  };

  GTG.removeClass = function(element, className) {
    var regex = GTG.classRegexp(className);
    var newClass = element.className.replace(regex, ' ');
    element.className = GTG.strip(newClass);
    return element;
  };

  GTG.strip = function(string) {
    return string.replace(/^\s+/, '').replace(/\s+$/, '');
  }

  GTG.toggleClass = function(element, className) {
    if (!GTG.hasClass(element, className)) {
      GTG.addClass(element, className);
    } else {
      GTG.removeClass(element, className); 
    }
  };

  GTG.addStylesheet = function(href) {
    var link = document.createElement("link");
    link.rel = "stylesheet";
    link.type = "text/css"
    link.href = href;
    var head = document.getElementsByTagName('head')[0];
    head.appendChild(link);
  };

  GTG.initialized = true;
};

GTG.player_widget = function(options) {
  this.version = "0.1";
  this.ie6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
  this.options = options;
  this.is_ssl = ("https:" == document.location.protocol);

  // initialization params
  params = [];
  if (this.options.game) {
    params.push("game=" + encodeURIComponent(this.options.game));
  }
  if (this.options.lock) {
    params.push("lock=true");
  }

  if (!this.options.base_url) { this.options.base_url = "http://www.gamesthatgive.net" };
  this.stylesheet_url = this.options.base_url + "/stylesheets/widget.css?v=" + encodeURIComponent(this.version);
  this.target_url = this.options.base_url + "/widgets/" + this.options.api_key + "?" + params.join("&")
  this.default_url = this.options.base_url + "/images/transparent.gif";
  this.iframe_html = '<div id="gtg_close"><a href="#"><img src="' + this.options.base_url + '/images/btn-close.png" /></a></div><iframe id="gtg_iframe" class="loading" allowtransparency="true" scrolling="no" frameborder="0"' +
    ' src="' + this.default_url + '"' +
    ' width="640" height="575" style="width: 640px; height: 575px;"></iframe>';
  if (this.options.button == "small") {
    this.button_html = '<img id="gtg_open" src="' + this.options.base_url + '/images/gtg-button-170x40.png?v1" height="40" width="170" />';
  } else {
    this.button_html = '<img id="gtg_open" src="' + this.options.base_url + '/images/gtg-button-200x50.png?v1" height="50" width="200" />';
  }
  this.overlay_html = '<div id="gtg_overlay" style="display: none;"></div>' +
    '<div id="gtg_container" style="display: none;">' +
    this.iframe_html +
    '</div>';
  
  GTG.addStylesheet(this.stylesheet_url);

  if (this.ie6) {
    // don't bother rendering the overlay html in this case
    this.overlay_html = "";
  }

  // emit HTML
  document.write(this.overlay_html);
  if (!this.options.target) {
    document.write(this.button_html);
  }

  var widget = this;

  if (!this.options.target) {
    GTG.getById("gtg_open").onclick = function() { widget.open(); return false; };
  } else {
    GTG.getById(this.options.target).onclick = function() { widget.open(); return false; };
  }
  GTG.getById("gtg_overlay").onclick = function() { widget.close(); return false; };
  GTG.getById("gtg_close").onclick = function() { widget.close(); return false; };
};

GTG.player_widget.prototype = {
  open: function() {
    if (window["google"]) {
      if (google.loader.ClientLocation && google.loader.ClientLocation.address.country_code != "US") {
        window.open(this.options.base_url + "/country/unsupported");
        return;
      }
    }
    if (this.ie6) {
      window.open(this.options.base_url + "/pages/ie6");
      return;
    }
    GTG.getById("gtg_overlay").style.display = "block";
    GTG.getById("gtg_container").style.display = "block";
    GTG.getById("gtg_iframe").setAttribute("src", this.target_url);
    GTG.getById("gtg_iframe").focus();
    GTG.register(GTG.getById("gtg_iframe"), "load", this.loaded);
  },

  close: function() {
    GTG.getById("gtg_overlay").style.display = "none";
    GTG.getById("gtg_container").style.display = "none";
    GTG.unregister(GTG.getById("gtg_iframe"), "load", this.loaded);
    GTG.getById("gtg_iframe").setAttribute("src", this.default_url);
    GTG.getById("gtg_iframe").className = "loading";
  },

  loaded: function() {
  }
};
