var host    = "itszotar.hu";
var timerId = 0;
var dict_data = new Array();

function dict_init(sid, timestamp)
{
  var lst = parent.document.getElementsByTagName("div");
  var ifrm = new Array();
  var j = 0;
  var sendHost = location.href.substr(7, location.href.indexOf('/',7)-7);
  for( var i=0; i < lst.length; ++i )
  {
      
      if ( lst[i].id == "itdict" )
      {
          ifrm[i] = dict_mkframe(i);
          dict_sendText(ifrm[i],sid, j, timestamp, lst[i],sendHost);
          j++;
      }
  }
  inc("http://"+host+"/js/dw_event.js");
  inc("http://"+host+"/js/dw_viewport.js");
  inc("http://"+host+"/js/dw_tooltip.js");  
}


function dict_reread(sid,tout)
{
  var ttime = new Date();
  //alert(parent.xxx + " -- " + currentTime.getMinutes() +"" + currentTime.getSeconds() );
  timeout = tout;
  var msg = self.location.toString();
  var idx = new Array()
  idx[0]  = msg.indexOf("#");
  if ( -1 == idx[0] ) return;
  if ( msg.substr(idx[0]+1,2) == "h=" ) return dict_setHeight(msg.substr(idx[0]+3));

  idx[1] = msg.indexOf("|",idx[0]);
  if ( -1 == idx[1] ) return;
  
  var divid = msg.substr(idx[0]+1,idx[1]-idx[0]-1);
  
  msg = msg.substr(idx[1]+1);
  inc("http://"+host+"/outdict/dictjs.php?sid="+sid+"&id="+msg);
  loadWait(tout, divid);
}


function loadWait(timeout, divid) 
{
    if ( !!( timerId ) ) 
    {
        if ( timeout == 0 ) 
        {
            document.getElementById('progress').innerHTML = "Load failed.";
            //alert( "Program load timeout" );
            return;
        }

        if ( typeof (dict_data[divid]) != 'undefined' ) 
        {
            //parent.document.getElementById('itdict').innerHTML = dict_data;
            var lst = parent.document.getElementsByTagName("div");
            var j = 0;
            for( var i=0; i < lst.length; ++i )
            {
                if ( lst[i].id == "itdict" )
                {
                  if ( divid == j )
                  {
                      lst[i].innerHTML = dict_data[divid];
                      return;
                  } else 
                  {
                      ++j;
                  }
                }
            }
            return;
        }
        timeout -= 1;
    }

    document.getElementById('progress').innerHTML += '.';
    timerId = setTimeout( "loadWait("+timeout+","+divid+")", 500);
    return;
}



function dict_mkframe(i)
{
  var ifrm = document.createElement("iframe"); 
  ifrm.style.width   = "640px"; 
  ifrm.style.height  = "480px"; 
  ifrm.style.display = "none";
  ifrm.setAttribute("id","dict_iframe"+i);  
  document.body.appendChild(ifrm);

  return ifrm;
}

function dict_mkform(ifrm)
{
  var submitForm = ifrm.createElement("form");
  if ( -1 != navigator.appVersion.indexOf("MSIE") )
      ifrm.appendChild(submitForm);
  else
      ifrm.body.appendChild(submitForm);
      
  submitForm.method = "POST";
  submitForm.setAttribute("accept-charset", "utf-8");
  return submitForm;
}

function dict_mkforminput(ifrm, inputForm, elementName, elementValue)
{ 
  var newElement = ifrm.createElement("textarea");
  newElement.setAttribute('name', elementName);
  newElement.style.display = 'none';
  inputForm.appendChild(newElement);
  newElement.value = elementValue;
  return newElement;
}

function dict_sendText(ifrm, sid, divid, timestamp,div,pb_host)
{
  var doc = dict_getDoc(ifrm);

  var submitForm = dict_mkform(doc);
  dict_mkforminput(doc, submitForm, "sid", sid);
  dict_mkforminput(doc, submitForm, "divid", divid);
  dict_mkforminput(doc, submitForm, "timestamp", timestamp);
  dict_mkforminput(doc, submitForm, "host",pb_host);
  dict_mkforminput(doc, submitForm, "msg", div.innerHTML);
  submitForm.action= "http://"+host+"/outdict/extdict.php";
  submitForm.submit();
}

function dict_getDoc(ifrm)
{
  var doc = false;
  if(ifrm.contentDocument)
      doc = ifrm.contentDocument;
  else if(ifrm.contentWindow)
      doc = ifrm.contentWindow.document;
  else if(ifrm.document)
      doc = ifrm.document;

  return doc;
}

function inc(filename)
{
    var body = document.getElementsByTagName('body').item(0);
    script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = filename;
    body.appendChild(script);
}

function dict_setHeight(size)
{
    if (size > 0 && parent && parent.parent && parent.parent.Tooltip) parent.parent.Tooltip.setSize(size);
}