/**
 * Outliner plugin JS library
 *
 * @author Pavel Vitis <pavel [dot] vitis [at] seznam [dot] cz>
 */
 
function writeLayer(layerID, txt) {
  try {
	    if (document.getElementById) {
        var layer = document.getElementById(layerID)
        if (layer) {
          layer.innerHTML = txt;
	    	}
      }
	    else
	    if (document.all) {
        document.all[layerID].innerHTML = txt;
      }
	    else
	    if (document.layers) {
        with (document.layers[layerID].document) {
          open();
          write(txt);
          close();
        }
      }
   }
	 catch (e) {
	 }
}

function outlineIsOpen(div) {
  if (typeof(div) == 'string') {
    div = document.getElementById('div_'+div);
  }
  try {
    return (div.className.indexOf(' visible') != -1);
  } catch (e) {
    div.visible = 0;
    return false;
  }
}

function outlineOpenByDefault(div) {
  if (typeof(div) == 'string') {
    div = document.getElementById('div_'+div);
  }
  try {
    return (div.className.indexOf('default-open') != -1);
  } catch (e) {
    return false;
  }
}

function switchOutline(out, show) {
  if (!document.getElementById) {
    return NULL;
  }

  var div = document.getElementById("div_"+out);
  var link = document.getElementById("wedge_"+out);
  if (typeof(show) == 'undefined') {
    show = !outlineIsOpen(out);
  }
  else {
    if ((show == false) && !outlineIsOpen(out)) {
      return false;
    }
  }

  if (show) {
//    alert(div.style.border);
    link.className = link.className.replace(/ wedgeClosed/gi, ' wedgeOpen');
    div.className = div.className.replace(/ invisible/gi, ' visible');
//	  div.style.height = 1;
    div.visible = 1;
    setCookie('outlineState_'+out, 'opened');
    return true;
  }
  else {
    link.className = link.className.replace(/ wedgeOpen/gi, ' wedgeClosed');
    div.className = div.className.replace(/ visible/gi, ' invisible');
    div.height = 0;
    div.visible = 0;
    setCookie('outlineState_'+out, 'collapsed');
    return false;
  }
}

function toggleLine(out, show) {
  if (!document.getElementById) {
    return;
  }
  var div = document.getElementById("div_"+out);
  if (outlineIsOpen(out)) {
    var bar = document.getElementById("bar_"+out);
    if (show) {
//    alert(div.style.border);
	    div.className = div.className.replace(/ unmarked/gi, ' marked');
	    bar.className = bar.className.replace(/ unmarked/gi, ' marked');
    }
    else {
  	  div.className = div.className.replace(/ marked/gi, ' unmarked');
	    bar.className = bar.className.replace(/ marked/gi, ' unmarked');
	  }
	}
}

function loadOutlineState(out) {
  var state = getCookie('outlineState_'+out);
  var show;
  if ((null == state) && outlineOpenByDefault(out)) {
    show = true;
//    alert('Initially open for '+out);
  }
  else {
    show = (state == 'opened');
  }
  switchOutline(out, show);
}

function setPopup(out, e, evt) {
  var div = document.getElementById("div_"+out);
  if (!outlineIsOpen(out)) {
//	  writeLayer('popup_'+out, document.getElementById('content_'+out).innerHTML);

//	  if (!document.getElementById("popup_"+out)) {
	    var content = new String(document.getElementById('content_'+out).innerHTML);
	    content = content.replace(/<div class=\"secedit\">.*?<\/div>/gi, '');
//	    alert('content created');
//    }
    var popup = document.getElementById("popup_"+out);
    if (document.getElementById("popup_"+out)) {
      writeLayer('popup_'+out, content);
    }

    domTT_activate(
      e,
      evt,
      'content', content,
      'type', 'velcro',
      'fade', 'out',
      'offsetX', 0, 'offsetY', 5,
//    'clearMouse', true,
//    'trail', true,
      'lazy', false,
      'id', 'popup_'+out,
      'styleClass', 'floating',
      'maxWidth', document.body.offsetWidth*0.8
    );

//    popup.style.display = 'block';
//    popup.style.left = document.mouseX + 0;
//    popup.style.top = document.mouseY + 10;
//    divopup.className = 'outline floating';
  }
  clearTimeout();
}

var tmid = null;

function showPopup(out, e, evt) {
  setPopup(out, e, evt);
//  tmid = setTimeout("setPopup('"+out+"');",500);
}

function clearPopup(out) {
  if (tmid != null) {
    clearTimeout(tmid);
    tmid = null;
  }
  var popup = document.getElementById('popup_'+out);
//  popup.style.display = 'none';
  writeLayer('popup_'+out, '');
  domTT_close('popup_'+out);
}

/**
 * Javascript for searchindex manager plugin
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 */

/**
 * Class to hold some values
 */
function plugin_searchindex_class(){
    this.pages = null;
    this.page = null;
    this.sack = null;
    this.done = 1;
    this.count = 0;
}
var pl_si = new plugin_searchindex_class();
pl_si.sack = new sack(DOKU_BASE + 'lib/plugins/searchindex/ajax.php');
pl_si.sack.AjaxFailedAlert = '';
pl_si.sack.encodeURIString = false;

/**
 * Display the loading gif
 */
function plugin_searchindex_throbber(on){
    obj = document.getElementById('pl_si_throbber');
    if(on){
        obj.style.visibility='visible';
    }else{
        obj.style.visibility='hidden';
    }
}

/**
 * Gives textual feedback
 */
function plugin_searchindex_status(text){
    obj = document.getElementById('pl_si_out');
    obj.innerHTML = text;
}

/**
 * Callback. Gets the list of all pages
 */
function plugin_searchindex_cb_clear(){
    ok = this.response;
    if(ok == 1){
        // start indexing
        window.setTimeout("plugin_searchindex_index()",1000);
    }else{
        plugin_searchindex_status(ok);
        // retry
        window.setTimeout("plugin_searchindex_clear()",5000);
    }
}

/**
 * Callback. Gets the list of all pages
 */
function plugin_searchindex_cb_pages(){
    data = this.response;
    pl_si.pages = data.split("\n");
    pl_si.count = pl_si.pages.length;
    plugin_searchindex_status(pl_si.pages.length+" pages found");

    pl_si.page = pl_si.pages.shift();
    window.setTimeout("plugin_searchindex_clear()",1000);
}

/**
 * Callback. Gets the info if indexing of a page was successful
 *
 * Calls the next index run.
 */
function plugin_searchindex_cb_index(){
    ok = this.response;
    if(ok == 1){
        pl_si.page = pl_si.pages.shift();
        pl_si.done++;
        // get next one
        window.setTimeout("plugin_searchindex_index()",1000);
    }else{
        plugin_searchindex_status(ok);
        // get next one
        window.setTimeout("plugin_searchindex_index()",5000);
    }
}

/**
 * Starts the indexing of a page.
 */
function plugin_searchindex_index(){
    if(pl_si.page){
        plugin_searchindex_status('indexing '+pl_si.page+' ('+pl_si.done+'/'+pl_si.count+')');
        pl_si.sack.onCompletion = plugin_searchindex_cb_index;
        pl_si.sack.URLString = '';
        pl_si.sack.runAJAX('call=indexpage&page='+encodeURI(pl_si.page));
    }else{
        plugin_searchindex_status('finished');
        plugin_searchindex_throbber(false);
    }
}

/**
 * Cleans the index
 */
function plugin_searchindex_clear(){
    plugin_searchindex_status('clearing index...');
    pl_si.sack.onCompletion = plugin_searchindex_cb_clear;
    pl_si.sack.URLString = '';
    pl_si.sack.runAJAX('call=clearindex');
}

/**
 * Starts the whole index rebuild process
 */
function plugin_searchindex_go(){
    document.getElementById('pl_si_gobtn').style.display = 'none';
    plugin_searchindex_throbber(true);

    plugin_searchindex_status('Finding all pages');
    pl_si.sack.onCompletion = plugin_searchindex_cb_pages;
    pl_si.sack.URLString = '';
    pl_si.sack.runAJAX('call=pagelist');
}

//Setup VIM: ex: et ts=4 enc=utf-8 :

