1// JSify the animal file system explorator
2function jsifyAnimalFs() {
3	var d = document.getElementById('animal_fs');
4	if(!d) return;
5
6	var items = d.getElementsByTagName('div');
7	var o = null;
8	for(var i=0; i<items.length; i++) {
9		if(!items[i].getAttribute('ref') || !items[i].className.match(/animal_(dir|file)/)) continue;
10
11		items[i].container = d;
12
13		items[i].open = function() {
14			var d = this.container.getElementsByTagName('div');
15			var p = this.clearPath();
16			p = p.substr(0, p.lastIndexOf('/'));
17			if(document.getElementById('editfile_path')) document.getElementById('editfile_path').value = p;
18			for(var i=0; i<d.length; i++) {
19				if(!d[i].getAttribute('ref') || !d[i].getAttribute('pref') || !d[i].className.match(/animal_(dir|file)/)) continue;
20				if(d[i].getAttribute('ref') == this.getAttribute('pref') && d[i].container) {
21					d[i].open();
22					return;
23				}
24			}
25		};
26
27		items[i].clearPath = function() {
28			input = this.getAttribute('ref');
29			var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
30			var output = "";
31			var chr1, chr2, chr3 = "";
32			var enc1, enc2, enc3, enc4 = "";
33			var i = 0;
34
35			input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
36
37			do {
38				enc1 = keyStr.indexOf(input.charAt(i++));
39				enc2 = keyStr.indexOf(input.charAt(i++));
40				enc3 = keyStr.indexOf(input.charAt(i++));
41				enc4 = keyStr.indexOf(input.charAt(i++));
42				chr1 = (enc1 << 2) | (enc2 >> 4);
43				chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
44				chr3 = ((enc3 & 3) << 6) | enc4;
45				output = output + String.fromCharCode(chr1);
46				if(enc3 != 64) output += String.fromCharCode(chr2);
47				if(enc4 != 64) output += String.fromCharCode(chr3);
48				chr1 = chr2 = chr3 = "";
49				enc1 = enc2 = enc3 = enc4 = "";
50			} while (i < input.length);
51			return unescape(output);
52		};
53
54		if(d.getAttribute('openned') == items[i].getAttribute('ref')) o = items[i];
55
56		if(!items[i].className.match(/animal_dir/)) continue;
57
58		items[i].openned = true;
59		items[i].toggle = function() {
60			if(this.openned) {
61				this.close();
62			}else{
63				if(document.getElementById('editfile_path')) document.getElementById('editfile_path').value = this.clearPath();
64				this.open();
65			}
66		};
67
68		items[i].onclick = items[i].toggle;
69
70		items[i].open = function() {
71			if(this.openned) return;
72			this.openned = true;
73			var d = this.container.getElementsByTagName('div');
74			for(var i=0; i<d.length; i++) {
75				if(!d[i].getAttribute('ref') || !d[i].className.match(/animal_(dir|file)/)) continue;
76				if(d[i].getAttribute('ref') == this.getAttribute('pref') && d[i].container) d[i].open();
77				if(d[i].getAttribute('pref') == this.getAttribute('ref')) d[i].style.display = 'block';
78			}
79		};
80
81		items[i].close = function() {
82			if(!this.openned) return;
83			this.openned = false;
84			var d = this.container.getElementsByTagName('div');
85			for(var i=0; i<d.length; i++) {
86				if(!d[i].getAttribute('ref') || !d[i].getAttribute('pref') || !d[i].className.match(/animal_(dir|file)/)) continue;
87				if(d[i].getAttribute('pref') == this.getAttribute('ref')) {
88					if(d[i].className.match(/animal_dir/)) d[i].close();
89					d[i].style.display = 'none';
90				}
91			}
92		};
93	}
94
95	for(var i=0; i<items.length; i++) {
96		if(!items[i].getAttribute('ref') || !items[i].className.match(/animal_dir/)) continue;
97		if(items[i].getAttribute('pref') == '') items[i].close();
98	}
99
100	if(o) {
101		var p = o.clearPath();
102		if(!o.className.match(/animal_dir/)) p = p.substr(0, p.lastIndexOf('/'));
103		if(document.getElementById('editfile_path')) document.getElementById('editfile_path').value = p;
104		o.open();
105	}
106}
107
108// Hud height trick
109function adaptAnimalHud() {
110	var d = document.getElementsByTagName('div');
111	for(var i=0; i<d.length; i++) {
112		if(!d[i].className.match(/animal_hud/)) continue;
113		var ph = d[i].parentNode.offsetHeight;
114		d[i].style.height = ph + 'px';
115	}
116}
117
118// Register functions
119addInitEvent(jsifyAnimalFs);
120addInitEvent(adaptAnimalHud);
121