/*

  A custom function can be called upon the clicking of every edit button.
  Simply define a function as below. The userdata parameter is optional.

  function ed_clickButton(btnID,userdata)
  {
	alert("clicked on "+btnID);
	return true; // if false the actual function of the clicked button is not executed
  }

  To prevent the editor from reloading the webpage, and optionally do some custom
  actions, define a function as described below.

  function ed_customReloadSite(extraPars,action,objectID,parentID)
  {
  }

*/

function ed_ObjCreate(id,clss,edit,userdata)
{
  if (edit == null) edit = 0;
  try { if (!ed_clickButton("c",userdata)) return; } catch(e) {}
  window.open(ed_editorPrefix+"edit="+edit+"&action=create&id="+id+(clss!=null?"&class="+clss:"")+(userdata!=null?"&udata="+encodeURIComponent(userdata):""),ed_windowID+"_Editor","left=100,top=100,width=300,height=300,status=yes,resizable=yes");
}

function ed_ObjModify(id,edit,userdata)
{
  if (edit == null) edit = 0;
  try { if (!ed_clickButton("m",userdata)) return; } catch(e) {}
  window.open(ed_editorPrefix+"edit="+edit+"&action=modify&id="+id+(userdata!=null?"&udata="+encodeURIComponent(userdata):""),ed_windowID+"_Editor","left=100,top=100,width=300,height=300,status=yes,resizable=yes");
}

function ed_ObjRemove(id,edit)
{
  if (edit == null) edit = 0;
  try { if (!ed_clickButton("r")) return; } catch(e) {}
  window.open(ed_editorPrefix+"edit="+edit+"&action=remove&id="+id,ed_windowID+"_REditor","left=300,top=250,width=350,height=125,status=no,resizable=yes");
}

function ed_ObjPerms(id,edit)
{
  if (edit == null) edit = 0;
  try { if (!ed_clickButton("p")) return; } catch(e) {}
  window.open(ed_editorPrefix+"edit="+edit+"&action=perms&id="+id,ed_windowID+"_Editor","left=100,top=100,width=300,height=300,status=yes,resizable=yes");
}

function ed_ObjMove(id,dir)
{
  try { if (!ed_clickButton(dir)) return; } catch(e) {}
  var url = new ed_URL();
  url.remPar("(edit|login|logout)");
  url.remPar("ed_[^=]*");
  url.addPar("ed_"+dir,id);
  url.go();
}

function ed_ObjCut(id)
{
  try { if (!ed_clickButton("cut")) return; } catch(e) {}
  var url = new ed_URL();
  url.remPar("(edit|login|logout)");
  url.remPar("ed_[^=]*");
  url.addPar("ed_cut",id);
  url.go();
}

function ed_ObjCopy(id)
{
  try { if (!ed_clickButton("copy")) return; } catch(e) {}
  var url = new ed_URL();
  url.remPar("(edit|login|logout)");
  url.remPar("ed_[^=]*");
  url.addPar("ed_copy",id);
  url.go();
}

function ed_ObjPaste(btn,id)
{
  ed_closePopup(btn);
  try { if (!ed_clickButton("paste")) return; } catch(e) {}
  var clipboard = document.getElementById("ed_clip");
  ed_pasteTop = id;
  ed_isOverClipboard = true;

  var dummy = btn;
  var x = dummy.offsetLeft;
  var y = dummy.offsetTop;
  while (dummy.offsetParent != null)
  {
    dummy = dummy.offsetParent;
    x += dummy.offsetLeft;
    y += dummy.offsetTop;
  }
  var sw = document.getElementsByTagName("body")[0].offsetWidth;
  if (document.all) sw -= 20; //IE includes scrollbar width
  var sh = document.getElementsByTagName("body")[0].offsetHeight;
  
  clipboard.style.left = "-500px";
  clipboard.style.display = ""; // must display first before getting dimensions
  var w = clipboard.offsetWidth;
  if (w > 200) { clipboard.style.width = "200px"; w = clipboard.offsetWidth; }
  var h = clipboard.offsetHeight;
  
  x += 18/2 - w/2;
  y += 18/2 - h/2;
  if (x < 5) x = 5;
  else if (x+w+5 > sw) x = sw-5-w;
  if (y < 5) y = 5;
  else if (y+h+5 > sh) y = sh-5-h;
  clipboard.style.left = x+"px";
  clipboard.style.top = y+"px";
}

function ed_selectClip(id)
{
  ed_isOverClipboard = false;
  ed_closeClipboard();
  var url = new ed_URL();
  url.remPar("(edit|login|logout)");
  url.remPar("ed_[^=]*");
  url.addPar("ed_pastetop",ed_pasteTop);
  url.addPar("ed_paste",id);
  if (document.getElementById("ed_cliprem").checked) url.addPar("ed_pasterem","1");
  url.go();
}

function ed_overClip(btn)
{
  btn.style.backgroundColor = "#f0f0f0";
}

function ed_outClip(btn)
{
  btn.style.backgroundColor = "#d6d3ce";
}

var ed_pasteTop = -1;
var ed_isOverClipboard = false;
var ed_cbBtn = null;

function ed_overClipboard()
{
  ed_isOverClipboard = true;
}

function ed_outClipboard()
{
  if (ed_isOverClipboard)
  {
    setTimeout("ed_closeClipboard()",50);
    ed_isOverClipboard = false;
  }
}

function ed_closeClipboard()
{
  if (!ed_isOverClipboard)
  {
    document.getElementById("ed_clip").style.display = "none";
    if (ed_openBtns != null) ed_outButton(ed_cbBtn);
  }
}

/*function ed_goURL(url)
{
  location.href = url+(url.indexOf("?")>-1?"&":"?")+"ed_dummy="+new Date().getTime();
}*/

// pa=Preserve Anchor: This method is to support the Java URL implementation which does not know about an optional anchor
function ed_goURLpa(url)
{
  var anchor = location.href.replace(/.*?(#|$)/,"");
  url += anchor!=""?'#'+anchor:"";
  location.href = url;
}

function debug(msg)
{
  var dbg = document.getElementById("debug");
  if (dbg == null)
  {
	dbg = document.createElement("div");
	dbg.id = "debug";
	dbg.style.position = "absolute";
	dbg.style.right = "0px";
	dbg.style.top = "0px";
	dbg.style.width = "300px";
	dbg.style.height = "300px";
	dbg.style.border = "1px solid black";
	dbg.style.padding = "5px";
	dbg.style.backgroundColor = "white";
	dbg.style.color = "black";
	dbg.style.fontFamily = "arial";
	dbg.style.fontSize = "8pt";
	dbg.style.overflow = "scroll";
	document.getElementsByTagName("body")[0].appendChild(dbg);
  }
  dbg.innerHTML = msg+"<br>"+dbg.innerHTML;
}

var ed_openBtns = null; // id
var ed_overBtn = null; // node

function ed_openButtons(id)
{
  //debug("openButtons(ed_btns"+id+")");
  if (ed_openBtns != null && ed_openBtns != "ed_btns"+id) edprv_closeButtons();
  var btns = document.getElementById("ed_btns"+id);
  if (btns == null)
  {
    alert("Buttons for id="+id+" not found!");
    return;
  }
  btns.style.display = "";
  ed_openBtns = "ed_btns"+id;
  ed_overButton(btns.firstChild);
  ed_overBtn = null;
  setTimeout("edprv_closeButtons()",2000); // in case buttons are left open due to rapid mouse movements
}

// assumes that when going from button a->b a.outButton is called before b.overButton
// tested IE6, FF1.5 
// tested via Remote Desktop: IE6, FF3.0, GC1.0
function ed_overButton(btn,text)
{
  //debug("overButton("+btn.id+")");
  ed_openBtns = btn.parentNode.id; // in case buttons are left open due to rapid mouse movements
  ed_overBtn = btn;
  ed_bbDiv = btn.parentNode.parentNode;
  if (text != null) ed_openPopup(btn,text);
}

function ed_outButton(btn)
{
  //debug("outButton("+btn.id+")");
  ed_overBtn = null;
  if (ed_isOverClipboard)
  {
    ed_cbBtn = btn;
    return;
  }
  ed_bbDiv = null;
  ed_closePopup(btn);
  setTimeout("edprv_closeButtons()",200);
}

var btnBgCol = null;

function ed_openPopup(btn,text)
{
  btnBgCol = btn.style.backgroundColor;
  btn.style.backgroundColor = "#f0f0f0";
  var div = edprv_getPopup();
  var point = edprv_getPopPoint();
  div.firstChild.data = text;
  var dummy = btn;
  var x = dummy.offsetLeft;
  var y = dummy.offsetTop;
  while (dummy.offsetParent != null)
  {
    dummy = dummy.offsetParent;
    x += dummy.offsetLeft-dummy.scrollLeft;
    y += dummy.offsetTop-dummy.scrollTop;
  }
  div.style.left = "0px";
  div.style.top = "-100px";
  div.style.width = "";
  div.style.display = ""; // must display first before getting dimensions
  var w = div.offsetWidth;
  if (w > 300) { div.style.width = "300px"; w = div.offsetWidth; }
  var h = div.offsetHeight;
  var sw = document.getElementsByTagName("body")[0].offsetWidth;
  if (document.all) sw -= 20; //IE includes scrollbar width
  x = x+18/2-9/2;
  var by = y; // box-y
  if (y-14-h < 5)
  {
    y = y+19;
    by = y+14;
    point.src = ed_adminURL+"extern/gui/images/editbuttons/pop-pointup.gif";
  }
  else
  {
    y = y-14;
    by = y-h+1;
    point.src = ed_adminURL+"extern/gui/images/editbuttons/pop-pointdown.gif";
  }
  point.style.left = x+"px";
  point.style.top = y+"px";
  x -= w/8;
  if (x < 5) x = 5;
  else if (x+w+5 > sw) x = sw-w-5;
  div.style.left = x+"px";
  div.style.top = by+"px";
  point.style.display = "";
}

function ed_closePopup(btn)
{
  btn.style.backgroundColor = btnBgCol; //"#d6d3ce";
  var div = edprv_getPopup();
  div.style.display = "none";
  var point = edprv_getPopPoint();
  point.style.display = "none";
}

function ed_reloadSite(extraPars,action,objectID,parentID)
{
  try
  {
    if (ed_isSubEdit)
    {
	  ed_changeClass(); // use changeClass procedure to reload the editor without saving and without loosing values
	  return;
    }
  }
  catch (e) {}
  try
  {
    ed_customReloadSite(extraPars,action,objectID,parentID);
	return;
  }
  catch (e) {}
  var url = new ed_URL();
  url.remPar("(edit|login|logout)");
  url.remPar("ed_[^=]*");
  var id = url.getPar("id");
  if (action == "REMOVE" && id == objectID) url.addPar("id",parentID);
  //var addr = url.get();
  //if (extraPars != null) addr = addr+(addr.indexOf("?")>-1?"&":"?")+extraPars;
  //ed_goURL(addr);
  if (extraPars != null) url.addExtraPars(extraPars);
  url.go();
}

/** Non-invasive onload handling ************************************/
// installs an onload handler but preserves the one set on the body-tag.

var ed_myOnLoadCalled = false;
var ed_origOnload = null;

function ed_myOnLoad()
{
  ed_myOnLoadCalled = true;
  if (ed_origOnload != null) ed_origOnload();

  // scroll to saved position
  var url = new ed_URL();
  var x = url.getPar("ed_sx");
  var y = url.getPar("ed_sy");
  if (x == "") x = 0;
  if (y == "") y = 0;
  if (x > 0 || y > 0) window.scrollTo(x,y);
  
  // display actionError
  if (typeof ed_actionError != "undefined") alert(ed_actionError);
}

function ed_onLoadWatcher()
{
  if (window.onload != ed_myOnLoad)
    ed_myOnLoad();
  else if (!ed_myOnLoadCalled)
    setTimeout("ed_onLoadWatcher()",200);
}

ed_origOnload = window.onload;
window.onload = ed_myOnLoad;
setTimeout("ed_onLoadWatcher()",200);

/********************************************************************/

function ed_URL()
{
  this.pars = location.href.replace(/.*?(\?|$)/,"").replace(/#.*/,"");
  this.anchor = location.href.replace(/.*?(#|$)/,"");
  this.url = location.href.replace(/(\?|#|$).*/,"");

  // returns the entire URL including any parameters and the optional anchor
  this.get = function()
  {
    //if (this.pars != "") this.pars = this.pars.replace(/^&+/,"").replace(/&+/g,"&").replace(/&+$/,""); // cleanup any left-overs from remPar()
    if (this.pars != "") this.pars = this.pars.replace(/^&/,""); // cleanup any left-overs from remPar()
    return this.url+(this.pars!=""?"?"+this.pars:"")+
                    (this.anchor!=""?"#"+this.anchor:"");
  }

  // gets a parameter, returns "" if not found
  this.getPar = function(name) // name is a regexp
  {
    return this.pars.replace(new RegExp(".*(^|&)"+name+"=([^&]*).*"),"$2");
  }

  // add a parameter (automatically removes any old duplicate)
  this.addPar = function(name,value)
  {
    this.remPar(name);
    if (this.pars != "") this.pars += "&"+name+"="+encodeURIComponent(value);
    else this.pars = name+"="+encodeURIComponent(value);
  }
  
  // add preformatted parameters
  this.addExtraPars = function(pars)
  {
    if (this.pars != "") this.pars += "&"+pars;
    else this.pars = pars;
  }
  
  // remove a parameter
  this.remPar = function(name) // name is a regexp
  {
    //this.pars = this.pars.replace(new RegExp("(^|.*&)"+name+"=[^&]*(&.*|$)",""),"$1$2");
    this.pars = this.pars.replace(new RegExp("(^|&)"+name+"=[^&]*(?=(&|$))","g"),"");
  }

  this.go = function()
  {
    // preserve scroll x/y values
    var x = window.pageXOffset;
    var y = window.pageYOffset;
    if (!y)
    {
      x = document.body.scrollLeft;
      y = document.body.scrollTop;
    }
    if (x > 0) this.addPar("ed_sx",x);
    if (y > 0) this.addPar("ed_sy",y);
    this.addPar("ed_dummy",new Date().getTime());
    //alert(this.get());
    location.href = this.get();
  }

}

/********************************************************************/

function edprv_closeButtons()
{
  //debug("closeButtons() open="+ed_openBtns+" over="+(ed_overBtn!=null?ed_overBtn.parentNode.id:""));
  if (ed_openBtns == null || ed_overBtn != null && ed_overBtn.parentNode.id == ed_openBtns) return;
  //debug("&nbsp;&nbsp;closing "+ed_openBtns);
  var btns = document.getElementById(ed_openBtns);
  if (btns != null) btns.style.display = "none";
  ed_openBtns = null;
  ed_overBtn = null;
  ed_bbDiv = null;
}

function edprv_getPopup()
{
  var div = document.getElementById("ed_hoverPop");
  if (div == null)
  {
    div = document.createElement("div");
    div.id = "ed_hoverPop";
    div.style.display = "none";
    div.style.position = "absolute";
    div.style.backgroundColor = "#ffffe7";
    div.style.color = "black";
    div.style.border = "1px solid black";
    div.style.padding = "2px 4px 2px 4px";
    div.style.fontFamily = "arial";
    div.style.fontSize = "12px";
    div.style.zIndex = "103";
    div.appendChild(document.createTextNode("dummy"));
    document.getElementsByTagName("body")[0].appendChild(div);
  }
  return div;
}

function edprv_getPopPoint()
{
  var point = document.getElementById("ed_hoverPoint");
  if (point == null)
  {
    point = document.createElement("img");
    point.id = "ed_hoverPoint";
    point.style.display = "none";
    point.style.position = "absolute";
    point.style.zIndex = "104";
    point.src = ed_adminURL+"extern/gui/images/editbuttons/pop-pointdown.gif";
    document.getElementsByTagName("body")[0].appendChild(point);
  }
  return point;
}

var ed_borderBlinker = setInterval("edprv_blinkBorder()",400);
var ed_bbDiv = null;
var ed_bbLastDiv = null;
var ed_bbOrigBorder = null;
var ed_bbOrigBorderL = null;
var ed_bbOrigBorderR = null;
var ed_bbOrigBorderB = null;
var ed_bbOrigBorderT = null;
var ed_bbCur = 0;

function edprv_blinkBorder()
{
  if (ed_bbDiv != ed_bbLastDiv)
  {
    if (ed_bbLastDiv != null)
    {
      ed_bbLastDiv.style.border = ed_bbOrigBorder;
      ed_bbLastDiv.style.borderLeft = ed_bbOrigBorderL;
      ed_bbLastDiv.style.borderRight = ed_bbOrigBorderR;
      ed_bbLastDiv.style.borderBottom = ed_bbOrigBorderB;
      ed_bbLastDiv.style.borderTop = ed_bbOrigBorderT;
    }
    if (ed_bbDiv != null)
    {
      ed_bbOrigBorder = ed_bbDiv.style.border;
      ed_bbOrigBorderR = ed_bbDiv.style.borderRight;
      ed_bbOrigBorderL = ed_bbDiv.style.borderLeft;
      ed_bbOrigBorderB = ed_bbDiv.style.borderBottom;
      ed_bbOrigBorderT = ed_bbDiv.style.borderTop;
    }
    ed_bbLastDiv = ed_bbDiv;
    ed_bbCur = 1;
  }
  if (ed_bbDiv != null)
  {
    if (ed_bbCur) ed_bbDiv.style.border = "1px dashed black";
    else ed_bbDiv.style.border = "1px dashed white";
    ed_bbCur = 1-ed_bbCur;
  }
}
