(function(jQuery){
  jQuery.fn.login_box = function( options ){
    // To avoid scope issues, use 'base' instead of 'this'
    // to reference this class from internal events and functions.
    var base = this;
	 var box_state = false; // false === closed; true === open

    base.init = function() {
      base.options = jQuery.extend( {}, jQuery.fn.login_box.defaults, options );
		base.options.box.hide();
    };
	 base.init();

    // local functions
    base.someLocalFunction = function() {
      // can call somePrivateFunction();
    };

	 base.openBox = function() {
		 base.options.box.show();
		 box_state = true;
	 }

	 base.closeBox = function() {
		 base.options.box.hide();
		 box_state = false;
	 }

	 base.togglePassword = function() {
		 var pw = base.options.password;
		 
		 if ( pw.val() === '' ) {
			 pw.hide();
			 jQuery('.'+pw.attr("class")+"[type=text]").show();
		 }
	 }

	 base.addFakePasswordField = function() {
		 var pw = base.options.password;

		 pw.before(
			'<input class="' + pw.attr("class") + '" name="fake_password_field" type="text" value="' + base.options.password_text + '" />'
		 );

		 pw.hide();
	 }

    // iterate through each element that matches the selector used to instantiate this plugin
    return base.each( function() {
      // stash the parent div in case we need to access it from one of the event handlers 
      var parent = this;

		// This will close the box if it's open and the page is clicked on
		/*
		jQuery( document, this ).bind("click", function(ev){
			if ( box_state ) {
				base.closeBox();
			}
		});
		*/

		// This will open the box when clicking the link
		jQuery( base.options.button, this ).bind("click", function(ev){
			// add the password field if it doesn't exist yet
			var pw = base.options.password;
			if ( !pw.prev().hasClass(pw.attr("class")) ) {
				base.addFakePasswordField();
			}

			if ( !box_state ) {
				base.openBox();
			}

			return false;
		});

		// This prevents the box from closing when clicking inside of it
		/*
		jQuery( base.options.box, this ).bind("click", function(ev){
			if ( box_state ) { // If the box is open
				return false;
			}
		});
		*/

		// This is the close button
		jQuery( base.options.close_button ).bind("click", function(ev){
			base.closeBox();
		});

		// This will handle the password field when clicked
		jQuery( '.'+base.options.password.attr("class") ).live("focus", function(ev) {
			var fake_pw_box = jQuery('.'+base.options.password.attr("class")+"[type=text]");
			if ( fake_pw_box.is(":visible") ) {
				fake_pw_box.hide();
				base.options.password.show().focus();
			}
		});

		jQuery( base.options.password, this).bind("blur", function(ev){
			var pw = base.options.password;
			if ( pw.val() === base.options.password_text || pw.val() === '' ) {
				base.togglePassword();
			}
		});

    });
  };

  /** Public functions **/
  jQuery.fn.login_box.login_box = function() {
	 base.optionx.box.show();
	 box_state = true;
    return;
  }

  jQuery.fn.login_box.close_box = function() {
	 base.optionx.box.hide();
	 box_state = false;
    return;
  }

  // defaults
  jQuery.fn.login_box.defaults = {
	 fake_password_input_class: "fake_password_input_class"
  };


  /** Private functions **/
  function openBox() {
	  base.options.box.show();
	  return;
  }

  function closeBox() {
	  base.options.box.hide();
	  return;
  }

})(jQuery);

