// JavaScript for SKS-E
// Version 1.3 KK 20081010

var minus='&#x2013;' , chk='&#x221a;' , space="&#160;&#160;" ,
    SKSblue_1='#00c0ff' , SKSblue_2='#bccbcf' ,
    skaktikon='&#x2195;' ;

if( !window.name ) window.name= "SKStop" ;

function showhide( id, idp ) {
  elem= document.getElementById( id );
  if( !elem ) return false ;
  plus= document.getElementById( idp );
//alert( "display="+elem.style.display );
  if( elem.style.display=='block' || (plus && !plus.innerHTML.match(/\+/)) ) {
    elem.style.display= 'none';
    if( plus) plus.innerHTML= '+';
    }
  else {
    elem.style.display= 'block';
    if( plus ) plus.innerHTML= minus ;
//alert( "display="+elem.style.display );
    }
  return true ;
  }

function tny( elem, tnid ) {
  tntxt= document.getElementById( tnid );
  lemma= elem.previousSibling ;
  tnote= elem.nextSibling ;
  if( tnote && tnote.nodeType==1 /*ELEMENT_NODE*/ && tnote.getAttribute("id")==tnid ) {
    // tnote present and visible
    elem.parentNode.removeChild( elem.nextSibling );
    lemma.style.backgroundColor= ''; // inherited color
    }
  else { // no tnote
    var newtnote= tntxt.cloneNode( true );
    newtnote.style.display= 'inline';
    elem.parentNode.insertBefore( newtnote, tnote );
    lemma.style.backgroundColor= SKSblue_2 ;
    }
}

function tnx( elem ) {
//  elem= document.getElementById( id );
  lemma= elem.previousSibling ;
  tnote= elem.nextSibling ;
  if( tnote.style.display ) {
    tnote.style.display= '';
    lemma.style.backgroundColor= ''; // inherited color
    }
  else { // Initially visibility may be undefined, therefore this must be the default
    tnote.style.display= 'inline';
    lemma.style.backgroundColor= SKSblue_2 ;
    }
}

function alltnx() {
  if( tick[4]==space )
    showhiderefx( 4 );
  window.setTimeout( "alltndelayed()", 0 );
}

function alltndelayed() {
  for( i=0 ; i<document.anchors.length ; i++ ) {
    a= document.anchors[i] ;
    if( a.className=='tn' ) {
      var tnid= a.href.match( /'([^']*)'/ );
      tny( a, tnid[1] );
    }
  }
  allposmn() ;
}

function alttn( here ) {
  here.style.backgroundColor= 'navy';
}

var userpref, neww= false ;

function blank(target, loc, x, y) {
  if( !userpref ) loadcookie( "neww" );
  if( !userpref && target!='SKStop' ) {
    neww= window.confirm( "Teksten vises i et nyt vindue\n(Vælg Annuller for at få teksten vist i nuværende vindue)\nThe text is shown in a new window\n(Choose Cancel to stay in current vindow)" );
    userpref= true ;
    storecookie( "neww" ) ;
    }
  if( neww ) {
    if( !x) { x= "675"; y= "300"; }
    w= window.open(loc,target,"width="+x+",height="+y+",resizable=yes,scrollbars=yes,status=no,menubar=no,titlebar=no,location=yes",true);
    w.focus();
    return false ;
    }
  else
    return true ;
 }

var cl= new Array( "korSK", "korSKS", "korSV3", "k", "tn", "skt", "korPap", "korEPI-II" ) ;
    tick= new Array( chk, space, space, space, space, space, space, space ); 

function loadcookie( name ) {
  var allcookie= document.cookie ;
  var pos= allcookie.indexOf( name );
//alert( "load "+name+" : "+allcookie );
  if( pos==-1 ) return false ;
  var start= pos + 5 ;
  var end= allcookie.indexOf( ";", start );
      if( end==-1 ) end= allcookie.length ;
  var cookieval= allcookie.substring( start, end );
  if( name=="tick" ) {
    if( cookieval=="10000000" ) return false ;
    for( i in cl ) {
      tick[i]= cookieval.substr(i,1)==0 ? space : chk ;
      }
    }
  if( name=="neww" ) {
    neww= cookieval=="true" ? true : false ;
    userpref= true ;
    }
  return true ;
}

function storecookie( name ) {
 var path= "; path=/" ;
 if( name=="tick" ) {
  var cookieval= "" ;
  for( i in cl )
    cookieval+= tick[i]==space ? "0" : "1" ;
  document.cookie= "tick=" + cookieval + path ;
  }
 else {
  if( neww )
    document.cookie= "neww=true" + path ;
  else
    document.cookie= "neww=false" + path ;
  }  
//alert( "store "+name+" : "+document.cookie );
}

var g1, g2, g3, g4;

function initrefx() {
  var chkd ;
  if( loadcookie( "tick" ) ) {
    g3= null;
    for( ix in cl ) {
      g1= cl[ix] ;
      chkd= (tick[ix]!=space)
      chk_box( ix, chkd );
      if( chkd ) {
        g2= "inline" ;
        switch( ix ) {
          case "3": g4= "*" ;
            break;
          case "4": g4= "]" ;
            break;
          case "5": g4= skaktikon ;
            break;
          default: g4= "noicon" ;
          }
        }
      else {
        g2= "none" ;
        switch( ix ) {
          case 3: case 4: case 5: g4= "" ;
            break;
          default: g4= "noicon" ;
          }
        }
      anchorsaway() ;
      }
    }
  }

function chk_box( ix, chkd ) {
  var chkbox ;
    chkbox= document.getElementById( cl[ix]+"top" );
    if( chkbox ) chkbox.checked= chkd ;
    chkbox= document.getElementById( cl[ix]+"bottom" );
    if( chkbox ) chkbox.checked= chkd ;
  }


function showhiderefx( ix ) {
  var droparea= document.getElementById( "Her-rulles" );
  var thiscl= cl[ix], icon ;
      if(tick[ix]!=space) {
        dis= "none";
        tick[ix]= space ;
        chk_box( ix, false );
        switch( ix ) {
          case 3: case 4: case 5: icon= "" ;
            break;
          default: icon= "noicon" ;
          }
        }
      else {
        dis= "inline";
        tick[ix]= chk ;
        chk_box( ix, true );
        switch( ix ) {
          case 3: icon= "*" ;
            break;
          case 4: icon= "]" ;
            break;
          case 5: icon= skaktikon ;
            break;
          default: icon= "noicon" ;
          }
        }
  storecookie( "tick" ) ;
  blinkdrop( droparea );
  g1=thiscl ; g2=dis ; g3=droparea ; g4=icon ;
  window.setTimeout( "anchorsaway()", 0 );
  }

function anchorsaway() {
  var thiscl=g1 , dis=g2 , droparea=g3, icon=g4 ;
//alert( g1+":"+g2+":"+g3+":"+g4 );
  for( i=0 ; i<document.anchors.length ; i++ ) {
    a= document.anchors[i] ;
    if( a.className==thiscl ) {
      a.style.display= dis ;
      if( icon!="noicon" )
        a.innerHTML= icon ;
      if( dis=="none" && thiscl=="tn" )   {
        var tnote= a.nextSibling ;
        if( tnote!=null && tnote.nodeType==1 /*ELEMENT_NODE*/ && tnote.className=="tnote" ) {
          a.previousSibling.style.backgroundColor= ""; // lemma
          a.parentNode.removeChild( tnote );           // tnote
          }
        }
      }
    }
  if( droparea ) blankdrop( droparea );
  window.setTimeout( "allposmn()", 0 );
  }

var oldcol ;
function hoover( here ) {
  oldcol= here.style.backgroundColor ;
  here.style.backgroundColor= SKSblue_1 ;
  }

function unhoover( here ) {
  here.style.backgroundColor= oldcol ;
  }

var kormenu0= "<p>Sidetal</p><p onMouseOver=\"hoover(this)\" onMouseOut=\"unhoover(this)\"><a href=\"javascript:showhiderefx(0)\" title=\"Show first print page numbers\">" ;
var kormenu1= "&#160;A</a></p><p onMouseOver=\"hoover(this)\" onMouseOut=\"unhoover(this)\"><a href=\"javascript:showhiderefx(1)\" title=\"Show SKS page numbers\">" ;
var kormenu2= "&#160;SKS</a></p><p onMouseOver=\"hoover(this)\" onMouseOut=\"unhoover(this)\"><a href=\"javascript:showhiderefx(2)\" title=\"Show SV3 page numbers\">" ;
var kormenu3= "&#160;SV3</a></p><p>Henvisninger til</p><p onMouseOver=\"hoover(this)\" onMouseOut=\"unhoover(this)\"><a href=\"javascript:showhiderefx(3)\" title=\"Show references * for explanatory notes\">" ;
var kormenu4= "&#160;kommentarer</a></p><p onMouseOver=\"hoover(this)\" onMouseOut=\"unhoover(this)\"><a href=\"javascript:showhiderefx(4)\" title=\"Show references ] for critical notes\">" ;
var kormenu5= "&#160;tekstkritik</a></p>" ;
/* incl. shafts
var kormenu5= "&#160;tekstkritik</a></p><p onMouseOver=\"hoover(this)\" onMouseOut=\"unhoover(this)\"><a href=\"javascript:showhiderefx(5)\" title=\"Show shafts for underlying text layers\">" ;
var kormenu6= "&#160;skakter</a></p>" ;
*/
var korjp0= "<p>Sidetal mv...</p><p><a onMouseOver=\"hoover(this)\" onMouseOut=\"unhoover(this)\" href=\"javascript:showhiderefx(0)\" title=\"Show ms. page numbers\">" ;
var korjp1= "&#160;Ms.</a></p><p onMouseOver=\"hoover(this)\" onMouseOut=\"unhoover(this)\"><a href=\"javascript:showhiderefx(1)\" title=\"Show SKS page numbers\">" ;
var korjp2= "&#160;SKS</a></p><p onMouseOver=\"hoover(this)\" onMouseOut=\"unhoover(this)\"><a href=\"javascript:showhiderefx(6)\" title=\"Show Pap. entry numbers\">" ;
var korjp3= "&#160;Pap.</a></p><p>Henvisninger til...</p><p onMouseOver=\"hoover(this)\" onMouseOut=\"unhoover(this)\"><a href=\"javascript:showhiderefx(3)\" title=\"Show references * for explanatory notes\">" ;

function blankdrop( droparea ) {
  droparea.innerHTML= "" ; 
  }

function blinkdrop( droparea ) {
  droparea.innerHTML= "<p style=\"text-decoration:blink;background-color:magenta\">Vent ...</p>" ; 
  }

function dropkor( kor, droptxt ) {
  var droparea= document.getElementById( "Her-rulles" );
  var x= kor.offsetLeft + kor.offsetWidth;
  var y= kor.offsetTop ;// + kor.offsetHeight;
  var p= kor.offsetParent;
  while(p!=null) {
    x+= p.offsetLeft;
    y+= p.offsetTop;
    p= p.offsetParent;
    }
  x= x+"px" ; y= y+"px" ; 
  if( droparea.innerHTML && x==droparea.style.left
                         && y==droparea.style.top )
    blankdrop( droparea )
  else {
    droparea.innerHTML= droptxt ;
//  if( x=="0px" ) // if id=="fix" in Netscape a.o. browsers
//    droparea.style.position= "fixed";
//  else // <kor>s in text or anywhere in MSIExplorer
    droparea.style.position= "absolute";
    droparea.style.left=x;
    droparea.style.top=y;
    }
  }

function dropkorts( id ) {
  var kor= document.getElementById( id );
  dropkor( kor,
           kormenu0+tick[0]
          +kormenu1+tick[1]
          +kormenu2+tick[2]
          +kormenu3+tick[3]
          +kormenu4+tick[4]
          +kormenu5
                 /*+tick[5]
          +kormenu6*/ );
  }

function dropkorjp( id ) {
  var kor= document.getElementById( id );
  dropkor( kor,
           korjp0+tick[0]
          +korjp1+tick[1]
          +korjp2+tick[6]
          +korjp3+tick[3]
          +kormenu4+tick[4]
          +kormenu5/*+tick[5]
          +kormenu6*/ );
  }

function dropskt( id, mss ) {
  var droparea= document.getElementById( "sktp" ); // dangerous if sktp is not initialised !
  var skt= document.getElementById( id );
  var x= skt.offsetLeft;
  var y= skt.offsetTop + skt.offsetHeight;
  var p= skt.offsetParent;
  var ms= mss.split( /\s+/ );
  var sktmenu= "" ;
  var pkt, kol, und, lit, litra, helms, prevms, fil, msn, tit ;
  id= id.replace( /[a-z]$/, "" ); // Remove trailing litra
  while(p!=null) {
    x+= p.offsetLeft;
    y+= p.offsetTop;
    p= p.offsetParent;
    }
  for( i in ms ) {
    msi= ms[i];
    lit= msi.search(/[a-z]$/);
    litra= lit>0?msi.substr(lit,1):"";
    und= msi.indexOf("_");
    if( und>=0 ) msi= msi.replace( "_", "" );
    kol= msi.indexOf(":");
    pkt= msi.indexOf(".");
    if( pkt<0 ) pkt= msi.lenght;
    helms= msi.substring( kol+1, pkt );
    if( helms=="0" ) {
      fil= "txt.htm#" + id + litra ;
      msn= "A" 
      }
    else {
      fil= "ms" + helms + ".htm#" + id + litra ;
      msn= "Ms. " + msi ;
      }
    if( kol>0 ) { // Foreign title
      tit= msi.substring( 0, kol );
      if( helms=="0" )
        msn= tit + ":" + msn ;
      fil= "../" + tit + "/" + fil ;
      }
    if( i>0 ) // but not the first time: Collations
      sktmenu+= "<p onMouseOver=\"hoover(this)\" onMouseOut=\"unhoover(this)\">" +
                "<a href=\"dif" + helms + "-" + prevms +
                ".htm#" + id + litra +  "\" target=\"dif" + helms + 
                "\" onClick=\"blankdrop(this.parentNode.parentNode);blank(\'dif" + helms + "\')\">&#160;&#160;&lt;&gt;&#160;&#160;</a></p>" ;
    prevms= helms ;
    if( und>=0 )
      sktmenu+= "<p>*" + msn + "</p>" ;
    else
      sktmenu+= "<p onMouseOver=\"hoover(this)\" onMouseOut=\"unhoover(this)\"><a href=\"" +
                fil + "\" target=\"ms" + helms + 
                "\" onClick=\"blankdrop(this.parentNode.parentNode);blank(\'ms" + helms + "\')\">&#160;&#160;" + 
                msn + "</a></p>" ;
    }
  if( droparea.innerHTML && x==parseInt(droparea.style.left)
                         && y==parseInt(droparea.style.top) )
    blankdrop( droparea )
  else {
    droparea.innerHTML= sktmenu ;
    droparea.style.top=y+"px";
    droparea.style.left=x+"px";
    }
  }

function drawpic( illfil, illtxt ) {
  w=open( "","Ill","toolbar=no,location=no,directories=no,status=no,menubar=no,width=400,height=600,resizable=yes,scrollbars=yes" );
  w.document.open( "text/html" );
  w.document.writeln( "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html\" charset=\"UTF-8\"><title>Ill SKS</title><link rel=\"stylesheet\" href=\"../kn1/sks-e.css\" type=\"text/css\"></head>" );
  w.document.writeln("<body><img src=\"" + illfil + "\" width=\"100%\"><p>" + codeUTF8(illtxt) + "</p></body></html>")
  w.document.close();
  w.focus();
  }

function codeUTF8( s ) { // code UTF-8 chars as entities to make up for a deficiency in MSIE
  var res= ent= "" ;
  var byte1= byte2= byte3= byte4= 0 ;
  for( var i=0 ; i<=s.length ; i++ ) {
    if( (byte1= s.charCodeAt(i)) > 127 ) {
      if( byte1 > 191 ) {
        if( byte1 > 223 ) {
          if( byte1 > 239 )
            {byte2= s.charCodeAt(++i); byte3= s.charCodeAt(++i); byte4= s.charCodeAt(++i);}
          else
            {byte2= s.charCodeAt(++i); byte3= s.charCodeAt(++i);}
          }
        else
          byte2= s.charCodeAt(++i);
          }
      else
        /*not UTF-8 encoded*/ return s;
      if( byte2 && byte2 < 128 || byte2 > 191 ) // following bytes must start with 10
          /*not UTF-8 encoded*/ return s;
      if( byte3 && byte3 < 128 || byte3 > 191 )
          /*not UTF-8 encoded*/ return s;
      if( byte4 && byte4 < 128 || byte4 > 191 )
          /*not UTF-8 encoded*/ return s;

      if( byte4 )
        ent= "&#" + ((((byte1 & 0x7)*64 + (byte2 & 0x3f))*64 + (byte3 & 0x3f))*64 + (byte4 & 0x3f)) + ";" ;
      else if( byte3 )
        ent= "&#" + (((byte1 & 0xf)*64 + (byte2 & 0x3f))*64 + (byte3 & 0x3f)) + ";" ;
      else if( byte2 )
        ent= "&#" + ((byte1 & 0x1f)*64 + (byte2 & 0x3f)) + ";" ;
      else
        /*not UTF-8 encoded*/ return s;
//    alert( byte1+":"+byte2+":"+byte3+":"+byte4+":"+ent );
      res= res + ent ;
      }
    else
      res= res + s.charAt(i) ;
    }
  return res ;
  }

/*
function posmn( ref, id, min ) {
  var not= document.getElementById( id );
  var y= ref.offsetTop;
  var p= ref.offsetParent;
  while(p!=null) {
    y+= p.offsetTop;
    p= p.offsetParent;
    }
  if( y<min ) y= min ;
  not.style.top=y+"px";
  not.style.position= "absolute";
  not.style.zIndex= 1;

  return y+not.offsetHeight;
  }
*/

function posmn( ref, id, freespace ) {
  var not= document.getElementById( id );
  if( not.className!="margen" ) return freespace ;
  not.style.position= "static"; // i.e. default placement
  var orig= not.offsetTop ;
  var y= ref.offsetTop ;
  if( ref.offsetParent.className=="tab" ) { // in case of <blok>/<tab>
    p= ref ;
    while( p.tagName.toLowerCase()!="table" ) {
      p= p.offsetParent ;
      y+= p.offsetTop
      } 
    }
  var room= not.offsetParent.offsetHeight ;
  if( orig==1 ) { // first <not> in <opt>
    if( freespace<0 )
      freespace= room + freespace ;
    else
      freespace= room ;
    }
  var top= room - freespace ;
  if( top > y ) y= top ;
  top= y + not.offsetHeight ;
  freespace= (room - top) - 10 ;
  y-= orig ;
  not.style.position= "relative";
  not.style.top=y+"px";
  return freespace ;
  }

function allposmn() {
  var savefree= 0 , num, savenum= 0 ;
  var xspace= document.getElementById( "xspace" );
  if( !xspace ) return ; // no mn's
  for( i=0 ; i<document.anchors.length ; i++ ) {
    var a= document.anchors[i] ;
    if( a.className=='ref' ) {
      var aid= a.name.substr(1) /*skip leading R*/;
      num= aid.substring( aid.indexOf("-")+1, aid.indexOf(".") );
      if( num-savenum>1 ) savefree= 0 ;
      savenum= num ;
      savefree= posmn( a, aid, savefree ) ;
    }
  }
  while( xspace.offsetHeight<-savefree )
    xspace.innerHTML+= "<br>" ;
}

function gotopage( p, last, fil ) {
  var n= p.match( /[0-9]+/ )-0;
  if( !fil ) fil= "txt_" ;
  if( n<0 ) n= 0 ;
  else if ( last-0<n ) n= last-0 ;
  window.location= fil + n + ".htm" ;
}

function gotoreg( fil, name ) {
  var suf, zoom, i ;
  if( fil=="bib" ) {
    if( name.substr(1,1)==" " )
      suf= name.substr(0,1) + name.substr(2) ;
    else
      suf= name ;
    if( (i=suf.indexOf(" "))>0 )
      suf= suf.substr(0,i)
    }
  else {
    suf= name.substr(0,1);
    }
  suf= suf.replace(/(æ|Æ|%E6|%C6)/g,"1");
  suf= suf.replace(/(ø|Ø|%F8|%D8)/g,"2");
  suf= suf.replace(/(å|Å|%E5|%C5)/g,"3");
  if( name.length>1 )
    zoom= "?zoom_highlightsub=" + name ;
  else
    zoom= "" ;
  window.location= fil + "_" + suf + ".asp" + zoom ;
}

function showbut( button ) {
}

function pushbut( button ) {
}

function leavebut( button ) {
}

