// Copyright 2007 Dreiling Terrones Bartos Architecture Inc.
// All Rights Reserved

// colorLinks goes through a frame and highlights (sets class="selected")
// any that are above the current page in the hierarchy

function colorLinks ( frame, baseurl ) {

  var doc = frame.contentDocument;

  if( !doc ) {
    // for IE:
    doc = frame.contentWindow.document;
  }

  var links = doc.links;

  for( var i = 0; i < links.length; i++ ) {
    if( baseurl.substr( 0, links[i].href.length ) == links[i].href ) {
      links[i].className = "selected";
    }
  }
}

// "choose" and "preview" implement the "chooser" widget

function choose( link ) {

  var node = getAncestralChild( link, "chooser" );
  var chooser = node.parentNode;

  for( var i = 0; i < chooser.childNodes.length; i++ ) {
    chooser.childNodes[i].className = null;
  }

  node.className = "selected";
  
  link.blur();
  window.status = "";

  return false;
}

function preview( link ) {

  var node = getAncestralChild( link, "chooser" );

  if( node.className != "selected" ) {
    node.className = "preview";
    window.status = "Click to change image";
  } else {
    window.status = "";
  }

  return true;

}

function unpreview( link ) {

  var node = getAncestralChild( link, "chooser" );

  if( node.className != "selected" ) {
    node.className = null;
  }

  window.status = "";
  return true;
}

// "chooseNext" cycles through choices in a chooser

function chooseNext( chooser ) {

  var node = firstElement( chooser );

  while( node.className != "selected" ) {
    node = nextElement( node );
  }

  if( node == null ) {
    // i.e., none were "selected"
    node = chooser.lastChild;
  }
  
  node.className = null;

  node = nextElement( node );

  if( node == null ) {
    // i.e., wrap around to the first one
    node = firstElement( chooser );
  }

  node.className = "selected";
}

function cycleChooser( name, msec ) 
{
  var cmd = "chooseNext( document.getElementById( '" + name + "'))";
  setInterval( cmd , msec );
}

// "pageNext" and "pagePrev" implement the "pager" widget

function pageNext( link ) {

  var node = getAncestralChild( link, "pager" );
  var next = nextElement( node );

  node.className = null;
  next.className = "selected";

  link.blur();

  return false;
}

function pagePrev( link ) {

  var node = getAncestralChild( link, "pager" );
  var prev = previousElement( node );

  node.className = null;
  prev.className = "selected";

  link.blur();

  return false;
}

// Utility function to find the highest parent of the current 
// node directly under a parent with a certain class name.
// Use by chooser and pager.

function getAncestralChild( node, classname ) {

  var parent = node.parentNode;

  while( parent.className != classname ) {
    node = parent;
    parent = node.parentNode;
  }
  
  return node;
}

// Utility functions to find the first/last/prev/next element (not text node)

function firstElement( node ) {
    node = node.firstChild;
    while( node != null && node.nodeName == "#text" ) {
      node = node.nextSibling;
    }
    return node;
}

function lastElement( node ) {
    node = node.lastChild;
    while( node != null && node.nodeName == "#text" ) {
      node = node.previousSibling;
    }
    return node;
}

function previousElement( node ) {
    node = node.previousSibling;
    while( node != null && node.nodeName == "#text" ) {
      node = node.previousSibling;
    }
    return node;
}

function nextElement( node ) {
    node = node.nextSibling;
    while( node != null && node.nodeName == "#text" ) {
      node = node.nextSibling;
    }
    return node;
}

// centerDiv is used to center a block on the browser window.  It has limits
// on it so the block won't run off the scrollable window on a small screen.

function centerDiv( name ) {

  var obj = document.getElementById( name );

  var wx = window.innerWidth || document.body.parentElement.clientWidth;
  var wy = window.innerHeight || document.body.parentElement.clientHeight;
  var ox = obj.offsetWidth;
  var oy = obj.offsetHeight;

  var x = Math.max( Math.round((wx-ox)/2), 0);
  var y = Math.max( Math.round((wy-oy)/2), 0);

  obj.style.left = x + "px";
  obj.style.top = y + "px";

}

// centerByStyle is used to rewrite a CSS style for a block in order to center
// it the browser window.  It has limits on it so the block won't run off the 
// scrollable window on a small screen.  It requires that the style for the 
// the block to explicitly specify a width and height, so it can compute the
// center before all content is loaded.

function centerByStyle( rulename ) {

  var wx = window.innerWidth || document.body.parentElement.clientWidth;
  var wy = window.innerHeight || document.body.parentElement.clientHeight;

  for( var i=0; i < document.styleSheets.length; i++ ) {
    var rules;
    if( document.styleSheets[i].cssRules ) {
      rules = document.styleSheets[i].cssRules;
    }else {
      rules = document.styleSheets[i].rules;
    }
    for( var j=0; j < rules.length; j++ ) {
      var rule = rules[j];
      if( rule.selectorText == rulename ) {

	var ox = parseInt( rule.style.width );
	var oy = parseInt( rule.style.height );

	var x = Math.max( Math.round((wx-ox)/2), 0);
	var y = Math.max( Math.round((wy-oy)/2), 0);

	rule.style.top = y + "px";
	rule.style.left = x + "px";

	return;
      }
    }
  }
}

// basic functions we call from the page

function pageLoad() {

  var base = document.URL;
  var frames = document.getElementsByTagName("iframe");
  for( var i = 0; i < frames.length; i++ ) {
    colorLinks( frames[i], base );
  }

  // may have already been done by calling centerByStyle from
  // a <script> in the page, but just to make sure...

  centerDiv( 'page-body' );

}

function pageResize() {
  centerDiv( 'page-body' );
}

function pagePreload() {
  centerByStyle( '#page-body' );
}
