/*
 *   main functions
 */

function lookup(whatFor, modify) {
  if (modify == 'true') {
    document.getElementById('inputObj').value = whatFor;
  }
  whatFor = ( whatFor == '' ? "sourceIframe" : "sourceIframe."+whatFor);
  writeProcess('processingObj', whatFor);
  var forObject = whatFor.slice(whatFor.lastIndexOf('.')+1, whatFor.length);
  if (forObject == 'sourceIframe') { forObject = ''; }
  try {
    output.buildOutput(whatFor, forObject);
  }
  catch (ex) {
    alert('Exception in lookup():   '+ex+'   (in '+whatFor+')');
  }
  finally {
    output.printOutput();
  }
}

function evalExp(which, modify) {
  if (modify == 'true') {
    document.getElementById('inputExp').value = which;
  }
  writeProcess('processingExp', which);
  try {
    eval('sourceIframe.'+which);
  }
  catch (ex) {
    alert('Exception in evalExp():   '+ex+'   (in '+which+')');
  }
}

function evalJSExp(which, modify) {
  if (modify == 'true') {
    document.getElementById('inputJS').value = which;
  }
  writeProcess('processingJS', which);
  try {
    eval(which);
  }
  catch (ex) {
    alert('Exception in evalJSExp():   '+ex+'   (in '+which+')');
  }
}

function writeProcess(forEngine, forWhat) {
  var tmp = document.getElementById(forEngine);
  tmp.innerHTML = forWhat;
  tmp.parentNode.setAttribute('title', forWhat);
}

function setDocument(whichOne, type) {
  var localDomain = window.location.host;
  if (localDomain == '') {	// local application
    switch (type) {
      case 'URL':
        alert("You cannot work with remote files locally!");
        break;
      case 'file':
        try { sourceIframe.location = "file:///" + whichOne; }
        catch (ex) { alert("Exception in setDocument():   "+ex+"   (for "+type+")"); }
        break;
    }
  } else {			// server application
    switch (type) {
      case 'URL':
        if ((whichOne.indexOf(localDomain) == 0) || (whichOne.indexOf(localDomain) == 7)) {
          if (whichOne.indexOf('http://') == -1) { whichOne = 'http://' + whichOne; }
          try { sourceIframe.location = whichOne; }
          catch (ex) { alert("Exception in setDocument():   "+ex+"   (for "+type+")"); }
        } else {
          alert("You cannot work with files from another domain!");
        }
        break;
      case 'file':
        alert("You cannot work with local files remotly!");
        break;
    }
  }
}



/*
 *   other functions
 */

function showDict(whichOne) {
  var tmp = eval("document.getElementById('dict"+whichOne+"');");
  tmp.style.visibility = 'visible';
  dict_popuped = whichOne;
}
function hideDict(whichOne) {
  // hide dictionary
  var tmp = eval("document.getElementById('dict"+whichOne+"');");
  tmp.style.visibility = 'hidden';
  dict_popuped = null;
  // clear selects
  for (var i=1; i<=2; i++) {
    tmp = eval("document.getElementById('inputPlus"+whichOne+"-"+i+"');");
    tmp.selectedIndex = 0;
  }
}
function addStringMain(into) {
  if (into == '1') {
    var tmpObj=document.getElementById('inputObj');
    var tmp=document.getElementById('inputPlus1-1');
    addString(tmp.options[tmp.selectedIndex].text, tmpObj);
    tmp=document.getElementById('inputPlus1-2');
    addString(tmp.options[tmp.selectedIndex].text, tmpObj);
    //tmp=document.getElementById('inputPlus1-3');
    //addString(tmp.options[tmp.selectedIndex].text, tmpObj);
  } else {
    var tmpObj=document.getElementById('inputExp');
    var tmp=document.getElementById('inputPlus2-1');
    addString(tmp.options[tmp.selectedIndex].text, tmpObj);
    tmp=document.getElementById('inputPlus2-2');
    addString(tmp.options[tmp.selectedIndex].text, tmpObj);
    //tmp=document.getElementById('inputPlus2-3');
    //addString(tmp.options[tmp.selectedIndex].text, tmpObj);
  }
}
function addString(what, tmpObj) {
  if (what != '') {
    var tmp = tmpObj.value;
    if (tmp != '') {
      tmpObj.value = tmp + '.' + what;
    } else {
      tmpObj.value = what;
    }
  }
}

function outputClick(action, data1, data2) {
  switch (action) {
    case 'addObject':
      OC_addObject(data1);
      break;
    case 'openDOM1':
      OC_openDOM1(data1, data2);
      break;
    case 'openCSS1':
      OC_openCSS1(data1);
      break;
    case 'openCSS2':
      OC_openCSS2(data1);
      break;
  }
}
function OC_addObject(whichOne) {
  var tmp = document.getElementById('processingObj').innerHTML;
  var tmpObj = document.getElementById('inputObj');
  if (tmp.indexOf('sourceIframe.') != -1) {
    tmpObj.value = tmp.slice(13)+'.'+whichOne;
  } else {
    tmpObj.value = whichOne;
  }
}
function OC_openCSS1(property) {
  var tmp = 'property_' + CSS1_properties[property] + '.html';
  var tmpWindow = open('CSS1-reference');
  tmpWindow.document.writeln('<frameset cols="250,*">');
  tmpWindow.document.writeln('<frame name="mainIndex" src="http://zvon.org/xxl/css1Reference/Output/properties.html">');
  tmpWindow.document.writeln('<frame name="mainWindow" src="http://zvon.org/xxl/css1Reference/Output/'+tmp+'">');
  tmpWindow.document.writeln('</frameset>');
}
function OC_openCSS2(property) {
  var tmp = 'prop_' + CSS2_properties[property] + '.html';
  var tmpWindow = open('CSS2-reference');
  tmpWindow.document.writeln('<frameset cols="250,*">');
  tmpWindow.document.writeln('<frame name="mainIndex" src="http://zvon.org/xxl/CSS2Reference/Output/short.html">');
  tmpWindow.document.writeln('<frame name="mainWindow" src="http://zvon.org/xxl/CSS2Reference/Output/'+tmp+'">');
  tmpWindow.document.writeln('</frameset>');
}
function OC_openDOM1(DOMproperty, DOMnode) {
  var DOMinterface = '';
  switch (DOMnode) {
    case '1':
      DOMinterface = 'Element';
      break;
    case '2':
      DOMinterface = 'Attr';
      break;
    case '3':
      DOMinterface = 'Text';
      break;
    case '4':
      DOMinterface = 'CDATASection';
      break;
    case '5':
      DOMinterface = 'EntityReference';
      break;
    case '6':
      DOMinterface = 'Entity';
      break;
    case '7':
      DOMinterface = 'ProcessingInstruction';
      break;
    case '8':
      DOMinterface = 'Comment';
      break;
    case '9':
      DOMinterface = 'Document';
      break;
    case '10':
      DOMinterface = 'DocumentType';
      break;
    case '11':
      DOMinterface = 'DocumentFragment';
      break;
    case '12':
      DOMinterface = 'Notation';
      break;
  }
  var tmp = 'interfaces/' + DOMinterface + '.html#' + DOMproperty;
  var tmpWindow = open('DOM1-reference');
  tmpWindow.document.writeln('<frameset rows="40,*">');
  tmpWindow.document.writeln('<frame src="http://zvon.org/xxl/DOM1reference/Output/_upbar.html" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" />');
  tmpWindow.document.writeln('<frameset cols="250,*">');
  tmpWindow.document.writeln('<frame name="mainIndex" src="http://zvon.org/xxl/DOM1reference/Output/interfaceIndex.html" marginwidth="5" marginheight="0" frameborder="0" />');
  tmpWindow.document.writeln('<frame name="mainWindow" src="http://zvon.org/xxl/DOM1reference/Output/'+tmp+'" />');
  tmpWindow.document.writeln('</frameset>');
  tmpWindow.document.writeln('</frameset>');
}



/*
 *   mouse functions
 */

function mouseDown(event) {
}
function mouseUp(event) {
}
function mouseClick(event) {
  if (menu.popuped != null) {
    menu.popup(menu.popuped.id);
  }
  if (dict_popuped != null) {
    hideDict(dict_popuped);
  }
}



/*
 *   main init function
 */

var dict_popuped = null;

function factoryInit() {
  // <IE superhack> - if IFRAME is on the page, DOM structure is COMPLETELY broken (no objects after IFRAME are found)
  document.getElementById('outputIframe').innerHTML = '<iframe name="sourceIframe" src="../data/_examples.html" height="100" width="97%" scrolling="yes" frameborder="1" title="Here is the tested file" />';
  // </IE superhack>
  design_lineup();
  menu._init();
  document.getElementById('URLtoView').value = 'http://'+window.location.hostname;
  //document.onmousedown = mouseDown;
  //document.onmouseup = mouseUp;
  document.onclick = mouseClick;
}


