// JavaScript Document
window.onload = function() {
	//add translucent captions to appropriate images
	  captionizeImages();
	//choose styles for screen size
      setScreenClass();
	
	//upon printing convert links to footnotes
	  footnoteLinks('content','content');
}
window.onresize = setScreenClass;


//determine swreensize/////////////////////////////////////////////
//  Following transition classes will be declared:
//
//	classname		  screenwidth	
//	------------------------------------------
//	pda_v			  240px			
//	pda_h			  320px			
//	ultralow		  320px -  640px	
//	screen_lo		  640px -  770px	
//	screen_med		  770px - 890px	
//	screen_hi		 890px - 1088px	
//	screen_wide				> 1088px			

function setScreenClass(){
	var fmt = document.documentElement.clientWidth;
	var cls = 'initial';
	
	if(fmt<=240){/*cls ='pda_ver';*/} //unused at this time
	else if(fmt>240&&fmt<=320){/*cls ='pda_hor';*/} //unused at this time
	else if(fmt>320&&fmt<=640){/*cls ='screen_ultralow';*/} //unused at this time
	else if(fmt>640&&fmt<=768){/*cls ='screen_low';*/} //unused at this time
	else if(fmt>768&&fmt<=890){cls ='screen_med';}
	else if(fmt>890&&fmt<=1088){cls ='screen_high';}
	else {cls ='screen_wide';}
	placeImages();
	//used for debugging
	//document.getElementById('count').innerHTML=fmt+'px -> '+cls;
	document.body.className=cls;
};

//add captions to images that need em/////////////////////////////////////////////
function captionizeImages() {
  if (!document.getElementsByTagName) return false;
  if (!document.createElement) return false;
  var images = document.getElementsByTagName("img");
  if (images.length < 1) return false; 
  for (var i=0; i<images.length; i++) {
	  
    if (images[i].className.indexOf("captioned") != -1) {
      var title = images[i].getAttribute("title");
      var divCaption = document.createElement("div");
      divCaption.className = "caption";
      var divCaption_text = document.createTextNode(title);
      divCaption.appendChild(divCaption_text);
      var divContainer = document.createElement("div");
	  if (images[i].className.indexOf("lefty") != -1){
		 images[i].className=images[i].className.replace("lefty","");
      	divContainer.className="imgcontainerL";
	  }else{
		 images[i].className=images[i].className.replace("righty","");
		divContainer.className="imgcontainerR";
	  }
      images[i].parentNode.insertBefore(divContainer,images[i]);
      divContainer.appendChild(images[i]);
      insertAfter(divCaption,images[i]);
    }
  }
};
function placeImages() {
  if (!document.getElementsByTagName) return false;
  if (!document.createElement) return false;
  var images = document.getElementById("content").getElementsByTagName("img");
  if (images.length < 1) return false; 
  for (var i=0; i<images.length; i++) {
	
	
   
   if (images[i].className.indexOf("captioned")!=-1) {
	    images[i].parentNode.className=images[i].parentNode.className.replace("undofloat"," ");
		images[i].parentNode.className=images[i].parentNode.className.replace("printOnly"," ");
		if((images[i].width)>(document.documentElement.clientWidth/2.5)){
			images[i].parentNode.className=images[i].parentNode.className+" undofloat";
		}
		if((images[i].width)>(document.documentElement.clientWidth/1.6)){
			images[i].parentNode.className=images[i].parentNode.className+" printOnly";
		}
    }else {
		images[i].parentNode.className=images[i].className.replace("undofloat"," ");
		images[i].parentNode.className=images[i].className.replace("printOnly"," ");
		if (images[i].className.indexOf("lefty")!=-1 || images[i].className.indexOf("righty")!=-1) {
			if((images[i].width)>(document.documentElement.clientWidth/2.5)){
				images[i].className=images[i].className+" undofloat";
			}
		}
		if((images[i].width)>(document.documentElement.clientWidth/1.8)){
			images[i].className=images[i].className+" printOnly";
		}
    }
  }
};

function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}

// <![CDATA[
//insert footnotes/////////////////////////////////////////////
function footnoteLinks(containerID,targetID) {
      if (!document.getElementById || 
          !document.getElementsByTagName ||
          !document.createElement) return false;
      if (!document.getElementById(containerID) ||
          !document.getElementById(targetID)) return false;
      var container = document.getElementById(containerID);
      var target    = document.getElementById(targetID);
      var h2        = document.createElement('h2');
	  
      addClass.apply(h2,['printOnly']);
      var h2_txt    = document.createTextNode('Links');
      h2.appendChild(h2_txt);
	  
      var coll = container.getElementsByTagName('*');
      var ol   = document.createElement('ol');
      addClass.apply(ol,['printOnly']);
      var myArr = [];
      var thisLink;
      var num = 1;
	  
	  
      for (var i=0; i<coll.length; i++) {
        var thisClass = coll[i].className;
        if ( (coll[i].getAttribute('href') || coll[i].getAttribute('cite')) && (thisClass == '' || thisClass.indexOf('ignore') == -1)) { 
          thisLink = coll[i].getAttribute('href') ? coll[i].href : coll[i].cite;
		  
          var note = document.createElement('sup');
          addClass.apply(note,['printOnly']);
          var note_txt;
          var j = inArray.apply(myArr,[thisLink]);
          if ( j || j===0 ) {
            note_txt = document.createTextNode(j+1);
          } else {
            var li     = document.createElement('li');
            var li_txt = document.createTextNode(thisLink);
            li.appendChild(li_txt);
            ol.appendChild(li);
            myArr.push(thisLink);
			
            note_txt = document.createTextNode(num);
            num++;
          }
          note.appendChild(note_txt);
		  
          if (coll[i].tagName.toLowerCase() == 'blockquote') {
            var lastChild = lastChildContainingText.apply(coll[i]);
            lastChild.appendChild(note);
          } else {
            coll[i].parentNode.insertBefore(note, coll[i].nextSibling);
          }
        }
      }
	  if(num>1){
      	target.appendChild(h2);
      	target.appendChild(ol);
	  }
      addClass.apply(document.getElementsByTagName('html')[0],['noted']);
      return true;
    }
// ]]>
// <![CDATA[
    /*------------------------------------------------------------------------------
    Excerpts from the jsUtilities Library
    Version:        2.1
    Homepage:       http://www.easy-designs.net/code/jsUtilities/
    License:        Creative Commons Attribution-ShareAlike 2.0 License
                    http://creativecommons.org/licenses/by-sa/2.0/
    Note:           If you change or improve on this script, please let us know.
    ------------------------------------------------------------------------------*/
    if(Array.prototype.push == null) {
      Array.prototype.push = function(item) {
        this[this.length] = item;
        return this.length;
      };
    };
    // ---------------------------------------------------------------------
    //                  function.apply (if unsupported)
    //           Courtesy of Aaron Boodman - http://youngpup.net
    // ---------------------------------------------------------------------
    if (!Function.prototype.apply) {
      Function.prototype.apply = function(oScope, args) {
        var sarg = [];
        var rtrn, call;
        if (!oScope) oScope = window;
        if (!args) args = [];
        for (var i = 0; i < args.length; i++) {
          sarg[i] = "args["+i+"]";
        };
        call = "oScope.__applyTemp__(" + sarg.join(",") + ");";
        oScope.__applyTemp__ = this;
        rtrn = eval(call);
        oScope.__applyTemp__ = null;
    	return rtrn;
      };
    };
    function inArray(needle) {
      for (var i=0; i < this.length; i++) {
        if (this[i] === needle) {
          return i;
        }
      }
      return false;
    }
    function addClass(theClass) {
      if (this.className != '') {
        this.className += ' ' + theClass;
      } else {
        this.className = theClass;
      }
    }
    function lastChildContainingText() {
      var testChild = this.lastChild;
      var contentCntnr = ['p','li','dd'];
      while (testChild.nodeType != 1) {
        testChild = testChild.previousSibling;
      } 
      var tag = testChild.tagName.toLowerCase();
      var tagInArr = inArray.apply(contentCntnr, [tag]);
      if (!tagInArr && tagInArr!==0) {
        testChild = lastChildContainingText.apply(testChild);
      }
      return testChild;
    }
    // ]]>