function ToggleEncryptionDialog(button, tag) {
	this.button    = button;
	this.tag       = tag;
	this.encrypted = null;
	this.decrypted = null;
	this.sack      = new sack(DOKU_BASE + "lib/exe/ajax.php");

	// read cached password
	if ((JSINFO['crypto_cache_password'] == 1) && (JSINFO['crypto_password'] != undefined)) {
		this.password = ' value="' + JSINFO['crypto_password'] + '"';
	} else {
		this.password = "";
	}

	// create HTML Structure
	this.div = document.createElement('div');
	this.div.id = 'toggle_encryption_dialog';
	this.div.className  = 'picker cryptodialog';
	this.div.style.top  = (findPosY(this.button.parentNode)+20)+'px';
	this.div.style.left = (findPosX(this.button.parentNode)+80)+'px';

	this.button.parentNode.appendChild(this.div);
	this.div.innerHTML =
		'<div id="toggle_encryption_dialog_header" class="cryptoheader">'+
		' <img src="' + DOKU_BASE + 'lib/images/close.png" width="16" height="16" align="right" alt="" id="toggle_encryption_dialog_close" />' +
		this.tag.attributes["hint"].nodeValue +
		'</div>' +
		'<div>' +
		' <table>' +
		'  <tr>' +
		'   <th>' + LANG.plugins.crypto['input_secret'] + ':</th>' +
		'   <td><input type="password" class="edit" id="toggle_encryption_dialog_password"' + this.password + '/></td>' +
		'  </tr>' +
		'  <tr>' +
		'   <td colspan="2"><button id="toggle_encryption_dialog_button" class="toolbutton">' + LANG.plugins.crypto['decrypt_button'] +'</button></td>' +
		'  </tr>' +
		' </table>' +
		'</div>';

	// attach event handlers
	drag.attach(this.div, $('toggle_encryption_dialog_header'));

	var dialog = this;
	this.toggle = function() {
		if (dialog.tag.className == "encrypted") {
			dialog.button.innerHTML = '<img src="' + DOKU_BASE + "lib/plugins/crypto/lock_break.png" + '"/>';
			dialog.tag.className = "decrypted";
			if (dialog.tag.attributes["inline"].nodeValue == "true") {
				dialog.tag.innerHTML = dialog.decrypted;
			} else {
				dialog.tag.innerHTML = '<pre>' + dialog.decrypted + '</pre>';
			}
		} else {
			dialog.button.innerHTML = '<img src="' + DOKU_BASE + "lib/plugins/crypto/lock.png" + '"/>';
			dialog.tag.className = "encrypted";
			dialog.tag.innerHTML = dialog.encrypted;
		}

		dialog.button.onclick = function() {
			dialog.toggle();
		};
	};

	$('toggle_encryption_dialog_password').focus();
	$('toggle_encryption_dialog_password').onkeypress = function(event) {
		if (event.keyCode==9) {
			$('toggle_encryption_dialog_button').focus();
			return false;
		}

		if (event.keyCode==13) {
			$('toggle_encryption_dialog_button').onclick();
		}

		return true;
	};

	$('toggle_encryption_dialog_button').onkeypress = function(event) {
		if (event.keyCode==9) {
			$('toggle_encryption_dialog_password').focus();
			return false;
		}

		return true;
	};

	// register destructor
	$('toggle_encryption_dialog_close').onclick = function() {
		dialog.button.parentNode.removeChild(dialog.div);
		dialog.div      = null;
	};

	$('toggle_encryption_dialog_button').onclick = function() {
		dialog.sack.setVar("call",   "crypto_decrypt");
		dialog.sack.setVar("secret", escape($('toggle_encryption_dialog_password').value));
		dialog.sack.setVar("data",   dialog.tag.textContent);
		dialog.sack.onCompletion = function() {
			if (dialog.sack.response) {
				$('toggle_encryption_dialog_close').onclick();
				dialog.encrypted = dialog.tag.textContent;
				dialog.decrypted = unescape(dialog.sack.response);
				dialog.toggle();
			} else {
				alert(LANG.plugins.crypto['decrypt_wrong_secret']);
			}
		};

		// cache the password
		if (JSINFO['crypto_cache_password'] == 1) {
			JSINFO['crypto_password'] = $('toggle_encryption_dialog_password').value;
		} else {
			JSINFO['crypto_password'] = "";
		}

		dialog.sack.runAJAX();
	};
};

