var email_bad;
bv_lib = {};
bv_lib.new_init = function(good_cb,bad_cb,reg_name,input_name) {

	(function($) {

		$(document).ready(function(){
			$.extend({
				getBvUser : function() {
					return "datinggold";
				},
				getBvURI : function(){
					//return "http://localhost:3000/"
					return "https://api.briteverify.com/"
				}
			});
			// The User CSS
			$("head").append('<link href="'+ $.getBvURI() +'stylesheets/briteverify.css" rel="stylesheet" type="text/css" media="screen" />');

			//$("head").append('<script type="text/javascript" src="https://10.0.1.12:3000/javascripts/jquery.tips.min.js"></script>')
			//$("head").append('<script type="text/javascript" src="https://10.0.1.12:3000/javascripts/jquery.briteverify.0.1.js"></script>')
			// Load the jQuery tools tooltip plugin.


			$.getScript($.getBvURI() + "javascripts/jquery.tips.min.js", function(){
				// now we are set to load the plugin
				// Load the briteVerify plugin and functions.
				$.getScript($.getBvURI() + "javascripts/jquery.briteverify.0.1.js", function(){
					try{
						bvInit();
					}catch(e){
						// do nothing
					}
				});
			});
		});
	})(jQuery);

	j = jQuery;
	if (!reg_name) reg_name = "registration_form";
	if (!input_name) input_name = "email_input";


	j("#" + reg_name).submit(
		function() {
			if (email_bad) return false;
			return true;
		}
	);

	if (good_cb) bv_lib.good_cb = function() {
		email_bad = false;
		good_cb();
	};

	else bv_lib.good_cb = function() {
		email_bad = false;
		j('#email_error').hide();
		j('#email_comm').show();
		j('#email_comm_angry').hide();
	};

	if (bad_cb) bv_lib.bad_cb = function() {
		email_bad = true;
		bad_cb();
	};

	else bv_lib.bad_cb = function() {
		email_bad = true;
		j('#email_error').show();
		j('#email_comm').hide();
		j('#email_comm_angry').show();
	};

	j("#" + input_name).change(
		function() {
			if (this.value.indexOf("@") == -1) return bv_lib.bad_cb();

			j.briteVerify.email(
				this.value,
				function(email) {
					if (email.errors_json) {
						if (email.errors_json.user) {
							//We haven't paid our BriteVerify bill
							bv_lib.good_cb();
						}else bv_lib.bad_cb();
					}else bv_lib.good_cb();
				}
			);

			//Set this to good while we wait for our callback to finish.
			bv_lib.good_cb();
		}
	);
};

