Prefinery = {
  feedback: function(konfig) {
    this.konfig = konfig ? konfig : {};
    this.konfig.link_id = this.konfig.link_id ? this.konfig.link_id : 'prefinery_feedback_link';
    this.konfig.host = this.konfig.host ? this.konfig.host : 'prefinery.com';
    
  	url = "https://" + this.konfig.account + "." + this.konfig.host + "/betas/" + this.konfig.beta_id + "/comments/new?display=popup"
  	if (this.konfig.tester_hash) {
  		url = url + "&tester_hash=" + this.konfig.tester_hash
  	};

  	Prefinery.draw_overlay();

  	if (this.konfig.show_tab) {
  		var tab = document.createElement('div');
  	  	tab.innerHTML = '<div id="prefinery_feedback_tab"><a href="#" id="prefinery_feedback_tab_link">Feedback</a></div>';
  	  	document.body.appendChild(tab);
  	};

  	if (this.getId(this.konfig.link_id)) {
  		Prefinery.bind_widget_to_eid(url, this.konfig.link_id);
  	};

  	if (this.konfig.link_class) {
  	  Prefinery.bind_widget_to_class(url, this.konfig.link_class);
  	};

  	if (this.konfig.show_tab) {
  		Prefinery.bind_widget_to_eid(url, 'prefinery_feedback_tab_link');
  	};
  },
  invite: function(url, konfig) {
    this.konfig = konfig ? konfig : {};
    this.konfig.link_id = this.konfig.link_id ? this.konfig.link_id : 'prefinery_invite_link';
    
  	Prefinery.draw_overlay();
	
  	if (this.getId(this.konfig.link_id)) {
  		Prefinery.bind_widget_to_eid(url, this.konfig.link_id);
  	}
  },
  apply: function(konfig) {
    this.konfig = konfig ? konfig : {};
    this.konfig.link_id = this.konfig.link_id ? this.konfig.link_id : 'prefinery_apply_link';
    this.konfig.host = this.konfig.host ? this.konfig.host : 'prefinery.com';
    
  	url = "https://" + this.konfig.account + "." + this.konfig.host + "/betas/" + this.konfig.beta_id + "/testers/new?display=popup"

  	Prefinery.draw_overlay();
	
  	if (this.getId(this.konfig.link_id)) {
  		Prefinery.bind_widget_to_eid(url, this.konfig.link_id);
  	};
  	
  	if (this.konfig.link_class) {
  	  Prefinery.bind_widget_to_class(url, this.konfig.link_class);
  	};
  },
  invite_a_friend: function(konfig) {
    this.konfig = konfig ? konfig : {};
    this.konfig.link_id = this.konfig.link_id ? this.konfig.link_id : 'prefinery_invite_a_friend_link';
    this.konfig.host = this.konfig.host ? this.konfig.host : 'prefinery.com';
    
  	url = "https://" + this.konfig.account + "." + this.konfig.host + "/betas/" + this.konfig.beta_id + "/friend_invitations/new?display=popup"
  	if (this.konfig.tester_hash) {
  		url = url + "&tester_hash=" + this.konfig.tester_hash
  	};

  	Prefinery.draw_overlay();

  	if (this.konfig.show_tab) {
  		var tab = document.createElement('div');
  	  	tab.innerHTML = '<div id="prefinery_invite_a_friend_tab"><a href="#" id="prefinery_invite_a_friend_tab_link">Invite a Friend</a></div>';
  	  	document.body.appendChild(tab);
  	};

  	if (this.getId(this.konfig.link_id)) {
  		Prefinery.bind_widget_to_eid(url, this.konfig.link_id);
  	};

  	if (this.konfig.show_tab) {
  		Prefinery.bind_widget_to_eid(url, 'prefinery_invite_a_friend_tab_link');
  	};
  },
  bind_widget_to_eid: function(url, id, force_reload) {
    this.getId(id).onclick = function() { Prefinery.show(url, force_reload); return false; }
    this.getId('prefinery_close').onclick = function() { Prefinery.hide(); return false; }
    this.getId('prefinery_iframe').setAttribute("src", "");
  },
  bind_widget_to_class: function(url, classname, force_reload) {
    var elements = this.getClass(document,classname), n = elements.length;
    for (var i = 0; i < n; i++) {
      var e = elements[i];
      e.onclick = function() { Prefinery.show(url, force_reload); return false; }
    }
    this.getId('prefinery_close').onclick = function() { Prefinery.hide(); return false; }
    this.getId('prefinery_iframe').setAttribute("src", "");
  },
  set_position: function() {
    this.scroll_top = document.documentElement.scrollTop || document.body.scrollTop;
    this.scroll_height = document.documentElement.scrollHeight;
    this.client_height = window.innerHeight || document.documentElement.clientHeight;
    this.getId('prefinery_screen').style.height = screen.height+this.client_height+"px";
    this.getId('prefinery_container').style.top = this.scroll_top+(this.client_height*0.1)+"px";
  },
  draw_overlay: function() {
  	if (!this.getId('prefinery_overlay')) {
  	    this.overlay_html = '<div id="prefinery_overlay" style="display:none">' +
  	                          '<div id="prefinery_container">' +
  	                            '<a href="#" id="prefinery_close"></a>' +
  	                            '<iframe src="" id="prefinery_iframe" allowTransparency="true" scrolling="no" frameborder="0"></iframe>' +
  	                          '</div>' +
  	                          '<div id="prefinery_screen"></div>' +
  	                        '</div>';
  	    document.write(this.overlay_html);
  	}
  },
  show: function(url, force_reload) {
    if(this.getId('prefinery_iframe').getAttribute("src") == "" ||
       this.getId('prefinery_iframe').getAttribute("src") != url ||
	     force_reload) {
      this.getId('prefinery_iframe').setAttribute("src", url);
      if (this.getId('prefinery_iframe').addEventListener) {
        this.getId('prefinery_iframe').addEventListener("load", Prefinery.loaded, false);
      } else if (this.getId('prefinery_iframe').attachEvent) {
        this.getId('prefinery_iframe').detachEvent("onload", Prefinery.loaded);
        this.getId('prefinery_iframe').attachEvent("onload", Prefinery.loaded);
      }
    }
    this.set_position();
    this.getId('prefinery_overlay').style.display = "block";
  },
  hide: function() {
    this.getId('prefinery_overlay').style.display = "none";
  },
  loaded: function() {
    Prefinery.getId('prefinery_iframe').className = "loaded";
  },
  getId: function(id) {
    return document.getElementById(id);
  },
  getClass: function(node,classname) {
    if (node.getElementsByClassName) {
      return node.getElementsByClassName(classname);
    } else {
      return (function getElementsByClass(searchClass,node) {
          if ( node == null )
            node = document;
          var classElements = [],
              els = node.getElementsByTagName("*"),
              elsLen = els.length,
              pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"), i, j;

          for (i = 0, j = 0; i < elsLen; i++) {
            if ( pattern.test(els[i].className) ) {
                classElements[j] = els[i];
                j++;
            }
          }
          return classElements;
      })(classname, node);
    }
  }
}

// Add the Prefinery Widget CSS to the DOM
var styles = "#prefinery_overlay { width: 100%; height: 100%; top: 0; left: 0; z-index: 9999; position: absolute;}";
styles +=    "#prefinery_screen { top: 0; left: 0; z-index: 1; width: 100%; position: absolute; background-color: #000; opacity: 0.50; -moz-opacity: 0.50; filter: alpha(opacity=50);}";
styles +=    "#prefinery_container { height: 1200px; margin: 0 auto; position: relative; width: 570px; z-index: 2;}";
styles +=    "#prefinery_container iframe { width: 558px; height: 100%; margin: 20px; background: transparent url('https://c468782.ssl.cf0.rackcdn.com/loading.png') no-repeat; background-position: top right;}";
styles +=    "#prefinery_container iframe.loaded { background: transparent none repeat scroll 0 0;}";
styles +=    "a#prefinery_close { position: absolute; cursor: pointer; outline: none; top: -12px; right: 0; z-index: 5; width: 48px; height: 48px; overflow: hidden; background: transparent url('https://c468782.ssl.cf0.rackcdn.com/close.png') no-repeat scroll 0 0;}";
styles +=    "#prefinery_invite_a_friend_tab { position: absolute; top: 180px; right: 0px; width: 27px; height: 131px; border-top: 2px solid #FFF; border-left: 2px solid #FFF; border-bottom: 2px solid #FFF;}";
styles +=    "#prefinery_invite_a_friend_tab_link { display: block; width: 100%; height: 100%; text-decoration: none; border: none; text-indent: -9999px; background: #006699 url('https://c468782.ssl.cf0.rackcdn.com/invite-a-friend-text.png') no-repeat scroll 0 0;}";
styles +=    "#prefinery_feedback_tab { position: absolute; top: 180px; right: 0px; width: 27px; height: 131px; border-top: 2px solid #FFF; border-left: 2px solid #FFF; border-bottom: 2px solid #FFF;}";
styles +=    "#prefinery_feedback_tab_link { display: block; width: 100%; height: 100%; text-decoration: none; border: none; text-indent: -9999px; background: #006699 url('https://c468782.ssl.cf0.rackcdn.com/feedback-text.png') no-repeat scroll 0 0;}";

var style = document.createElement('style');
style.setAttribute("type", "text/css");
style.setAttribute("charset", "utf-8");
try {
  style.appendChild(document.createTextNode(styles));
  document.getElementsByTagName("head").item(0).appendChild(style);
} catch(e) { }

// For IE
if(document.createStyleSheet) {
  document.createStyleSheet('https://www.prefinery.com/stylesheets/widget.css');
}
