/* ---------------------------------------------------------------------

   Purpose...
     These routines implement collapsing and expanding of the
     description field in the annotated bibliography. The field is
     initially expanded, to facilitate "viewers" with Javascript
     disabled. They are collapsed on load. Triangle "wedgies" are
     used to indicate collapesed, expanded, and hover states. The
     implementation relies on appropriate CSS classes being defined
     for the description "div".

   Programmed by...
     Keith Eric Grant

   Date...
      30 September 2006

 ---------------------------------------------------------------------*/
function replaceWedgie ( event )
{

  var wedgie; // Wedgie image
  var re;     // Regular expression
  var parent; // Parent node of wedgie
  var sibling // Used for actual description
  var state;  // Open/Closed state

  // Handle IE or W3C event models. W3C event pass the event
  // as an argument. IE stores in under window.event. The
  // names of the "target" element creating the event are
  // also different.

  if (!event) {
    var event = window.event;
    wedgie = event.srcElement;
  } else {
    wedgie = event.target;
  }

  parent  = wedgie.parentNode;
  sibling = parent.nextSibling;

  // Parse the name of the current wedgie image

  re = /(wedge_((closed)|(open)))(_hover)?\.png/;
  state = wedgie.src.match(re);

  switch ( event.type.toLowerCase() ) {

  case 'mouseover' :

    wedgie.src = "images/"+state[1]+"_hover.png";

    // Turn on hover visibility of the description.
	// The manuvering with the z-index of the .mods div is
	// needed to get IE to position the hovering box above
	// the next .mods div.

    if ( state[2] == 'closed' ) {
		sibling.className = "desc_hover";
		parent.parentNode.style.zIndex = "1";
	}

    break;

  case 'mouseout' :

    re = /(wedge_((closed)|(open)))(_hover)?\.png/;
    wedgie.src = "images/" + state[1]+".png";

    // Turn off hover visibility of the description.
	// Reset the z-index of the containing .mods div

    if ( state[2] == 'closed' ) {
		sibling.className = "desc_hidden";
		parent.parentNode.style.zIndex = "";
	}

    break;

  }
}  // replaceWedgie


function flipDescription ( event )
{

  var parent; // Event target parent
  var wedgie; // Wedgie image
  var sibling;


  // Handle IE or W3C event models. W3C event pass the event
  // as an argument. IE stores in under window.event. The
  // names of the "target" element creating the event are
  // also different.

  if (!event) {
    var event = window.event;
    wedgie = event.srcElement;
  } else {
    wedgie = event.target;
  }


  // The event was created by the wedgie image within the description
  // header. Step up to the parent and then to the next parent sibling
  // to get the actual description.

  parent = wedgie.parentNode;
  sibling = parent.nextSibling;

  if ( sibling.className == "desc_show" ) {

    sibling.className = "desc_hidden";
    wedgie.src = "images/wedge_closed.png";
    wedgie.alt = "Click to expand description";

  } else {

    sibling.className = "desc_show";
    wedgie.src = "images/wedge_open.png";
    wedgie.alt = "Click to collapse description";

  }
}  // flipDescription


// This routine collapses the entry descriptions on load
// and registers the event handlers to enable expansion.

function collapseDescriptions ()
{

    var idiv;
    var div;
    var divs;
    var sibling;
    var wedge;


    // Don't do anything if the Document Object Model (DOM)
    // isn't supported by the browser

    if ( !document.getElementsByTagName ) {
      return;
    }


    // XML files differ from HTML in being case sensitive
    // as to tag names.

    divs = document.getElementsByTagName("DIV");
    if ( divs.length == 0 ) {
      divs = document.getElementsByTagName("div");
    }

    for (idiv=0; idiv < divs.length; idiv++) {

      div = divs[idiv];
      if ( div.className != "descHdr" ) continue;


      // Found a description header. Change the wedgie to closed.

      wedge = div.firstChild;
      wedge.src = "images/wedge_closed.png";
      wedge.alt = "Click to expand description";


      // Register the event handlers for the wedgie image

      wedge.onmouseover = replaceWedgie;
      wedge.onmouseout  = replaceWedgie;
      wedge.onclick     = flipDescription;


      // Change the description class to make it hidden, using
      // the description being the next sibling of the header

      sibling = div.nextSibling;
      sibling.className = "desc_hidden";

    }
}



// This function randomly selects among masthead images

function chooseMastImg () {
	var bkg = new Array ();
	var imgDir = 'http://www.ramblemuse.com/newdesign/images/';
	var ix, masthead;

	bkg[0] = 'utah_viewpoint_pan_en_720.jpg';
	bkg[1] = 'wheeler_ridge_pan_720.jpg';
	bkg[2] = 'utah_redrock_snow_720.jpg';

	ix = Math.round((bkg.length-1)*Math.random());

	if ( document.getElementById ) {
	   masthead = document.getElementById('masthead');
	   masthead.style.backgroundImage = 'url(' + imgDir + bkg[ix] + ')';
	}
}


/*

  Script for using Javascript target attribute to replace the use of
  the depricated HTML target attribute of the <a> tag. The target
  attribute is replace with the attribute 'rel="external"'. This allows
  opening a link in a new window while maintaining valid XHTML. The
  DOM is a separate specification in which the target attribute
  is not deprecated. If Javascript is not enabled or the browser
  is not DOM aware, then the link will occur in the current window.

  Keven Yank: 2003. New-Window Links in a Standards-Compliant World.
  March 4th. <http://www.sitepoint.com/print/standards-compliant-world>

*/

function externalLinks() {

  // Check whether the browser supports the DOM
  if (!document.getElementsByTagName) return;
  var anchors = document.getElementsByTagName("a");
  for (var i=0; i<anchors.length; i++) {
    var anchor = anchors[i];
    if (anchor.getAttribute("href") &&
        anchor.getAttribute("rel") == "external") {
      anchor.target = "_blank";
    }
  }
}


window.onload = function () {
	collapseDescriptions();
	chooseMastImg();
	externalLinks();
}


