var ns4=false;

function doNothing(){}

function PreloadImages()  {
  var d=document;
  if(d.images) {
    if(!d.img) d.img=new Array();
    var i,j=d.img.length,a=imgreloadImages.arguments;
  for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){
    d.img[j]=new Image;
    d.img[j++].src=a[i];
    }
  }
}

function CalcPole()  {
  var dHeight, d = document;
  if (ie) {
    dHeight = d.body.offsetHeight - 4;
  }
  else {
    dHeight = window.innerHeight;
  }
  if (d.imgBook!=null) {
    bHeight = d.imgBook.height;    
    d.imgPole.height = Math.max(dHeight, bHeight);
  }
}

function ReloadPage(init) {
  if (init==true) with (navigator) {
    if (ns4) {
      document.PageWidth=innerWidth;
      document.PageHeight=innerHeight;
      onresize=ReloadPage;
    }
    else {
      onresize=CalcPole;
    }
  }
  else
  if (innerWidth!=document.PageWidth || innerHeight!=document.PageHeight)
    location.reload();
}
ReloadPage(true);

function FindObj(n, d) {
  var p,i,x;
  if(!d) d=document;
  if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document;
  n=n.substring(0,p);
  }
  if(!(x=d[n])&&d.all) x=d.all[n];
  for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=FindObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n);
  return x;
}

function SwapImgRestore() {
  var i,x,a=document.sr;
  for (i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function SwapImage() {
  var i,j=0,x,a=SwapImage.arguments;
  document.sr=new  Array;
  for(i=0;i<(a.length-2);i+=3)
    if ((x=FindObj(a[i]))!=null) {
      document.sr[j++]=x;
      if(!x.oSrc)  x.oSrc=x.src;  x.src=a[i+2];
    }
}

function DragLayer(objName,x,hL,hT,hW,hH,toFront,dropBack,cU,cD,cL,cR,targL,targT,tol,dropJS,et,dragJS) {
  var i,j,aLayer,retVal,curDrag=null,curLeft,curTop,IE=document.all,NS4=document.layers;
  var NS6=(!IE&&document.getElementById), NS=(NS4||NS6); if (!IE && !NS) return false;
  retVal = true; if(IE && event) event.returnValue = true;
  if (DragLayer.arguments.length > 1) {
    curDrag = FindObj(objName); if (!curDrag) return false;
    if (!document.allLayers) { document.allLayers = new Array();
      with (document) if (NS4) { for (i=0; i<layers.length; i++) allLayers[i]=layers[i];
        for (i=0; i<allLayers.length; i++) if (allLayers[i].document && allLayers[i].document.layers)
          with (allLayers[i].document) for (j=0; j<layers.length; j++) allLayers[allLayers.length]=layers[j];
      } else {
        if (NS6) { var spns = getElementsByTagName("span"); var all = getElementsByTagName("div");
          for (i=0;i<spns.length;i++) if (spns[i].style&&spns[i].style.position) allLayers[allLayers.length]=spns[i];}
        for (i=0;i<all.length;i++) if (all[i].style&&all[i].style.position) allLayers[allLayers.length]=all[i];
    } }
    curDrag.dragOk=true; curDrag.targLeft=targL; curDrag.targTop=targT;
    curDrag.toler=Math.pow(tol,2); curDrag.hLeft=hL; curDrag.htop=hT;
    curDrag.hWidth=hW; curDrag.hHeight=hH; curDrag.toTheFront=toFront;
    curDrag.dropToBack=dropBack; curDrag.dropTheJS=dropJS;
    curDrag.everyTime=et; curDrag.dragTheJS=dragJS;
    curDrag.oldZ = (NS4)?curDrag.zIndex:curDrag.style.zIndex;
    curLeft= (NS4)?curDrag.left:(NS6)?parseInt(curDrag.style.left):curDrag.style.pixelLeft;
    if (String(curLeft)=="NaN") curLeft=0; curDrag.startL = curLeft;
    curTop = (NS4)?curDrag.top:(NS6)?parseInt(curDrag.style.top):curDrag.style.pixelTop;
    if (String(curTop)=="NaN") curTop=0; curDrag.startT = curTop;
    curDrag.bl=(cL<0)?null:curLeft-cL; curDrag.bt=(cU<0)?null:curTop-cU;
    curDrag.bR=(cR<0)?null:curLeft+cR; curDrag.bB=(cD<0)?null:curTop+cD;
    curDrag.leftright=0; curDrag.updown=0; curDrag.snapped=false; //use in your JS!
    document.onmousedown = DragLayer; document.onmouseup = DragLayer;
    if (NS) document.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP);
  } else {
    var theEvent = ((NS)?objName.type:event.type);
    if (theEvent == 'mousedown') {
      var mouseX = (NS)?objName.pageX : event.clientX + document.body.scrollLeft;
      var mouseY = (NS)?objName.pageY : event.clientY + document.body.scrollTop;
      var maxDragZ=null; document.maxZ = 0;
      for (i=0; i<document.allLayers.length; i++) { aLayer = document.allLayers[i];
        var aLayerZ = (NS4)?aLayer.zIndex:parseInt(aLayer.style.zIndex);
        if (aLayerZ > document.maxZ) document.maxZ = aLayerZ;
        var isVisible = (((NS4)?aLayer.visibility:aLayer.style.visibility).indexOf('hid') == -1);
        if (aLayer.dragOk != null && isVisible) with (aLayer) {
          var parentL=0; var parentT=0;
          if (NS6) { parentLayer = aLayer.parentNode;
            while (parentLayer != null && parentLayer.style.position) {
              parentL += parseInt(parentLayer.offsetLeft); parentT += parseInt(parentLayer.offsetTop);
              parentLayer = parentLayer.parentNode;
          } } else if (IE) { parentLayer = aLayer.parentElement;
            while (parentLayer != null && parentLayer.style.position) {
              parentL += parentLayer.offsetLeft; parentT += parentLayer.offsetTop;
              parentLayer = parentLayer.parentElement; } }
          var tmpX=mouseX-(((NS4)?pageX:((NS6)?parseInt(style.left):style.pixelLeft)+parentL)+hLeft);
          var tmpY=mouseY-(((NS4)?pageY:((NS6)?parseInt(style.top):style.pixelTop) +parentT)+htop);
          if (String(tmpX)=="NaN") tmpX=0; if (String(tmpY)=="NaN") tmpY=0;
          var tmpW = hWidth;  if (tmpW <= 0) tmpW += ((NS4)?clip.width :offsetWidth);
          var tmpH = hHeight; if (tmpH <= 0) tmpH += ((NS4)?clip.height:offsetHeight);
          if ((0 <= tmpX && tmpX < tmpW && 0 <= tmpY && tmpY < tmpH) && (maxDragZ == null
              || maxDragZ <= aLayerZ)) { curDrag = aLayer; maxDragZ = aLayerZ; } } }
      if (curDrag) {
        document.onmousemove = DragLayer; if (NS4) document.captureEvents(Event.MOUSEMOVE);
        curLeft = (NS4)?curDrag.left:(NS6)?parseInt(curDrag.style.left):curDrag.style.pixelLeft;
        curTop = (NS4)?curDrag.top:(NS6)?parseInt(curDrag.style.top):curDrag.style.pixelTop;
        if (String(curLeft)=="NaN") curLeft=0; if (String(curTop)=="NaN") curTop=0;
        oldX = mouseX - curLeft; oldY = mouseY - curTop;
        document.currentDrag = curDrag;  curDrag.snapped=false;
        if(curDrag.toTheFront) {
          eval('curDrag.'+((NS4)?'':'style.')+'zIndex=document.maxZ+1');
          if (!curDrag.dropToBack) document.maxZ++; }
        retVal = false; if(!NS4&&!NS6) event.returnValue = false;
    } } else if (theEvent == 'mousemove') {
      if (document.currentDrag) with (document.currentDrag) {
        var mouseX = (NS)?objName.pageX : event.clientX + document.body.scrollLeft;
        var mouseY = (NS)?objName.pageY : event.clientY + document.body.scrollTop;
        newLeft = mouseX-oldX; newTop  = mouseY-oldY;
        if (bl!=null) newLeft = Math.max(newLeft,bl);
        if (bR!=null) newLeft = Math.min(newLeft,bR);
        if (bt!=null) newTop  = Math.max(newTop ,bt);
        if (bB!=null) newTop  = Math.min(newTop ,bB);
        leftright = newLeft-startL; updown = newTop-startT;
        if (NS4) {left = newLeft; top = newTop;}
        else if (NS6){style.left = newLeft; style.top = newTop;}
        else {style.pixelLeft = newLeft; style.pixelTop = newTop;}
        if (dragTheJS) eval(dragTheJS);
        retVal = false; if(!NS) event.returnValue = false;
    } } else if (theEvent == 'mouseup') {
      document.onmousemove = null;
      if (NS) document.releaseEvents(Event.MOUSEMOVE);
      if (NS) document.captureEvents(Event.MOUSEDOWN); //for mac NS
      if (document.currentDrag) with (document.currentDrag) {
        if (typeof targLeft =='number' && typeof targTop == 'number' &&
            (Math.pow(targLeft-((NS4)?left:(NS6)?parseInt(style.left):style.pixelLeft),2)+
             Math.pow(targTop-((NS4)?top:(NS6)?parseInt(style.top):style.pixelTop),2))<=toler) {
          if (NS4) {left = targLeft; top = targTop;}
          else if (NS6) {style.left = targLeft; style.top = targTop;}
          else {style.pixelLeft = targLeft; style.pixelTop = targTop;}
          snapped = true; leftright = startL-targLeft; updown = startT-targTop; }
        if (everyTime || snapped) eval(dropTheJS);
        if(dropToBack) {if (NS4) zIndex = oldZ; else style.zIndex = oldZ;}
        retVal = false; if(!NS) event.returnValue = false; }
      document.currentDrag = null;
    }
    if (NS) document.routeEvent(objName);
  } return retVal;
}

function ShowHideLayers() {
  var i,p,v,obj,args=ShowHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=FindObj(args[i]))!=null) {
    v=args[i+2];
    if (obj.style) {
      obj=obj.style;
    v=(v=='show')?'visible':(v='hide')?'hidden':v;
    }
    obj.visibility=v;
  }
}

function CheckReference(ref) { 
if ((ref!='') && (ref!=undefined)) { 
    return (ref!='none'); 
  } else {return false;}  
} 


function OpenIndex(urlDest) {
  if (ie) {
    if  (isXP) {
      var ref = window.showModalDialog(urlDest,'','dialogWidth=308px;dialogHeight=410px;dialogLeft=30px;dialogTop=30px;center=0;status=0');}
    else {
      var ref = window.showModalDialog(urlDest,'','dialogWidth=308px;dialogHeight=410px;dialogLeft=30px;dialogTop=30px;center=0;status=0');}
    if (CheckReference(ref)) {
      location = ref;
    }
  }
  else {
    var ref = window.open(urlDest,'','screenX=30,left=30,screenY=30,top=30,dependent=1,location=0,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0,width=350,height=360');
     ref.focus();
  }
}

function OpenNew(urlDest, text, inOther) {
  if (text != '') {
    if (window.confirm(text)) {
      if (inOther) {
        window.open(urlDest, '');
      }
      else {
        location = urlDest;
      }
    }
  }
  else {
    if (inOther) {
      window.open(urlDest, '');
    }
    else {
      location = urlDest;
    }
  }
}


function InitializeIndex() {
  window.returnValue = 'none';
}

function SetText() {
  var f = document.indexForm;
  var list = f.lstSelect;
  var btn = f.btnView;
  var les = f.hidLesson;

  if (list.selectedIndex > 0) {
    les.value = list.options[list.selectedIndex].value;
  }
  else {
    les.value = 'none';
  }
  btn.disabled = (list.selectedIndex == 0);  // not in  Netscape 4.x
}

function doOK() {
  returnValue = document.indexForm.hidLesson.value;
  if (returnValue != 'none') {
    if (ie) {
    }
    else {
      opener.location = returnValue;
    }
   close();
  }
}

function doCancel()  {
  returnValue = 'none';
  close();
}

function ControlSound(x, _sndObj, sndFile) {
  var i, method = "",  sndObj = eval(_sndObj);
  if (sndObj != null) {
    if (navigator.appName == 'Netscape') method = "play";
    else {
      if (window.MedPlay == null) {
        window.MedPlay = false;
        for(i in sndObj) if (i == "ActiveMovie") {
          window.MedPlay = true;
      break;
        }
    }
      if (window.MedPlay) method = "play";
      else if (sndObj.FileName) method = "run";
    }
  }
  if (method) eval(_sndObj + "." + method + "()");
  else window.location = sndFile;
}


