window.onload = function main() {
	//sizeNav();
	addLinkerEvents();
	if (document.getElementById('right-box') != null) {
		if (document.getElementById('right-box').getElementsByTagName('li').length > 0) {
			stripTag();
		}
	} else {
		document.getElementById("main").className = "fullwidth";
	}
}

// Dynamically pads each of the elements in the nav bar, based on the number of nav items and the width of the nav bar. Because I kind of hate the lack of flexibility in hard-coded nav menu widths.
function sizeNav() {
			// Grab each nav element and drop them into an array.
			var navs = []; 
			navs = document.getElementById("nav").getElementsByTagName("li");
			
			// Check the width of the nav bar.
			var navWidth = document.getElementById("nav").scrollWidth;
			
			// Iterate through all of the nav elements in order to find their total width.
			var liWidth = 0;
			for (var i = 0; i < navs.length; i++) {
				liWidth += navs[i].scrollWidth;
			}
			
			// Calculate the left and right padding that each nav element should have. This is based on the total nav bar width, minus the total width of all of nav elements, divided by the number of nav elements. This is then halved to find the value that should be applied to the left and right element padding. This is then converted to a whole number, since certain browsers can't take fractional values for padding.
			var avgPadding = parseInt((navWidth - liWidth) / navs.length / 2);
			
			// Iterate through each of the elements and apply the calculated left and right padding.
			var totalWidth = 0;
			for (var i = 0; i < navs.length; i++) {
				var element = navs[i].getElementsByTagName("a")[0];
				element.style.paddingLeft = avgPadding + "px";
				element.style.paddingRight = avgPadding + "px";
				totalWidth += element.scrollWidth;

				// Add a little extra padding to the last element in order to make up for the fact that we weren't able to use fractional values for the left and right padding.
				if (i == (navs.length - 1)) {
					element.style.paddingRight = ( avgPadding + (navWidth - totalWidth) ) + "px";
				}
				
				// This function doesn't have enough comments, so here's another line for you.  ;)
			}
}
		
function stripTag() {
	var currentList;
	var lists = [];
	var pageName;
	
	lists = document.getElementById('right-box').getElementsByTagName('li');
	
	for (var i = 0; i < lists.length; i++) {
		if (lists[i].className == "current") {
			currentList = lists[i];
		}
	}
	
	pageName = currentList.childNodes[0].innerHTML;
	currentList.removeChild(currentList.childNodes[0]);
	currentList.innerHTML = pageName;
}

function MM_CheckFlashVersion(reqVerStr,msg){
  with(navigator){
    var isIE  = (appVersion.indexOf("MSIE") != -1 && userAgent.indexOf("Opera") == -1);
    var isWin = (appVersion.toLowerCase().indexOf("win") != -1);
    if (!isIE || !isWin){  
      var flashVer = -1;
      if (plugins && plugins.length > 0){
        var desc = plugins["Shockwave Flash"] ? plugins["Shockwave Flash"].description : "";
        desc = plugins["Shockwave Flash 2.0"] ? plugins["Shockwave Flash 2.0"].description : desc;
        if (desc == "") flashVer = -1;
        else{
          var descArr = desc.split(" ");
          var tempArrMajor = descArr[2].split(".");
          var verMajor = tempArrMajor[0];
          var tempArrMinor = (descArr[3] != "") ? descArr[3].split("r") : descArr[4].split("r");
          var verMinor = (tempArrMinor[1] > 0) ? tempArrMinor[1] : 0;
          flashVer =  parseFloat(verMajor + "." + verMinor);
        }
      }
      // WebTV has Flash Player 4 or lower -- too low for video
      else if (userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 4.0;

      var verArr = reqVerStr.split(",");
      var reqVer = parseFloat(verArr[0] + "." + verArr[2]);
  
      if (flashVer < reqVer){
        if (confirm(msg))
          window.location = "http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash";
      }
    }
  } 
}


/****************************************************
     Author: Brian J Clifton
     Url: http://www.advanced-web-metrics.com/scripts
     This script is free to use as long as this info is left in
     
     Combined script for tracking external links, file downloads and mailto links
     
     All scripts presented have been tested and validated by the author and are believed to be correct
     as of the date of publication or posting. The Google Analytics software on which they depend is 
     subject to change, however; and therefore no warranty is expressed or implied that they will
     work as described in the future. Always check the most current Google Analytics documentation.

     Thanks to Nick Mikailovski (Google) for intitial discussions & Holger Tempel from webalytics.de
     for pointing out the original flaw of doing this in IE.

****************************************************/
// Only links written to the page (already in the DOM) will be tagged
// This version is for ga.js (last updated Jan 15th 2009)


function addLinkerEvents()
{
   var as = document.getElementsByTagName("a");
   var extTrack = ["chsamerica.org"];
   // List of local sites that should not be treated as an outbound link. Include at least your own domain here
   
   var extDoc = [".doc",".zip",".pdf"];
   //List of file extensions on your site. Add/edit as you require
   
   /*If you edit no further below this line, Top Content will report as follows:
      /ext/url-of-external-site
      /downloads/filename
      /mailto/email-address-clicked
   */

   //alert( "I'm here (" + String(as.length) +" <a> tags)" ) ;

   for( var i=0; i<as.length; i++ )
   {
      var flag = 0;
      var tmp = as[i].getAttribute("onclick");

      // IE6-IE7 fix (null values error) with thanks to Julien Bissonnette for this
      if( tmp != null )
      {
         tmp = String(tmp);
         //alert(tmp);
         if( tmp.indexOf('urchinTracker') > -1 || tmp.indexOf('_trackPageview') > -1 ) continue;
      }

      // Tracking outbound links off site - not the GATC
      for( var j=0; j<extTrack.length; j++ )
      {
         if( as[i].href.indexOf(extTrack[j]) == -1 && as[i].href.indexOf('google-analytics.com') == -1 )
         {
            //alert(as[i].href);
            flag++;
         }
      }

      //alert(as[i].href +" (" + String( flag ) +" == " + String( extTrack.length ) +")[" + String(as[i].href.indexOf("mailto:"))+"]" ) ;

      if( flag == extTrack.length && as[i].href.indexOf("mailto:") == -1 )
      {
         as[i].onclick = function()
         {
            var splitResult = this.href.split("//");
            _gaq.push(["_trackPageview", "/ext/" +splitResult[1] + ";" +((tmp != null) ? tmp+";" : "")]);
            //alert( "/ext/" +splitResult[1] + ";" +((tmp != null) ? tmp+";" : "") );
         };
      }

      // Tracking electronic documents - doc, xls, pdf, exe, zip
      for( var j=0; j<extDoc.length; j++ )
      {
         if( as[i].href.indexOf(extTrack[0]) != -1 && as[i].href.indexOf(extDoc[j]) != -1 )
         {
            as[i].onclick = function()
            {
               var splitResult = this.href.split(extTrack[0]);
               _gaq.push(["_trackPageview", "/downloads/" +splitResult[1] + ";" +((tmp != null) ? tmp+";" : "")]);
               //alert( "/download/" +splitResult[1] );
            }
            break;
         }
      }


      // added to track mailto links 23-Oct-2007
      // updated 31-Oct-2008 to remove break command - thanks to Victor Geerdink for spotting this
      if( as[i].href.indexOf("mailto:") != -1 )
      {
         as[i].onclick = function()
         {
            var splitResult = this.href.split(":");
            _gaq.push(["_trackPageview", "/mailto/" +splitResult[1] + ";" +((tmp != null) ? tmp+";" : "")]);
            //alert( "/mailto/" +splitResult[1] );
         }
      }
   }
}
