var NS4 = (document.layers);
var IE4 = (document.all);
var arWin = new Array;
var arWinBar = new Array;
var selectedWin, offsetX, offsetY, testWin;
var winZCount = 0;
var BarPlacement = (NS4) ? 13 : 16;	
var BarTitlePlacement = (NS4) ? 2 : 1;
var WinPadding = (NS4) ? 2 : 4 ;
var WinBgColor = '#CCCC99';
var WinBorderColor = '#006633';
var BarTitleColor = '#FFFFFF';		

if (NS4) {
  var coll = '';
  var styleObj = '';
} else {
  var coll = 'all.';
  var styleObj = '.style';
} 

function styleEnabled() {
  return ((NS4 && document.test) || IE4);
}

//           __    __ _           _
//          / / /\ \ (_)_ __   __| | _____      _____
//          \ \/  \/ / | '_ \ / _` |/ _ \ \ /\ / / __|
//           \  /\  /| | | | | (_| | (_) \ V  V /\__ \
//            \/  \/ |_|_| |_|\__,_|\___/ \_/\_/ |___/
// 
// The following lines have to be in the target document:
// <span id="test" style="position: absolute;"></span>
// How else to test NS stylesheets turned off?
// init_windows(); at the end

var crosson = new Image();
var crossoff = new Image();
var arrowupon = new Image();
var arrowupoff = new Image();
var arrowdownon = new Image();
var arrowdownoff = new Image();
crosson.src = "pix/crossb.gif";
crossoff.src = "pix/cross.gif";
arrowupon.src = "pix/arrowupb.gif";
arrowupoff.src = "pix/arrowup.gif";
arrowdownon.src = "pix/arrowdownb.gif";
arrowdownoff.src = "pix/arrowdown.gif";

function barOn(imgLayer,imgLayerBar,imgName) {
  if (NS4) {
    if (imgName.indexOf("cross") != -1) 
        document[imgLayerBar].document[imgName].src = crosson.src;
    if ((imgName.indexOf("arrow") != -1) && 
        ((document[imgLayer].visibility == "show") ||
         (document[imgLayer].visibility == "inherit"))  )
        document[imgLayerBar].document[imgName].src = arrowupon.src;
    if ((imgName.indexOf("arrow") != -1) && 
        (document[imgLayer].visibility == "hide") )
        document[imgLayerBar].document[imgName].src = arrowdownon.src;
  } else {
    var tempWin = eval("document.all."+imgLayer+".style");
    if (imgName.indexOf("cross") != -1) 
        document[imgName].src = crosson.src;
    if ((imgName.indexOf("arrow") != -1) && 
        ((tempWin.visibility == "visible") ||
        (tempWin.visibility == "")) )
        document[imgName].src = arrowupon.src;
    if ((imgName.indexOf("arrow") != -1) && 
        (tempWin.visibility == "hidden") )
        document[imgName].src = arrowdownon.src;
  }
}

function barOff(imgLayer,imgLayerBar,imgName) {
  if (NS4) {
    if (imgName.indexOf("cross") != -1) 
        document[imgLayerBar].document[imgName].src = crossoff.src;
    if ((imgName.indexOf("arrow") != -1) && 
        ((document[imgLayer].visibility == "show") ||
         (document[imgLayer].visibility == "inherit"))  )
      document[imgLayerBar].document[imgName].src = arrowupoff.src;
    if ((imgName.indexOf("arrow") != -1) && 
        (document[imgLayer].visibility == "hide") )
        document[imgLayerBar].document[imgName].src = arrowdownoff.src;
  } else {
    var tempWin = eval("document.all."+imgLayer+".style");
    if (imgName.indexOf("cross") != -1) 
        document[imgName].src = crossoff.src;
    if ((imgName.indexOf("arrow") != -1) && 
        ((tempWin.visibility == "visible") ||
        (tempWin.visibility == "")) )
        document[imgName].src = arrowupoff.src;
    if ((imgName.indexOf("arrow") != -1) && 
        (tempWin.visibility == "hidden") )
        document[imgName].src = arrowdownoff.src;
  }
}

function shiftTo(obj,x,y) {		
  if (NS4) {
    obj.moveTo(x,y);
  } else {
    obj.pixelLeft = x;
    obj.pixelTop = y;
  }
}

function FindWin(evt){
  for (var i = arWinBar.length-1; i >=0; i--) {
    var testWin = eval(arWinBar[i]);
    if (NS4) {
      var X = evt.pageX;
      var Y = evt.pageY;
      if ( (X > testWin.left) && 
           (X < testWin.left + testWin.clip.width) && 
           (Y > testWin.top) && 
           (Y < testWin.top + testWin.clip.height) ) {
        selectedWinBar = testWin;
        selectedWin = eval(arWin[i]);
      }
    } else {
      if ((testWin == window.event.srcElement.style) ||
          (testWin == window.event.srcElement.parentElement.style)) {
        selectedWinBar = testWin;
        selectedWin = eval(arWin[i]);
      }
    }
  }
  if (selectedWin) {
    selectedWin.zIndex = ++winZCount;
    selectedWinBar.zIndex = ++winZCount;
    return;
  }
  selectedWin = null;
  return;
}

function dragIt(evt) {
  if (selectedWin) {
    if (NS4) {
      shiftTo(selectedWin, (evt.pageX - offsetX), (evt.pageY - offsetY));
      shiftTo(selectedWinBar, (evt.pageX - offsetX), (evt.pageY - offsetY - BarPlacement));
    } else {
      shiftTo(selectedWin, (window.event.clientX - offsetX), (window.event.clientY - offsetY));
      shiftTo(selectedWinBar, (window.event.clientX - offsetX), (window.event.clientY - offsetY - BarPlacement));
      return false;
    }
  }
}

function engage(evt) {
  FindWin(evt);
  if (NS4) document.routeEvent(evt);
  if (selectedWin){
    if (NS4) {
      offsetX = evt.pageX - selectedWin.left;
      offsetY = evt.pageY - selectedWin.top;
    } else {
      offsetX = window.event.offsetX;
      offsetY = window.event.offsetY;
    }
  }
  // return false; for Macs, but prevents links from working
}

function release(evt) {
  if (selectedWin) selectedWin = null;
}

function popWinArray(id){
  arWin[arWin.length] = "document." + coll + id + styleObj;
  arWinBar[arWinBar.length] = "document." + coll + id + "Bar" + styleObj;
}

function kill(id){
  var tempWin = eval("document." + coll + id + styleObj);
  var tempWinBar = eval("document." + coll + id + "Bar" + styleObj);
  tempWin.visibility = "hidden";
  tempWinBar.visibility = "hidden";
}

function winShade(id){
  var tempWin = eval("document." + coll + id + styleObj);
  if (NS4) {
    var tempWinBar = eval("document." + id + "Bar.document." + id + "arrow");
  } else {
    var tempWinBar = eval("document." + id + "arrow");
  }
  if ((tempWin.visibility == "hidden") || (tempWin.visibility == "hide")) {
    tempWin.visibility = "visible";
    tempWinBar.src = "pix/arrowup.gif";
  } else {
    tempWin.visibility = "hidden";
    tempWinBar.src = "pix/arrowdown.gif";
  }
  return false;
}

function makeWindow(id, title, left, top, width, content) {

  document.write(
'<style type="text/css">',
'#',id,' {',
'position:absolute;',
'left:',left,';',
'top:',top,';',
'width:',width,';',
'border: 2px solid ',WinBorderColor,';',
'background-color: ',WinBgColor,';',
'layer-background-color: ',WinBgColor,';',
'padding: ',WinPadding,';',
'z-index:0',
'}',
'#',id,'Bar {',
'position:absolute;',
'left:',left,';',
'top:',top-BarPlacement,';',
'width:',width,';',
'background-color: ',WinBorderColor,';',
'layer-background-color: ',WinBorderColor,';',
'margin: 0px;',
'padding: 0px;',
'cursor: move;',
'z-index:0',
'}',
'#',id,'Title {',
'position:relative;',
'font-size:smaller;',
'font-family: Arial, Helvetica, sans-serif;',
'color:',BarTitleColor,';',
'left:2;',
'top:',BarTitlePlacement,';',
'}',
'</style>',
'<div id="',id,'">',content,'</div>',
'<div id="',id,'Bar">',
'<a href="javascript:void(0)" onMouseDown="kill(\'',id,'\'); return false" onMouseOver="barOn(\'',id,'\',\'',id,'Bar\',\'',id,'cross\')" onMouseOut="barOff(\'',id,'\',\'',id,'Bar\',\'',id,'cross\')">',
'<img src="pix/cross.gif" name="',id,'cross" width="18" height="18" align="left" hspace="0" border="0"></a>',
'<a href="javascript:void(0)" onMouseDown="winShade(\'',id,'\'); return false" onMouseOver="barOn(\'',id,'\',\'',id,'Bar\',\'',id,'arrow\')" onMouseOut="barOff(\'',id,'\',\'',id,'Bar\',\'',id,'arrow\')">',
'<img src="pix/arrowup.gif" name="',id,'arrow" width="18" height="18" align="right" hspace="0" border="0"></a>',
'<span id="',id,'Title">',title,'</span>',
'</div>'

  );

popWinArray(id);

}

function init_windows() {

if (!styleEnabled()) return;

// id, title, left, top, width, content 

makeWindow('WinA', 'What\'s New', 150, 190, 250,'<p>We just won an award as the 26th most linked-to web site among Native American resource directories!</p><p>This makes me even more slightly guilty about the fact that I haven\'t updated in a while...but I will very shortly, I have a number of corrections and additions in the database at the moment. Thanks for everyone\'s patience.</p>'
);

}

//                       __ __     ___ _
//                    /\ \ Y _\   / __(_)_  __
//                   /  \/ | \   / _\ | \ \/ /
//                  / /\  /_\ \ / /   | |>  <
//                  \_\ \/ \__/ \/    |_/_/\_\
// 

function NSFix(){
  if (document.NSFix.Width != window.innerWidth ||
      document.NSFix.Height != window.innerHeight) {
    document.location = document.location;
  }
}

function NSFixCheck() {
  if (NS4){
    if (typeof document.NSFix == 'undefined') {
      document.NSFix = new Object;
    }
    document.NSFix.Width = window.innerWidth;
    document.NSFix.Height = window.innerHeight;
    window.onresize = NSFix;
  }
}

//                   __                 _
//                  /__\_   _____ _ __ | |_ ___
//                 /_\ \ \ / / _ \ '_ \| __/ __|
//                //__  \ V /  __/ | | | |_\__ \
//                \__/   \_/ \___|_| |_|\__|___/
// 

if (NS4) {
  document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP | Event.LOAD)
  document.onload = NSFixCheck();
}
document.onmousedown = engage;
document.onmousemove = dragIt;
document.onmouseup = release;
