// This file contains miscellaneous javascript code of WMI2

// Variables

var extraHelpOpen = null;  // is there opened a helpdiv already or not
var ehon = null;  // used by registerTimedLink
var linkTimer = null;  // used by registerTimedLink
var linkEventX = null;  // used by registerTimedLink
var linkEventY = null;  // used by registerTimedLink

// Functions

// Register a link to a timed link - used at helpdiv
function registerTimedLink(linkid,call,time)
{
  var timedLinkOver = function(event) {
    if (window.event) {
      linkEventX = window.event.clientX; // +document.body.scrollLeft;
      linkEventY = window.event.clientY; // +document.body.scrollTop;
    } else {
      linkEventX = event.clientX; // +window.scrollX;
      linkEventY = event.clientY; // +window.scrollY;
    }
    ehon = linkid;
    linkTimer = setTimeout(call+"(ehon,linkEventX,linkEventY)",time);
    if (newLayoutHelp) {
      extraHelpDivFirst(linkEventX,linkEventY);
      newLayoutHelp = false;
    }
  }
  document.getElementById(linkid).onmouseover = timedLinkOver;
  var timedLinkOut = function(event) {
    linkEventX = null;
    linkEventY = null;
    clearTimeout(linkTimer);
  }
  document.getElementById(linkid).onmouseout = timedLinkOut;
}

// Puts the content of the worksheet in a popup window and returns it
function docWorksheet()
{
  var originalInnerHTML = document.getElementById("workspace").innerHTML;
  document.getElementById("uidiv"+worksheetnew).innerHTML = "";
  document.getElementById("codiv"+worksheetnew).innerHTML = "";
  var formulas = document.getElementsByTagName("img");
  for (lv = 0; lv < formulas.length; lv++) {
    if (formulas[lv].className == "uia" ||
    formulas[lv].className == "co") 
      formulas[lv].removeAttribute("title");
  }
  var lv = 1;
  while (document.getElementById("uidiv"+lv) != null) {
    comap = document.getElementById("comap"+lv);
    if (comap != null) {
      document.getElementById("comap"+lv).innerHTML = "";
    }
    unzoom = document.getElementById("unzoom"+lv);
    if (unzoom != null) {
      document.getElementById("unzoom"+lv).removeAttribute("onClick");
      document.getElementById("unzoom"+lv).innerHTML = "";
    }
    copic = document.getElementById("copic"+lv);
    if (copic != null) {
      document.getElementById("copic"+lv).removeAttribute("useMap");
    }
    uidivt = document.getElementById("uidivt"+lv);
    if (uidivt != null) {
      document.getElementById("uidivt"+lv).innerHTML = "";
    }
    codivt = document.getElementById("codivt"+lv);
    if (codivt != null) {
      document.getElementById("codivt"+lv).innerHTML = "";
    }
    lv++;
  }
  var worksheet = document.getElementById("workspace").innerHTML;
  docw = window.open('','Worksheet','height=700,width=800,scrollbars=yes,menubar');
  docw.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"\n');
  docw.document.write('            "http://www.w3.org/TR/html4/loose.dtd">\n');
  docw.document.write('<html><head>\n');
  docw.document.write('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n');
  docw.document.write('<link rel="stylesheet" type="text/css" href="css/worksheet.css">\n');
  docw.document.write('<title>matek.hu</title>\n');
  docw.document.write('</head>\n<body>\n');
  docw.document.write(worksheet);
  docw.document.write('</body>\n</html>\n');
  docw.document.close();
  document.getElementById("workspace").innerHTML = originalInnerHTML;
  if (browserdetected == "Konqueror")
  {
    lv = 1;
    while (document.getElementById("uidiv"+lv) != null) {
      uidivt = document.getElementById("uidivt"+lv);
      uidivt.style.display = "none";
      codivt = document.getElementById("codivt"+lv);
      if (codivt != null)
        codivt.style.display = "none";
      lv++;
    }
  }
  return docw;
}

// Makes it possible to print the worksheet
function printWorksheet()
{
  forprint = docWorksheet();
  forprint.print();
}

// Makes it possible to save the worksheet
function saveWorksheet()
{
  freshsaved = true;
  forsave = docWorksheet();
  var fsave = function(restext) {
    forsave.alert(restext);
  }
  ajaxMessage("ws_save",fsave);
}

// This function starts a new worksheet and clears the old
function newWorksheet()
{
  var confirmed = window.confirm(ws_reallynewmess);
  if (confirmed) {
    document.getElementById("workspace").innerHTML = '<div id="wtop"></div>';
    ajaxLayoutMessage("default");
    worksheetnew = 1;
    ajaxLoadNew(1);
    eraseInput();
  }
}

// Opens the extra help div again
function extraHelpDivOpen(linkid,eventx,eventy)
{
  if (eventy < document.body.clientHeight/2) {
    document.getElementById("helpdiv").style.top = eventy + 10 + "px";
  } else {
    document.getElementById("helpdiv").style.top = eventy + 10 - 170 + "px";
  }
  document.getElementById("helpdiv").style.right = "20px";
  document.getElementById("helpdiv").style.display = "block";
}

// Opens the extra help div at the first time
function extraHelpDivFirst(eventx,eventy)
{
  if (extraHelpOpen != null) {
    var hd = document.getElementById("helpdiv");
    document.body.removeChild(hd);
    extraHelpOpen = null;
  }
  var ehdiv = document.createElement("div");
  ehdiv.setAttribute("id","helpdiv");
  ehdiv.setAttribute("class","helpdiv");
  ehdiv.setAttribute("className","helpdiv");
  if (eventy < document.body.clientHeight/2) {
    ehdiv.style.top = eventy + 10 + "px";
  } else {
    ehdiv.style.top = eventy + 10 - 170 + "px";
  }
  ehdiv.style.right = "20px";
  document.body.appendChild(ehdiv);
  var xsign = document.createElement("a");
  xsign.setAttribute("id","xsign");
  xsign.setAttribute("class","xsign");
  xsign.setAttribute("className","xsign")
  var xoncl = function(event) {
    document.getElementById("helpdiv").style.display = "none";
  }
  xsign.onclick = xoncl;
  var ximg = document.createElement("img");
  ximg.setAttribute("src","./image/bezar.png");
  ximg.setAttribute("alt","close");
  xsign.appendChild(ximg);
  ajaxHelpContent("helpdiv",xsign);
  extraHelpOpen = true;
  var messf = function(restext) {
    document.getElementById("xsign").setAttribute("title",restext);
  }
  ajaxMessage("ws_close",messf);
}

// Opens the popup window with all teachers
function popupAllTeachers()
{
  window.open('teachers.php','Informations','width=640px, height=150px, scrollbars=yes, menubar=no');
}

// Preloads the calculator button images of WMI2 (or an array of images)
function imagePreload(iarray)
{
  var ihtml = '<div style="position:absolute;top:-100px;left:0px;visibility:hidden;">';
  for (lv= 0; lv < iarray.length-1; lv++)
    ihtml += '<img src="'+iarray[lv]+'" height="10" width="10" alt="">';
  ihtml += '<img src="'+iarray[iarray.length-1]+'" height="10" width="10"';
  if (navigator.appName == "Netscape") {
    ihtml += ' onload="showLayout()"';
  }
  ihtml += ' alt="">';
  ihtml += '</div>';
  document.getElementById("hidden").innerHTML = ihtml;
}

