var GSFN;
if(GSFN == undefined) {
  GSFN = {};
}
 
if(!GSFN.initialized) {
  
  GSFN.gId = function(id) {
    return document.getElementById(id);
  };

  GSFN.hasClassName = function(element, className) {
    var elementClassName = element.className;

    return (elementClassName.length > 0 && (elementClassName == className ||
      new RegExp("(^|\\s)" + className + "(\\s|$)").test(elementClassName)));
  };

  GSFN.addClassName = function(element, className) {
    if (!GSFN.hasClassName(element, className))
      element.className += (element.className ? ' ' : '') + className;
    return element;
  };

  GSFN.removeClassName = function(element, className) {
    var newClass = GSFN.strip(element.className.replace(new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' '));
    element.className = newClass;
    return element;
  };

  GSFN.strip = function(string) {
    return string.replace(/^\s+/, '').replace(/\s+$/, '');
  };
  
  GSFN.add_css = function(css_content) {
    var head = document.getElementsByTagName('head')[0];
    var style = document.createElement('style');
    style.type = 'text/css';
    
    if(style.styleSheet) {
      style.styleSheet.cssText = css_content;
    } else {
      rules = document.createTextNode(css_content);
      style.appendChild(rules);
    }
    head.appendChild(style);
  }

  GSFN.initialized = true;
}

GSFN.feedback_widget = function(options) {
  this.widget_disabled = false;

  this.options = options;
  this.is_ssl = ("https:" == document.location.protocol);

  if(!this.options.display){ this.options.display = "overlay";}

  if(this.is_ssl) {
    this.feedback_base_url = this.local_ssl_base_url;
    this.asset_base_url = this.s3_ssl_base_url;
  } else {
    this.feedback_base_url = this.local_base_url;
    this.asset_base_url = this.s3_base_url;
  }

  if(this.options.local_assets == true) {
    this.asset_base_url = this.feedback_base_url;
  }

  var disable_tagging = this.options.auto_tag == false;

  this.query = [];

  if(!disable_tagging){
    if(this.options.product){
      this.query.push("product=" + encodeURIComponent(this.options.product));
    }

    if(this.options.tag){
      this.query.push("tag=" + encodeURIComponent(this.options.tag));
    }

    if(this.options.user_defined_code){
      this.query.push("user_defined_code=" + encodeURIComponent(this.options.user_defined_code));
    }
  }

  if(this.options.display){
    this.query.push("display=" + encodeURIComponent(this.options.display));
  }

  if(this.options.style){
    this.query.push("style=" + encodeURIComponent(this.options.style));
  }

  if(this.options.popular_topics){
    this.query.push("popular_topics=" + encodeURIComponent(this.options.popular_topics));
  }

  if(this.options.limit){
    this.query.push("limit=" + encodeURIComponent(this.options.limit));
  }

  if(this.options.problem){
    this.query.push("problem=" + encodeURIComponent(this.options.problem));
  }

  if(this.options.powered_by){
    this.query.push("powered_by=" + encodeURIComponent(this.options.powered_by));
  }

  if(this.options.custom_css){
    this.query.push("custom_css=" + encodeURIComponent(this.options.custom_css));
  }
  
  if(this.options.custom_ie_css){
    this.query.push("custom_ie_css=" + encodeURIComponent(this.options.custom_ie_css));
  }

  if(this.options.custom_js){
    this.query.push("custom_js=" + encodeURIComponent(this.options.custom_js));
  }
  
  if(this.options.auto_tag == false){
    this.query.push("auto_tag=" + encodeURIComponent(this.options.auto_tag));
  }

  if(this.options.interstitial) {
    this.query.push("interstitial=" + encodeURIComponent(this.options.interstitial));
  }

  if(this.options.community_base_url) {
    this.query.push("community_base_url=" + encodeURIComponent(this.options.community_base_url));
  }

  if(this.options.document_domain) {
    this.query.push("document_domain=" + encodeURIComponent(this.options.document_domain));
  }

  var fastpass = this.get_fastpass_url(this.options);
  if(fastpass) {
    this.query.push("fastpass=" + encodeURIComponent(fastpass));
  }

  this.query_string = "?" + this.query.join("&");

  this.feedback_url = this.feedback_base_url + "/" + this.options.company + "/feedback/topics/new" + this.query_string;

  this.options = options ? options : {};
  this.options.placement = this.options.placement ? this.options.placement : 'left';
  this.options.color = this.options.color ? this.options.color : '#222';

  if(this.options.display == 'overlay') {
    this.initial_iframe_url = '';
    if(!this.options.width)   { 
      if(this.options.company == 'rightcliq_by_visa') {
        this.options.width = "780px;"
      } else {
      this.options.width = "658px"; 
      }
    }
    if(!this.options.height)  { this.options.height = "100%"; }
  } else {
    this.initial_iframe_url = this.feedback_url;
    if(!this.options.width)   { this.options.width = "100%"; }
    if(!this.options.height)  { this.options.height = "500px"; }
  }
  if (this.widget_disabled){
    this.iframe_html = '<div id="fdbk_iframe" style="position:relative; top: 20px; margin:20px;background:orange;color:purple;font-size:72px; padding: 20px;">'+
                       'WIDGETS ARE UNAVAILABLE LOL'+
                       '</div>';
  }

  else {
    this.iframe_html = '<iframe id="fdbk_iframe" allowTransparency="true" scrolling="no" frameborder="0" class="loading"' +
                       ( this.initial_iframe_url != '' ? ' src="'    + this.initial_iframe_url + '"' : '' ) +
                       ' width="'  + this.options.width + '"' +
                       ' height="'  + this.options.height + '"' +
                       ' style="width: '  + this.options.width + '; height: '  + this.options.height + ';"></iframe>';
  }

  this.tab_html = '<a href="#" id="fdbk_tab" class="fdbk_tab_'+this.options.placement+'" style="background-color:'+this.options.color+'"></a>';
  this.overlay_html = '<div id="fdbk_overlay" style="display:none">' +
                        '<div id="fdbk_container">' +
                          '<a href="#" id="fdbk_close"></a>' +
                          this.iframe_html +
                        '</div>' +
                        '<div id="fdbk_screen"></div>' +
                      '</div>';

  if(this.options.display == 'overlay') {
    raw_css = "#ctsb_tab {\n  top: 25%;\n  left: 0;\n  width: 33px;\n  height: 131px;\n  color: white;\n  cursor: pointer;\n  text-indent: -100000px;\n  overflow: hidden;\n  position: fixed;\n  z-index: 100000;\n  margin-left: -7px;\n  background-image: url(assets/images/contact_sidebar.png);\n  _position: absolute;\n ); }\n  a#ctsb_tab:hover {\n    margin-left: -4px; background-image: url(assets/images/contact_sidebar_hover.png);\n }\n\na.ctsb_tab_right {\n  right: 0 !important;\n  left: auto !important;\n  margin-right: 0 !important;\n  margin-left: auto !important;\n  width: 33px !important; }\n  a.ctsb_tab_right:hover {\n    width: 33px !important;\n    margin-right: 0 !important;\n    margin-left: auto !important; }\n\na";

	  replacer_regex = new RegExp(this.s3_ssl_base_url, "g");
    translated_css = raw_css.replace(replacer_regex, this.asset_base_url);
    GSFN.add_css(translated_css);

    if(this.options.container) {
      var container_el = GSFN.gId(this.options.container);
      container_el.innerHTML = this.tab_html + this.overlay_html;
    } else {
      document.write(this.tab_html);
      document.write(this.overlay_html);
    }

    var Contact_obj = this;
    GSFN.gId('fdbk_tab').onclick = function() { Contact_obj.show(); return false; }
    GSFN.gId('fdbk_close').onclick = function() { Contact_obj.hide(); return false; }
    GSFN.gId('fdbk_iframe').removeAttribute("src");

  } else {
    if(this.options.container) {
      var container_el = GSFN.gId(this.options.container);
      container_el.innerHTML = this.iframe_html;
    } else {
      document.write(this.iframe_html);
    }
  }

};

GSFN.feedback_widget.prototype = {
  local_base_url: "",
  local_ssl_base_url: "",
  s3_base_url: "",
  s3_ssl_base_url: "",

  asset_url: function(asset) {
    return this.asset_base_url + asset;
  },

  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;

    GSFN.gId('fdbk_screen').style.height = this.scroll_height+"px";
    GSFN.gId('fdbk_container').style.top = this.scroll_top+(this.client_height*0.1)+"px";
  },

  show : function() {
    GSFN.gId('fdbk_iframe').setAttribute("src", this.feedback_url);
    if (GSFN.gId('fdbk_iframe').addEventListener) {
      GSFN.gId('fdbk_iframe').addEventListener("load", this.loaded, false);
    } else if (GSFN.gId('fdbk_iframe').attachEvent) {
      GSFN.gId('fdbk_iframe').attachEvent("onload", this.loaded);
    }
    this.set_position();

    GSFN.addClassName(document.getElementsByTagName('html')[0], 'feedback_tab_on');
    GSFN.gId('fdbk_overlay').style.display = "block";
  },

  hide : function() {
    if (GSFN.gId('fdbk_iframe').addEventListener) {
      GSFN.gId('fdbk_iframe').removeEventListener("load", this.loaded, false);
    } else if (GSFN.gId('fdbk_iframe').attachEvent) {
      GSFN.gId('fdbk_iframe').detachEvent("onload", this.loaded);
    }

    GSFN.gId('fdbk_overlay').style.display = "none";
    GSFN.gId('fdbk_iframe').removeAttribute("src");
    GSFN.gId('fdbk_iframe').className = "loading";

    GSFN.removeClassName(document.getElementsByTagName('html')[0], 'feedback_tab_on');
  },

  loaded : function() {
    GSFN.gId('fdbk_iframe').className = "loaded";
  },

  get_fastpass_url : function(options) {
    if(options.fastpass){
      return options.fastpass;
    }
    var script_tags = document.getElementsByTagName('script');
    for (var i = 0; i < script_tags.length; i++) {
      var tag = script_tags[i];
      if(tag.src.match(/\/fastpass/ && tag.src.match(/oauth/))){
        return tag.src;
      }
    }
  }
}

