//  =======================================================================
//  University of Minnesota Nanotechnology Coordinating Office
//  http://www.nano.umn.edu
//  Institute of Technology Nanofabrication Center
//  http://www.nfc.umn.edu
//
//  /scripts/umn_nfc_utlities.js
//
//  Common javascript utilities for inclusion in html pages
//
//
//  Programmer:  John Schafer   jschafer :at: umn :dot: edu
//
//  =======================================================================
//  Copyright (C) 2006, 2007   University of Minnesota
//  All rights reserved.
//  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR 
//  PURPOSE.
//  =======================================================================


/*

   DOM functions

   These functions allow you to interact with the document object model 
   (DOM) components.  Picked up from A List Apart (www.alistapart.com)

*/

function setIdProperty(id, property, value)  {
   var styleObject = document.getElementById( id );
   if (styleObject != null)  {
      styleObject = styleObject.style;
      styleObject[ property ] = value;
      }
   }


function swapAll(num, action)  {
   for (var cnt = 0; cnt < num; cnt++)  {
      swapBlock(cnt, action);
      }
   }


/**********************************************************
    <span>s or <div>s come in pairs. The
    "contracted" forms have id=c0, c1, c2 (etc.), and
    the "expanded" forms have id=e0, e1, e2 (etc.)

    This function swaps the pair with the given
    objNum, and shows the expanded block if expanding is
    true, or the contracted one if expanding is false.
**********************************************************/

function swapBlock(objNum, expanding)  {
   if (expanding)      {
      setIdProperty("c" + objNum, "display", "none");
      setIdProperty("e" + objNum, "display", "inline");
      }
   else  {
      setIdProperty("e" + objNum, "display", "none");
      setIdProperty("c" + objNum, "display", "inline");
      }
   }




/*

  E-mail functions
  
  These functions allow you to include an e-mail address on a web page 
  as a javascript function so it won't be seen by harvesters.  Picked up 
  and modified from CharFac web site (http://www.charfac.umn.edu).

*/

var tld_ = new Array("com", "org", "net", "ws", "info", "edu", "gov", 
                     "ca", "co.uk", "org.uk", "gov.uk", "ac.uk");
var num_dom = 12;
var m_ = "mailto:";
var a_ = "@";
var d_ = ".";


function mail(name, dom, tl)  {
   //  Generate a mailto: link with the e-mail address as the
   //  anchor text.
   var s = e(name, dom, tl);
   document.write('<a href="' + m_ + s + '">' + s + '</a>');
   }

function mailp(name, dom, tl, params)  {
   //  Generate a mailto: link with the e-mail address as the
   //  anchor text, and address parameters.
   var s = e(name, dom, tl);
   document.write('<a href="' + m_ + s + params + '">' + s + '</a>');
   }


function mail2(name, dom, tl, display)  {
   //  Generate a mailto: link with a passed-in display string as
   //  the anchor text.
   var s = e(name, dom, tl);
   document.write('<a href="' + m_ + s + '">' + display + '</a>');
   }

function mail2p(name, dom, tl, display, params)  {
   //  Generate a mailto: link with a passed-in display string as
   //  the anchor text, and address parameters.
   var s = e(name, dom, tl);
   document.write('<a href="' + m_ + s + params + '">' + display + '</a>');
   }


function mail3(name, dom, tl, title)  {
   //  Generate a mailto: link with the e-mail address as the
   //  anchor text and with a hover tool-tip.
   var s = e(name,dom,tl);
   document.write('<a title="' + title + '" href="' + m_ + s + params + '">' + s + '</a>');
   }

function mail3p(name, dom, tl, title, params)  {
   //  Generate a mailto: link with the e-mail address as the
   //  anchor text, a hover tool-tip, and address parameters.
   var s = e(name,dom,tl);
   document.write('<a title="' + title + '" href="' + m_ + s + params + '">' + s + '</a>');
   }


function mail4(name, dom, tl, display, title)  {
   //  Generate a mailto: link with a passed-in display string as
   //  the anchor text and with a hover tool-tip.
   var s = e(name,dom,tl);
   document.write('<a title="' + title + '" href="' + m_ + s + '">' + display + '</a>');
   }

function mail4(name, dom, tl, display, title, params)  {
   //  Generate a mailto: link with a passed-in display string as
   //  the anchor text, a hover tool-tip, and address parameters.
   var s = e(name,dom,tl);
   document.write('<a title="' + title + '" href="' + m_ + s + params + '">' + display + '</a>');
   }


function e(name, dom, tl)  {
   //  Generate the e-mail address given username, local domain, and
   //  toplevel domain index number.
   var s = name + a_;
   if (tl != -2)  {
      s += dom;
      if ((tl >= 0) && (tl < num_dom))
         s += d_ + tld_[tl];
      }
   else
      s += swapper(dom);
   return s;
   }


function swapper(d)  {
   //  Switch each 2 characters and then globaly replace ?s with .s, this 
   //  allows the domain portion of the address to be obfuscated where 
   //  the domain can't be generated by the e() function directly as 
   //  indicated by a toplevel domain index of -2.
   var s = "";
   for (var i = 0; i < d.length; i += 2)
      if (i + 1 == d.length)
         s += d.charAt(i)
      else
         s += d.charAt(i + 1) + d.charAt(i);
   return s.replace(/\?/g,'.');
   }




/*

  Resolution functions
  
  These functions set the stylsheet used depending upon browser width.
  Needs to be used in conjuction with umn_nano_init.js so check is run 
  on initial page load.

*/

function checkBrowserWidth()  {
   var theWidth = getBrowserWidth();

   if (theWidth > 850)  {
      setStylesheet("Liquid layout");
      // setColumnHeights();
      }
   else  {
      setStylesheet("Gelled layout");
      // setColumnHeights("auto");
      }
   return true;
   }


function getBrowserWidth()  {
   if (window.innerWidth)  {
      return window.innerWidth;
      }
   else if (document.documentElement && document.documentElement.clientWidth != 0)  {
      return document.documentElement.clientWidth;
      }
   else if (document.body)  {
      return document.body.clientWidth;
      }
   return 0;
   }


function setColumnHeights(auto)  {
   var theFeature2 = document.getElementById("feature2Inner");
   var theFeature3 = document.getElementById("feature3Inner");
   var theFeature4 = document.getElementById("feature4Inner");
   var theHeight = "auto";

   if (!auto)  {
      theHeight = parseInt(theFeature2.scrollHeight);
      if (theFeature3 && theFeature3.scrollHeight > theHeight)  {
         theHeight = parseInt(theFeature3.scrollHeight);
         }
      if (theFeature4 && theFeature4.scrollHeight > theHeight)  {
         theHeight = parseInt(theFeature4.scrollHeight);
         }
      theHeight += "px";
      }

   theFeature2.style.height = theHeight;
   theFeature3.style.height = theHeight;
   theFeature4.style.height = theHeight;
   
   return true;
   }


function setStylesheet(styleTitle)  {
   var currTag;

   if (document.getElementsByTagName)  {
      for (var i = 0; (currTag = document.getElementsByTagName("link")[i]); i++)  {
         if (currTag.getAttribute("rel").indexOf("style") != -1 && currTag.getAttribute("title"))  {
            currTag.disabled = true;
            if(currTag.getAttribute("title") == styleTitle)  {
               currTag.disabled = false;
               }
            }  //  end if (currTag.getAttribute("rel")
         }
      }  //  end if (document.
  
   return true;
   }



/*

  Image functions
  
  These functions allow you to swap images out on rollover.

*/

function preloadImages(the_images_array)  {
   for(var loop = 0; loop < the_images_array.length; loop++)  {
      var an_image = new Image();
      an_image.src = the_images_array[loop];
      }
   }

function swapImgRestore()  {
   var i, x;
   var a = document.imageTrades;
   for(i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++)
      x.src = x.oSrc;
   }

function swapImage()  {
   var i, x;
   var j = 0
   var a = swapImage.arguments;
   document.imageTrades = new Array;
   for (i = 0; i < (a.length - 1); i += 2)
      if ( (x = document[a[i]]) != null )  {
         document.imageTrades[j++] = x;
         if (!x.oSrc)
            x.oSrc = x.src;
         x.src = a[i + 1];
         }
   }
