1function ToggleEncryptionDialog(button, tag) {
2	this.button    = button;
3	this.tag       = tag;
4	this.encrypted = null;
5	this.decrypted = null;
6	this.sack      = new sack(DOKU_BASE + "lib/exe/ajax.php");
7
8	// read cached password
9	if ((JSINFO['crypto_cache_password'] == 1) && (JSINFO['crypto_password'] != undefined)) {
10		this.password = ' value="' + JSINFO['crypto_password'] + '"';
11	} else {
12		this.password = "";
13	}
14
15	// create HTML Structure
16	this.div = document.createElement('div');
17	this.div.id = 'toggle_encryption_dialog';
18	this.div.className  = 'picker cryptodialog';
19	this.div.style.top  = (findPosY(this.button.parentNode)+20)+'px';
20	this.div.style.left = (findPosX(this.button.parentNode)+80)+'px';
21
22	this.button.parentNode.appendChild(this.div);
23	this.div.innerHTML =
24		'<div id="toggle_encryption_dialog_header" class="cryptoheader">'+
25		' <img src="' + DOKU_BASE + 'lib/images/close.png" width="16" height="16" align="right" alt="" id="toggle_encryption_dialog_close" />' +
26		this.tag.attributes["hint"].nodeValue +
27		'</div>' +
28		'<div>' +
29		' <table>' +
30		'  <tr>' +
31		'   <th>' + LANG.plugins.crypto['input_secret'] + ':</th>' +
32		'   <td><input type="password" class="edit" id="toggle_encryption_dialog_password"' + this.password + '/></td>' +
33		'  </tr>' +
34		'  <tr>' +
35		'   <td colspan="2"><button id="toggle_encryption_dialog_button" class="toolbutton">' + LANG.plugins.crypto['decrypt_button'] +'</button></td>' +
36		'  </tr>' +
37		' </table>' +
38		'</div>';
39
40	// attach event handlers
41	drag.attach(this.div, $('toggle_encryption_dialog_header'));
42
43	var dialog = this;
44	this.toggle = function() {
45		if (dialog.tag.className == "encrypted") {
46			dialog.button.innerHTML = '<img src="' + DOKU_BASE + "lib/plugins/crypto/lock_break.png" + '"/>';
47			dialog.tag.className = "decrypted";
48			if (dialog.tag.attributes["inline"].nodeValue == "true") {
49				dialog.tag.innerHTML = dialog.decrypted;
50			} else {
51				dialog.tag.innerHTML = '<pre>' + dialog.decrypted + '</pre>';
52			}
53		} else {
54			dialog.button.innerHTML = '<img src="' + DOKU_BASE + "lib/plugins/crypto/lock.png" + '"/>';
55			dialog.tag.className = "encrypted";
56			dialog.tag.innerHTML = dialog.encrypted;
57		}
58
59		dialog.button.onclick = function() {
60			dialog.toggle();
61		};
62	};
63
64	$('toggle_encryption_dialog_password').focus();
65	$('toggle_encryption_dialog_password').onkeypress = function(event) {
66		if (event.keyCode==9) {
67			$('toggle_encryption_dialog_button').focus();
68			return false;
69		}
70
71		if (event.keyCode==13) {
72			$('toggle_encryption_dialog_button').onclick();
73		}
74
75		return true;
76	};
77
78	$('toggle_encryption_dialog_button').onkeypress = function(event) {
79		if (event.keyCode==9) {
80			$('toggle_encryption_dialog_password').focus();
81			return false;
82		}
83
84		return true;
85	};
86
87	// register destructor
88	$('toggle_encryption_dialog_close').onclick = function() {
89		dialog.button.parentNode.removeChild(dialog.div);
90		dialog.div      = null;
91	};
92
93	$('toggle_encryption_dialog_button').onclick = function() {
94		dialog.sack.setVar("call",   "crypto_decrypt");
95		dialog.sack.setVar("secret", escape($('toggle_encryption_dialog_password').value));
96		dialog.sack.setVar("data",   dialog.tag.textContent);
97		dialog.sack.onCompletion = function() {
98			if (dialog.sack.response) {
99				$('toggle_encryption_dialog_close').onclick();
100				dialog.encrypted = dialog.tag.textContent;
101				dialog.decrypted = unescape(dialog.sack.response);
102				dialog.toggle();
103			} else {
104				alert(LANG.plugins.crypto['decrypt_wrong_secret']);
105			}
106		};
107
108		// cache the password
109		if (JSINFO['crypto_cache_password'] == 1) {
110			JSINFO['crypto_password'] = $('toggle_encryption_dialog_password').value;
111		} else {
112			JSINFO['crypto_password'] = "";
113		}
114
115		dialog.sack.runAJAX();
116	};
117};
118