1/* Queue of loaded script files */
2var indexmenu_jsqueue = [];
3/* Queue of loaded css files */
4var indexmenu_cssqueue = [];
5/* Queue of nojs trees */
6var indexmenu_nojsqueue = [];
7
8function indexmenu_findExt(path){
9    var ext = "gif";
10    var cext = path.lastIndexOf(".");
11    if ( cext > -1){
12	cext++;
13	cext = path.substring(cext, path.length).toLowerCase();
14	if ((cext == "png") || (cext == "jpg")) {ext = cext;}
15    }
16    return ext;
17}
18
19function indexmenu_createTocMenu(get,picker,btn) {
20    var toc_picker = $(picker);
21    if (!toc_picker) {
22	toc_picker=indexmenu_createPicker(picker);
23	toc_picker.className='dokuwiki indexmenu_toc';
24	toc_picker.innerHTML='<a href="#"><img src="'+DOKU_BASE+'lib/plugins/indexmenu/images/close.gif" class="indexmenu_close" /></a><div />';
25        addEvent(toc_picker.firstChild, 'click',function (event) {event.stopPropagation();return indexmenu_showPicker(picker)});
26    } else {
27	toc_picker.style.display = 'none';
28    }
29    indexmenu_ajaxmenu(get,toc_picker,$(btn),toc_picker.childNodes[1]);
30}
31
32function indexmenu_ajaxmenu(get,picker,btn,container,oncomplete) {
33    var indx_list;
34    if (container) {
35	indx_list = container;
36    } else {
37	indx_list = picker;
38    }
39    if (!indexmenu_showPicker(picker,btn)) return;
40    // We use SACK to do the AJAX requests
41    var ajax = new sack(DOKU_BASE+'lib/plugins/indexmenu/ajax.php');
42    ajax.encodeURIString=false;
43    ajax.onLoading = function () {
44	indx_list.innerHTML='<div class="tocheader">Loading .....</div>';
45    };
46
47    // define callback
48    ajax.onCompletion = function(){
49        var data = this.response;
50	indx_list.innerHTML="";
51	if (isFunction(oncomplete)) {
52	    oncomplete(data,indx_list);
53	} else {
54	    indx_list.innerHTML=data;
55	}
56    };
57
58    ajax.runAJAX(encodeURI(get));
59}
60
61function indexmenu_createPicker(id,cl) {
62    var indx_list = document.createElement('div');
63    indx_list.className = cl || 'picker';
64    indx_list.id=id;
65    indx_list.style.position = 'absolute';
66    indx_list.style.display  = 'none';
67    var body = document.getElementsByTagName('body')[0];
68    body.appendChild(indx_list);
69    return indx_list;
70}
71
72function indexmenu_showPicker(pickerid,btn){
73    var x = 3, y = 3, picker = $(pickerid);
74    if(picker.style.display == 'none'){
75	x += findPosX(btn);
76	y += findPosY(btn);
77	if (picker.id != 'picker_plugin_indexmenu') {
78	    x += btn.offsetWidth-3;
79	} else {
80	    y += btn.offsetHeight;
81	}
82	picker.style.display = 'block';
83	picker.style.left = x+'px';
84	picker.style.top = y+'px';
85	return true;
86    }else{
87	picker.style.display = 'none';
88	return false;
89    }
90}
91
92function indexmenu_loadtoolbar(){
93	var element = 'tool__bar';
94	var toolbar = null;
95	if (typeof element == 'string')
96		toolbar = document.getElementById(element);
97    if(!toolbar) return;
98    indexmenu_loadJs(DOKU_BASE+'lib/plugins/indexmenu/edit.js');
99}
100
101function indexmenu_loadJs(f){
102    var basef = f.replace(/^.*[\/\\]/g, '');
103    if (indexmenu_notinarray(indexmenu_jsqueue,basef)) {
104	var oLink = document.createElement("script");
105	oLink.src = f;
106	oLink.type = "text/javascript";
107	oLink.charset="utf-8";
108	indexmenu_jsqueue.push(basef);
109	document.getElementsByTagName("head")[0].appendChild(oLink);
110    }
111}
112
113function indexmenu_checkcontextm(n,obj,e){
114  var k=0;
115  e=e||event;
116  if ((e.which == 3 || e.button == 2) || (window.opera && e.which == 1 && e.ctrlKey)) {
117    obj.contextmenu (n,e);
118    indexmenu_stopevt(e);
119  }
120}
121
122function indexmenu_stopevt(e) {
123    if (!window.indexmenu_contextmenu) {
124	return true;
125    }
126    e=e||event;
127    e.preventdefault? e.preventdefault() : e.returnValue = false;
128    return false;
129}
130
131function indexmenu_notinarray(array,val) {
132    for (var i = 0; i < array.length; i++) {
133	if (array[i] == val) {
134	    return false;
135	}
136    }
137    return true;
138}
139
140function indexmenu_mouseposition(obj,e) {
141/*http://www.quirksmode.org/js/events_properties.html*/
142    if (!e) e = window.event;
143    if (e.pageX || e.pageY) 	{
144      X = e.pageX;
145      Y = e.pageY;
146    }
147    else if (e.clientX || e.clientY) 	{
148      X = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
149      Y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
150    }
151    obj.style.left=X-5+'px';
152    obj.style.top=Y-5+'px';
153}
154
155jQuery(indexmenu_loadtoolbar);
156