var NetPopjQuery = jQuery;

(function() {
  if (NetPopjQuery) return;
//  if (NetPopjQuery().jquery >= '1.4')
//    return;
  var script = document.createElement('script');
  script.setAttribute("type","text/javascript");
  script.setAttribute("src", 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');
  if (typeof script!="undefined")
    document.getElementsByTagName("head")[0].appendChild(script);
  NetPopjQuery = jQuery.noConflict(true);
})();

function OptOutButton(selector, css_class, opt_out_form_server_url) {
    this.parent_selector = selector;
    this.css_class = css_class || 'quidget-opt-out';
    this.opt_out_form_server_url = opt_out_form_server_url || 'http://production.netpop.com/opt_out/new';

    var obj = this;

    this.form_loaded = function(data,textStatus,xmlHttpReq) {
        obj.parent.empty();
        obj.parent.append(data.html);
        obj.form = obj.parent.children('form');
        obj.form.addClass(obj.css_class);
        obj.form.submit(function() {
            NetPopjQuery.getJSON(obj.form.attr('action')+'?callback=?',
                                 obj.form.serializeArray(),
                                 obj.form_loaded);
            return false;
        });
    };

    NetPopjQuery(document).ready(function() {
        obj.parent = NetPopjQuery(obj.parent_selector);
        if (obj.parent.length == 0) {
            alert("Selector for parent container tag did not yield any elements: " + selector);
        }
        NetPopjQuery.getJSON(obj.opt_out_form_server_url+'?callback=?', obj.form_loaded)
    });
};

