var _inline_login = function() {
  var il = this;
  this.loaded = false;

  this.init = function() {
    this.base_path = (Drupal.settings && Drupal.settings.basePath) || '/';
    this.form = jQuery("#inline-login-block form");
    this.block = jQuery("#inline-login-block");
    this.form.submit( function() {
      var values = $(this).find(":input").serializeArray();
      var action = $(this).attr("action");
      var url = il.build_url(action);
      $.post( il.base_path+action, values, il.ajax_handler );
      return false;
    });
    $('#inline-login-block').jqmAddClose('a.jqmClose');
    this.loaded = true;
  }

  this.call_success = function() {
    if (!this.success) {
      window.location.reload();
    }
    else if (typeof(this.success) == "string") {
      window.location.href = this.success;
    }
    else if (typeof(this.success) == "function") {
      this.success();
    }
  }

  this.ajax_handler = function(data) {
    data = Number(data);
    if ( data == 1) {
      il.form.find(".error").hide();
      il.call_success();
    }
    else if (data == -1) {
      il.form.find(".error").hide();
      il.call_success();
    }
    else {
      il.form.find(".error").show();
      $('#inline-login-form #edit-name').val("");
      $('#inline-login-form #edit-pass').val("");
    }
  }

  this.activate_form = function(evt,success) {
		// Do not show if not loaded. loaded means user is not logged in
		var offset = { 'top': evt.pageY, 'left': evt.pageX };
		if (!this.loaded) { return; }
    $('#inline-login-form .input-wrapper .error').removeClass('error');
    $('#inline-login-form .error').hide();
    $('#inline-login-form #edit-name').val("");
    $('#inline-login-form #edit-pass').val("");
    $('#inline-login-block').jqmShow();
    this.success = success;
  }

  this.build_url = function(part) {
    return window.location.protocol + "://" + window.location.host + this.base_path + part;
  }
};

var _inline_parser = function() {
  this.init = function() {
    jQuery("form#node-form.require_login textarea").each( function(item) {
      $(this).focus( function(evt) {
        inline_login.activate_form(evt);
      });
    });
    
    jQuery("form.require_login").each( function(item) {
      var the_form = $(this);
      the_form.find("input[type=submit]").click( function(evt) { 
        var token = the_form.find(":input[name=inline_login_token]").val();

        evt.preventDefault();

        if (token) {
          inline_login.activate_form(evt,function() {
              inline_login.get_token(token, function(data) { 
                the_form.find(":input[name=form_token]").val(data);
                the_form.submit();
              });
          });
        }
        else {
          inline_login.activate_form(evt,function() {
              the_form.submit();
          });
        }

        return false;
      })
    });

    //Handles any tag with the id edit-comment
    jQuery("#edit-comment").focus( function(evt) {
      inline_login.activate_form(evt);
    });
    
  };
};

inline_login = new _inline_login();
inline_parser = new _inline_parser();

$(document).ready( function() {
	inline_parser.init();
});

// Inline Login Handler
$(document).click( function(evt) {
  var node_add = /node\/add/;
  var source = evt.target.href;

  if (source && source.search(node_add) != -1 && inline_login.loaded) {
    evt.preventDefault();
    inline_login.activate_form(evt,evt.target.href);   
  }
  var user_login = /user\/login/;
  if (source && source.search(user_login) != -1 && inline_login.loaded) {
    evt.preventDefault();
    inline_login.activate_form(evt);   
  }
  var user_login = /\/user$/;
  if (source && source.search(user_login) != -1 && inline_login.loaded) {
    evt.preventDefault();
    inline_login.activate_form(evt);   
  }
  var user_login = /\/forward/;
  if (source && source.search(user_login) != -1 && inline_login.loaded) {
    evt.preventDefault();
    inline_login.activate_form(evt);   
  }
  if (evt.target.id == 'main-login-link') {
    evt.preventDefault();
    inline_login.activate_form(evt);
  }
});
